Two Sum
来源:互联网 发布:网页调用excel数据 编辑:程序博客网 时间:2024/06/03 19:23
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1].
解决方案
public int[] twoSum(int[] numbers, int target) { int[] result = new int[2]; Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < numbers.length; i++) { if (map.containsKey(target - numbers[i])) { result[1] = i + 1; result[0] = map.get(target - numbers[i]); return result; } map.put(numbers[i], i + 1); } return result;}
https://discuss.leetcode.com/topic/2447/accepted-java-o-n-solution
阅读全文
0 0
- Two Sum
- Two Sum
- Two Sum
- two sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- 微软亚洲研究院童欣:VR/AR 里的手势交互到底难在哪儿
- 高性能服务器编程之epoll
- 使用lombok消除冗余代码
- 期末考试 编程题#3:计算数列平方和(Coursera 程序设计与算法 专项课程3 C++程序设计 郭炜、刘家瑛;函数对象作参数)
- Idea2017部署项目到tomcat时没有artifacts解决办法
- Two Sum
- 最近编写过程中遇到的问题总结(在练习qt pcl vs vtk的融合中)
- PAT (Basic Level) Practise (中文)1045. 快速排序(25)
- JDK环境变量配置
- (8)使用p命名空间简化setter注入
- 算法导论2-1.3:n个数的一个序列A中查找v
- 自学Python之Python基础:(六)可迭代对象与反迭代技巧
- wxWidgets教程(17)——wxSqlite3用法
- PHP基础之字符串(5)