leetcode .

Posted wheleetcode

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode .相关的知识,希望对你有一定的参考价值。

66

2 758 . Bold Words in String

    public String boldWords(String[] words, String S) {
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < S.length(); i++) {
            for (String str : words) {
                if (i + str.length() <= S.length() && str.equals(S.substring(i, i + str.length()))) {
                    for (int j = i; j < i + str.length(); j++) {
                        set.add(j);
                    }
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < S.length(); i++) {
            if (set.contains(i) && (i == 0 || !set.contains(i - 1))) sb.append("<b>");
            sb.append(S.charAt(i));
            if (set.contains(i) && (i == S.length() - 1 || !set.contains(i + ))) sb.append("</b>");
        }
        return sb.toString();
    }
View Code

3 759 Employee Free Time 

   public List<Interval> employeeFreeTime(List<List<Interval>> avails) {
        List<Interval> list = new ArrayList<>();
        for (List<Interval> list1 : avails) {
            for (Interval interval : list1) {
                list.add(interval);
            }
        }
        Collections.sort(list, (a,b)->a.start - b.start);
        List<Interval> res = new ArrayList<>();
        Interval cur = list.get(0);
        for (Interval val : list) {
            if (cur.end < val.start) {
                res.add(new Interval(cur.end, val.start));
                cur = val;
            } else {
                cur = cur.end < val.end ? val : cur;
            }
        }
        return res;
    }


}

class Interval {
    int start, end;
    public Interval(int start, int end) {
        this.start = start;
        this.end = end;
    }
View Code

4 Special Binary String

 public String makeLargestSpecial(String S) {
        int i = 0;
        List<String> list = new ArrayList<>();
        for (int j = 0, c = 0; j < S.length(); j++) {
            if (S.charAt(j) == \'1\') {
                c++;
            } else {
                c--;
            }
            if (c == 0) {
                list.add("1" + makeLargestSpecial(S.substring(i + 1, j)) + "0");
                i = j + 1;
            }
        }
        Collections.sort(list, Collections.reverseOrder());
        return String.join("", list);
    }
View Code

 

65 Pour Water

   public int[] pourWater(int[] heights, int v, int k) {
        int n = heights.length;
        int l = 0, r = 0;
        for (int i = 1; i <= v; i++) {
            l = k; r = k;
            while (l - 1 >= 0 && heights[l - 1] <= heights[l]) l--;
            while (l + 1 <= k && heights[l] == heights[l + 1]) l++;
            while (r + 1 < n && heights[r] >= heights[r + 1]) r++;
            while (r - 1 >= k && heights[r - 1] == heights[r]) r--;
            if ((heights[l] < heights[k])) {
                ++heights[l];
            } else {
                ++heights[r];
            }
        }
        return heights;
    }
View Code

 

64

751 IP to CIDR

   public List<String> ipToCIDR(String ip, int range) {
        List<String> res = new ArrayList<>();
        long num = 0;
        for (String str : ip.split("//.")) {
            num = num * 256 + Integer.valueOf(str);
        }
        while (range > 0) {
            long step = num & -num;
            while (step > range) {
                step /= 2;
            }
            res.add(converToIp(num, step));
            num += step;
            range -= step;
        }
        return res;
    }

    private String converToIp(long num, long step) {
        int n = 0;
        while (step > 0) {
            step >>= 1;
            n++;
        }
        return "" + ((num >> 24) & 255) + "." +  ((num >> 16) & 255) + "." + ((num >> 8) & 255) + "." +  (num & 255) + (33-n);
    }
View Code

 

63

750 Number Of Corner Rectangles

    public int countCornerRectangles(int[][] grid) {
        int n = grid.length;
        if (n == 0) return 0;
        int m = grid[0].length;
        if (m == 0) return 0;
        int res = 0;
        for (int i = 0; i < m - 1; i++) {
            for (int j = 0; j < n - 1; j--) {
                if (grid[i][j] == 0) continue;
                for (int d = i + 1; d < m; d++) {
                    if (grid[d][j] == 0) continue;
                    for (int u = j + 1; u < n; u++) {
                        if (grid[i][u] == 0 || grid[d][u] == 0) continue;
                        res++;
                    }
                }
            }
        }
        return res;
    }
View Code
   public int countCornerRectangles(int[][] grid) {
        int n = grid.length;
        if (n == 0) return 0;
        int m = grid[0].length;
        if (m == 0) return 0;
        int res = 0;
        for (int i = 0; i < m; i++) {
            for (int j = i + 1; j < m; i++) {
                int cur = 0;
                for (int k = 0; k < n; k++) {
                    if (grid[i][k] == grid[j][k] && grid[i][k] == 1) {
                        cur++;
                    }
                }
                res += cur * (cur + 1) / 2;
            }
        }
        return res;
    }
View Code

 

62 742  Closest Leaf in a Binary Tree

    int findClosestLeaf(TreeNode root, int k) {
        Map<TreeNode, TreeNode> map = new HashMap<>();
        TreeNode kRoot = dfs(root, k, map);
        Queue<TreeNode> q = new LinkedList<TreeNode>();
        Set<TreeNode> set = new HashSet<TreeNode>();
        q.offer(kRoot);
        set.add(kRoot);
        while (!q.isEmpty()) {
            TreeNode cur = q.poll();
            if (cur.left == null && cur.right == null) return cur.val;
            if (cur.left != null && !set.contains(cur.left)) {
                q.offer(cur.left);
                set.add(cur.left);
            }
            if (cur.right != null && !set.contains(cur.right)) {
                q.offer(cur.right);
                set.add(cur.right);
            }
            if (map.containsKey(cur) && !set.contains(map.get(cur))) {
                q.offer(map.get(cur));
                set.add(map.get(cur));
            }
        }
        return -1;
    }
    TreeNode dfs(TreeNode root, int k, Map<TreeNode, TreeNode> map) {
        if (root.val == k) return root;
        if (root.left != null) {
            map.put(root.left, root);
            Tree kRoot = dfs(root.left, k, map);
            if (kRoot != null) {
                return kRoot;
            }
        }
        if (root.right != null) {
            map.put(root.right, root);
            Tree kRoot = dfs(root.right, k, map);
            if (kRoot != null) {
                return kRoot;
            }
        }
        return null;
    }
}
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
}
View Code

 

61 734 Sentence Similarity

    public boolean areSentencesSimilar(String[] words1, String[] words2, String[][] pairs) {
        if (words1.length != words2.length) return false;
        Map<String, Set<String>> map = new HashMap<>();
        for (String[] pair : pairs) {
            map.putIfAbsent(pair[0], new HashSet<>());
            map.putIfAbsent(pair[1], new HashSet<>());
            map.get(pair[0]).add(pair[1]);
            map.get(pair[1]).add(pair[0]);
        }
        for (int i = 0; i < words1.length; i++) {
            if (words1[i].equals(words2[i])) {
               continue; 
            }
            if (!map.containsKey(words1[i]) || !map.get(words1[i]).contains(words2[i])) {
                return false;
            }
        }
        return true;
    }
View Code

 

57 723 Candy Crush

 

以上是关于leetcode .的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode810. 黑板异或游戏/455. 分发饼干/剑指Offer 53 - I. 在排序数组中查找数字 I/53 - II. 0~n-1中缺失的数字/54. 二叉搜索树的第k大节点(代码片段

LEETCODE 003 找出一个字符串中最长的无重复片段

Leetcode 763 划分字母区间

LeetCode:划分字母区间763

Leetcode:Task Scheduler分析和实现

817. Linked List Components - LeetCode