leetcode---TwoSum
来源:互联网 发布:穿越小说改编的网络剧 编辑:程序博客网 时间:2024/06/05 15:51
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 { vector<int> result;public: vector<int> twoSum(vector<int>& nums, int target) { if(nums.size()<2) return result; // int length=nums.size(); // sort(nums.begin(),nums.end()); // auto index=find_if(nums.begin(),nums.end(),[target](int &data){return target<=data;}); for(auto beg1=nums.begin();beg1!=nums.end()-1;beg1++){ for(auto beg2=beg1+1;beg2!=nums.end();beg2++){ if(*beg1+*beg2==target){ result.push_back(beg1-nums.begin()); result.push_back(beg2-nums.begin()); break; } } // if(result.size()>0) break; } return result; } };
for(auto beg=nums.begin();beg!=nums.end()-1;beg++){ int anothernum=target-*beg; auto f=find(nums.begin()+(beg-nums.begin())+1,nums.end(),anothernum); if(f!=nums.end()){ result.push_back(beg-nums.begin()); result.push_back(f-nums.begin()); break; } }
阅读全文
0 0
- [leetcode]-twoSum
- [leetcode]twoSum
- LeetCode-TwoSum
- 【Leetcode】TwoSum
- LeetCode - TwoSum
- leetcode :twoSum
- leetcode TwoSum
- leetcode twosum
- leetcode--twoSum
- LeetCode twoSum
- LeetCode-TwoSum
- leetcode--twoSum
- leetcode TwoSum
- TwoSum leetcode
- leetcode:twosum
- leetcode:TwoSum
- LeetCode---TwoSum
- LeetCode-TwoSum
- IOS AFNetworking各种功能封装类
- baidu apollo(1)—启动环境分析
- EasyDemo*ExpandableListView及其子类体系结构图(on Github)
- C/C++ 函数指针、虚函数、纯虚函数
- Clojure Applied.pdf 英文原版 免费下载
- leetcode---TwoSum
- 真·sd卡存储
- [毕业设计]第一弹--macbook air上安装ubuntu双系统
- SpringMvc框架介绍
- [英语]关于人生的10个残酷真相, 希望你越早知道越好
- (二)HotSpot虚拟机对象
- LR如何在语言理解NLU中实现文本分类
- EasyDemo*Adapter接口及其实现类体系结构图(on Github)
- 简单的过滤器模糊查询方法