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 ...
随机推荐
- Clob类型转化String类型
Clob clob=rs.getClob(列数); Clob clob=rs.getClob("列名");String content=clob.getSubString((lon ...
- Bug测试报告--连连看——天天向上
测试时间:2016-11-23 20:10 测试者:刘芳芳(nice!团队) 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git. ...
- 一种比较少见的C#代码段
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- GAT2.0使用文档(单接口开发)
3 开始写用例 3.1接口用例开发 1) 准备工作 l 第一步从github(https://github.com/GeneralAutomationTesting/GAT2.0 ...
- win7 APPCRASH问题解决!
真是废了老劲了..什么清理插件,各种运行msconfig/启动都试了 问题:**.exe已停止工作 问题事件名称: APPCRASH 应用程序名: compute_image_mean.exe 应用程 ...
- mysql分区表的原理和优缺点
1.分区表的原理 分区表是由多个相关的底层表实现,这些底层表也是由句柄对象表示,所以我们也可以直接访问各个分区,存储引擎管理分区的各个底层表和管理普通表一样(所有的底层表都必须使用相同的存储引擎),分 ...
- 【Prince2科普】Prince2七大主题之概论
[Prince2科普]Prince2七大主题之概论 PRINCE2的七大主题,就是项目管理中持续关注的七个方面,分别是: 1.商业论证 2.组织 3.质量 4.计划 5.风险 6.变更 7.进展 ...
- Cookie与Session的区别
cookie机制 Cookies是服务器在本地机器上存储的小段文本并随每一个请求发送至同一个服务器.IETF RFC 2965 HTTP State Management Mechanism 是通用c ...
- mac上安装opencv3
转载于:http://blog.csdn.net/sanwandoujiang/article/details/51159983 在macosx上安装opencv2 brew tap homebrew ...
- CSS之盒子模型及常见布局
盒子模型的综合应用 CSS提高1 Div ul li 的综合应用很多的网页布局现在都用到这种模式 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTM ...