题目来源: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(二分法)的更多相关文章

  1. 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 ...

  2. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  3. [leetCode][013] Two Sum 2

    题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...

  4. [LeetCode] #167# Two Sum II : 数组/二分查找/双指针

    一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...

  5. [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针

    一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...

  6. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  7. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  8. LeetCode one Two Sum

    LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...

  9. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  10. [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 ...

随机推荐

  1. 你需要知道的Sass插值

    你也许会不时地写写 Sass 玩玩,你也会很享受它带给你各种便利.但还有一件事,你并不一定完全了解:插值 (interpolation) - 将一个占位符,替换成一个值.好了,你们都很幸运,因为今天我 ...

  2. Robot Framework自动化测试(二)---元素定位

    说明: 不要误认为Robot framework 只是个web UI测试工具,更正确的理解Robot framework是个测试框架,之所以可以拿来做web UI层的自动化是国为我们加入了seleni ...

  3. js实现页面a向页面b传参的方法

    方法一:使用HTML5本地化存储(localStorage) 组件(本地最大能存储5M数据)localStorage是本地永久存储数据,是cookie的优化 方法二:使用cookie将数据存放在客户的 ...

  4. SpringMVC核心——映射问题

    一.SpringMVC 使用 RequestMapping 来解决映射问题. 二.在学习 RequestMapping 之前,首先来看一张图. 这张图表示的是发送一次 http 请求时,所包含的请求 ...

  5. 0406.复利计算器5.0版-release

    复利计算器5.0-release 目录 项目简介 Github链接推送 客户需求 新增需求分析 项目设计 效果演示 操作说明 程序结构 结对分工 合作照片 总结 1.项目简介 项目名称:复利计算器 目 ...

  6. Winform开发框架之通用数据导入导出操作的事务性操作完善

    1.通用数据导入导出操作模块回顾 在我的Winfrom开发框架里面,有一个通用的导入模块,它在默默处理这把规范的Excel数据导入到不同的对象表里面,一直用它来快速完成数据导入的工作.很早在随笔< ...

  7. 利用Aspose.Cell控件导入Excel非强类型的数据

    导入Excel的操作是非常常见的操作,可以使用Aspose.Cell.APOI.MyXls.OLEDB.Excel VBA等操作Excel文件,从而实现数据的导入,在导入数据的时候,如果是强类型的数据 ...

  8. [moka学习笔记]yii2设置语言和时区

    1.在web/index.php中 (new yii\web\Application($config))->run(); $app = new \yii\web\Application($con ...

  9. Servlet3.0 Test

    1. Servlet3.0 Test and Annotation used 你可以从tomcat7中lib文件夹中找到servlet-api.jar package com.goodfan.serv ...

  10. LGLCalender (价格日历)

    一直未能找到自己想要的日历价格,就算右也不是我想要的,今天自己封装了一个,欢迎各位来查阅,不足的地方请指教 最新代码下载地址https://github.com/liguoliangiOS/LGLCa ...