我是靠谱客的博主 耍酷星月,这篇文章主要介绍Leetcode1,现在分享给大家,希望可以做个参考。

Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

class Solution {

    public int[] twoSum2(int[] numbers, int target) {
         //返回int类型的数组
        int[] result = new int[2];
         //声明map
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < numbers.length; i++) {
            if (map.containsKey(target - numbers[i])) {
                result[1] = i ;
                result[0] = map.get(target - numbers[i]);
                return result;
            }
            map.put(numbers[i], i );
        }
        return result;
    }
}

最后

以上就是耍酷星月最近收集整理的关于Leetcode1的全部内容,更多相关Leetcode1内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(123)

评论列表共有 0 条评论

立即
投稿
返回
顶部