LeetCode 1 Two Sum
开始刷LeetCode
刷完换工作!!!
先排序后左右搜索
struct Element
{int iNumber;int iIndex;
};bool compareFunc(const Element& arg1, const Element& arg2)
{return arg1.iNumber<arg2.iNumber;
}class Solution {
public:vector<int> twoSum(vector<int> &numbers, int target) {vector<int> results;vector<Element> vInput;vector<int>::iterator itv = numbers.begin();int i =0;Element temp;while(itv!=numbers.end()){temp.iNumber = *itv;temp.iIndex = ++i;vInput.push_back(temp);}sort(vInput.begin(),vInput.end(),compareFunc);vector<Element>::iterator itv1 = vInput.begin();vector<Element>::iterator itv2 = vInput.end();itv2--;while(itv1<=itv2 && itv1->iNumber + itv2->iNumber!=target){if(itv1->iNumber+itv2->iNumber<target)itv1++;if(itv1->iNumber+itv2->iNumber>target)itv2--;}if(itv1->iNumber + itv2->iNumber== target){if(itv1->iIndex<itv2->iIndex){results.push_back(itv1->iIndex);results.push_back(itv2->iIndex);}else{results.push_back(itv2->iIndex);results.push_back(itv1->iIndex);}} return results;}
};
LeetCode 1 Two Sum相关推荐
- leetcode 304. Range Sum Query 2D - Immutable |304. 二维区域和检索 - 矩阵不可变(二维前缀和问题)
题目 https://leetcode.com/problems/range-sum-query-2d-immutable/ 题解 本题是 medium 难度,二维前缀和问题.相似题目有: Easy: ...
- 【动态规划】LeetCode 377. Combination Sum IV
LeetCode 377. Combination Sum IV Solution1: 我的未能AC的答案 题目描述的和前几道题差不多,但实际上不能用DFS来做(会超时),要用动态规划,还是记录一下吧 ...
- 【DFS】LeetCode 39. Combination Sum
LeetCode 39. Combination Sum Solution1: DFS,这个套路要熟记啊! class Solution { public:vector<vector<in ...
- LeetCode 167.Two Sum II 解题报告
LeetCode 167.Two Sum II 解题报告 题目描述 Given an array of integers that is already sorted in ascending ord ...
- leetcode 1005 Maximize Sum Of Array After K Negations
leetcode 1005 Maximize Sum Of Array After K Negations 1.题目描述 2.解题思路 3.Python代码 1.题目描述 给定一个整数数组 A,我们只 ...
- [勇者闯LeetCode] 1. Two Sum
[勇者闯LeetCode] 1. Two Sum Description Given an array of integers, return indices of the two numbers s ...
- [勇者闯LeetCode] 112. Path Sum
[勇者闯LeetCode] 112. Path Sum Description Given a binary tree and a sum, determine if the tree has a r ...
- 数据结构线段树介绍与笔试算法题-LeetCode 307. Range Sum Query - Mutable--Java解法
此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 线段树(Segme ...
- LeetCode 653. Two Sum IV - Input is a BST--Python解法
题目地址:Two Sum IV - Input is a BST - LeetCode Given a Binary Search Tree and a target number, return t ...
- LeetCode 167. Two Sum II - Input array is sorted--Python解法
题目地址:Two Sum II - Input array is sorted - LeetCode Given an array of integers that is already sorted ...
最新文章
- 【CNN结构设计】无痛的涨点技巧:ACNet
- Future is IMPACT! 未来就是影响力!(演讲稿)
- define关键字的使用,以及宏函数的定义
- 计算机科学入门指南游戏攻略,【基础攻略】从零开始新手入门指南
- 欧拉心算(反演 + 积性函数筛)
- CentOS yum安装mcrypt详细图解教程[linux]
- 【uni-app】使用定义在App.vue的方法
- STL源码剖析-map
- Vue-注册全局组件的两种方法
- 登录日志怎么实现_运维必看:日志标准化必须面对的 4 类问题
- 转场动画UINavigationControllerDelegate
- 【网络入侵检测】K均值与层次聚类分析(sklearn,scipy,手写实现)
- 使用listen()和accept()函数
- Andriod动画效果总结以及帧动画实现
- 计算机常见网络故障,网络故障有哪些?常见网络故障处理方法
- 2020最新版前端学习路线图--Javascript网页编程重点知识
- 第一节、同步回调和异步回调?
- 2-2日期时间类型基础
- Java随笔记 - 内核缓冲区与进程缓冲区
- Preface Numbering序言页码