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;
    }   
相关推荐
稚南城才子,乌衣巷风流14 小时前
ST 表(Sparse Table)算法详解:原理、实现与应用
算法
hold?fish:palm14 小时前
9 找到字符串中所有字母异位词
c++·算法·leetcode
Sw1zzle14 小时前
算法入门(六):贪心算法 - 基础入门(Leetcode 121/455/860/376/738)
算法·leetcode·贪心算法
青山木14 小时前
Hot 100 --- 岛屿数量
java·数据结构·算法·leetcode·深度优先·广度优先
不会就选b15 小时前
算法日常・每日刷题--<归并排序>1
数据结构·算法
危桥带雨15 小时前
排序算法(快排、归并、计数、基数排序)
数据结构·算法·排序算法
啦啦啦啦啦zzzz15 小时前
算法:回溯算法
c++·算法·leetcode
IT探索15 小时前
Linux 查找文件指令总结
linux·算法
攻城狮Soar15 小时前
C++子类访问父类成员
c++·算法