用 Hash Table 实现

Time : O(n)

Space: O(n)

import java.util.HashMap;public class Solution {public int[] twoSum(int[] nums, int target) {HashMap< Integer, Integer> map = new HashMap<>();for (int i = 0; i < nums.length; i++) {if (map.containsKey(target-nums[i])) {int[] result = {map.get(target-nums[i])+1, i+1};return result;}map.put(nums[i], i);}return null;}public static void main(String[] args) {Solution solu = new Solution();int[] a = {2, 7, 11, 15};int target = 9;int[] result = solu.twoSum(a, target);for (int i = 0; i < result.length; i++)System.out.println(result[i]);}
}

2015-10-13

转载于:https://www.cnblogs.com/whuyt/p/4875503.html

LeetCode 1:Two Sum相关推荐

  1. Leetcode题目:Range Sum Query - Immutable

    题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...

  2. LeetCode OJ:Combination Sum III(组合之和III)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. LeetCode小白菜笔记[1]:Two Sum

    LeetCode小白菜笔记[1]:Two Sum 1. Two Sum [Easy] 题目: Given an array of integers, return indices of the two ...

  4. LeetCode实战:二叉树中的最大路径和

    背景 为什么你要加入一个技术团队? 如何加入 LSGO 软件技术团队? 我是如何组织"算法刻意练习活动"的? 为什么要求团队的学生们写技术Blog 题目英文 Given a non ...

  5. LeetCode实战:最大子序和

    题目英文 Given an integer array nums, find the contiguous subarray (containing at least one number) whic ...

  6. LeetCode实战:字符串相加

    题目英文 Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and ...

  7. LeetCode实战:最接近的三数之和

    题目英文 Given an array nums of n integers and an integer target, find three integers in nums such that ...

  8. LeetCode实战:三数之和

    题目英文 Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? ...

  9. LeetCode实战:快乐数

    题目英文 Write an algorithm to determine if a number is "happy". A happy number is a number de ...

最新文章

  1. Delphi XE5 常见问题解答
  2. 图像检索:几种基于纹理特征的图像检索算法
  3. Alpha(5/10)
  4. wave文件 fft_STM32F103 如何实现 FFT?
  5. Android开发之设置listview分割线的颜色
  6. java 两个线程同步_Java 多线程(二)—— 线程的同步
  7. iKcamp团队制作|基于Koa2搭建Node.js实战(含视频)☞ 中间件用法
  8. php cms 那个安全,PHPCMS系统安全设置步骤
  9. 北航计算机组成原理课程设计-2020秋 PreProject-Logisim-Logisim仿真与调试应用与挑战
  10. 下载安装MinGW-w64详细步骤(c/c++的编译器gcc的windows版,win10真实可用)
  11. l05173芯片针脚图_芯片引脚图及引脚描述
  12. 十大python开发软件-介绍6款实用的python编程开发工具
  13. MySQL生成36位、32位UUID以及32位大写的UUID
  14. description The server encountered an internal error that prevented it from fulfilling this request错
  15. ubuntu换源 树莓派
  16. 目标检测与位姿估计(五):A Survey: Visual Place Recognition
  17. SAP WM上架策略为Fixed Bin的Storage Type如何以FIFO出库?
  18. 《寄居者》 - 严歌苓
  19. BMP文件RGB颜色数据存放方式
  20. IE浏览器下实现rtmp、rtsp协议视频播放

热门文章

  1. python爬虫不错的文章
  2. Fiddler代理手机抓包
  3. 2018-12-25 数据库插入数据
  4. 2057. [ZLXOI2015]殉国
  5. leetcode之Divide Two Integers
  6. cursor_sharing用法
  7. 四说大数据时代“神话”:从大数据到深数据
  8. Eclipse错误提示: Symbol 'xxxx' could not be resolved
  9. Flutter - International 国际化,Localization 本地化, 使用Intl
  10. Java EE(五)