Two Sum
来源:互联网 发布:php 上传类 编辑:程序博客网 时间:2024/04/19 13:19
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].
Solution
public class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); int[] rs = new int[2]; for(int index = 0; index < nums.length; index++){ map.put(nums[index], index); } for(int index = 0; index < nums.length; index++){ if(map.get(target-nums[index]) != null && index != map.get(target-nums[index])){ rs[0] = index; rs[1] = map.get(target-nums[index]); break; } } return rs; }}
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
- JS实现简单的登录界面(不连接数据库,把用户名密码写死)
- 安卓之插件化开发使用DexClassLoader&AssetManager来更换皮肤
- 简单选择排序算法
- Linux多媒体开发基础
- theano深度学习实现:LR模型代码讲解
- Two Sum
- bat文件执行java
- MySQL运行状态show status详解
- Employee SaveEmployee(Employee e)
- 渗透攻防Web篇
- 文章标题
- 什么是P问题、NP问题和NPC问题
- 【数学】n阶幻方
- 谷歌浏览器和大一暑假心得