美文网首页
LeetCode 136-140

LeetCode 136-140

作者: 1nvad3r | 来源:发表于2020-11-05 14:56 被阅读0次

136. 只出现一次的数字

class Solution {
    public int singleNumber(int[] nums) {
        int res = nums[0];
        for (int i = 1; i < nums.length; i++) {
            res ^= nums[i];
        }
        return res;
    }
}

137. 只出现一次的数字 II

class Solution {
    public int singleNumber(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
        }
        for (Integer key : map.keySet()) {
            if (map.get(key) == 1) {
                return key;
            }
        }
        return -1;
    }
}

138. 复制带随机指针的链表

先拷贝val值,原结点到新结点映射到map中,然后再更新next和random。

class Solution {
    public Node copyRandomList(Node head) {
        if (head == null) {
            return null;
        }
        Map<Node, Node> map = new HashMap<>();
        Node cur = head;
        while (cur != null) {
            map.put(cur, new Node(cur.val));
            cur = cur.next;
        }
        cur = head;
        while (cur != null) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);
    }
}

139. 单词拆分

class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        Set<String> set = new HashSet<>(wordDict);
        boolean[] dp = new boolean[s.length()];
        if (set.contains(String.valueOf(s.charAt(0)))) {
            dp[0] = true;
        }
        for (int i = 1; i < s.length(); i++) {
            if (set.contains(s.substring(0, i + 1))) {
                dp[i] = true;
                continue;
            }
            for (int j = 0; j < i; j++) {
                if (dp[j] == true && set.contains(s.substring(j + 1, i + 1))) {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[s.length() - 1];
    }
}

140. 单词拆分 II

class Solution {
    List<String> res = new ArrayList<>();
    List<String> temp = new ArrayList<>();
    Set<String> set;

    public void dfs(int begin, String s) {
        if (begin == s.length()) {
            StringBuilder sb = new StringBuilder();
            for (String str : temp) {
                sb.append(str + " ");
            }
            sb.deleteCharAt(sb.length() - 1);
            res.add(sb.toString());
            return;
        }

        for (int i = begin; i < s.length(); i++) {
            String sub = s.substring(begin, i + 1);
            if (set.contains(sub) && wordBreak2(s.substring(i + 1))) {
                temp.add(sub);
                dfs(i + 1, s);
                temp.remove(temp.size() - 1);
            }
        }
    }

    public boolean wordBreak2(String s) {
        if ("".equals(s)) {
            return true;
        }
        boolean[] dp = new boolean[s.length()];
        if (set.contains(String.valueOf(s.charAt(0)))) {
            dp[0] = true;
        }
        for (int i = 1; i < s.length(); i++) {
            if (set.contains(s.substring(0, i + 1))) {
                dp[i] = true;
                continue;
            }
            for (int j = 0; j < i; j++) {
                if (dp[j] == true && set.contains(s.substring(j + 1, i + 1))) {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[s.length() - 1];
    }

    public List<String> wordBreak(String s, List<String> wordDict) {
        set = new HashSet<>(wordDict);
        dfs(0, s);
        return res;
    }
}

相关文章

  • LeetCode 136-140

    136. 只出现一次的数字[https://leetcode-cn.com/problems/single-num...

  • 诗词136-140

    20150920 136 卜算子·我住长江头 李之仪我住长江头,君住长江尾。日日思君不见君,共饮长江水。此水几时休...

  • 136-140梳理

    136通过一个命题,可以说出什么?弗雷格,命题的涵义是思想,其意谓真。可以把涵义的思想看作一个共相,或一个概念——...

  • 诗篇136-140

    诗篇 第136篇 你们要称谢耶和华,因他本为善,他的慈爱永远长存!你们要称谢万神之神,因他的慈爱永远长存!你们要称...

  • 体重急剧上升

    我身高180,长期体重维持在136-140斤之间,超过140斤的机会非常少,每次体检的感觉都挺好的,身高体重标准指...

  • 英语口语 136-140

    1. extend an olive branch 主动求和 make peace with sb 向某人求和 s...

  • week 2019-06-23

    LeetCode 16[M] LeetCode 17[M] LeetCode 926

  • leetcode

    leetcode leetcode to practice leetcode ac code 数组专题 done ...

  • 【LeetCode】Fizz Buzz 解题报告

    【LeetCode】Fizz Buzz 解题报告 [LeetCode] https://leetcode.com/...

  • 【LeetCode】Fizz Buzz 解题报告

    【LeetCode】Fizz Buzz 解题报告 [LeetCode] https://leetcode.com/...

网友评论

      本文标题:LeetCode 136-140

      本文链接:https://www.haomeiwen.com/subject/ajrnvktx.html