[LeetCode 001] Two Sum
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
:j
decreasesum < target
:i
increase
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
保存出现过的不重复的数值。- 遍历的时候比使用
HashMap
的keySet()
更快
- 遍历的时候比使用
- 设置一个
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相关推荐
- leetcode 304. Range Sum Query 2D - Immutable |304. 二维区域和检索 - 矩阵不可变(二维前缀和问题)
题目 https://leetcode.com/problems/range-sum-query-2d-immutable/ 题解 本题是 medium 难度,二维前缀和问题.相似题目有: Easy: ...
- 【动态规划】LeetCode 377. Combination Sum IV
LeetCode 377. Combination Sum IV Solution1: 我的未能AC的答案 题目描述的和前几道题差不多,但实际上不能用DFS来做(会超时),要用动态规划,还是记录一下吧 ...
- 【DFS】LeetCode 39. Combination Sum
LeetCode 39. Combination Sum Solution1: DFS,这个套路要熟记啊! class Solution { public:vector<vector<in ...
- LeetCode 167.Two Sum II 解题报告
LeetCode 167.Two Sum II 解题报告 题目描述 Given an array of integers that is already sorted in ascending ord ...
- leetcode 1005 Maximize Sum Of Array After K Negations
leetcode 1005 Maximize Sum Of Array After K Negations 1.题目描述 2.解题思路 3.Python代码 1.题目描述 给定一个整数数组 A,我们只 ...
- [勇者闯LeetCode] 1. Two Sum
[勇者闯LeetCode] 1. Two Sum Description Given an array of integers, return indices of the two numbers s ...
- [勇者闯LeetCode] 112. Path Sum
[勇者闯LeetCode] 112. Path Sum Description Given a binary tree and a sum, determine if the tree has a r ...
- 数据结构线段树介绍与笔试算法题-LeetCode 307. Range Sum Query - Mutable--Java解法
此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 线段树(Segme ...
- 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 ...
最新文章
- 《信息系统安全等级保护定级报告》
- 【科普】为什么985大学在大一上C语言课??
- 深度学习和目标检测系列教程 17-300: 3 个类别面罩检测类别数据集训练yolov5s模型
- SCI科技论文写作技巧-核心价值
- 华为计算机如何计算sina,一道华为的“简单四则运算”题
- websocket的加密和解密
- PhpStorm调用浏览器运行php文件
- 电脑报合订本_电脑报2018年合订本
- 正则表达式修正符的学习
- Java依赖包下载地址
- system.gc()和-XX:+DisableExplicitGC启动参数,以及DirectByteBuffer的内存释放
- android拖拽 字体变形,17种文字变形的创意方法
- Java内存模型(JMM)学习总结
- Java中抽象类的概述及其详解(什么是抽象类?为什么要使用抽象类?抽象类的特点?)
- ESP8266-01 MQTT固件烧录并连接阿里云服务器
- 如何通过JavaScript获取搜索引擎搜索关键词
- OpenCV基础篇——图形图像旋转
- 华三模拟器完成交换机堆叠IRF实验
- 和Android的第一次美丽邂逅
- Python 数据处理数据挖掘(六):决策树模型 之 CART算法