import java.util.HashMap;
import java.util.Map;

    /**
     * 分析:
     * 普通实现-嵌套循环两次,时间O(n2),空间O(1)
     * 复杂实现-循环一次,时间O(n),空间O(n)
     * 注意点:
     * 使用HashMap的containsKey函数和get函数
     * @param nums [2,7,11,15]
     * @param target 9
     * @return [0,1]
     */
    public int[] twoSum(int[] nums, int target) {
        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.containsKey(target-nums[i]) && i != map.get(target-nums[i])) {
                return new int[]{i, map.get(target-nums[i])};
            }
        }
        return null;
    }

Array - Two Sum的更多相关文章

  1. Leetcode: Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  2. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  3. Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  4. [Swift]LeetCode410. 分割数组的最大值 | Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  5. 动态规划——Split Array Largest Sum

    题意大概就是,给定一个包含非负整数的序列nums以及一个整数m,要求把序列nums分成m份,并且要让这m个子序列各自的和的最大值最小(minimize the largest sum among th ...

  6. Split Array Largest Sum LT410

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  7. 410. Split Array Largest Sum 把数组划分为m组,怎样使最大和最小

    [抄题]: Given an array which consists of non-negative integers and an integer m, you can split the arr ...

  8. [LeetCode] 410. Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. 【leetcode】410. Split Array Largest Sum

    题目如下: Given an array which consists of non-negative integers and an integer m, you can split the arr ...

  10. 410. Split Array Largest Sum

    做了Zenefits的OA,比面经里的简单多了..害我担心好久 阴险的Baidu啊,完全没想到用二分,一开始感觉要用DP,类似于极小极大值的做法. 然后看了答案也写了他妈好久. 思路是再不看M的情况下 ...

随机推荐

  1. 修改mac host文件绑定域名

    打开终端在终端terminal中输入 sudo vi /etc/hosts 上一步输入完成之后按enter回车键,如果当前用户账号有密码,则在按完之后会提示输入密码,此时输入当前账户密码后继续按ert ...

  2. Scipy实现图片去噪

    先贴要处理的图片如下 由图片显示可知: # 图片中存在噪声点,白色的圆环# 圆环上的数据和圆环里面和外面不同,所以可以显示出肉眼可识别的图片# 波动# 存在噪声的地方,波动比较大 # 傅里叶变换可以将 ...

  3. 使用BCG创建Pie

    1.新建一个BCG的对话框,添加一个文本框,并修改属性,添加成员变量,并设置Category为Control. 2.在class CCharPieDlg : public CBCGPDialog修改文 ...

  4. uoj#340. 【清华集训2017】小 Y 和恐怖的奴隶主(矩阵加速)

    传送门 uoj上的数据太毒了--也可能是我人傻常数大的缘故-- 三种血量的奴隶主加起来不超过\(8\)个,可以枚举每种血量的奴隶主个数,那么总的状态数只有\(165\)种,设\(dp_{t,i,j,k ...

  5. js组件化(转载)

    今天想着开始封装自己的UI库和组件库,从网上看到一篇很好的关于js组件化的文章,现在分享一下. 转载地址:https://blog.csdn.net/Prince_fmx/article/detail ...

  6. jsp内置对象作用域白话演示

    内置对象就是JSP中不需要自己定义和声明的对象,可以在JSP中直接使用.JSP中有9大内置对象,它们有两个常用的方法:setAttribute("key","value& ...

  7. Hexo写作系列(3) - 文章标题含有双引号"导致页面渲染失败无法打开

    问题 在用Hexo写文章时,如果文章标题含有双引号",也就是说如果在文件头里的title出现双引号,如下: --- title: Hexo - 文章标题含有双引号"导致页面渲染失败 ...

  8. css3椭圆运动

    通过使用css3实现让元素椭圆运动.而不是圆形运动. 效果1:http://sandbox.runjs.cn/show/ignefell 效果2:http://runjs.cn/code/w2wxjy ...

  9. JQuery入门一

    1.为什么要用jquery? 写起来简单,省事,开发效率高,兼容性好 2.什么是jQuery? jQuery是一个兼容多浏览器的JavaScript库 3.如何使用jQuery? 1.导入 <s ...

  10. Netty-flush

    TimerServer: ch.pipeline().addLast(new TimeEncoder()); ch.pipeline().addLast(new TimeServerHandler() ...