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 ...
随机推荐
- SQL Server数据库性能优化(三)之 硬件瓶颈分析
参考文献 http://isky000.com/database/mysql-performance-tuning-hardware 由于对DBA 工作了解不多 所以只从网上简单的看了下 硬件 ...
- 安装 Open Live Writer
2016年12月19日20:22:02 一直希望能有一个独立的客户端来书写博客,作为一个新手,希望能够有一个强大的编辑器来实现这个功能.然后就赶着去下载windows live Writer.然后才发 ...
- 初识python第二天(2)
整理Python常见数据类型内置函数的使用方法如下: 一.int 首先我们来查看一下int包含了哪些函数 #python3.x print(dir(int)) #['__abs__', '__add_ ...
- Ajax中dataType数据类型
今天项目中使用Ajax向后台保存数据,其中dataType为'json';当请求成功后,没有走success回调,反而走了error:数据库已经成功保存数据了. 后来搞半天才知道原来dataType指 ...
- RecyclerView使用总结
遇到的异常: java.lang.ExceptionInInitializerError静态块初始化异常 NetworkOnMainThreadException访问网线不能在主线程中进行 我的参考资 ...
- double函数和int函数
可以看到,当tensor全是double型时,int函数会把所有元素取整,从1.5可以看出,不是四舍五入,而是取整.double函数又把整数型元素变成double型. th> a 0.0000 ...
- Duilib实现QQ聊天窗口晃动
转载:http://blog.csdn.net/arbboter/article/details/26282717 转载:http://blog.csdn.net/zerolusta/article/ ...
- 关于Extjs MVC模式上传文件的简单方式
Extjs新手研究上传文件的事情估计是件很头痛的问题,毕竟,我就在头痛.最近两天一直在忙文件上传问题,终于小有收获. 用的是Extjs+MVC3.0+EF开发,语言为C#.前台window代码显示列内 ...
- android 修改framework下资源文件后如何编译
在framework/base/core/res/res 下添加资源文件后需要先编译资源 然后编译framework 才可正常引用 进入项目根目录 cd frameworks/base/core/re ...
- CentOS6.5 mysql 5.5安装
CentOS 6 mysql5.5安装配置 1 安装所需软件 2 安装cmake 3 tar.gz形式安装mysql 4 配置与启动 MySQL自5.5版本以后,就开始使用cmake编译工具了.tar ...