LeetCode【1】两数之和

题目:

代码:

java 复制代码
public int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];

        Map<Integer, Integer> map = new HashMap<>();

//        for (int i = 0; i < nums.length; i++) {    // 这么写不能防重复啊!注意这里不要提前放好
//            map.put(nums[i], i);
//        }

        for (int i=0;i<nums.length;i++) {
            if (map.get(target-nums[i]) != null) {
                result[0] = i;
                result[1] = map.get(target-nums[i]);
            }
            map.put(nums[i], i);
        }
        return result;
    }   
相关推荐
风中的微尘17 分钟前
数学基础2
算法
一只小小的芙厨1 小时前
树状数组总结
数据结构·算法
郝学胜-神的一滴3 小时前
Effective STL 条款 17:用 swap 技巧去掉多余容量
c语言·开发语言·数据结构·c++·算法·rpc
玩转单片机与嵌入式4 小时前
深入浅出TinyML 36:如何验证一个TinyML系统真正具备工程应用价值?
网络·人工智能·算法
Nazarick5 小时前
力扣LeetCode热题75题(1)
java·算法·leetcode·力扣
16月6日-晴6 小时前
斐波那契数列题解
算法·leetcode
郝学胜-神的一滴6 小时前
Effective Python 条款47:吃透惰性属性
大数据·开发语言·数据库·人工智能·python·算法·oracle
Keven_118 小时前
算法札记:有限差分
算法
忙里偷闲学python8 小时前
GK2A 林火蔓延预报算法说明
算法
我是伪码农8 小时前
11.21程序设计
数据结构·算法