11.9 -11.15

11.9日

973.最接近原点的K个点

class Solution {
    public int[][] kClosest(int[][] points, int K) {
        PriorityQueue<int[]> queue = new PriorityQueue<int[]>(points.length, (o1, o2) -> {
            return o1[0] * o1[0] + o1[1] * o1[1] - o2[0] * o2[0] - o2[1] * o2[1];
        });
        for (int[] a : points) {
            queue.add(a);
        }
        int[][] ans = new int[K][2];
        for (int i = 0; i < K; i++) {
            ans[i] = queue.poll();
        }
        return ans;
    }
}

46.全排列

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> ans = new ArrayList<>();
        int[] visited = new int[nums.length];
        backtrack(ans, nums, new ArrayList<Integer>(), visited);
        return ans;
    }

    public void backtrack(List<List<Integer>> ans, int[] nums, ArrayList<Integer> tmp, int[] visited) {
        if (tmp.size() == nums.length) {
            ans.add(new ArrayList<>(tmp));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (visited[i] == 1) continue;
            visited[i] = 1;
            tmp.add(nums[i]);
            backtrack(ans, nums, tmp, visited);
            visited[i] = 0;
            tmp.remove(tmp.size() - 1);
        }
    }
}

11.10日

31.下一个排序

class Solution {
    public void nextPermutation(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        //找到从左往右最小的数,不包含最后一位数(若数组是递减的,则仍为0)
        for(int i = 0; i < nums.length - 1; i ++) {
            if(nums[i] < nums[i + 1]) {
                left = i;
            }
        }
        //找到left右边,最靠右,且比nums[left]大的数
        for(int i = left + 1; i < nums.length; i ++) {
            if(nums[i] > nums[left]) {
                right = i;
            }
        }
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
        Arrays.sort(nums, left+1, nums.length);
    }
}

11.11日

39.组合总和

class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> ans = new ArrayList<>();
        Arrays.sort(candidates);
        help(candidates, target, ans, 0, new ArrayList<Integer>());
        return ans;
    }

    public void help(int[] candidates, int target, List<List<Integer>> ans, int index, ArrayList<Integer> list) {
        if(target < 0) {
            return ;
        }
        if(target == 0) {
            ArrayList<Integer> oneAns = new ArrayList<>(list);
            ans.add(oneAns);
            return ;
        }
        for(int i = index; i < candidates.length; i ++) {
            if(target < 0) {
                break;
            }
            list.add(candidates[i]);
            help(candidates, target-candidates[i], ans, i, list);
            list.remove(list.size() - 1);
        }
    }
}

11.12日

922.按奇偶排序数组Ⅱ

class Solution {
    public int[] sortArrayByParityII(int[] A) {
        int t = 1;
        for (int i = 0; i < A.length - 1; i = i + 2) {
            if (A[i] % 2 != 0) {
                while (A[t] % 2 != 0) {
                    t = t + 2;
                }
                int tmp = A[i];
                A[i] = A[t];
                A[t] = tmp;
            }
        }
        return A;
    }
}

11.13日

328.奇偶链表

class Solution {
    public ListNode oddEvenList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode p = head;
        ListNode q = head;
        while(q != null && q.next != null && q.next.next != null) {
            ListNode t = q.next;
            q = t.next;
            t.next = q.next;
            q.next = p.next;
            p.next = q;
            p = q;
            q = t;
        }
        return head;
    }
}
class Solution {
    public ListNode oddEvenList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        // head 为奇链表头结点,o 为奇链表尾节点
        ListNode o = head;
        // p 为偶链表头结点
        ListNode p = head.next;
        // e 为偶链表尾节点
        ListNode e = p;
        while (o.next != null && e.next != null) {
            o.next = e.next;
            o = o.next;
            e.next = o.next;
            e = e.next;
        }
        o.next = p;
        return head;
    }
}

11.14日

1122.数组的相对排序

class Solution {
    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        int max = 0;
        for(int i = 0; i < arr1.length; i ++) {
            max = Math.max(max, arr1[i]);
        }
        int[] nums = new int[max+1];
        for(int i = 0; i < arr1.length; i ++) {
            nums[arr1[i]] ++;
        }
        int idx = 0;
        for(int i : arr2) {
            while(nums[i] > 0) {
                arr1[idx] = i;
                idx ++;
                nums[i] --;
            }
        }
        for(int i = 0; i < nums.length; i ++) {
            while(nums[i] > 0) {
                arr1[idx] = i;
                idx ++;
                nums[i] --;
            }
        }
        return arr1;
    }
}
class Solution {
    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        int idx = 0;
        for(int i = 0; i < arr2.length; i ++) {
            for(int j = idx; j < arr1.length; j ++) {
                if(arr1[j] == arr2[i]) {
                    idx ++;
                    continue;
                } else {
                    int n = j;
                    while(n < arr1.length && arr1[n] != arr2[i]) {
                        n ++;
                    }
                    if(n == arr1.length) {
                        continue;
                    }
                    int temp = arr1[n];
                    arr1[n] = arr1[j];
                    arr1[j] = temp;
                    idx ++;
                }
            }
        }
        Arrays.sort(arr1, idx, arr1.length);
        return arr1;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值