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;
    }   
相关推荐
Victory_orsh1 小时前
“自然搞懂”深度学习(基于Pytorch架构)——010203
人工智能·pytorch·python·深度学习·神经网络·算法·机器学习
CoovallyAIHub1 小时前
突破360°跟踪极限!OmniTrack++:全景MOT新范式,HOTA指标狂飙43%
深度学习·算法·计算机视觉
得物技术1 小时前
得物管理类目配置线上化:从业务痛点到技术实现
后端·算法·数据分析
CoovallyAIHub2 小时前
首个大规模、跨模态医学影像编辑数据集,Med-Banana-50K数据集专为医学AI打造(附数据集地址)
深度学习·算法·计算机视觉
熬了夜的程序员2 小时前
【LeetCode】101. 对称二叉树
算法·leetcode·链表·职场和发展·矩阵
却道天凉_好个秋2 小时前
目标检测算法与原理(二):Tensorflow实现迁移学习
算法·目标检测·tensorflow
柳鲲鹏3 小时前
RGB转换为NV12,查表式算法
linux·c语言·算法
橘颂TA3 小时前
【剑斩OFFER】算法的暴力美学——串联所有单词的字串
数据结构·算法·c/c++
Kuo-Teng3 小时前
LeetCode 73: Set Matrix Zeroes
java·算法·leetcode·职场和发展