题目导航

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 提交记录

LeetCode每日一练(1-3)的更多相关文章

  1. CSS3每日一练之内容处理-嵌套编号

    出处:http://www.w3cfuns.com/thread-5592229-1-17.html 1.大标题一   1.子标题   2.子标题   3.子标题2.大标题二   1.子标题   2. ...

  2. CSS3每日一练之选择器-结构性伪类选择器

    <!DOCTYPE HTML> <html> <head> <meta charset="gb2312"> <title> ...

  3. HTML5每日一练之progress标签的应用

    progress标签:从名字上来看,估计大家也能猜到这个标签是什么标签了,没错,他是一个进度条.在HTML5中我们终于可以不用模拟了. <progress id="W3Cfuns_pr ...

  4. HTML5每日一练之figure新标签的应用

    igure元素是一种元素的组合,可带有标题(可选).figure标签用来表示网页上一块独立的内容,将其从网页上移除后不会对网页上的其他内容产生影响.figure所表示的内容可以是图片.统计图或代码示例 ...

  5. HTML5每日一练之details展开收缩标签的应用

    details标签的出现,为我们带来了更好的用户体验,不必为这种收缩展开的效果再编写JS来实现.注:目前仅Chrome支持此标签. details有一个新增加的子标签——summary,当鼠标点击su ...

  6. 每日一小练——Eratosthenes 筛选法

    上得厅堂.下得厨房,写得代码.翻得围墙,欢迎来到睿不可挡的每日一小练! 题目:Eratosthenes筛选法 内容: 求质数是一个非常普遍的问题,通常不外乎用数去除.除到不尽时,给定的数就是质数.可是 ...

  7. 每日一小练——高速Fibonacci数算法

    上得厅堂,下得厨房,写得代码,翻得围墙,欢迎来到睿不可挡的每日一小练! 题目:高速Fibonacci数算法 内容:先说说Fibonacci数列,它的定义是数列:f1,f2....fn有例如以下规律: ...

  8. linux每日一练:Enable multithreading to use std::thread: Operation not permitted问题解决

    linux每日一练:Enable multithreading to use std::thread: Operation not permitted问题解决 在linux在需要使用c++11时会遇到 ...

  9. linux命令每日一练:find与rm实现查找并删除目录或文件

    linux命令每日一练 linux中find与rm实现查找并删除目录或文件 linux 下用find命令查找文件,rm命令删除文件. 删除指定目录下指定文件 find 要查找的目录名 -name .s ...

随机推荐

  1. c++ 标准 字符串转换为时间 时间大小比较 判断有效期 简洁办法

    c# php delphi java 等各种语言 对字符串转换为日期 然后与当前日期进行比较 是非常容易的 因为有现成的函数可用 标准 c++ 硬是找不到 合适的代码可用 于是 百度了很多 没百出个结 ...

  2. 编写第一个dart程序hello dart

    /* 入口方法的两种定义方式 main(){ print('hello dart'); } */ ///这也是一个注释 //表示main方法没有返回值 void main(){ print('hell ...

  3. CDH 部署 Hadoop:5.开始安装

    Cloudera Enterprise 6.2.x   或者参考https://blog.csdn.net/shawnhu007/article/details/52579204 第零步:优化相关 e ...

  4. window.location.href重定向失败的问题

    如题,在js中通过window.location.href=URL来跳转到另一个页面(也可以是另一个项目的另一个页面). 打开的页面地址是:www.a.com/project1/index 要跳转的页 ...

  5. 【插件】thinkphp5+百度编辑器自定义上传

    1 官方下载sdk 2 在引入编辑器页面.写入js // 百度编辑器 UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActi ...

  6. extends 类的继承 / super关键字,调用继承类里面的函数和变量

    Son 继承Father 当其他脚本想调用 Father类里面的变量 or 方法 可以把 Son r=new Son()   等价于 Father r=new Father() 注意: 函数只能单继承 ...

  7. spring 加载属性(properties)文件

    在开发的过程中,配置文件往往就是那些属性(properties)文件,比如使用properties文件配置数据库文件,又如database-config.properties 代码清单:databas ...

  8. 组件文档系统-md-react-styleguidist

    推荐指数:

  9. 【转】百万年薪挖了p8,难道是水货?

    大厦新搬进来一家创业公司,老板红光满面地提着果篮上楼拜访,说是刚拿到了投资人的钱,正准备扩充团队大干一场.那个时候的他踌躇满志,顾盼生辉.当时我想,能在这个大环境下拿到投资的公司,做的产品应该是有前景 ...

  10. skynet sproto 问题

    刚碰到一个小细节,纠结了半个小时 sproto的协议,request 和{ 必须有空格