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 ...
随机推荐
- [Python] Basic operations in Pycharm
From: http://learnpythonthehardway.org/book Comment with line comment: Ctrl + slash Run: Shift + F10 ...
- extractCSS – 帮助你从 HTML 中快速分离出 CSS
extractCSS 是一个免费的基于 Web 的应用程序,能够从 HTML 中提取风格相关的信息,包括 id.class 和内联样式,而且输出可以定制(缩进和括号的用法).该工具非常有用,当我们快速 ...
- .Net魔法堂:史上最全的ActiveX开发教程——ActiveX与JS间交互篇
一.前言 经过上几篇的学习,现在我们已经掌握了ActiveX的整个开发过程,但要发挥ActiveX的真正威力,必须依靠JS.下面一起来学习吧! 二.JS调用ActiveX方法 只需在UserContr ...
- sql date()函数,时间格式
(1).GETDATE() 函数从 SQL Server 返回当前的日期和时间. 语法 GETDATE() 实例 下面是 SELECT 语句: SELECT GETDATE() AS CurrentD ...
- ExtJs4 笔记(4) Ext.XTemplate 模板
ExtJs4 笔记(4) Ext.XTemplate 模板 摘自:http://www.cnblogs.com/lipan/ 本篇将涉及到ExtJs中一个重要的概念,模板.话说Razor很神奇,但是我 ...
- [持续更新] 文章列表 last updated SEP 18, 2016
1.前端 HTML5快速学习二 Canvas@20141125 HTML5快速学习一@20141122 2.ASP.NET(MVC) MVC5+EF6 入门完整教程14--动态生成面包屑@201608 ...
- 基于Eclipse的Go语言可视化开发环境
http://jingyan.baidu.com/article/d7130635032e2f13fdf475b8.html 基于Eclipse的Go语言可视化开发环境 | 浏览:2924 | 更新: ...
- [Eclipse] - 解决导入flask模块出现的Unresolved Import flask问题
http://www.cnblogs.com/mizhon/p/4242073.html [Eclipse] - 解决导入flask模块出现的Unresolved Import flask问题 最近想 ...
- Chrome浏览器的Timing分析
以W3C网站为例: Stalled是浏览器得到要发出这个请求的指令,到请求可以发出的等待时间,一般是代理协商.以及等待可复用的TCP连接释放的时间,不包括DNS查询.建立TCP连接等时间等. SSL( ...
- Window下生成OpenSSL自签证书
:OPenSSL下载地址:https://www.openssl.org/source/ 编译好的OpenSSL下载地址: http://slproweb.com/products/Win32Open ...