Skip to content

Commit 8cca3a7

Browse files
author
atwood
committed
stash
1 parent 967a889 commit 8cca3a7

File tree

5 files changed

+273
-60
lines changed

5 files changed

+273
-60
lines changed

Interview/Iherb.cs

+122
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,80 @@ public List<int> GetMaxSumIntList(List<List<int>> data)
109109
}
110110

111111
//4.最长公共前缀
112+
public string LongestCommonPrefix(string[] strs)
113+
{
114+
////测试用例
115+
//string[] data = new string[] { "flower", "flow", "flight" };
116+
//var res = solution.LongestCommonPrefix(data);
117+
//ConsoleX.WriteLine(res);
118+
119+
if (strs == null || strs.Length == 0)
120+
return string.Empty;
121+
122+
string minStr = strs[0];
123+
foreach (string str in strs)
124+
{
125+
if (str.Length < minStr.Length)
126+
minStr = str;
127+
}
128+
129+
//因为这里的 right 是给 substring 方法用的,所以不用 -1
130+
int left = 0, right = minStr.Length;
131+
while (left < right)
132+
{
133+
//这里 +1 补齐除法的缺损
134+
int mid = (left + right + 1) / 2;
135+
bool isCP = strs.All(s => s.StartsWith(minStr.Substring(0, mid)));
136+
//出现失败要不断减少才行,避免除法缺损导致始终没有减少,所以要在失败的情况下 -1
137+
if (isCP)
138+
left = mid;
139+
else
140+
right = mid - 1;
141+
}
142+
return minStr.Substring(0, left);
143+
}
112144

113145
//5.三数之和为 0
146+
public IList<IList<int>> ThreeSum(int[] nums)
147+
{
148+
//测试用例
149+
////int[] intArr = new int[] { 0, 0, 0 };
150+
////int[] intArr = new int[] { -1, 0, 1, 2, -1, -4 };
151+
////int[] intArr = new int[] { -2, -1, -1, -1, 0, 1, 1, 1, 2 };
152+
//int[] intArr = new int[] { -2, 0, 1, 1, 2 };
153+
//var res = solution.ThreeSum(intArr);
154+
//ConsoleX.WriteLine(res);
155+
156+
IList<IList<int>> res = new List<IList<int>>();
157+
Array.Sort(nums);
158+
for (int left = 0; left < nums.Length; left++)
159+
{
160+
//排序之后,左指针最小,都大于0了,那一定就没有其他答案了
161+
if (nums[left] > 0)
162+
break;
163+
//去重
164+
if (left - 1 >= 0 && nums[left] == nums[left - 1])
165+
continue;
166+
167+
int mid = left + 1;
168+
int right = nums.Length - 1;
169+
while (mid < right)
170+
{
171+
int sum = nums[left] + nums[mid] + nums[right];
172+
if (sum == 0)
173+
{
174+
res.Add(new int[] { nums[left], nums[mid], nums[right] });
175+
do { right--; } while (mid < right && nums[right + 1] == nums[right]);
176+
do { mid++; } while (mid < right && nums[mid] == nums[mid - 1]);
177+
}
178+
else if (sum > 0)
179+
right--;
180+
else
181+
mid++;
182+
}
183+
}
184+
return res;
185+
}
114186

115187
//6.查找中位数
116188

@@ -121,6 +193,23 @@ public List<int> GetMaxSumIntList(List<List<int>> data)
121193
//9.数组最大合子集
122194

123195
//10.反转数组
196+
public int[] ReverseArray(int[] arr)
197+
{
198+
//测试用例
199+
//int[] data = new int[] { 1, 2, 3, 4, 5, 6, 7 };
200+
//var res = solution.ReverseArray(data);
201+
//ConsoleX.WriteLine(res);
202+
203+
int len = arr.Length;
204+
for (int i = 0; i < len / 2; i++)
205+
{
206+
int temp = arr[i];
207+
//需要注意这里要 len - 1,因为是总长度
208+
arr[i] = arr[len - 1 - i];
209+
arr[len - 1 - i] = temp;
210+
}
211+
return arr;
212+
}
124213

125214
//11.囚徒困境
126215
//单纯的应该怎么做的话,会选择都沉默,达到帕累托最优解。
@@ -135,6 +224,39 @@ public List<int> GetMaxSumIntList(List<List<int>> data)
135224
//13.最长字符串子集
136225

