leetcode 1. Two Sum排序+双指针
来源:互联网 发布:淘宝返现规定 编辑:程序博客网 时间:2024/05/18 13:44
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], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1].
python3的lambda排序方式
class Solution: class Node(object): def __init__(self, v, id): self.v = v self.id = id def __lt__(self, other): return self.v < other.v def twoSum(self, nums, target): nodes = [] n = len(nums) for i in range(0, n): nodes.append(self.Node(nums[i], i)) nodes.sort() l = 0; r = n - 1 while l <= r: sum = nodes[l].v + nodes[r].v if sum == target: return [nodes[l].id, nodes[r].id] elif sum < target: l += 1 elif sum > target: r -= 1if __name__ == '__main__': solve = Solution() print(solve.twoSum([70, 2, 17, 11, 15], 19))
阅读全文
0 0
- leetcode 1. Two Sum排序+双指针
- LeetCode 1. Two Sum
- [LeetCode]1.Two Sum
- LeetCode 1.Two Sum
- LeetCode --- 1. Two Sum
- [Leetcode] 1. Two Sum
- leetcode---1.Two sum
- [Leetcode] 1. Two Sum
- LeetCode 1.Two Sum
- LeetCode 1.Two Sum
- LeetCode 1.Two Sum
- 【LeetCode]1.Two Sum
- LeetCode 1.Two Sum
- leetcode 1. Two Sum
- [leetcode] 1. Two Sum
- leetcode 1. Two Sum
- Leetcode- 1. Two Sum
- LeetCode-1.Two Sum
- [Caffe]:关于Check failed: registry.count(type) == 0 (1 vs. 0)错误
- Mathematic for Computer Science Lecture 1
- 算法谜题36 有限的差异
- Exchange发送邮件
- SSH连接VMware中NAT模式主机
- leetcode 1. Two Sum排序+双指针
- Python包和类的基本用法
- 2017.05.19回顾 TSS = ESS + RSS证明
- oracle 聚合函数 LISTAGG ,将多行结果合并成一行
- 【javascript】关于localstorage的使用感想
- Web应用中使用JavaMail发送邮件
- 常用SQL Server 小语法、函数 等的实例汇总
- 20个实用的webApp前端开发技巧
- UML类图关系(泛化 、实现、依赖、关联、聚合、组合)