LeetCode:Tow Sum程序以及分析
2017-10-08 23:02
260 查看
URL:https://leetcode.com/problems/two-sum/description/
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:
Code:
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].
Code:
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { map<int,int> array; vector<int> result; for(int i = 0;i < nums.size(); i ++) { array[nums[i]] = i; } for(int i = 0;i < nums.size(); i ++) { result.push_back(i); int dif = target - nums[i]; if(array.find(dif) != array.end() && i != array[dif]) { result.push_back(array[dif]); return result; } else result.pop_back(); } } };
相关文章推荐
- Android 软件安装程序(*.apk)的结构分析、反编译以及汉化
- 通过一个气象分析程序来掌握二维数组以及多维数组的应用
- Commons-net FTPClient completePendingCommand()经常使程序死掉的原因分析以及解决方式
- xhprof 的安装方法以及xhprof 的使用方法(分析php程序)
- Monkey源码分析4—程序入口以及初始化命令行参数
- Leetcode各题目的难度以及出现频率分析
- Android:分析app、Process、Task之间的区别,以及获取最近运行的程序(近期任务)与正在运行的程序
- C#WinForm程序最小化后在托盘中隐藏方法以及几个关键点的简单分析
- 分析obj文件,理解重定位过程以及obj文件在程序中扮演的角色
- 位图文件(BMP)格式分析以及程序实现
- Linux中程序的栈帧分析以及修改函数地址
- 【Storm】storm安装、配置、使用以及Storm单词计数程序的实例分析
- Commons-net FTPClient completePendingCommand()经常使程序死掉的原因分析以及解决方式
- 用xdebug分析PHP以及结果分析程序webgrind的使用
- Android 软件安装程序(*.apk)的结构分析、反编译以及汉化
- 字符指针的初始化以及程序分析实例
- c和指针 前三章知识点以及常见的问题和程序分析
- Tga图片格式分析以及程序实现
- 图片资源加载路径分析以及netbeans打包java程序,并包含图片资源
- Tga图片格式分析以及程序实现