Leetcode:Two Sum
来源:互联网 发布:淘宝ar怎么用 编辑:程序博客网 时间:2024/06/11 09:04
题目:
最先思路:
public class Solution { public int[] TwoSum(int[] nums, int target) { for (int i = 0; i < nums.Length; i++) { for (int j = i + 1; j < nums.Length; j++) { if (nums[i] + nums[j] == target) return new int[]{i,j}; } } throw new Exception("No two sum solution"); }}运行发现效率不是最高,就学习其中的解法,模仿哈希表解法,个人用C#编写,发现C#中的Dictionary和其他语言有点不同,无法根据Value获取Key,在这里通过百度学习了通过linq获取Dictionary的Key,可是运算的时候发现最后一个超时,算法看了感觉没问题,如果有大神看到,可帮我修改一下。
代码:
public class Solution { public int[] TwoSum(int[] nums, int target) { Dictionary<int, int> dt = new Dictionary<int, int>(); int y = -1; for (int i = 0; i < nums.Length; i++) { dt.Add(i, nums[i]); } for (int i = 0; i < nums.Length; i++) { int x = target - nums[i]; if (dt.ContainsValue(x) && dt.FirstOrDefault(q => q.Value == x).Key != i) return new int[] { i, dt.FirstOrDefault(q => q.Value == x).Key }; } throw new Exception("No two sum solution"); }}
关于看书想到的第三种排序法,思路和第二段重合,这段代码是有错误的,关键在于如何根据Value得到key,当一段数组有两个相等的value的时候,我的代码只能获取第一个value的Key,关键想取出一个和key不相同的位置。
代码如下:
public class Solution { public int[] TwoSum(int[] nums, int target) { Dictionary<int,int> dt = new Dictionary<int,int>(); for(int i = 0;i < nums.Length;i++){ dt.Add(i,nums[i]); } Array.Sort(nums); int first = 0,end = nums.Length - 1; while(first < end){ if(nums[first] + nums[end] == target){ return new int[]{dt.FirstOrDefault(q => q.Value == nums[first]).Key,dt.FirstOrDefault(q => q.Value == nums[end]).Key}; } else if(nums[first] + nums[end] < target) first++; else end--; } throw new Exception("No two sum solution"); }}可能我的语言描述还不够详细,所以贴出错误案例:
如果有人阅读到了,请指教一下,我也会自己思考,在这里先谢过大家,我想知道这样编写的效率如何。
在这里,我学习leetcode中第三种单哈希表的算法,给C#编程的提供一个思路,毕竟C#写leetcode的网上资料太少。
代码:
public class Solution { public int[] TwoSum(int[] nums, int target) { Dictionary<int,int> dt = new Dictionary<int,int>(); int num = 0; for (int i = 0; i < nums.Length; i++){ num = target - nums[i]; if(dt.ContainsValue(num)) return new int[]{dt.FirstOrDefault(q => q.Value == num).Key,i}; dt.Add(i,nums[i]); } throw new Exception("No two sum solution"); }}
三种思路,加一种错误代码,运行最后一段代码的时候,运行在500ms,最后结果变800ms,不知道为什么。第三种和第二种的差别在于,第二种先构建Dictionary,再进行查询,第三种是查询了再建Dictionary。错误代码的思路是先数组排序,查询到索引再到dictionary里确定索引位置,但是个人的第二个根据Value取key出现问题。第一种是暴风算法。后两种是哈希算法。
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---如果文中出现错别字以及其他问题,欢迎指出
一个新人:17
阅读全文
0 0
- 【Leetcode】Two Sum (Sum)
- LeetCode: Two Sum
- LeetCode: Two Sum
- [Leetcode] Two Sum
- LeetCode : Two Sum
- Leetcode: Two Sum
- leetcode 46: Two Sum
- [LeetCode] Two Sum
- LeetCode 1 - Two Sum
- Leetcode : Two Sum
- 【leetcode】Two Sum
- [LeetCode]Two Sum
- leetcode Two Sum
- [Leetcode] Two Sum
- [leetcode] Two Sum
- LeetCode - Two Sum
- leetcode之Two Sum
- LeetCode | Two Sum
- C++ explicit关键字详解
- Linux-RAID详解及配置
- centos7mini版网络链接配置
- 12.MariaDB笔记——cmake使用介绍七编译安装包
- 分治法求最大公约数gcd
- Leetcode:Two Sum
- 160个练手CrackMe-008
- 实验一(模板类操作)
- 【greenDAO3】 项目搭建与增删改查操作
- 13.MariaDB笔记——cmake使用介绍八支持面板
- java使用Servlet进行文件下载
- 欢迎使用CSDN-markdown编辑器
- Python3.6 str内置方法
- 走穿java23种设计模式-6代理模式