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. 设置irb和ri

    设置irb和ri 输入ruby -v查看是否能够显示ruby版本 现在可以 配置irb了. irb是交互式Ruby 的命令行工具,即输入一句就立即执行并给出结果. 默认的irb不够强大,现在给它配置一 ...

  2. php调用一个c语言写的接口问题

    用php调用一个c语言写的soap接口时,遇到一个问题:不管提交的数据正确与否,都无法请求到接口 1.用php标准的soap接口去请求 2.拼接xml数据去请求 以上两种方式都不正确 解决办法:php ...

  3. WDR7500 花生壳问题

    新进一WDR7500 居然不能解析花生壳. 百度一番, 发现别人有同样的问题. 找来找去从别人的只言片语中发现需要升级固件. 好不容易加入一个群, 把固件下下来. 升级固件, 重新设置, 解析成功. ...

  4. Fragment的隐藏与显示

    package com.bwie.helpdemo; import com.bwie.fragment.BillFrag; import com.bwie.fragment.IndexFrag; im ...

  5. android架构

    周日没事,简单总结了一下Android开发中使用到的知识,以脑图的形式呈现.  

  6. U3D使anim,unity,prefab文件不显示乱码

    Edit-Project Setting-Editor-Asset Serialization-mode Force Text

  7. noi 6049 买书

    题目链接: http://noi.openjudge.cn/ch0206/6049/ 6049:买书 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 小明手里有n ...

  8. 至强CPU性能排行,从X3210起,由低至高排列。

    X3210X3220E5410E5506X5355X3320E5507X5365E5-2603E3-1220LE5-2403E5607X3330L5506X3230L5420E5-2407W3520E ...

  9. Entity Framework 第八篇 结构优化

    在之前的文章里,业务层直接调用一个包装的仓储类入口,忽略了DAL层,在业务层绕过DAL直接调用仓储类似乎也没什么大的问题,但是这样做有一个很大的弊端,就是无法做到DAL层的原子操作的复用.假如多个业务 ...

  10. 思科Cisco 2960系列交换机配置命令

    配置密码: 2960>en :第一次密码为空 2960h#conf t :进入全局配置模式 2960(config)#hostname swa :设置交换机名 2960(config)#enab ...