Leetcode——1. Two Sum
2019独角兽企业重金招聘Python工程师标准>>>
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.
Example:
Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
UPDATE (2016/2/13):
The return format had been changed to zero-based indices. Please read the above updated description carefully.
import java.util.Hashtable;
public class Solution {public int[] twoSum(int[] nums, int target) {int index[]=new int[2];Hashtable<Integer, Integer> hashTable = new Hashtable<Integer, Integer>();for(int item=0;item<nums.length;item++){if(hashTable.get(nums[item])==null&&hashTable.get(target-nums[item])==null){hashTable.put(nums[item], item);}else if(hashTable.get(target-nums[item])!=null){index[1] = item;index[0] = hashTable.get(target-nums[item]);break;}}return index;}
}
转载于:https://my.oschina.net/wtzheng/blog/646738
Leetcode——1. 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 ...
- LeetCode 167. Two Sum II - Input array is sorted--Python解法
题目地址:Two Sum II - Input array is sorted - LeetCode Given an array of integers that is already sorted ...
最新文章
- SAP BADI应用
- TabelView嵌套CollectionView高度适应
- mysql amoeba 配置_Amoeba新版本MYSQL读写分离配置
- 8个超震撼的HTML5和纯CSS3动画源码
- jupyter notebook切换到其他配置好的conda虚拟环境
- php this validate,php扩展ZF——Validate扩展
- linux源码Makefile详解(完整)-转
- 巴西矿坝决堤事故已致58人死亡 多达300人失踪
- NDK开发基础④增量更新之客户端合并差分包
- hp440g5拆机_HP 惠普 ProBook 440 G5 评测 | ZMMOO
- 虎牙代理php,huya虎牙PHP
- 【前端】vuecli项目引入ant-design-vue并用ant-design-vue配置阿里巴巴矢量图标库
- win7计算机管理快捷键,win7系统中的常用快捷键
- gdb调试查看内存数据
- php开发证书转换工具,各类证件生成器
- 计算机连接电视显示超范围,HDMI连接后电脑操作界面的边框超出电视屏幕,怎么解决...
- ai人工智能将替代人类_AI再次击败人类
- 结合xxx.cpp:(.text+0x55):对‘xxx’未定义的引用剖析Linux环境下动态库.so文件
- Ubuntu: failed to fetch
- 【python之父】:从他的经历中我又重拾对编程的热爱,程序员也不是一直写代码就没有出路
热门文章
- html5--6-13 CSS3中的颜色表示方式
- 懒加载方式下的单例模式
- (原+转)使用opencv的DFT计算卷积
- 2014仲秋校招之面试篇
- CDialog中使用工具栏和状态栏
- 排除“使用主题 css 文件要求页上有标头控件。(例如 head runat=server /)”错误...
- QQ号双主号要求验证码解决方法
- 台式计算机睡眠了怎么唤醒,台式电脑睡眠了怎么唤醒
- vs2005 c# mysql_在VS2010中怎样用C#创建数据库联接并执行sql语句 最好举个例子讲一下...
- postman 不安全网站_Postman所有版本下载_Postman for windows x64 mac下载