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

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

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

 class Solution:
"""
@param numbers : An array of Integer
@param target : target = numbers[index1] + numbers[index2]
@return : [index1 + 1, index2 + 1] (index1 < index2)
"""
def twoSum(self, numbers, target):
# write your code here
tmp = []
for i in numbers:
tmp.append(i)
numbers = sorted(numbers)
a = 0
b = len(numbers) - 1
while True:
if numbers[a] + numbers[b] == target:
break
elif numbers[a] + numbers[b] < target:
a += 1
elif numbers[a] + numbers[b] > target:
b -= 1
reta = numbers[a]
retb = numbers[b]
a = -1
b = -1
for i in range(0, len(tmp)):
if tmp[i] == reta and a == -1:
a = i
elif tmp[i] == retb and b == -1:
b = i
if a > b:
a = a ^ b
b = a ^ b
a = a ^ b
return [a+1, b+1]

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

  1. [LintCode] Two Sum 两数之和

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  2. 【LeetCode】1. Two Sum 两数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...

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

    1. Two Sum 两数之和 Given an array of integers, return indices of the two numbers such that they add up ...

  4. 【数据结构】Hash表简介及leetcode两数之和python实现

    文章目录 Hash表简介 基本思想 建立步骤 问题 Hash表实现 Hash函数构造 冲突处理方法 leetcode两数之和python实现 题目描述 基于Hash思想的实现 Hash表简介 基本思想 ...

  5. [LeetCode] 1. Two Sum 两数之和

    Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...

  6. leetcode 两数之和 python

      两数之和     给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 1 ...

  7. [LeetCode] Two Sum 两数之和

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  8. LeetCode两数之和-Python<一>

    下一篇:LeetCode链表相加-Python<二> 题目:https://leetcode-cn.com/problems/two-sum/description/ 给定一个整数数组和一 ...

  9. [LeetCode] 1.Two Sum 两数之和分析以及实现 (golang)

    题目描述: /* Given an array of integers, return indices of the two numbers such that they add up to a sp ...

  10. [LeetCode]1.Two Sum 两数之和&&第一次刷题感想

    ---恢复内容开始--- 参考博客: https://www.cnblogs.com/grandyang/p/4130379.html https://blog.csdn.net/weixin_387 ...

随机推荐

  1. Matlab动态数组实现

    clear all; clc; a = []; %不是null,也不能什么都不是 for i=1:10 a = [a i]; end

  2. jdbc读取数据库表

    把结果集封装为List // 通过结果集元数据封装List结果集 public static List<Map<String, Object>> read(String sql ...

  3. C51关键字

    C51 中的关键字 关键字 用途 说明 auto 存储种类说明 用以说明局部变量,缺省值为此 break 程序语句 退出最内层循环 case 程序语句 Switch语句中的选择项 char 数据类型说 ...

  4. PostgreSQL数据库中获取表主键名称

    PostgreSQL数据库中获取表主键名称 一.如下表示,要获取teacher表的主键信息: select pg_constraint.conname as pk_name,pg_attribute. ...

  5. 如何在Asp.net中备份Access数据库?

    public   void   Create(   string   mdbPath   ) { if(   File.Exists(mdbPath)   )   //检查数据库是否已存在 { thr ...

  6. jQuery中的Deferred-详解和使用

    首先,为什么要使用Deferred? 先来看一段AJAX的代码: var data; $.get('api/data', function(resp) { data = resp.data; }); ...

  7. iOS字符串大小转换

    NSString *test = @"Hello World";            // 小写      NSString *lower = [test lowercaseSt ...

  8. Android Source Code

    一. Android 框架 http://elinux.org/Master-android Android框架层级 : Android 自下 而 上 分为 4层; -- Linux内核层; -- 各 ...

  9. 深入浅出ES6(十二):代理 Proxies

    作者 Jason Orendorff  github主页  https://github.com/jorendorff 请看这样一段代码: var obj = new Proxy({}, { get: ...

  10. poj 3621(最优比率环)

    题目链接:http://poj.org/problem?id=3621 思路:之前做过最小比率生成树,也是属于0/1整数划分问题,这次碰到这道最优比率环,很是熟悉,可惜精度没控制好,要不就是wa,要不 ...