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;
    }   
相关推荐
Nil20820 小时前
leetcode 199二叉树的右视图
算法·leetcode·深度优先
M78佐菲21 小时前
c语言学习笔记:排序与查找方法整理
linux·c语言·笔记·学习·算法
AI备案指南-满满1 天前
人工智能拟人化互动服务安全自评估报告的评估要点有哪些?
人工智能·算法·安全·机器人·大模型备案·算法备案
土司大王1 天前
LeetCode hot100——两两交换链表中的节点
算法·leetcode·职场和发展
大熊背1 天前
树莓派IspPipeline LSC模块原理详解
算法·lsc·isppipeline·mesh lsc
测试19981 天前
Selenium 无法定位元素的几种解决方案
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
zander2581 天前
LeetCode 300. 最长递增子序列
算法·leetcode·深度优先
欧叶冲冲冲1 天前
Python常见数据结构的CRUD(LeetCode高频版速查)
数据结构·python·leetcode
祖力551 天前
Linux应用软件编程:目录IO与framebuffer
linux·运维·算法·framebuffer·目录io