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

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> index(2,0);
vector<int> n2;
int numsSize = nums.size();
int f = -1;
for(int i = 0;i < numsSize;i++){
n2.push_back(nums[i]);
if(nums[i] == target/2 && target%2 ==0){
if(f != -1){
index[0] = min(f,i + 1);
index[1] = max(f,i + 1);
return index;
}
else f = i + 1;
}
}
sort(nums.begin(),nums.end());
for(int i = 0;i < numsSize;i++){
int j = lower_bound(nums.begin(),nums.end(),target - nums[i]) - nums.begin();
int a,b;
if(nums[i] + nums[j] == target && j < numsSize){
for(int k = 0;k < numsSize;k++){
if(nums[i] == n2[k]) a = k + 1;
if(nums[j] == n2[k]) b = k + 1;
}
index[0] = min(a,b);
index[1] = max(a,b);
}
}
return index;
}
};

  

 

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. JVM常见配置汇总

    堆设置 -Xms:初始堆大小 -Xmx:最大堆大小 -XX:NewSize=n:设置年轻代大小 -XX:NewRatio=n:设置年轻代和年老代的比值.如:为3,表示年轻代与年老代比值为1:3,年轻代 ...

  2. -XX:+TraceClassLoading 监控类的加载

    -XX:+TraceClassLoading –监控类的加载 •[Loaded java.lang.Object from shared objects file] •[Loaded java.io. ...

  3. 替换所有字符串,获取url参数值

    替换所有的字符串: var newStr = str.replace(/null/g, ""); 获取url参数值 <script type="text/javas ...

  4. 【笔记】after,before,insertAfter,insertBefore的作用

    这几个方法的作用是插入外部节点,所谓外部插入节点就是我们平常在网页编程中手动添加代码到某一句语句的前面或后面,如图: 红色框的P是在蓝色框span的前面插入的外部节点,反过来说蓝色框的span是在红色 ...

  5. MVC5+EF6 入门完整教程11--细说MVC中仓储模式的应用

    摘要: 第一阶段1~10篇已经覆盖了MVC开发必要的基本知识. 第二阶段11-20篇将会侧重于专题的讲解,一篇文章解决一个实际问题. 根据园友的反馈, 本篇文章将会先对呼声最高的仓储模式进行讲解. 文 ...

  6. js相关总结

    1.都是gbk格式的页面使用ajax请求,发起前改变编码格式,可避免乱码. $.ajaxSetup({     'beforeSend' : function(xhr) {         xhr.o ...

  7. Entity Framework 第十篇 条件查询

    业务类中 我们根据条件来动态的查询 创建IQueryable接口 public IQueryable<TEntity> GetQueryable() { IQueryable<TEn ...

  8. 我的android学习经历38

    anddroid studio的内存修改 昨天有位朋友问到了下面的一个问题 这个判断为android studio的分配的内存不够用. 据我的了解造成这个的原因主要有以下几个方面: 1.电脑的内存本来 ...

  9. sql索引实例

    1.创建表并插入数据 在Sql Server2008中创建测试数据库Test,接着创建数据库表并插入数据,sql代码如下: USE Test IF EXISTS (SELECT * FROM INFO ...

  10. <转>关闭 程序崩溃时 windows 正在检查该问题的解决方案

    本文转自:http://www.cnblogs.com/dabaopku/archive/2011/07/04/2097029.html 尤其是使用visual studio开发程序 ,自己特意thr ...