https://leetcode.com/problems/two-sum/

  1. class Solution {
  2. public:
  3. vector<int> twoSum(vector<int>& nums, int target) {
  4. vector<int> index(2,0);
  5. vector<int> n2;
  6. int numsSize = nums.size();
  7. int f = -1;
  8. for(int i = 0;i < numsSize;i++){
  9. n2.push_back(nums[i]);
  10. if(nums[i] == target/2 && target%2 ==0){
  11. if(f != -1){
  12. index[0] = min(f,i + 1);
  13. index[1] = max(f,i + 1);
  14. return index;
  15. }
  16. else f = i + 1;
  17. }
  18. }
  19. sort(nums.begin(),nums.end());
  20. for(int i = 0;i < numsSize;i++){
  21. int j = lower_bound(nums.begin(),nums.end(),target - nums[i]) - nums.begin();
  22. int a,b;
  23. if(nums[i] + nums[j] == target && j < numsSize){
  24. for(int k = 0;k < numsSize;k++){
  25. if(nums[i] == n2[k]) a = k + 1;
  26. if(nums[j] == n2[k]) b = k + 1;
  27. }
  28. index[0] = min(a,b);
  29. index[1] = max(a,b);
  30. }
  31. }
  32. return index;
  33. }
  34. };

  

 

Leetcode 1 two sum 难度:0的更多相关文章

  1. [LeetCode] 918. Maximum Sum Circular Subarray 环形子数组的最大和

    Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...

  2. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  4. [LeetCode] #167# Two Sum II : 数组/二分查找/双指针

    一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...

  5. [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针

    一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...

  6. LeetCode 560. Subarray Sum Equals K (子数组之和等于K)

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  7. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  8. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  9. [LeetCode] Minimum Index Sum of Two Lists 两个表单的最小坐标和

    Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...

随机推荐

  1. 使用 Redis 实现排行榜功能 (转载 https://segmentfault.com/a/1190000002694239)

    排行榜功能是一个很普遍的需求.使用 Redis 中有序集合的特性来实现排行榜是又好又快的选择. 一般排行榜都是有实效性的,比如"用户积分榜".如果没有实效性一直按照总榜来排,可能榜 ...

  2. web工程目录结构

    /WEB-INF/web.xml Web应用程序配置文件,描述了 servlet 和其他的应用组件配置及命名规则. /WEB-INF/classes/包含了站点所有用的 class 文件,包括 ser ...

  3. EventBus使用详解(一)——初步使用EventBus

    一.概述 EventBus是一款针对Android优化的发布/订阅事件总线.主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间 ...

  4. css 打字动画

    使用 css 将文字逐字打出 <h1>css is awesome</h1> 要使<h1>标签里的文字逐字打出,对应的样式如下: h1{ width: 14ch;/ ...

  5. nginx 配置文件备份

    1. /etc/nginx/sites-enabled/default 的原始文件 # You may add here your # server { # ... # } # statements ...

  6. stoneniqiu 理想就是自己喜欢做,并对社会和他人都有意义的事情!

    H5嵌入原生开发小结----兼容安卓与ios的填坑之路 2016-11-19 08:29 by stoneniqiu, 1038 阅读, 8 评论, 收藏, 编辑 一开始听说开发H5,以为就是做适配现 ...

  7. android 修改framework下资源文件后如何编译

    在framework/base/core/res/res 下添加资源文件后需要先编译资源 然后编译framework 才可正常引用 进入项目根目录 cd frameworks/base/core/re ...

  8. php 面向对象中的魔术方法

    1.__construct() 实例化对象是被自动调用.当__construct和以类名为函数名的函数 同时存在时调用__construct,另一个不背调用. 类名为函数名的函数为老版的构造函数. 2 ...

  9. 事务码 ListSchema:查看Cube星型结构Schema

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  10. aws在线技术峰会笔记-电商解决方案

    Redshift PB级别的数据仓库