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 ...
随机推荐
- 二叉查找树(一)之 图文解析 和 C语言的实现
概要 本章先对二叉树的相关理论知识进行介绍,然后给出C语言的详细实现.关于二叉树的学习,需要说明的是:它并不难,不仅不难,而且它非常简单.初次接触树的时候,我也觉得它似乎很难:而之所产生这种感觉主要是 ...
- Microsoft Visual Studio 2013 VSTS单元测试指南
安装vs2013时并未安装VSTS工具包,所以在工具栏:工具->拓展和更新 进行下载安装 vs13已经用了两年了,相比于之前老师推荐的vc6.0感觉要强出很多,刚上手时感觉比较困难,在使用一 ...
- .NET ORM 哪家强
ORM到底哪家强? 很多人都想知道这个问题,自已也没测试过,只能道听途说. 闲的无聊就将几个ORM拿出来比一比,假如怀疑测试代码有问题可以将它下载下来慢慢研究. 参赛ORM 1.SqlSugar:是一 ...
- EncryptionHelper
public static class EncryptionHelper { #region const /// <summary> /// 默认使用的适合于DES,RC2算法的Key / ...
- javascript之IE版本检测
近年来随着操作系统的升级以及各种新技术的开发普及,抛弃低版本IE已经是大势所趋,这对于前端人员来时是个好消息,可以不用花费太多的时间来做低版本的兼容,很多站点采用给予低版本IE以提示的方式(恩,很友好 ...
- Python基础:序列(列表、元组)
一.概述 列表(list)是由一个个 Python对象 组成的序列.其中,Python对象 可以是任何类型的对象,包括 Python标准类型(数值.字符串.列表.元组和字典)以及 用户自定义类型(类) ...
- 1. windows环境安装Node.js
1. 下载 地址: https://nodejs.org/en/ 2. 下载最新版本v6.1.0 Currrent
- 关于SVN删除后的文件不能重新添加(正常途径不行)
在你自己的机器上(即SVN客户端),把“新建test”文件夹标记为删除,然后提交,在删除之前可以备份“新建test”文件夹,提交后,在当前文件夹下更新SVN.然后把你刚刚备份的文件夹重新放到该目录下. ...
- C#的注释和快速开启工具的命令
1.注释的方法 1)sqlserver中,单行注释:—— 多行注释:/****/ 2)C#中,单行注释:// 多行注释:/****/ 3)C#中多行注释的快捷方式:启用ctrl+E+C ,撤 ...
- Access-Control-Allow-Origin: Dealing with CORS Errors in Angular
https://daveceddia.com/access-control-allow-origin-cors-errors-in-angular/ Getting this error in you ...