LeetCode每日一练(1-3)

题目导航

 

1. 两数之和

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

解决方案:

题目比较简单:

 1 public class Solution {
 2     public int[] TwoSum(int[] nums, int target) {
 3         
 4             int[] result = { };
 5             bool found = false;
 6             for (int i = 0; i < nums.Length;i++)
 7             {
 8                 for (int j = i + 1; j < nums.Length; j++)
 9                 {
10                     if (nums[i] + nums[j] == target)
11                     {
12                         result = new int[2] { i, j };
13                         found = true;
14                         break;
15                     }
16                 }
17                 if(found)
18                 {
19                     break;
20                 }
21             }
22             if(!found)
23             {
24 
25             }
26             return result;
27     }
28 }
View Code

提交了之后可以正常运行,但运行时间比较长,优化之后的代码如下:

 1 public class Solution {
 2     public int[] TwoSum(int[] nums, int target) {
 3         
 4             for (int i = 0; i < nums.Length - 1;i++)
 5             {
 6                 for (int j = i + 1; j < nums.Length; j++)
 7                 {
 8                     if (nums[i] + nums[j] == target)
 9                     {
10                         return new int[2] {i,j};
11                     }
12                 }
13             }
14         return new int[]{};
15     }
16 }
View Code

执行用时:572 ms  已经战胜 66.16 % 的 csharp 提交记录

执行时间依然超过了570ms.

参照其他人的答案如下:

 1 public class Solution {
 2     public int[] TwoSum(int[] nums, int target) {
 3         Dictionary <int,int> contain = new Dictionary<int,int>();
 4         for (int i = 0; i < nums.Length; i++)
 5         {
 6             if (contain.ContainsKey(target - nums[i])) return new int[]{contain[target - nums[i]], i};
 7             else if (!contain.ContainsKey(nums[i])) contain.Add(nums[i], i);
 8         }
 9         return new int[]{0, 0};
10     }
11 }
View Code

执行时间为360ms   已经战胜 95.18 % 的 csharp 提交记录.

2. 两数相加

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

示例:

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807

解决方案:

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     public int val;
 5  *     public ListNode next;
 6  *     public ListNode(int x) { val = x; }
 7  * }
 8  */
 9 public class Solution {
10     public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
11         
12         ListNode list = null,curNode = null;
13         int curSum = 0;
14         ListNode node1 = l1,node2 = l2;
15         bool needPreAdd = false;
16         while(node1!=null || node2!=null || needPreAdd)
17         {
18             int v1 = node1==null ? 0 : node1.val;
19             int v2 = node2==null ? 0 : node2.val;
20             
21             curSum = v1+v2;
22             if(needPreAdd)
23             {
24                 curSum++;
25             }
26             
27             if(curSum>=10)
28             {
29                 needPreAdd = true;
30                 curSum -=10;
31             }
32             else
33             {
34                 needPreAdd = false;
35             }
36             ListNode node = new ListNode(curSum);
37             if(list == null)
38             {
39                 list = node;
40                 curNode = node;
41             }
42             else
43             {
44                 curNode.next = node;
45                 curNode = node;
46             }
47             node1 = node1?.next;
48             node2 = node2?.next;
49         }
50         return list;
51     }
52 }
View Code

执行用时:152 ms    已经战胜 94.11 % 的 csharp 提交记录

3. 无重复字符的最长子串

给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

示例 1:

输入: "abcabcbb"
输出: 3
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
示例 2:

输入: "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
示例 3:

输入: "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
  请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。

解决方案:

最初的解决方法,是将当前字符串放到一个字典中,如果遇到相同的字符串,从该相同字符串处重新开始统计.Code如下:

 1 public class Solution {
 2     public int LengthOfLongestSubstring(string s) {
 3         int maxLength = 0;
 4         Dictionary<char, int> curDic = new Dictionary<char, int>();
 5         for (int i = 0; i < s.Length; i++)
 6         {
 7             if (curDic.ContainsKey(s[i]))
 8             {
 9                 i = curDic[s[i]] + 1;
10                 curDic.Clear();
11             }
12             curDic.Add(s[i], i);
13             if (maxLength < curDic.Count)
14             {
15                 maxLength = curDic.Count;
16             }
17         }
18         return maxLength;
19     }
20 }
View Code

执行用时:252 ms  已经战胜 28.62 % 的 csharp 提交记录

后来经过改进,只需要记录当前字符串的开始位置,当前长度即可以.改进后代码如下:

 1 public class Solution {
 2     public int LengthOfLongestSubstring(string s) {        
 3         int maxLength = 0;
 4         int curStartIndex = 0;
 5         int curLength = 0;
 6 
 7         for (int i = 0; i < s.Length; i++)
 8         {
 9             int charIndex = s.IndexOf(s[i], curStartIndex, curLength);
10             if (charIndex != -1)
11             {
12                 curLength = curLength - (charIndex - curStartIndex);
13                 curStartIndex = charIndex + 1;
14             }
15             else
16             {
17                 curLength++;
18             }
19             if (curLength > maxLength)
20             {
21                 maxLength = curLength;
22                 //Console.WriteLine(s.Substring(curStartIndex,curLength));
23             }
24         }
25         return maxLength;
26     }
27 }
View Code

执行用时 :120 ms  已经战胜 92.03 % 的 csharp 提交记录

原文地址:https://www.cnblogs.com/arthur3k/p/11468679.html