137226
//14.Money changing problem
227+
public int CoinChange(int[] coins, int amount)
228+
{
229+
//测试用例
230+
////int[] coins = new int[] { 1, 2, 5 };//11
231+
//int[] coins = new int[] { 186, 419, 83, 408 };//6249
232+
//var res = solution.CoinChange(coins, 6249);
233+
//ConsoleX.WriteLine(res);
234+
if (amount == 0)
235+
return 0;
236+
237+
//用 int.MaxValue 初始化之后,就不用把做 0 的判断了
238+
//int[] dp = Enumerable.Repeat(int.MaxValue, amount + 1).ToArray();
239+
//或者这个写法 Array.Fill(dp, int.MaxValue);
240+
int[] dp = new int[amount + 1];
241+
for (int i = 0; i < amount; i++)
242+
{
243+
if (i == 0 || dp[i] != 0)
244+
{
245+
//用 long 隐式转换防止 int 越界
246+
foreach (long coin in coins)
247+
{
248+
if (i + coin <= amount)
249+
{
250+
if (dp[i + coin] == 0)
251+
dp[i + coin] = dp[i] + 1;
252+
else
253+
dp[i + coin] = Math.Min(dp[i] + 1, dp[i + coin]);
254+
}
255+
}
256+
}
257+
}
258+
return dp[amount] == 0 ? -1 : dp[amount];
259+
}
138260

139261
//15.Longest increasing subsequence
140262

No14_String.cs

+37-35
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,52 @@ namespace LeetCode_14
2626
// }
2727
//}
2828

29+
/// <summary>
30+
/// REVIEW
31+
/// 2020.08.21: 更新了二分法写法,需要注意几个关键点,二分加一减一这种
32+
/// </summary>
33+
2934
public class Solution
3035
{
3136
/// <summary>
32-
/// 因为官方解题给的答案里,直接纵向比对会比二分法快(纵向时间复杂度O(mn),二分法O(mnLogm)),我就不解了,试了下,还是二分法快
33-
/// 时间复杂度:O(strs.length * min(strs))
37+
/// 二分法,最优解
38+
/// 设 strs 中最短字符串长度为 m, strs数组个数为 n
39+
/// 时间复杂度:O(mnlogm)
3440
/// 空间复杂度:O(1)
3541
/// </summary>
3642
/// <param name="strs"></param>
3743
/// <returns></returns>
3844
public string LongestCommonPrefix(string[] strs)
3945
{
40-
string res = string.Empty;
4146
if (strs == null || strs.Length == 0)
42-
return res;
47+
return string.Empty;
4348

44-
if (strs.Length == 1)
45-
return strs[0];
46-
47-
//遍历找出最长公共前缀
48-
int prefixIndex = 0;
49-
for (int i = 0; i < strs[0].Length; i++)
49+
string minStr = strs[0];
50+
foreach (string str in strs)
5051
{
51-
if (strs.All(s => s.StartsWith(strs[0].Substring(0, i + 1))))
52-
prefixIndex = i + 1;
52+
if (str.Length < minStr.Length)
53+
minStr = str;
5354
}
5455

55-
return strs[0].Substring(0, prefixIndex);
56+
//因为这里的 right 是给 substring 方法用的,所以不用 -1
57+
int left = 0, right = minStr.Length;
58+
while (left < right)
59+
{
60+
//这里 +1 补齐除法的缺损
61+
int mid = (left + right + 1) / 2;
62+
bool isCP = strs.All(s => s.StartsWith(minStr.Substring(0, mid)));
63+
//出现失败要不断减少才行,避免除法缺损导致始终没有减少,所以要在失败的情况下 -1
64+
if (isCP)
65+
left = mid;
66+
else
67+
right = mid - 1;
68+
}
69+
return minStr.Substring(0, left);
5670
}
5771

5872
/// <summary>
59-
/// 找最短的字符串来纵向对比,对比使用二分法
60-
/// 时间复杂度:O(strs.length * log Min(strs))
73+
/// 因为官方解题给的答案里,直接纵向比对会比二分法快(纵向时间复杂度O(mn),二分法O(mnLogm)),我就不解了,试了下,还是二分法快
74+
/// 时间复杂度:O(strs.length * min(strs))
6175
/// 空间复杂度:O(1)
6276
/// </summary>
6377
/// <param name="strs"></param>
@@ -68,30 +82,18 @@ public string LongestCommonPrefix(string[] strs)
6882
// if (strs == null || strs.Length == 0)
6983
// return res;
7084

71-
// //找出最短的字符串
72-
// res = strs[0];
73-
// for (int i = 1; i < strs.Length; i++)
74-
// {
75-
// if (strs[i].Length < res.Length)
76-
// res = strs[i];
77-
// }
78-
// //如果最短的是空字符串,那就返回
79-
// if (res == string.Empty)
80-
// return res;
85+
// if (strs.Length == 1)
86+
// return strs[0];
8187

82-
// //用最短的字符串做二分法比较
83-
// int left = 0;
84-
// int right = res.Length - 1;
85-
// while (left <= right)
88+
// //遍历找出最长公共前缀
89+
// int prefixIndex = 0;
90+
// for (int i = 0; i < strs[0].Length; i++)
8691
// {
87-
// int mid = (left + right) / 2;
88-
// if (strs.All(s => s.StartsWith(res.Substring(0, mid + 1))))
89-
// left = mid + 1;
90-
// else
91-
// right = mid - 1;
92+
// if (strs.All(s => s.StartsWith(strs[0].Substring(0, i + 1))))
93+
// prefixIndex = i + 1;
9294
// }
9395

94-
// return res.Substring(0, left);
96+
// return strs[0].Substring(0, prefixIndex);
9597
//}
9698
}
9799
}

