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;
    }   
相关推荐
不会就选b5 小时前
算法日常・每日刷题--<贪心>14
算法
mmmmath_38 小时前
LeetCode.541.反转字符串II
数据结构·算法·leetcode
Navigator_Z8 小时前
LeetCode //MySQL - 1251. Average Selling Price
c语言·算法·leetcode
醇氧9 小时前
MySQL 8.0 系统表损坏与引擎转换故障排查实战
数据结构·算法
大熊背9 小时前
《Color constancy by characterization of illumination chromaticity》之色度色域最大化算法(二)
算法·白平衡·色度·色温
钓鱼的肝9 小时前
梳理(1-5)
c++·经验分享·笔记·算法·青少年编程
参.商.10 小时前
【Day 53】76. 最小覆盖子串
leetcode·golang
HZZD_HZZD10 小时前
CSDN_批发市场水电漏损归因算法LAM的原理与落地
嵌入式硬件·物联网·算法
shirsl11 小时前
算法 Day 5 树 / 二叉树 + DFS
数据结构·python·算法