LeetCode 1 Two sum Java
来源:互联网 发布:淘宝网店装修工具 编辑:程序博客网 时间:2024/06/16 21:49
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
空间换时间:
程序转自:https://leetcode.com/discuss/41366/my-short-java-solution-o-n-hashmap
public class Solution { public int[] twoSum(int[] nums, int target) { HashMap<Integer, Integer> tracker = new HashMap<Integer, Integer>(); int len = nums.length; for(int i = 0; i < len; i++){ if(tracker.containsKey(nums[i])){ int left = tracker.get(nums[i]); return new int[]{left+1, i+1}; }else{ tracker.put(target - nums[i], i); } } return new int[2]; }}
0 0
- LeetCode 1 Two sum Java
- [leetcode-1]Two Sum(java)
- LeetCode 1 - Two Sum ( JAVA )
- LeetCode【1】-Two Sum JAVA
- leetcode#1-Two Sum-java
- LeetCode: Two Sum (Java)
- [Leetcode] Two Sum (Java)
- 【Leetcode】two sum JAVA
- LeetCode java Two Sum
- Two Sum leetcode Java
- [LeetCode][Java] Two Sum
- leetcode:Two Sum 【Java】
- LeetCode|Two Sum-java
- leetcode Two Sum(Java)
- [leetcode NO.1] Two Sum (JAVA)
- LeetCode 1 Two Sum for java
- Leetcode-1 Two Sum(Java) -by zzy
- 【leetcode with java】1 Two Sum
- C++中的Cast的用法总结
- 设计模式(1): 工厂模式
- 查看当前用户的权限信息SQL SELECT * FROM SESSION_PRIVS;
- android使用PHP上传图片到服务器
- rstudio-server安装问题解决笔记
- LeetCode 1 Two sum Java
- 远程视频基础
- Android开发经验
- PAT 1023. Have Fun with Numbers (20)
- Java-初始化过程
- spring aop日志(两种方式)
- Linux内核模块:模块的编译
- MONGODB高可用Repl Set+Sharding配置
- 小米运维讨论