LeetCode每日一练(1-3)
题目导航
1. 两数之和
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
解决方案:
题目比较简单:
public class Solution {
public int[] TwoSum(int[] nums, int target) { int[] result = { };
bool found = false;
for (int i = ; i < nums.Length;i++)
{
for (int j = i + ; j < nums.Length; j++)
{
if (nums[i] + nums[j] == target)
{
result = new int[] { i, j };
found = true;
break;
}
}
if(found)
{
break;
}
}
if(!found)
{ }
return result;
}
}
提交了之后可以正常运行,但运行时间比较长,优化之后的代码如下:
public class Solution {
public int[] TwoSum(int[] nums, int target) { for (int i = ; i < nums.Length - ;i++)
{
for (int j = i + ; j < nums.Length; j++)
{
if (nums[i] + nums[j] == target)
{
return new int[] {i,j};
}
}
}
return new int[]{};
}
}
执行用时:572 ms 已经战胜 66.16 % 的 csharp 提交记录
执行时间依然超过了570ms.
参照其他人的答案如下:
public class Solution {
public int[] TwoSum(int[] nums, int target) {
Dictionary <int,int> contain = new Dictionary<int,int>();
for (int i = ; i < nums.Length; i++)
{
if (contain.ContainsKey(target - nums[i])) return new int[]{contain[target - nums[i]], i};
else if (!contain.ContainsKey(nums[i])) contain.Add(nums[i], i);
}
return new int[]{, };
}
}
执行时间为360ms 已经战胜 95.18 % 的 csharp 提交记录.
2. 两数相加
给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
解决方案:
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) { ListNode list = null,curNode = null;
int curSum = ;
ListNode node1 = l1,node2 = l2;
bool needPreAdd = false;
while(node1!=null || node2!=null || needPreAdd)
{
int v1 = node1==null ? : node1.val;
int v2 = node2==null ? : node2.val; curSum = v1+v2;
if(needPreAdd)
{
curSum++;
} if(curSum>=)
{
needPreAdd = true;
curSum -=;
}
else
{
needPreAdd = false;
}
ListNode node = new ListNode(curSum);
if(list == null)
{
list = node;
curNode = node;
}
else
{
curNode.next = node;
curNode = node;
}
node1 = node1?.next;
node2 = node2?.next;
}
return list;
}
}
执行用时:152 ms 已经战胜 94.11 % 的 csharp 提交记录
3. 无重复字符的最长子串
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: "abcabcbb"
输出: 3
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
示例 2:
输入: "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
示例 3:
输入: "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
解决方案:
最初的解决方法,是将当前字符串放到一个字典中,如果遇到相同的字符串,从该相同字符串处重新开始统计.Code如下:
public class Solution {
public int LengthOfLongestSubstring(string s) {
int maxLength = ;
Dictionary<char, int> curDic = new Dictionary<char, int>();
for (int i = ; i < s.Length; i++)
{
if (curDic.ContainsKey(s[i]))
{
i = curDic[s[i]] + ;
curDic.Clear();
}
curDic.Add(s[i], i);
if (maxLength < curDic.Count)
{
maxLength = curDic.Count;
}
}
return maxLength;
}
}
执行用时:252 ms 已经战胜 28.62 % 的 csharp 提交记录
后来经过改进,只需要记录当前字符串的开始位置,当前长度即可以.改进后代码如下:
public class Solution {
public int LengthOfLongestSubstring(string s) {
int maxLength = ;
int curStartIndex = ;
int curLength = ; for (int i = ; i < s.Length; i++)
{
int charIndex = s.IndexOf(s[i], curStartIndex, curLength);
if (charIndex != -)
{
curLength = curLength - (charIndex - curStartIndex);
curStartIndex = charIndex + ;
}
else
{
curLength++;
}
if (curLength > maxLength)
{
maxLength = curLength;
//Console.WriteLine(s.Substring(curStartIndex,curLength));
}
}
return maxLength;
}
}
执行用时 :120 ms 已经战胜 92.03 % 的 csharp 提交记录
最新文章
- 学习AOP之透过Spring的Ioc理解Advisor
- JS:call()和apply的区别
- Android动画之淡入淡出
- HTML5_Canvas_属性、定义及方法
- **对比$_POST、$GLOBALS[&#39;HTTP_RAW_POST_DATA&#39;]和file_get_contents(&#39;php://input&#39;)
- 《zw版&#183;Halcon-delphi系列原创教程》 3d汽车模型自动区域分割
- 《Java编程那点事儿》读书笔记(六)——异常处理
- VI 命令学习指南
- 【Linux】鸟哥的Linux私房菜基础学习篇整理(七)
- Mysql的MySqlDataReader对于MysqlConnection是独占式
- Spring AOP With AspectJ
- gitlab 实现自动部署(简单Python实现)
- Redis常用命令--SortedSet
- [Swift]LeetCode168. Excel表列名称 | Excel Sheet Column Title
- Spring Boot的Listener机制的用法和实现原理详解
- Vue:在vue-cli中使用Bootstrap
- compact 创建一个包含变量名为数组的键和它们的值为数组的值的数组
- CodeForces 430A Points and Segments (easy)(构造)题解
- XHR的对象及用法
- SoftwareEngineering.APIDesign.iOS
热门文章
- Windows 开始 运行中所打开的默认程序以及优先级
- 使用layui框架的select获取选中的值
- Diffie-Hellman算法简介
- 品优购商城项目(一)mybatis逆向工程
- RabbitMQ 入门教程(PHP版) 第二部分:工作队列(Work queues)
- 转 Zabbix 3.2.6通过SNMP和iDRAC监控DELL服务器
- 图片缩放——利用layui的滑块
- 报错:ModuleNotFoundError: No module named &#39;_ctypes&#39;
- Jrebel激活方法(转)
- 一些Python中的二维数组的操作方法