开始刷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相关推荐

  1. leetcode 304. Range Sum Query 2D - Immutable |304. 二维区域和检索 - 矩阵不可变(二维前缀和问题)

    题目 https://leetcode.com/problems/range-sum-query-2d-immutable/ 题解 本题是 medium 难度,二维前缀和问题.相似题目有: Easy: ...

  2. 【动态规划】LeetCode 377. Combination Sum IV

    LeetCode 377. Combination Sum IV Solution1: 我的未能AC的答案 题目描述的和前几道题差不多,但实际上不能用DFS来做(会超时),要用动态规划,还是记录一下吧 ...

  3. 【DFS】LeetCode 39. Combination Sum

    LeetCode 39. Combination Sum Solution1: DFS,这个套路要熟记啊! class Solution { public:vector<vector<in ...

  4. LeetCode 167.Two Sum II 解题报告

    LeetCode 167.Two Sum II 解题报告 题目描述 Given an array of integers that is already sorted in ascending ord ...

  5. leetcode 1005 Maximize Sum Of Array After K Negations

    leetcode 1005 Maximize Sum Of Array After K Negations 1.题目描述 2.解题思路 3.Python代码 1.题目描述 给定一个整数数组 A,我们只 ...

  6. [勇者闯LeetCode] 1. Two Sum

    [勇者闯LeetCode] 1. Two Sum Description Given an array of integers, return indices of the two numbers s ...

  7. [勇者闯LeetCode] 112. Path Sum

    [勇者闯LeetCode] 112. Path Sum Description Given a binary tree and a sum, determine if the tree has a r ...

  8. 数据结构线段树介绍与笔试算法题-LeetCode 307. Range Sum Query - Mutable--Java解法

    此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 线段树(Segme ...

  9. 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 ...

  10. 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 ...

最新文章

  1. 【CNN结构设计】无痛的涨点技巧:ACNet
  2. Future is IMPACT! 未来就是影响力!(演讲稿)
  3. define关键字的使用,以及宏函数的定义
  4. 计算机科学入门指南游戏攻略,【基础攻略】从零开始新手入门指南
  5. 欧拉心算(反演 + 积性函数筛)
  6. CentOS yum安装mcrypt详细图解教程[linux]
  7. 【uni-app】使用定义在App.vue的方法
  8. STL源码剖析-map
  9. Vue-注册全局组件的两种方法
  10. 登录日志怎么实现_运维必看:日志标准化必须面对的 4 类问题
  11. 转场动画UINavigationControllerDelegate
  12. 【网络入侵检测】K均值与层次聚类分析(sklearn,scipy,手写实现)
  13. 使用listen()和accept()函数
  14. Andriod动画效果总结以及帧动画实现
  15. 计算机常见网络故障,网络故障有哪些?常见网络故障处理方法
  16. 2020最新版前端学习路线图--Javascript网页编程重点知识
  17. 第一节、同步回调和异步回调?
  18. 2-2日期时间类型基础
  19. Java随笔记 - 内核缓冲区与进程缓冲区
  20. Preface Numbering序言页码

热门文章

  1. 金仓数据库KingbaseES序列的操作
  2. LabVIEW控制Arduino驱动数码管(基础篇—11)
  3. K8S篇-安装Syncthing
  4. 实验记录 | 6/16
  5. Kaggle神器LightGBM最全解读(附代码说明)!
  6. 深度特征融合---高低层(多尺度)特征融合
  7. Git之cherry-pick
  8. 那位整天嚷嚷要辞职的前台,两个月过去了她还在公司里做前台
  9. ESP32 ESP-IDF看门狗TWDT
  10. 华硕灵焕3装鸿蒙系统,神操作:学生拿iPhone换3部手机后竟然