LeetCode算法题

LeetCode55. 跳跃游戏

给定一个非负整数数组 nums ,你最初位于数组的 第一个下标 。数组中的每个元素代表你在该位置可以跳跃的最大长度。
判断你是否能够到达最后一个下标。

示例 1:
输入:nums = [2,3,1,1,4]
输出:true
解释:可以先跳 1 步,从下标 0 到达下标 1, 然后再从下标 1 跳 3 步到达最后一个下标。

示例 2:
输入:nums = [3,2,1,0,4]
输出:false
解释:无论怎样,总会到达下标为 3 的位置。但该下标的最大跳跃长度是 0 , 所以永远不可能到达最后一个下标。

1、动态规划

class Solution {
    public boolean canJump(int[] nums) {
        int arrLength = nums.length;
        boolean[] ifReach = new boolean[arrLength]; //创建时,值全为false
        ifReach[0] = true; //initialization

        for (int arrIndex = 1; arrIndex < arrLength; ++arrIndex){
            ifReach[arrIndex] = false;

            //i从0开始,表示arr的下标,自己的值与自己下标相加i + arr[i],看是否能比当前arrIndex大于或等于。
            for (int i = 0; i < arrIndex; ++i){
                //满足条件,表示可以到达,赋值ifReach为true。
                //不满足条件则 i自增1,直到arrIndex为止,全不满足就是false
                if (ifReach[i] && i + nums[i] >= arrIndex){
                    ifReach[arrIndex] = true;
                    break;
                }
            }
        }
        return ifReach[arrLength - 1];
    }
}

2、贪心算法

思想:先求局部最优解,在求整体最优解。可能失效。

class Solution {
    public static boolean canJump(int[] nums) {
        //贪心:存储当前位置所能到达的最大位置(局部最优),初始在nums[0]
        int maxJump = nums[0];
        //之后如果可以到达更远位置,则存入maxJump
        for (int i = 0; i < nums.length; i++) {
            if (maxJump < i + nums[i] && maxJump >= i){
                maxJump = i + nums[i];
            }
        }
        if (maxJump >= nums.length - 1) return true;
        else return false;
    }
}

LeetCode1.两数之和

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。

示例 1:
输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。

示例 2:
输入:nums = [3,2,4], target = 6
输出:[1,2]

示例 3:(数据可重复,但只能用一次)
输入:nums = [3,3], target = 6
输出:[0,1]

解:利用哈希表的key值进行直接查找,将数据存入key值(key值重复时,value被覆盖),索引存入value。
用target - nums[i],所求值减去当前值,在key中查找该值。

1、哈希表

先全部存入哈希表,再进行查找。(数据只可用一次)

import java.util.Hashtable;
class Solution {
    public int[] twoSum(int[] nums, int target) {
        Hashtable hashtable = new Hashtable<Integer, Integer>();
        for (int i = 0; i < nums.length; i++) {
            hashtable.put(nums[i],i);
        }
        for (int i = 0; i < nums.length; i++) {
            int diff = target - nums[i];
            //如果哈希表中key值相同,索引value会被覆盖。两数相加不影响,因为返回的是i。
            //如示例3,当有两数相同时,哈希表中有diff,此时hashtable.get(diff) != i 一定为true。
            //因为diff对应的索引i是被覆盖后的索引,比较的索引是没有覆盖的索引(相同的key值的第一次索引)。所以不相等。
            if (hashtable.containsKey(diff) 
                    && (int)hashtable.get(diff) != i) { //数据key值只能用一次。当前索引不能等于获取的索引:取了两次值
                return new int[]{i, (int) hashtable.get(diff)};
            }
        }
        return new int[0];
    }
}

2、哈希表优化

因为数据不可重复取,且只有唯一解

class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; ++i) {
        	//开始时哈希表是空的,在循环中加入数据,没加入一个就与之前加入的数据匹配查找。
            if (hashtable.containsKey(target - nums[i])) {
                return new int[]{hashtable.get(target - nums[i]), i};	//符合后返回数据。
            }
            //没有找到对应数据,则把当前数据加入哈希表。可以避免一个值被用两次,且减少循环。
            hashtable.put(nums[i], i);
        }
        return new int[0];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值