LeetCode解題(20200623)

67. 二進制求和、十進制求和

/**
 * Copyright (C), 2018-2020
 * FileName: addBinary67
 * Author:   xjl
 * Date:     2020/6/23 8:52
 * Description: 67. 二進制求和
 */
package String;

import org.junit.Test;

/**
 * 二進制求和
 */
public class addBinary67 {
    public String addBinary(String a, String b) {
        int n1 = a.length() - 1;
        int n2 = b.length() - 1;
        int carry = 0;
        StringBuilder result = new StringBuilder();
        while (n1 >= 0 && n2 >= 0) {
            int sum = a.charAt(n1) - '0' + b.charAt(n2) - '0' + carry;
            carry = sum / 2;
            sum %= 2;
            result.append(sum);
            n1--;
            n2--;
        }
        while (n1 >= 0) {
            int sum = a.charAt(n1) - '0' + carry;
            carry = sum / 2;
            sum %= 2;
            result.append(sum);
            n1--;
        }
        while (n2 >= 0) {
            int sum = b.charAt(n2) - '0' + carry;
            carry = sum / 2;
            sum %= 2;
            result.append(sum);
            n2--;
        }
        if (carry > 0) {
            result.append(carry);
        }
        return result.reverse().toString();
    }

    @Test
    public void test(){
        String s = addBinary("1010", "1011");
        System.out.println(s);
    }

}

923. 三數之和的多種可能

/**
 * Copyright (C), 2018-2020
 * FileName: threeSumMulti
 * Author:   xjl
 * Date:     2020/6/23 9:15
 * Description: 923. 三數之和的多種可能
 */
package Dynamic_programming;

import org.junit.Test;

import java.util.Arrays;

public class threeSumMulti {
    /**
     *暴力枚舉法 時間超出限制
     * @param A
     * @param target
     * @return
     */
    public int threeSumMulti(int[] A, int target) {
        int result = 0;
        for (int i = 0; i < A.length; i++) {
            for (int j = i + 1; j < A.length; j++) {
                for (int k = j + 1; k < A.length; k++) {
                    if (A[i] + A[j] + A[k] == target && i < j && j < k) {
                        result++;
                    }
                }
            }
        }
        return result;
    }

    public int threeSumMulti2(int[] A, int target) {
        int MOD = 1_000_000_007;
        long ans = 0;
        Arrays.sort(A);
        for (int i = 0; i < A.length; ++i) {
            int T = target - A[i];
            int j = i + 1, k = A.length - 1;
            while (j < k) {
                if (A[j] + A[k] < T)
                    j++;
                else if (A[j] + A[k] > T)
                    k--;
                else if (A[j] != A[k]) {
                    int left = 1, right = 1;
                    while (j + 1 < k && A[j] == A[j + 1]) {
                        left++;
                        j++;
                    }
                    while (k - 1 > j && A[k] == A[k - 1]) {
                        right++;
                        k--;
                    }
                    ans += left * right;
                    ans %= MOD;
                    j++;
                    k--;
                } else {
                    ans += (k - j + 1) * (k - j) / 2;
                    ans %= MOD;
                    break;
                }
            }
        }
        return (int) ans;
    }

    @Test
    public void test() {
        int[] A = {1, 1, 2, 2, 2, 2};
        int i = threeSumMulti2(A, 5);
        System.out.println(i);
    }
}

15. 三數之和

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList();
        int len = nums.length;
        if (nums == null || len < 3) return result;
        // 排序
        Arrays.sort(nums);
        //遍歷第一個數據
        for (int i = 0; i < len; i++) {
            // 如果當前數字大於0,則三數之和一定大於0,所以結束循環
            if (nums[i] > 0) break;
            // 去重
            if (i > 0 && nums[i] == nums[i - 1]) continue;
            //制定兩個指針
            int L = i + 1;
            int R = len - 1;
            //如果是左邊的指針小於就表示可以進行
            while (L < R) {
                int sum = nums[i] + nums[L] + nums[R];
                if (sum == 0) {
                    //添加進入list中
                    result.add(Arrays.asList(nums[i], nums[L], nums[R]));
                    // 左邊去重
                    while (L < R && nums[L] == nums[L + 1]) L++;
                    // 右邊去重
                    while (L < R && nums[R] == nums[R - 1]) R--;
                    L++;
                    R--;
                } else if (sum < 0) L++;
                else if (sum > 0) R--;
            }
        }
        return result;
    }
}

18. 四數之和

