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 ...
随机推荐
- android 代码控制控件的长宽,小技巧
要在代码里改变ImageView 的长宽,如图 通过拿到contentImage这对象的控件参数,再去改变,再设置 , 上图的contentImage为ImageView对象: 而这里 要提醒的是,L ...
- C#语法糖之 cache操作类 asp.net
因为考虑到我下面我将写session cookies 等 操作类 ,与cache具有共性. 所以都统一继承了IHttpStorageObject abstract class 来保函数风格的统一 , ...
- 使用fat-jar打包多个java工程为可执行文件
对于一个从C++转向Java的程序员来说,制作java的可执行文件,也算是比较棘手的问题.项目是前几个同事留下来的,几个必备的库文件和制作可执行文件的工具居然都是加密未解封的:不知道是不是因为公司和前 ...
- js-弹出窗口
一.JS最常用三种弹出对话框 1.对话框 //弹出对话框并输出一段提示信息 function ale() { //弹出一个对话框 alert("提示信息!"); } 2.询问框 / ...
- Linux内核知识
版本 linus树 Linux内核最初创始人--Linus Torvalds管理一个Linus树,linus树也称为主线(mainline).一般指的upstream,"上游",也 ...
- sql语句查询数据库表结构信息
开发中经常用到查询指定表及其字段的信息,以下是我整理的SQL语句查询方法,供自己平时使用也提供给大家参考! 1.适用MS SQL SERVER: SELECT 表名 = then d.name els ...
- 微软必应词典客户端的案例分析——个人Week3作业
第一部分 调研,评测 Bug探索 Bug No1.高亮语义匹配错位 环境: windows8,使用必应词典版本PC版:3.5.0 重现步骤: 1. 搜索"funny face"这一 ...
- Github教程(2)
一些小技巧: 在Github的个人主页中按Shift+/ 可以显示快捷键的操作: 在某个项目的文件列表中,按下t,即可根据输入的文件名和部分文件名查找文件: 对比两个分支之间的差别,以mybatis项 ...
- Eclipse颜色主题插件:Eclipse Color Theme
一个很赞的eclipse插件,可以简单方便地实现eclipse下的代码配色.另外插件作者还专门为此插件做了一个eclipse配色网站,配色多达728个,开发者们也可以在此网站分享自己的配色方案. 插件 ...
- WPF后台设置xaml控件的样式System.Windows.Style
WPF后台设置xaml控件的样式System.Windows.Style 摘-自 :感谢 作者: IT小兵 http://3w.suchso.com/projecteac-tual/wpf-zhi ...