No15_Array.cs

-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ public IList<IList<int>> ThreeSum(int[] nums)
6464
else
6565
mid++;
6666
}
67-
6867
}
6968
return res;
7069
}

No322_Dp.cs

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace LeetCode
6+
{
7+
class No322_Dp
8+
{
9+
//static void Main(string[] args)
10+
//{
11+
// var solution = new Solution();
12+
// while (true)
13+
// {
14+
// //int input = int.Parse(Console.ReadLine());
15+
// //int input2 = int.Parse(Console.ReadLine());
16+
// //string input = Console.ReadLine();
17+
// //string input2 = Console.ReadLine();
18+
// //int[] intArr = input.Split(',').Select(s => int.Parse(s)).ToArray();
19+
// //int input2 = int.Parse(Console.ReadLine());
20+
// //var builder = new DataStructureBuilder();
21+
// //int?[] data = new int?[] { 1, 2, 3, 4, 5, null, 6, null, null, 7, 8 };
22+
// //var tree = builder.BuildTree(data);
23+
// //var listNode = builder.BuildListNode(new int[] { 1, 4, 5 });
24+
// //int[][] arr = new int[3][] { new int[] { 1, 3, 1 }, new int[] { 1, 5, 1 }, new int[] { 4, 2, 1 } };
25+
// //int[] nums1 = new int[] { 2, 1, 7, 5, 6, 4, 3 };
26+
// //int[] nums1 = new int[] { 2, 1, 1, 5, 11, 5, 1, 7, 5, 6, 4, 3 };
27+
// //int[] nums2 = new int[] { 10, 15, 20 };
28+
// //string input = "adceb";
29+
// //string input2 = "*a*b";
30+
// //int[] coins = new int[] { 1, 2, 5 };//11
31+
// int[] coins = new int[] { 186, 419, 83, 408 };//6249
32+
// var res = solution.CoinChange(coins, 6249);
33+
// ConsoleX.WriteLine(res);
34+
// }
35+
//}
36+
37+
public class Solution
38+
{
39+
/// <summary>
40+
/// 动态规划
41+
/// 设 coins 数组长度为 m, amount为 n
42+
/// 时间复杂度:O(mn)
43+
/// 空间复杂度:O(n)
44+
/// </summary>
45+
/// <param name="coins"></param>
46+
/// <param name="amount"></param>
47+
/// <returns></returns>
48+
public int CoinChange(int[] coins, int amount)
49+
{
50+
if (amount == 0)
51+
return 0;
52+
53+
//用 int.MaxValue 初始化之后,就不用把做 0 的判断了
54+
//int[] dp = Enumerable.Repeat(int.MaxValue, amount + 1).ToArray();
55+
//或者这个写法 Array.Fill(dp, int.MaxValue);
56+
int[] dp = new int[amount + 1];
57+
for (int i = 0; i < amount; i++)
58+
{
59+
if (i == 0 || dp[i] != 0)
60+
{
61+
//用 long 隐式转换防止 int 越界
62+
foreach (long coin in coins)
63+
{
64+
if (i + coin <= amount)
65+
{
66+
if (dp[i + coin] == 0)
67+
dp[i + coin] = dp[i] + 1;
68+
else
69+
dp[i + coin] = Math.Min(dp[i] + 1, dp[i + coin]);
70+
}
71+
}
72+
}
73+
}
74+
return dp[amount] == 0 ? -1 : dp[amount];
75+
}
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)