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 ...
随机推荐
- POJ3276(遍历+区间修改)
http://poj.org/problem?id=3276 题意:n(n<=5000)头牛站成线,有朝前有朝后的的,然后每次可以选择大小为k的区间里的牛全部转向,会有一个最小操作m次使得它们全 ...
- 关于springmvc 返回json数据null字段的显示问题-转https://blog.csdn.net/qq_23911069/article/details/62063450
最近做项目(ssm框架)的时候,发现从后台返回的json(fastjson)数据对应不上实体类,从数据库查询的数据,如果对应的实体类的字段没有信息的话,json数据里面就不显示,这不是我想要的结果,准 ...
- 详解Hadoop Slots的含义
Slots是Hadoop的一个重要概念.然而在Hadoop相关论文,slots的阐述难以理解.网上关于slots的概念介绍也很少,而对于一个有经验的Hadoop开发者来说,他们可能脑子里已经理解了sl ...
- hdu - 1823 - Luck and Love(线段树)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/SCNU_Jiechao/article/details/24406391 题意:Wiskey招女友, ...
- NOSQL之MONGODB
MongoDB 基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案,它是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富, ...
- RESTful 接口设计规范
get 用来获取,post 用来新建(也可以用于更新),put 用来更新,delete 用来删除.
- 虚拟机 VMware Tools 安装
Ubuntu 或具有图形用户界面的 Ubuntu Server 要挂载 CD 镜像并解压,请按以下步骤操作: 启动此虚拟机. 使用具有管理员权限或 root 用户权限的帐户登录此虚拟机. 选择:对于F ...
- led的驱动及测试程序
一.驱动源码 #include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> # ...
- 获取post发送过来的xml包
if (Request.HttpMethod.ToLower() == "post") { byte[] ar; ar = new byte[this.Request.Input ...
- ASP.NET 实现验证码以及刷新验证码
实现代码 /// <summary> /// 生成验证码图片,保存session名称VerificationCode /// </summary> public static ...