Leetcode 1 two sum 难度:0
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的更多相关文章
- [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 ...
- 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 ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- 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 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- [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 ...
随机推荐
- Entity Framework CodeFirst commands
TOPIC about_EntityFramework SHORT DESCRIPTION Provides information about Entity Framework commands. ...
- C#使用二叉树算法设计一个无限分级的树表
效果图: 数据库: 操作树的示意图: 控制器代码: using Dw.Business; using Dw.Entity; using Dw.Utilities; using System; usin ...
- 让keepalived监控NginX的状态
经过前面的配置,如果主服务器的keepalived停止服务,从服务器会自动接管VIP对外服务:一旦主服务器的keepalived恢复,会重新接管VIP. 但这并不是我们需要的,我们需要的是当NginX ...
- Java多线程之Lock的使用
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util ...
- zigbee学习之路(四):按键控制(中断方式)
一.前言 通过上次的学习,我们学习了如何用按键控制led,但是在实际应用中,这种查询方式占用了cpu的时间,如果通过中断控制就可以解决这个问题,我们今天就来学习按键控制的中断方式. 二.原理分析 传统 ...
- Reflection
Reflection 反射能在运行时获取一个类的全部信息,并且可以调用类方法,修改类属性,创建类实例. 而在编译期间不用关心对象是谁 反射可用在动态代理,注解解释,和反射工厂等地方. -------- ...
- iphone 3gs 美版,6.1.3+降基带+越狱+解锁。成功分享(转)
本人参照这个帖子成功把一个白苹果的机器救活了 2014年1月26日 13点 转自:http://bbs.app111.com/thread-510632-1-1.html 时间:2013年5月31日 ...
- 从BATS交易所获取空头头寸
Getting short volume from BATS In my last post I have gone through the steps needed to get the sho ...
- 客户端判断是否为IE9以上版本
function detectBrowser() { var browser = navigator.appName if(navigator.userAgent.indexOf("MSIE ...
- 【转】linux命令详解:md5sum命令
[转]linux命令详解:md5sum命令 转自:http://blog.itpub.net/29320885/viewspace-1710218/ 前言 在网络传输.设备之间转存.复制大文件等时,可 ...