算法打卡,用于自律(nsum通解)

题目一

算法打卡,用于自律(nsum通解)_第1张图片

 解法一

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] a = {-1,-1};
        for(int i = 0;i

解法二

class Solution {
    public int[] twoSum(int[] nums, int target) {
        HashMap index = new HashMap<>();
        for(int i = 0;i

题目二

算法打卡,用于自律(nsum通解)_第2张图片

 解法

class Solution {
    public List> threeSum(int[] nums) {
        Arrays.sort(nums);
        return nSumTarget(nums, 3, 0, 0);
    }
    public List> nSumTarget(int[] nums, int n, int start, int target){
        int size = nums.length;
        List> res = new ArrayList<>();
        if(n < 2 || size < n) return res;
        if(n == 2){
            int lo = start, hi = size - 1;
            while(lo < hi){
                int left = nums[lo], right = nums[hi];
                int sum = left + right;
                if(sum < target){
                    while(lo < hi && nums[lo] == left) lo++;
                }else if(sum > target){
                    while(lo < hi && nums[hi] == right) hi--;
                }else{
                    List list = new ArrayList<>();
                    list.add(nums[lo]);
                    list.add(nums[hi]);
                    res.add(list);
                    while(lo < hi && nums[lo] == left) lo++;
                    while(lo < hi && nums[hi] == right) hi--;
                }
            }
        }else{
            for(int i = start; i < size; i++){
                List> temp = nSumTarget(nums, n - 1, i + 1, target - nums[i]);
                for(List list : temp){
                    list.add(nums[i]);
                    res.add(list);
                }
                while(i < size - 1 && nums[i] == nums[i + 1]) i++;
            }
        }
        return res;
    }
}

题目三

算法打卡,用于自律(nsum通解)_第3张图片

 解法

class Solution {
    public List> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        return nSumTarget(nums,4,0,target);
    }
    public List> nSumTarget(int[] nums, int n, int start, int target){
        int size = nums.length;
        List> res = new ArrayList<>();
        if(n < 2 || size < n) return res;
        if(n == 2){
            int lo = start, hi = size - 1;
            while(lo < hi){
                int left = nums[lo], right = nums[hi];
                int sum = left + right;
                if(sum < target){
                    while(lo < hi && nums[lo] == left) lo++;
                }else if(sum > target){
                    while(lo < hi && nums[hi] == right) hi--;
                }else{
                    List list = new ArrayList<>();
                    list.add(nums[lo]);
                    list.add(nums[hi]);
                    res.add(list);
                    while(lo < hi && nums[lo] == left) lo++;
                    while(lo < hi && nums[hi] == right) hi--;
                }
            }
        }else{
            for(int i = start; i < size; i++){
                List> temp = nSumTarget(nums, n - 1, i + 1, target - nums[i]);
                for(List list : temp){
                    list.add(nums[i]);
                    res.add(list);
                }
                while(i < size - 1 && nums[i] == nums[i + 1]) i++;
            }
        }
        return res;
    }    
}

模板理解背下来~

题目四

算法打卡,用于自律(nsum通解)_第4张图片

解法

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head==null||head.next==null){
            return head;
        }
        ListNode last = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return last;
    }
}

 题目五

算法打卡,用于自律(nsum通解)_第5张图片

解法

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    ListNode cure = null;
    public ListNode reverseBetween(ListNode head, int left, int right) {
        if(left==1){
            return reverseN(head, right);
        }
        head.next = reverseBetween(head.next,left-1,right-1);
        return head;
    }
    public ListNode reverseN(ListNode head,int n){
        if(n==1){
            cure = head.next;
            return head;
        }
        ListNode last = reverseN(head.next,n-1);
        head.next.next = head;
        head.next = cure;
        return last;
    }
}

你可能感兴趣的:(算法,算法,leetcode)