LeetCOde OJ Two Sum map应用
来源:互联网 发布:域名城论坛 编辑:程序博客网 时间:2024/06/06 09:22
Two Sum
Total Accepted: 73626 Total Submissions: 410357Given 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
class Solution {public: vector<int> twoSum(vector<int> &numbers, int target) { vector<int> result; map<int,int> mm;//数 和 位置 if(numbers.size()<2) return result; for(int i=0;i<numbers.size();i++){ if(mm.count(target-numbers[i])) { result.push_back(mm[target-numbers[i]]+1); result.push_back(i+1); return result; }else{ mm.insert(make_pair(numbers[i],i)); } } }};
public class Solution { public int[] twoSum(int[] nums, int target) { int[] result =new int[2]; HashMap<Integer,Integer> mm=new HashMap<Integer,Integer>(); if(nums.length<2) return result; for(int i=0;i<nums.length;i++){ if(mm.get(target-nums[i])!=null){ result[0]=mm.get(target-nums[i])+1; result[1]=i+1; break; }else{ mm.put(nums[i],i); } } return result; }}
0 0
- LeetCOde OJ Two Sum map应用
- LeetCode OJ:Two Sum
- LeetCode OJ : two sum
- LeetCode OJ -Two Sum
- LeetCode OJ Two Sum
- [LeetCode OJ] Two Sum
- Two Sum LeetCode OJ
- LeetCode OJ - Two Sum
- LeetCode OJ ----Two Sum
- LeetCode OJ--Two Sum
- Two Sum (LeetCode OJ)
- LeetCode OJ:Two sum
- [LeetCode OJ][001]Two Sum
- 【LeetCode OJ 001】Two Sum
- LeetCode OJ 1Two Sum
- LeetCode OJ 01 Two Sum
- LeetCode OJ : 1 Two Sum
- LeetCode OJ 1. Two Sum
- java 防止 XSS 攻击的常用方法总结
- XListView使用指南
- MySql存储引擎
- Unity中添加组件的几种方法
- 求100以内的质数
- LeetCOde OJ Two Sum map应用
- phpcms v9最实用的23个调用代码
- 黑马程序员——IOS基础---Objective-C第二段视频学习---面向对象---基本语法
- 2015-3-23: (2)apache 的 prefork 和 worker 模式
- 第三周项目五 数组做数据成员(文件读取+多文件)
- tween.js 用户指南
- 住房公积金约定提取业务问答
- android学习之使用pull方式解析xml
- 反射