leetcode167
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的更多相关文章
- [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 ...
- LeetCode167.两数之和II-输入有序数组
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值 ...
- leetcode-167周赛-1292-元素和小于等于阈值的正方形的最大边长
题目描述; 自己的提交:超时 class Solution: def maxSideLength(self, mat: List[List[int]], threshold: int) -> i ...
- leetcode-167周赛-1290-二进制链表转整数
题目描述: 提交: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val ...
- leetcode-167周赛-1291-顺次数
题目描述: 自己的提交: class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: l,h = ...
- leetcode-167周赛-1293-网格中的最短路径
题目描述: 自己的提交:广度优先 O(mn*min(k,m+n)) class Solution: def shortestPath(self, grid, k: int) -> int: vi ...
- LeetCode167. Two Sum II - Input array is sorted(双指针)
题意:对于一个有序数组,输出和为target的两个元素的下标.题目保证仅有唯一解. 分析: 法一:二分.枚举第一个元素,二分找另一个元素,时间复杂度O(nlogn),非最优解. class Solut ...
- LeetCode167 两数之和 II - 输入有序数组
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值 ...
- leetcode15
class Solution { public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(nums); Li ...
随机推荐
- LeetCode Single Number I II Python
Single Number Given an array of integers, every element appears twice except for one. Find that sing ...
- gridview 合并单元格 并原样导出数据
使用的方式都是比较简单的,asp.net 如何进行数据的导出有好多种方法,大家可以在网上找到, 一下提供一些合并并原样输出的一个简单的代码: public void ToExcel(System.We ...
- nodejs express 学习
nodejs的大名好多人应该是听过的,而作为nodejs web 开发的框架express 大家也应该比较熟悉. 记录一下关于express API 的文档: express() 创建express ...
- 批处理(bat)命令学习的一些总结
这篇笔记是我对批处理学习的一些总结,能在系统帮助里找到的内容我就不写了,太偏门的也不写,只写些个人感觉很好用的技巧,大部分属于整理 一.set 篇: 1.set(无开关) set .=test set ...
- web上传照片
.toDataURL() FileReader对象也有类似的方法,比如.readAsDataURL(),然而它只接受file或blob类型,而这两种类型一般只能通过<input[type=fil ...
- nyoj 单调递增子序列(二)
单调递增子序列(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给定一整型数列{a1,a2...,an}(0<n<=100000),找出单调递增最长 ...
- chrome浏览器network面板出现:Provisional headers are shown 提示
一般来说,如果看到这个提示,说明这个请求并没有发送出去. 具体原因有多种: 请求被某些扩展如 Adblock 拦截了 请求被墙了 走本地缓存或者 dataurl 的请求 client发送请求后,由于各 ...
- 高大上的JS工具
EtherPad (协同文件编辑): EtherCalc (协同电子表格编辑)
- codevs 1131 统计单词数
#include<iostream> #include<string> using namespace std; int main() { string s, s0; getl ...
- CentOS Linux解决Device eth0 does not seem to be present 但是没有发现eth1
http://www.linuxidc.com/Linux/2012-12/76248.htm 此标题已经是有人写过的了.但是为什么拿来重写? 我复制完,没有发现有eth1这个网卡 为什么呢?需要选中 ...