public class Solution {
public int[] TwoSum(int[] numbers, int target) {
Dictionary<int, int> dic = new Dictionary<int, int>();
int count = numbers.Count();
List<KeyValuePair<int, int>> list = new List<KeyValuePair<int, int>>(); for (int i = ; i < count; i++)
{
if (!dic.ContainsKey(numbers[i]))
{
dic.Add(numbers[i], i);
list.Add(new KeyValuePair<int, int>(numbers[i], i));
}
} int[] ary = new int[]; for (int i = ; i < list.Count; i++)
{
for (int j = i; j < list.Count; j++)
{
var d1 = list[i];
var d2 = list[j];
if (d1.Key + d1.Key == target && d2.Value - d1.Value > )
{
ary[] = d1.Value + ;
ary[] = d1.Value + ;
return ary;
}
else if (i != j && d1.Key + d2.Key == target)
{
ary[] = d1.Value + ;
ary[] = d2.Value + ;
return ary;
}
}
} return ary;
}
}

https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/#/description

补充一个“双指针”思想的解决方案,使用python实现:

 class Solution:
def twoSum(self, numbers: 'List[int]', target: 'int') -> 'List[int]':
i=0
j=len(numbers)-1
while numbers[i] + numbers[j] != target:
if numbers[i] + numbers[j]>target:
j-=1
else:
i+=1 return [i+1,j+1]

leetcode167的更多相关文章

  1. [Swift]LeetCode167. 两数之和 II - 输入有序数组 | Two Sum II - Input array is sorted

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

  2. LeetCode167.两数之和II-输入有序数组

    给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值 ...

  3. leetcode-167周赛-1292-元素和小于等于阈值的正方形的最大边长

    题目描述; 自己的提交:超时 class Solution: def maxSideLength(self, mat: List[List[int]], threshold: int) -> i ...

  4. leetcode-167周赛-1290-二进制链表转整数

    题目描述: 提交: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val ...

  5. leetcode-167周赛-1291-顺次数

    题目描述: 自己的提交: class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: l,h = ...

  6. leetcode-167周赛-1293-网格中的最短路径

    题目描述: 自己的提交:广度优先 O(mn*min(k,m+n)) class Solution: def shortestPath(self, grid, k: int) -> int: vi ...

  7. LeetCode167. Two Sum II - Input array is sorted(双指针)

    题意:对于一个有序数组,输出和为target的两个元素的下标.题目保证仅有唯一解. 分析: 法一:二分.枚举第一个元素,二分找另一个元素,时间复杂度O(nlogn),非最优解. class Solut ...

  8. LeetCode167 两数之和 II - 输入有序数组

    给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值 ...

  9. leetcode15

    class Solution { public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(nums); Li ...

随机推荐

  1. jq 分页

    转自:https://www.cnblogs.com/wolflower/p/6898479.html参考:https://github.com/tianxiangbing/paging先引入这两个插 ...

  2. 当php版本为5.6时的提示信息解决方法

    ecshop修饰符preg_replace/e不安全的几处改动 Strict standards: Only variables should be passed by reference in D: ...

  3. MySQL账号安全设置

    ======================================================================== 推荐账号安全设置 在数据库服务器上严格控制操作系统的账 ...

  4. 特殊字符搜索网站 http://symbolhound.com/

    最近在学习makefile,想搜索一下 $@是啥意思,结果google由于忽略了特殊字符,结果啥也没找到, 后来在stackoverflow上看到了别人同样的问题 http://stackoverfl ...

  5. cookie 知识点

    cookie失效是由浏览器实现的,根据时间来控制,服务器端并不做cookie是否失效的验证. 某个cookie失效了浏览器发送请求时便不会带上它,服务器端自然就没有这个cookie了,所以对于服务器来 ...

  6. idea新用法

    https://blog.csdn.net/linsongbin1/article/details/80211919

  7. java数组变量

    数组变量是一种引用类型的变量,能够指向数组对象.数组对象存储在堆内存中,当数组变量为局部变量时存储在栈内存中. int[] p = new int[]{5, 6, 7, 8, 9}; p是数组变量,指 ...

  8. json格式字符串处理

    public class InternalClass         {             public int MID;             public string Name;     ...

  9. jquery select radio

    Query获取Select选择的Text和Value: 语法解释: 1. $("#select_id").change(function(){//code...});   //为S ...

  10. Python 中的sort()排序

    v = [1, 3, 5, 2, 4, 6] v.sort() print(v) # [1, 2, 3, 4, 5, 6] v2 = [(1, 2), (2, 2), (2, 3), (3, 1)] ...