Two Sum

  • HashMap中存储numbers[i]尚需要都少数值达到target,以及下表i

Implementation

public class Solution {public int[] twoSum(int[] numbers, int target) {HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();int[] result = new int[2];for (int i = 0; i < numbers.length; i++) {if (map.containsKey(numbers[i])) {int index = map.get(numbers[i]);result[0] = index;result[1] = i;break;} else {map.put(target - numbers[i], i);}}return result;}
}

Two Sum II - Input array is sorted

  • 设置两个指针j指向最大值,i指向最小值

    • sum > target: jdecrease
    • sum < target: iincrease

Implementation

public class Solution {public int[] twoSum(int[] numbers, int target) {int left = 0;int right = numbers.length - 1;int[] result = new int[2];while (left < right) {int sum = numbers[left] + numbers[right];if (sum == target) {result[0] = left + 1;result[1] = right + 1;break;} else if (sum < target) {left++;} else {right--;}}return result;}
}

Two Sum III - Data structure design

  • 设置一个ArrayList保存出现过的不重复的数值。

    • 遍历的时候比使用HashMapkeySet()更快
  • 设置一个HashMap保存出现过得不重复的数值,以及出现次数。
  • find()函数
    • 当value需要两个相同的数值得到时,判断该数值出现的次数是否大于1
    • 否则,判断HashMap是否包含这两个数值

Implementation

public class TwoSum {private HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();private ArrayList<Integer> list = new ArrayList<Integer>();// Add the number to an internal data structure.public void add(int number) {if (map.containsKey(number)) {map.put(number, map.get(number) + 1);}else {map.put(number, 1);list.add(number);}}// Find if there exists any pair of numbers which sum is equal to the value.public boolean find(int value) {for (int i = 0; i < list.size(); i++) {int num1 = list.get(i);int num2 = value - num1;if ((num1 == num2 && map.get(num1) > 1) || (num1 != num2 && map.containsKey(num2))) {return true;}}return false;}
}

转载于:https://www.cnblogs.com/Victor-Han/p/5192407.html

[LeetCode 001] Two Sum相关推荐

  1. leetcode 304. Range Sum Query 2D - Immutable |304. 二维区域和检索 - 矩阵不可变(二维前缀和问题)

    题目 https://leetcode.com/problems/range-sum-query-2d-immutable/ 题解 本题是 medium 难度,二维前缀和问题.相似题目有: Easy: ...

  2. 【动态规划】LeetCode 377. Combination Sum IV

    LeetCode 377. Combination Sum IV Solution1: 我的未能AC的答案 题目描述的和前几道题差不多,但实际上不能用DFS来做(会超时),要用动态规划,还是记录一下吧 ...

  3. 【DFS】LeetCode 39. Combination Sum

    LeetCode 39. Combination Sum Solution1: DFS,这个套路要熟记啊! class Solution { public:vector<vector<in ...

  4. LeetCode 167.Two Sum II 解题报告

    LeetCode 167.Two Sum II 解题报告 题目描述 Given an array of integers that is already sorted in ascending ord ...

  5. leetcode 1005 Maximize Sum Of Array After K Negations

    leetcode 1005 Maximize Sum Of Array After K Negations 1.题目描述 2.解题思路 3.Python代码 1.题目描述 给定一个整数数组 A,我们只 ...

  6. [勇者闯LeetCode] 1. Two Sum

    [勇者闯LeetCode] 1. Two Sum Description Given an array of integers, return indices of the two numbers s ...

  7. [勇者闯LeetCode] 112. Path Sum

    [勇者闯LeetCode] 112. Path Sum Description Given a binary tree and a sum, determine if the tree has a r ...

  8. 数据结构线段树介绍与笔试算法题-LeetCode 307. Range Sum Query - Mutable--Java解法

    此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 线段树(Segme ...

  9. LeetCode 653. Two Sum IV - Input is a BST--Python解法

    题目地址:Two Sum IV - Input is a BST - LeetCode Given a Binary Search Tree and a target number, return t ...

最新文章

  1. 《信息系统安全等级保护定级报告》
  2. 【科普】为什么985大学在大一上C语言课??
  3. 深度学习和目标检测系列教程 17-300: 3 个类别面罩检测类别数据集训练yolov5s模型
  4. SCI科技论文写作技巧-核心价值
  5. 华为计算机如何计算sina,一道华为的“简单四则运算”题
  6. websocket的加密和解密
  7. PhpStorm调用浏览器运行php文件
  8. 电脑报合订本_电脑报2018年合订本
  9. 正则表达式修正符的学习
  10. Java依赖包下载地址
  11. system.gc()和-XX:+DisableExplicitGC启动参数,以及DirectByteBuffer的内存释放
  12. android拖拽 字体变形,17种文字变形的创意方法
  13. Java内存模型(JMM)学习总结
  14. Java中抽象类的概述及其详解(什么是抽象类?为什么要使用抽象类?抽象类的特点?)
  15. ESP8266-01 MQTT固件烧录并连接阿里云服务器
  16. 如何通过JavaScript获取搜索引擎搜索关键词
  17. OpenCV基础篇——图形图像旋转
  18. 华三模拟器完成交换机堆叠IRF实验
  19. 和Android的第一次美丽邂逅
  20. Python 数据处理数据挖掘(六):决策树模型 之 CART算法

热门文章

  1. ZooKeeper典型应用场景一览
  2. java上机练习01
  3. ASP.NET2.0的multiview和wizard控件
  4. I、Q 通道幅相不平衡的影响及改善
  5. 如何修改远程登录端口号?
  6. latex Label ' ' multiply defined
  7. 处理音频文件的python模块
  8. markdown数学公式全集
  9. 解决通过vue-router打开tab页,下次进入还是上次history缓存的界面状态的问题
  10. Android权限处理分类