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;
    }   
相关推荐
小L~~~7 小时前
基于贪心策略的混合遗传算法求解01背包问题
python·算法
洛水水8 小时前
【力扣100题】53.最长回文子串
算法·leetcode·职场和发展
jieyucx8 小时前
Go 语言 sort 包详解:从基础排序到自定义排序(含底层原理+零基础看懂)
算法·golang·排序算法·sort
仙俊红8 小时前
Integer\int对比,equals()\hashcode面试
java·面试·职场和发展
叁散9 小时前
ESP32 LCD1602显示实验报告
算法
过期动态9 小时前
【LeetCode 热题 100】盛最多水的容器
java·数据结构·spring boot·算法·leetcode·spring cloud·职场和发展
凌波粒9 小时前
LeetCode--700.二叉搜索树中的搜索(二叉树)
算法·leetcode·职场和发展
君为先-bey9 小时前
LeMiCa——基于扩散模型的高效视频生成的词典序最小化路径缓存
python·算法·机器学习·扩散模型
洛水水9 小时前
【力扣100题】58.轮转数组
算法·leetcode
资深流水灯工程师10 小时前
LMS 最小均方算法在 DSP 上的 C 语言实现
算法