美文网首页
LeetCode 86-90

LeetCode 86-90

作者: 1nvad3r | 来源:发表于2020-10-06 18:50 被阅读0次

    86. Partition List

    分析:

    给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前。你应当保留两个分区中每个节点的初始相对位置。

    最终答案的链表一定包含两部分,左边的一部分所有值都比x小,右边的一部分所有值都大于等于x。
    那么首先得到这两部分链表,再把它们连起来就行了。
    遍历head结点,只要比x小,就加到left链表,反之加到right链表。
    注意最后right链表最后一个结点的next值要设为null,否则会形成环。
    然后将left链表和right链表连起来即可。

    Java:
    class Solution {
        public ListNode partition(ListNode head, int x) {
            ListNode left = new ListNode(0);
            ListNode right = new ListNode(0);
            ListNode curLeft = left, curRight = right;
            while (head != null) {
                if (head.val < x) {
                    curLeft.next = head;
                    curLeft = curLeft.next;
                } else {
                    curRight.next = head;
                    curRight = curRight.next;
                }
                head = head.next;
            }
            curRight.next = null;
            curLeft.next = right.next;
            return left.next;
        }
    }
    

    87. Scramble String

    先判断两个字符串所含字母是否完全相等,如果不等直接返回false。
    dp[length][i][j]代表s1从i开始,s2从j开始length长度的两个子串是否是扰乱字符串。如果length等于1时,只需比较s1的第i个字符是否等于s2的第j个字符即可。

    dp[length][i][j]的判断还需要k从1遍历到length-1,k代表切割完之后左侧有k个字符,右侧有length-k个字符。
    如果切割完之后没有交换,只要dp[k][i][j]为真且 dp[length - k][i + k][j + k]为真,那么dp[length][i][j]为真。
    如果切割完之后交换了,只要dp[k][i][j + length - k] 为真且 dp[length - k][i + k][j]为真,那么dp[length][i][j]为真。
    所以dp[length][i][j]只要上面两种情况其中一种成立即可。
    最后返回return dp[len][0][0]
    时间复杂度O(n4),空间复杂度O(n3)

    class Solution {
        public boolean isScramble(String s1, String s2) {
            int len = s1.length();
            int[] letters = new int[26];
            for (int i = 0; i < len; i++) {
                letters[s1.charAt(i) - 'a']++;
                letters[s2.charAt(i) - 'a']--;
            }
            for (int i = 0; i < 26; i++) {
                if (letters[i] != 0) {
                    return false;
                }
            }
            boolean[][][] dp = new boolean[len + 1][len][len];
            for (int length = 1; length <= len; length++) {
                for (int i = 0; i + length <= len; i++) {
                    for (int j = 0; j + length <= len; j++) {
                        if (length == 1) {
                            dp[length][i][j] = s1.charAt(i) == s2.charAt(j);
                        } else {
                            for (int k = 1; k < length; k++) {
                                dp[length][i][j] = (dp[k][i][j] && dp[length - k][i + k][j + k])
                                        || (dp[k][i][j + length - k] && dp[length - k][i + k][j]);
                                if (dp[length][i][j] == true) {
                                    break;
                                }
                            }
    
                        }
                    }
                }
            }
            return dp[len][0][0];
        }
    }
    

    88. Merge Sorted Array

    分析:

    给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组。
    从后往前比较大小,从后往前填充。

    Java:
    class Solution {
        public void merge(int[] nums1, int m, int[] nums2, int n) {
            int len = m + n;
            int pos = len - 1, pos1 = m - 1, pos2 = n - 1;
            while (pos >= 0) {
                if (pos1 < 0) {
                    while (pos2 >= 0) {
                        nums1[pos--] = nums2[pos2--];
                    }
                    break;
                }
                if (pos2 < 0) {
                    while (pos1 >= 0) {
                        nums1[pos--] = nums1[pos1--];
                    }
                    break;
                }
                if (nums1[pos1] >= nums2[pos2]) {
                    nums1[pos--] = nums1[pos1--];
                } else {
                    nums1[pos--] = nums2[pos2--];
                }
            }
            return;
        }
    }
    

    89. Gray Code

    分析:

    格雷码转换规则:

    1. 1位格雷码有两个码字
    2. (n+1)位格雷码中的前2n个码字等于n位格雷码的码字,按顺序书写,加前缀0
    3. (n+1)位格雷码中的后2n个码字等于n位格雷码的码字,按逆序书写,加前缀1
    4. n+1位格雷码的集合 = n位格雷码集合(顺序)加前缀0 + n位格雷码集合(逆序)加前缀1

    从0阶一步一步往高阶递推。
    其中第2步在最高位加前缀0,不改变原来所有元素的值,所以可以忽略这一步。只需要进行第3步,把逆序书写加前缀1之后的数添加到结果中即可。
    逆序只要从尾往头遍历,加前缀1之后的值,就是加上最高位代表的十进制数。
    最高位每次加的值为1,2,4,8....可以通过移位操作得到新的值。

    Java:
    class Solution {
        public List<Integer> grayCode(int n) {
            List<Integer> res = new ArrayList<>();
            res.add(0);
            int head = 1;
            for (int i = 0; i < n; i++) {
                for (int j = res.size() - 1; j >= 0; j--) {
                    res.add(res.get(j) + head);
                }
                head <<= 1;
            }
            return res;
        }
    }
    

    90. Subsets II

    分析:

    先把数组排序,方便去重,和第78题的区别就是,这道题数组中含有重复的元素。
    在加到结果集的过程中先判断一下是否包含重复的集合,如果不包含,再加到结果集中。

    Java:
    class Solution {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> temp = new ArrayList<>();
    
        void dfs(int index, int[] nums) {
            if (index == nums.length) {
                List<Integer> list = new ArrayList<>(temp);
                if (!res.contains(list)) {
                    res.add(list);
                }
                return;
            }
            temp.add(nums[index]);
            dfs(index + 1, nums);
            temp.remove(temp.size() - 1);
            dfs(index + 1, nums);
        }
    
        public List<List<Integer>> subsetsWithDup(int[] nums) {
            Arrays.sort(nums);
            dfs(0, nums);
            return res;
        }
    }
    
    class Solution {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> temp = new ArrayList<>();
    
        void dfs(int index, int[] nums) {
            if (index == nums.length) {
                return;
            }
            for (int i = index; i < nums.length; i++) {
                if (i - 1 >= index && nums[i] == nums[i - 1]) {
                    continue;
                }
                temp.add(nums[i]);
                res.add(new ArrayList<>(temp));
                dfs(i + 1, nums);
                temp.remove(temp.size() - 1);
            }
        }
    
        public List<List<Integer>> subsetsWithDup(int[] nums) {
            Arrays.sort(nums);
            res.add(new ArrayList<>());
            dfs(0, nums);
            return res;
        }
    }
    

    相关文章

      网友评论

          本文标题:LeetCode 86-90

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