Leetcode练习(6.9~6.15)

## 2019.6.9-2019.6.15 
### Range in leetcode 145 Top Interview Questions
easy:
1. https://leetcode.com/problems/two-sum/
2. https://leetcode.com/problems/power-of-three/ 

medium:
1. https://leetcode.com/problems/add-two-numbers/
2. https://leetcode.com/problems/3sum/  
3. https://leetcode.com/problems/jump-game/
4. https://leetcode.com/problems/word-break/
5. https://leetcode.com/problems/sort-list/

hard:
1. https://leetcode.com/problems/median-of-two-sorted-arrays/
2. https://leetcode.com/problems/first-missing-positive/
3. https://leetcode.com/problems/trapping-rain-water/

1 No.1 Two Sum

https://leetcode.com/problems/two-sum/

public static int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if(map.containsKey(complement)){
                return new int[] {map.get(complement),i};
            }

            map.put(nums[i],i);
        }

        throw new IllegalArgumentException("No solution");
    }

2 No.326 Power of Three

2.1 方法1

  • 时间复杂度和空间复杂度都是:O(log N)
public static boolean isPowerOfThree(int n) {
        return Integer.toString(n,3).matches("^10*$");
    }

^ : 表示匹配字符串开始的位置
* : 表示匹配前面的子表达式 0 次或者多次
$ : 匹配输入字符串结尾的位置


在这里插入图片描述
在这里插入图片描述

2.2 方法2

  • 时间复杂度和空间复杂度都是: O(1)
public static boolean isPowerOfThree(int n) {
        double e = Math.log(Integer.MAX_VALUE) / Math.log(3); // e = 19.5588
        int intE = (int)e;
        double MaxPower_3 = Math.pow(3,intE); // 1162261467

        return n > 0 && MaxPower_3 % n == 0;
    }

3 No.2 Add Two Numbers

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummyHead = new ListNode(0);
        ListNode p = l1, q = l2, cur = dummyHead;
        int jinwei = 0;
        while (p != null || q != null) {
            int x = (p != null) ? p.val : 0;
            int y = (q != null) ? q.val : 0;
            int sum = jinwei + x + y;
            jinwei = sum / 10;
            cur.next = new ListNode(sum % 10);
            cur = cur.next;

            if (p != null) {
                p = p.next;
            }
            if (q != null) {
                q = q.next;
            }
        }

        if(jinwei > 0){
            cur.next = new ListNode(jinwei);
        }

        return dummyHead.next;
    }

4 No.15 3Sum

  • 固定一个值,找另外两个值,他们的和为0;
  • 如何找另外两个值: 双指针
  public static List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        for (int i = 0; i < nums.length - 2; i++) {
            if (i == 0 || (i > 0 && nums[i] != nums[i - 1])) { // 跳过可能重复的答案
                int head = i + 1, tail = nums.length - 1, sum = 0 - nums[i];
                while (head < tail) {
                    if (nums[head] + nums[tail] == sum) {
                        res.add(Arrays.asList(nums[i], nums[head], nums[tail]));
                        while (head < tail && nums[head] == nums[head + 1]) {
                            head++;
                        }
                        while (head < tail && nums[tail] == nums[tail - 1]) {
                            tail--;
                        }
                        head++;
                        tail--;
                    } else if (nums[head] + nums[tail] < sum) {
                        while (head < tail && nums[head] == nums[head + 1]) {
                            head++;
                        }
                        head++;
                    } else {
                        while (head < tail && nums[tail] == nums[tail - 1]) {
                            tail--;
                        }
                        tail--;
                    }
                }
            }
        }

        return res;
    }

    public static void main(String[] args) {
        int[] nums = {-1, 0, 1, 2, -1, -4};
        List<List<Integer>> res = threeSum(nums);

        System.out.println(res);

    }

在这里插入图片描述

5 No.55 Jump Game

5.1 法1:自顶向下的动态规划

  • 时间复杂度:O(N^2)
  • 空间复杂度:O(2N)
enum Index {
        GOOD, BAD, UNKNOWN
    }

    static Index[] status;

    public static boolean canJumpFromPosition(int position, int[] nums) {
        if (status[position] != Index.UNKNOWN) {
            return status[position] == Index.GOOD ? true : false;
        }

        int furthestJump = Math.min(position + nums[position], nums.length - 1);
        for (int nextPos = position + 1; nextPos <= furthestJump; nextPos++) {
            if (canJumpFromPosition(nextPos, nums)) {
                status[position] = Index.GOOD;
                return true;
            }
        }
        status[position] = Index.BAD;
        return false;
    }


    public static boolean canJump(int[] nums) {
        status = new Index[nums.length];
        for (int i = 0; i < status.length; i++) {
            status[i] = Index.UNKNOWN;
        }
        status[status.length - 1] = Index.GOOD;
        return canJumpFromPosition(0, nums);
    }

    public static void main(String[] args) {
        int[] nums1 = {2, 3, 1, 1, 4};
        boolean flag1  = canJump(nums1);
        System.out.println(flag1);

        int[] nums2 = {3,2,1,0,4};
        boolean flag2  = canJump(nums2);
        System.out.println(flag2);
    }

5.2 法2:自底向上的动态规划

  • 时间复杂度: O(N^2)
  • 空间复杂度: O(N)
   enum Index {
        GOOD, BAD, UNKNOWN
    }

    public boolean canJump(int[] nums) {

        Index[] status = new Index[nums.length];
        for (int i = 0; i < status.length; i++) {
            status[i] = Index.UNKNOWN;
        }

        status[nums.length - 1] = Index.GOOD;

        for (int i = status.length - 2; i >= 0; i--) {
            int furthestJump = Math.min(i + nums[i], nums.length - 1);
            for (int j = i + 1; j <= furthestJump; j++) {
                if(status[j] == Index.GOOD){
                    status [i] = Index.GOOD;
                    break;
                }
            }
        }

        return status[0] == Index.GOOD;

    }

    public static void main(String[] args) {
        int[] nums1 = {2, 3, 1, 1, 4};
        boolean flag1 = new JumpGame_45().canJump(nums1);
        System.out.println(flag1);

        int[] nums2 = {3, 2, 1, 0, 4};
        boolean flag2 = new JumpGame_45().canJump(nums2);
        System.out.println(flag2);
    }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值