/**
 * Copyright (C), 2018-2020
 * FileName: fourSum18
 * Author:   xjl
 * Date:     2020/6/23 10:13
 * Description: 給定一個包含 n 個整數的數組 nums 和一個目標值 target,判斷 nums 中是否存在四個元素 a,b,c 和 d ,使得 a + b + c + d 的值與 target 相等?找出所有滿足條件且不重複的四元組。  來源:力扣(LeetCode) 鏈接:https://leetcode-cn.com/problems/4sum 著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。
 */
package Math;

import org.junit.Test;

import java.util.*;

public class fourSum18 {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        Arrays.sort(nums);//排序
        int len = nums.length;
        if (nums == null || len < 4)//長度小於四,直接返回。測試用例好像沒有長度小於四的,去掉也行
            return result;
        for (int i = 0; i < len - 3; ++i) {
            if (nums[i] > target / 4)//最小的數都大於target的1/4,後面的不用看了
                break;
            if (i > 0 && nums[i] == nums[i - 1])//去重
                continue;
            for (int j = i + 1; j < len - 2; ++j) {
                if (j > i + 1 && nums[j] == nums[j - 1])//去重
                    continue;
                int low = j + 1;
                int high = len - 1;
                while (low < high) {
                    if (nums[high] < target / 4)
                        //最大的都小於targer的1/4,後面的不用看了
                        break;
                    int sum = nums[i] + nums[j] + nums[low] + nums[high];
                    if (sum == target) {
                        result.add(Arrays.asList(nums[i], nums[j], nums[low], nums[high]));
                        while (low < high && nums[low] == nums[low + 1]) ++low;//跳過相同的值
                        while (low < high && nums[high] == nums[high - 1]) --high;//跳過相同的值
                        ++low;
                        --high;
                    } else if (sum < target) {
                        //和小於目標值,low增大,使和增大
                        while (low < high && nums[low] == nums[low + 1]) ++low;
                        ++low;
                    } else {
                        //和大於目標值,high減小,使值減小
                        while (low < high && nums[high] == nums[high - 1]) --high;
                        --high;
                    }
                }
            }
        }
        return result;
    }

    public List<List<Integer>> fourSum1(int[] nums, int target) {
        //除重複
        Set<List<Integer>> set = new HashSet<>();
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        Arrays.sort(nums);//排序
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                int p = j + 1;
                int q = nums.length - 1;
                int currentTarget = target - nums[i] - nums[j];
                while (p < q) {
                    int sum = nums[p] + nums[q];
                    if (sum == currentTarget) {
                        List<Integer> list = new ArrayList<>();
                        list.add(nums[i]);
                        list.add(nums[j]);
                        list.add(nums[p]);
                        list.add(nums[q]);
                        set.add(list);
                        p++;
                        q--;
                    } else if (sum < currentTarget) {
                        p++;
                    } else {
                        q--;
                    }
                }
            }
        }
        for (List list : set) {
            result.add(list);
        }
        return result;
    }

    @Test
    public void test() {
        int[] A = {-3,-2,-1,0,0,1,2,3};
        List<List<Integer>> lists = fourSum1(A, 0);
        for (List list : lists) {
            for (Object o : list) {
                System.out.print(o+" ");
            }
            System.out.println();
        }

    }
}

454. 四數相加 II

 

674. 最長連續遞增序列

/**
 * Copyright (C), 2018-2020
 * FileName: findLengthOfLCIS674
 * Author:   xjl
 * Date:     2020/6/23 14:07
 * Description: 674. 最長連續遞增序列
 */
package LinkList;

import org.junit.Test;

import java.util.Stack;

public class findLengthOfLCIS674 {
    public int findLengthOfLCIS(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        int result = 1;
        int length = 1;
        //遍歷 一次比較大小
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] > nums[i - 1]) {
                length++;
                result = result >= length ? result : length;
            } else {
                length = 1;
            }
        }
        return result;
    }

    public int findLengthOfLCIS2(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        if (nums.length == 1) {
            return 1;
        }
        Stack<Integer> stack = new Stack<>();
        int result = 0;
        stack.add(nums[0]);

        for (int i = 1; i < nums.length; i++) {
            if (stack.peek() < nums[i]) {
                //入棧
                stack.add(nums[i]);
            } else {
                //清空棧
                stack.clear();
                //添加新的元素
                stack.add(nums[i]);
            }
            //比較大小值
            result = result > stack.size() ? result : stack.size();
        }

        return result;
    }

    @Test
    public void test() {
        int[] array = {2, 2, 2, 2, 2, 2, 2, 2, 2};
        int lengthOfLCIS = findLengthOfLCIS2(array);
        System.out.println(lengthOfLCIS);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章