[Lintcode two-sum]两数之和(python,双指针)

题目链接:http://www.lintcode.com/zh-cn/problem/two-sum/

给一个整数数组,找到两个数使得他们的和等于一个给定的数target

备份一份,然后排序。搞两个指针分别从左从右开始扫描,每次判断这两个数相加是不是符合题意,如果小了,那就把左边的指针向右移,同理右指针。然后在备份的数组里找到位置。

 1 class Solution:
 2     """
 3     @param numbers : An array of Integer
 4     @param target : target = numbers[index1] + numbers[index2]
 5     @return : [index1 + 1, index2 + 1] (index1 < index2)
 6     """
 7     def twoSum(self, numbers, target):
 8         # write your code here
 9         tmp = []
10         for i in numbers:
11             tmp.append(i)
12         numbers = sorted(numbers)
13         a = 0
14         b = len(numbers) - 1
15         while True:
16             if numbers[a] + numbers[b] == target:
17                 break
18             elif numbers[a] + numbers[b] < target:
19                 a += 1
20             elif numbers[a] + numbers[b] > target:
21                 b -= 1
22         reta = numbers[a]
23         retb = numbers[b]
24         a = -1
25         b = -1
26         for i in range(0, len(tmp)):
27             if tmp[i] == reta and a == -1:
28                 a = i
29             elif tmp[i] == retb and b == -1:
30                 b = i
31         if a > b:
32             a = a ^ b
33             b = a ^ b
34             a = a ^ b
35         return [a+1, b+1]
时间: 2024-08-25 11:13:07

[Lintcode two-sum]两数之和(python,双指针)的相关文章

[LintCode] 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

leetcode——Two Sum 两数之和(AC)

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

LeetCode算法题——两数之和(python)

两数之和: 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数.你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用.例如:给定 nums = [2, 7, 11, 15], target = 9.由nums[0] + nums[1] = 2 + 7 = 9,所以返回 [0, 1] 原文地址:http://blog.51cto.com/13921683/2318829

LeetCode Two Sum 两数之和

题意:在一个整数序列中找到两个元素,他们之和为target,并用vector返回这两个元素的位置(升序),位置从1开始算起. 思路: 方法(1):两个指针法.也就是排序,然后一个从头扫,一个从尾扫,前提是先排序,但是给的数组是无序的,一旦排序就失去了他们的具体位置.如果是ACM的题还可以弄个结构体把他们打包起来,再根据值大小来排序,这样找到了也就能找到他们的位置.可是这里是在一个类里面的,用不上结构体,肯定还有其他方法 ,比如 pair,但是我还不会实现.于是,用multimap来代替,将元素值

LeetCode刷题 1. Two Sum 两数之和 详解 C++语言实现 java语言实现

Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums = [2, 7, 11, 15]

领扣-两数之和-Python实现

领扣每日一题 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] ''' 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的 两个 整数.

【数据结构与算法】双指针思想——两数之和

两数之和 II - 输入有序数组 LeetCode:两数之和 II - 输入有序数组 题目描述: 给定一个已按照升序排列?的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1?必须小于?index2. 示例: 输入: numbers = [2, 7, 11, 15], target = 9 输出: [1,2] 解释: 2 与 7 之和等于目标数 9 .因此 index1 = 1, index2 = 2 . 思想: 使用双

[LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树

Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target. Example 1: Input: 5 / 3 6 / \ 2 4 7 Target = 9 Output: True Example 2: Input: 5 / 3 6 / \ 2 4 7 Targe

LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)

Given an array of integers that is already sorted in ascending order, 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 m

167. Two Sum II - Input array is sorted两数之和

1. 原始题目 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值(index1 和 index2)不是从零开始的. 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素. 示例: 输入: numbers = [2, 7, 11, 15], target = 9 输出: [1,2] 解释: 2 与 7 之和等于目标数 9 .因此 in