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 ...
随机推荐
- ruby -检查json数据类型
HashObj={","language"=>"zh","make"=>"Apple"," ...
- 转 微软Sysinternals Suite工具13年12月版下载
Sysinternals Suite 是微软出品的一套集成数十个绿色软件的系统工具包.Sysinternals Suite 和IT之家的魔方电脑大师设计一样,里面的各个小工具组件都可以单独拿出来运行, ...
- MySQL Group Replication
group replication是一种全新的高可用,高扩张的MySQL集群服务.高一致性,基于原生复制及paxos协议的组复制技术,以插件方式提供一致数据安全保证:高容错性,大多数服务正常就可继续工 ...
- NOSQL详解
Nosql的全称是Not Only Sql,这个概念早起就有人提出,在09年的时候比较火.Nosql指的是非关系型数据库,而我们常用的都是关系型数据库.就像我们常用的mysql,sqlserver一样 ...
- markdown 知识点
符号 说明 作用 ___ 三个下划线 一条直线 * 或_ 1个星号 或 1个下划线 文字斜体 ** 或__ 2个星号 或 2个下划线 文字加粗 全角2个空格 缩进2个汉字 竖线之间加3个间隔符放在第二 ...
- Tomcat 8 中的startup.bat
1. bat文件 批处理文件,在DOS和Windows(任意版本)的系统中,bat文件是可执行文件 2. startup.bat 中涉及的bat语法 2.1 @. echo. echo off @ec ...
- HttpCookieCollection类
一.最近在研究HttpRequest类的时候,发现返回的cookie集合是存在放这个类的对象的.而实际上这个类只是一个HttpCookie对象的集合,关于HttpCookie类可以查看http://w ...
- golang channel 的使用
本文对channel使用中的几个疑惑,以例子的形式加以说明. 普通channel 缺省情况下,发送和接收会一直阻塞着,直到另一方准备好. 例如: package main import ( " ...
- innotop监控mysql
InnoTop 是一个系统活动报告,类似于Linux性能工具,它与Linux的top命令相仿,并参考mytop工具而设计. 它专门用后监控InnoDB性能和MySQL服务器.主要用于监控事务,死锁,外 ...
- Django 书籍分享(PDF)
轻量级Django 链接:https://pan.baidu.com/s/1FD5zdRIkzHI44SZ54fjt6Q 密码:6z50 精通Django 链接:https://pan.baidu.c ...