leetcode——1——Two Sum
来源:互联网 发布:单片机电源模块电路图 编辑:程序博客网 时间:2024/06/07 01:15
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].
class Solution{ public: vector<int> twoSum(vector<int> &numbers, int target) { vector<int> num = numbers; std::sort(num.begin(), num.end()); int length = numbers.size(); int left = 0; int right = length - 1; int sum = 0; vector<int> index; while(left < right) { sum = num[left] + num[right]; if(sum == target) { // find the index from the old array for(int i = 0; i < length; ++i) { if(numbers[i] == num[left]) { index.push_back(i); } else if(numbers[i] == num[right]) { index.push_back(i); } if(index.size() == 2) { break; } } break; } else if(sum > target) { --right; } else { ++left; } } return index; }};
0 0
- LeetCode — Two Sum
- LeetCode—Two Sum
- LeetCode—Two Sum
- leetcode—Two Sum
- LeetCode —-Two Sum
- Sum—LeetCode-1 Two Sum
- LeetCode——Two Sum
- LeetCode——Two Sum
- Leetcode —— Two Sum
- LeetCode——Two Sum
- Two Sum —— Leetcode
- LeetCode——Two Sum
- Two Sum —— leetcode
- LeetCode——Two Sum
- LeetCode:Algorith1 —Two Sum
- #1 LeetCode——Two Sum
- Leetcode 1——Two Sum
- LeetCode之1—-Two Sum
- 将web应用部署到Linux下面的tomcat服务器
- C# winform程序防止前台卡死
- 关于Adroid Studio在64位系统上不能打开的问题
- 设计模式之备忘录模式
- 0-1背包问题
- leetcode——1——Two Sum
- 单例模式
- Android-银联支付开发
- css盒子模型+小三角
- Fedoera 20 安装 samba 服务器.md
- GCC的内嵌汇编语法 AT&T汇编语言语法
- 问题 I: 函数---判断某年某月某日是这一年中的第几天
- Hosts
- [BZOJ2456] mode - 贪心