Two Sum
来源:互联网 发布:金山软件股价 编辑:程序博客网 时间:2024/06/05 01:54
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].
class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { int x1 = 0; int x2 = x1 + 1; vector<int> v1; for(;;) { if(nums[x1] + nums[x2] == target) { v1.push_back(x1); v1.push_back(x2); return v1; } else { if(x2 == nums.size()-1) { x1++; x2 = x1+1; } else x2++; } } }};备注:1 题目条件中给定了输入一定有解,而且只有唯一解,所以不需要考虑x1溢出的请客 2 因为是遍历所有情况,所以x2的最大值只会为nums.size() -1 因此不需要用》= 的条件
阅读全文
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
- TX2017秋招笔试题之geohash编码
- Pointers on C——11 Dynamic Memory Allocation.2
- hibernate4.3获取session的方法
- 欢迎使用CSDN-markdown编辑器
- react-native-redux 简易教程
- Two Sum
- python机器学习及实战代码13-16,程序运行时出现提醒及修改
- 中国地理-一些知识的总结
- Pointers on C——11 Dynamic Memory Allocation.3
- JDK下载、安装和环境变量配置
- vm虚拟机安装centos7,配置网卡
- springmvc报400错误,并用@inintBinder解决类型转换问题
- Android AsyncTask源码分析
- Pointers on C——11 Dynamic Memory Allocation.4