LeetCode 1 Two Sum(二分法)
题目来源:https://leetcode.com/problems/two-sum/
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
解题思路:
题目要求:给出一个数组numbers 以及 目标和target. 要求找到数组中两个数相加等于target所对应的下标.(下标不为0!)
/*第一次做LeetCode不熟悉.一直写着int main(),一直给WA.(明明本地的测试已经过了).之后才明白代码的要求.*/
具体方法:数组进行升序或降序排序(下面采用升序排序),采用二分法进行寻找.
分为三种情况:
if(num[left].x+num[right].x==target)
else if(num[left].x+num[right].x>target)
else if(num[left].x+num[right].x<target)
相对应的操作:
if(num[left].x+num[right].x==target)
{
l=num[num[left].sort_id].id;
r=num[num[right].sort_id].id;
break;
}//下面的left和right的移动取决于排序是按照升序还是降序
else if(num[left].x+num[right].x>target)
{
right=right-;
}
else if(num[left].x+num[right].x<target)
{
left=left+;
}
给出代码:
#include <bits/stdc++.h>
#define MAX 10010 using namespace std; struct Node{
int x;
int id;
int sort_id;
};
bool cmp(Node a,Node b)
{
return a.x<b.x;
}
Node num[MAX]; int main()
{
int n,target,l,r;
while(~scanf("%d",&n))
{
for(int i=;i<n;i++)
{
scanf("%d",&num[i].x);
num[i].id=i+;
}
scanf("%d",&target);
sort(num,num+n,cmp);
for(int i=;i<n;i++)
{
num[i].sort_id=i;
}
int left=,right=n-;
while(left<right)
{
if(num[left].x+num[right].x==target)
{
l=num[num[left].sort_id].id;
r=num[num[right].sort_id].id;
break;
}
else if(num[left].x+num[right].x>target)
{
right=right-;
}
else
{
left=left+;
}
}
printf("%d %d\n",l,r);
} }
提交代码:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> result;
int n = nums.size();
if(n < )
return result;
vector<int> original = nums;
sort(nums.begin(), nums.end());
int left=, right=n-;
int i, j, smaller, bigger;
while(left < right)
{
if(nums[left]+nums[right] == target)
{
for(i=; i<n; i++)
{
if(nums[left] == original[i])
{
result.push_back(i+);
break;
}
}
for(j=n-; j>=; j--)
{
if(nums[right] == original[j])
{
result.push_back(j+);
break;
}
}
if(result[] < result[])
{
smaller = result[];
bigger = result[];
}
else
{
smaller = result[];
bigger = result[];
}
result[] = smaller;
result[] = bigger;
return result;
}
else if(nums[left]+nums[right] < target)
left = left + ;
else
right = right - ;
}
return result;
}
};
LeetCode 1 Two Sum(二分法)的更多相关文章
- 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][013] Two Sum 2
题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...
- [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 ...
- [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 one Two Sum
LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 437. Path Sum III_ Easy tag: DFS
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
随机推荐
- supervisor 安装 配置 及 使用
supervisor是微软官方推荐的一个工具,传送门, 所以我们也使用这个工具来管理我们的asp.net core应用进程 服务器环境:ubuntu14.04 x64 安装 apt-get ...
- [Python] Search navigation in Pycharm
From: http://blog.csdn.net/u013088062/article/details/50323393 From: http://blog.csdn.net/u013088062 ...
- Scrum1.2--spring计划
项目功能--深入分析 燃尽图
- springMVC注解@initbinder日期类型的属性自动转换
在实际操作中经常会碰到表单中的日期 字符串和Javabean中的日期类型的属性自动转换, 而springMVC默认不支持这个格式的转换,所以必须要手动配置, 自定义数据类型的绑定才能实现这个功能. 一 ...
- nodejs+express+jade+mongodb给我baby做个小相册(2)-留言板
上一篇简单的实现了下照片的展现跟浏览功能,这一篇我将给这个程序添加一个留言的功能.那么留言的话肯定要涉及到数据持久了,其实对于这个小功能的话,用个xml就可以,不过为了看起来更加高大上,我决定使用mo ...
- Tempdb的并发阻塞
9.3 Tempdb的并发阻塞 在介绍Tempdb的并发问题前,先介绍几个比较特殊的数据页. PFS(Page Free Space),用于标识数据页空间的使用情况,以字节标识,可以表示数据页使用百分 ...
- LeetCode - 30. Substring with Concatenation of All Words
30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...
- ACM中的浮点数精度处理
在ACM中,精度问题非常常见.其中计算几何头疼的地方一般在于代码量大和精度问题,代码量问题只要平时注意积累模板一般就不成问题了.精度问题则不好说,有时候一个精度问题就可能成为一道题的瓶颈,让你debu ...
- 带复杂表头合并单元格的HtmlTable转换成DataTable并导出Excel
步骤: 一.前台JS取HtmlTable数据,根据设定的分隔符把数据拼接起来 <!--导出Excel--> <script type="text/javascript&qu ...
- css3中的zoom元素属性值测试
在样式表里头看到zoom:1的设置,很是好奇就去找了一些资料发现关于这个的讲述还是比较少. 理论知识 语法: zoom:normal | <number> | <percentage ...