Skip to content

Commit 7bc724f

Browse files
author
Li Li
committed
re-arrange folder and files
1 parent 1af3b02 commit 7bc724f

16 files changed

+126
-108
lines changed
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
public class Solution {
1+
/*
2+
Similar problems:
3+
1. Two Sum
4+
653. Two Sum IV - Input is a BST
5+
*/
6+
public class Solution {
27
public int[] TwoSum(int[] nums, int target) {
38
int[] res = null;
49
if (nums == null || nums.Length == 0) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*
2+
Similar problems:
3+
438. Find All Anagrams in a String
4+
*/
15
public class Solution {
26
public bool IsAnagram(string s, string t) {
37
if (s == null) return t == null;
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
class Solution
1+
/*
2+
Similar problems:
3+
27. Remove Element;
4+
203. Remove Linked List Elements;
5+
283. Move Zeroes;
6+
*/
7+
class Solution
28
{
39
public int RemoveDuplicates(int[] nums)
410
{
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*
2+
Similar Problem:
3+
459. Repeated Substring Pattern
4+
*/
15
public class Solution {
26
public int StrStr(string haystack, string needle) {
37
if (haystack == null || needle == null) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
public class Solution {
2+
/*
3+
Requirements for atoi:
4+
1. trim whitespace
5+
2. takes an optional '+' or '-'
6+
3. when meet non-numetic char, break " - 0012a43" return -12
7+
4. If no valid conversion could be performed, 0 is returned.
8+
5. If out of int range, return INT_MAX(2147483647) or INT_MIN(-2147483648)
9+
*/
10+
11+
public int MyAtoi(string str)
12+
{
13+
if (str == null) return 0;
14+
str = str.Trim();
15+
if (str.Length == 0) return 0;
16+
int sign = 1;
17+
int index = 0;
18+
if (str[index] == '+') index++;
19+
else if (str[index] == '-') {
20+
sign = -1;
21+
index++;
22+
}
23+
long num = 0;
24+
for (; index < str.Length; index++) {
25+
if (str[index] < '0' || str[index] > '9')
26+
{
27+
break;
28+
}
29+
num = num * 10 + str[index] - '0';
30+
if (num > int.MaxValue)
31+
{
32+
break;
33+
}
34+
}
35+
if (num * sign >= int.MaxValue)
36+
{
37+
return int.MaxValue;
38+
}
39+
if (num * sign <= int.MinValue)
40+
{
41+
return int.MinValue;
42+
}
43+
return (int)num * sign;
44+
}
45+
}

LeetcodeCShaprDotNetCore/Program.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
using System;
2-
2+
using System.Collections.Generic;
33
namespace LeetcodeCShaprDotNetCore {
44
class Program {
55
static void Main(string[] args) {
6-
var s = new Solution();
7-
var r = s.TwoSum(new int[] { 11, 7, 2, 15 }, 9);
8-
System.Console.WriteLine(r[0] + " " + r[1]);
6+
var solution = new Solution2();
7+
var s1 = "abab";
8+
var s2 = "aba";
9+
var s3 = "abcabcabcabc";
10+
System.Console.WriteLine(solution.RepeatedSubstringPattern(s1));
11+
System.Console.WriteLine(solution.RepeatedSubstringPattern(s2));
12+
System.Console.WriteLine(solution.RepeatedSubstringPattern(s3));
913
}
1014
}
1115
}
1216

1317

1418
/*
15-
Input: numbers={2, 7, 11, 15}, target=9
16-
Output: index1=1, index2=
17-
*/
19+
Given "abcabcbb", the answer is "abc", which the length is 3.
20+
21+
Given "bbbbb", the answer is "b", with the length of 1.
22+
23+
Given "pwwkew", the answer is "wke", with the length of 3.
24+
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
25+
*/

LeetcodeCShaprDotNetCore/Solution.cs

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,48 @@
44

55
namespace LeetcodeCShaprDotNetCore {
66
public class Solution {
7-
public bool IsAnagram(string s, string t) {
8-
if (s == null) return t == null;
9-
if (t == null) return false;
10-
int m = s.Length, n = t.Length;
11-
if (m != n) return false;
12-
var dict = new Dictionary<char, int>();
13-
for (var i = 0; i < m; i++) {
14-
if (!dict.ContainsKey(s[i])) {
15-
dict[s[i]] = 1;
16-
} else {
17-
dict[s[i]]++;
18-
}
7+
/*
8+
Requirements for atoi:
9+
1. trim whitespace
10+
2. takes an optional '+' or '-'
11+
3. when meet non-numetic char, break " - 0012a43" return -12
12+
4. If no valid conversion could be performed, 0 is returned.
13+
5. If out of int range, return INT_MAX(2147483647) or INT_MIN(-2147483648)
14+
*/
15+
16+
public int MyAtoi(string str)
17+
{
18+
if (str == null) return 0;
19+
str = str.Trim();
20+
if (str.Length == 0) return 0;
21+
int sign = 1;
22+
int index = 0;
23+
if (str[index] == '+') index++;
24+
else if (str[index] == '-') {
25+
sign = -1;
26+
index++;
1927
}
20-
for (var i = 0; i < n; i++) {
21-
if (!dict.ContainsKey(t[i])) {
22-
return false;
28+
long num = 0;
29+
for (; index < str.Length; index++) {
30+
if (str[index] < '0' || str[index] > '9')
31+
{
32+
break;
2333
}
24-
dict[t[i]]--;
25-
if (dict[t[i]] < 0) {
26-
return false;
34+
num = num * 10 + str[index] - '0';
35+
if (num > int.MaxValue)
36+
{
37+
break;
2738
}
2839
}
29-
return true;
40+
if (num * sign >= int.MaxValue)
41+
{
42+
return int.MaxValue;
43+
}
44+
if (num * sign <= int.MinValue)
45+
{
46+
return int.MinValue;
47+
}
48+
return (int)num * sign;
3049
}
3150
}
3251
}

README.md

Lines changed: 7 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
I will use this repository to make notes for leetcode solutions and sort similar questions into the same group or category.
44

5-
### Solved Problem (8)
5+
### Solved Problem List
6+
7+
## Algorithms Basics
8+
9+
First, I will start from the very basic and useful part. The Leetcode Course Section, which is selected interview questions aimed for beginners who do not know algorithms or need a refresher. Learn by doing.
10+
11+
### Chapter 1. Array / String (with similar problem)
612

713
ID | Difficulty | Tags | Solution
814
-- | ---------- | ---- | --------
@@ -22,82 +28,3 @@ ID | Difficulty | Tags | Solution
2228
<br />
2329
<br />
2430

25-
## Algorithms Basics
26-
27-
First, I will start from the very basic and useful part. The Leetcode Course Section, which is selected interview questions aimed for beginners who do not know algorithms or need a refresher. Learn by doing.
28-
29-
### Chapter 1. Array / String
30-
31-
#### 26. Remove Duplicates from Sorted Array
32-
33-
This problem can be solved by typical double pointer technique.
34-
35-
**Algorithm**
36-
Since the array is already sorted, we can keep two pointers *slow* and *fast*. As long as nums[*fast*] = nums[*slow*], we increse *fast* to skip the duplicate.
37-
38-
When we encounter nums[*fast*] != nums[*slow*], the duplicate run has ended so we must copy its value to nums[*slow* + 1]. ii is then incremented and we repeat the same process again until *fast* reaches the end of array.
39-
The core part for the solution code is as below:
40-
41-
```csharp
42-
class Solution
43-
{
44-
public int RemoveDuplicates(int[] nums)
45-
{
46-
if (nums == null || nums.Length == 0)
47-
{
48-
return 0;
49-
}
50-
int slow = 0;
51-
for (int fast = 0; fast < nums.Length; fast++)
52-
{
53-
if (nums[fast] != nums[slow])
54-
{
55-
nums[++slow] = nums[fast];
56-
}
57-
}
58-
return slow + 1;
59-
}
60-
}
61-
```
62-
63-
Similar problems:
64-
65-
*27.* Remove Element;
66-
*203.* Remove Linked List Elements;
67-
*283.* Move Zeroes (error prone, double think for more cases)
68-
69-
#### 167. Two Sum II - Input array is sorted
70-
71-
This problem can be solved by typical double pointer technique.
72-
73-
**Algorithm**
74-
Since the array is already sorted, we can keep two pointers *left* and *right*. let v = nums[*left*] + nums[*right*], if v < target, left++; if v > target, right--; if v == target, then return.
75-
76-
Similar problems:
77-
*1.* Two Sum (use dict for each item just use almost O(1) check time, so totally O(n) time);
78-
*653.* Two Sum IV - Input is a BST (3 solution to revisit)
79-
80-
#### 242. Valid Anagram
81-
82-
This problem can be solved by using Dictionary.
83-
84-
**Algorithm**
85-
First use dict record the counts for each char in string1, then check for string2, for each char c, if !dict.ContainsKey(c), return fasle; else dict[c]--, if dict[c] < 0, return false.
86-
87-
Similar problems:
88-
*438.* Find All Anagrams in a String (using sliding window in substring)
89-
90-
#### 3. Longest Substring Without Repeating Characters
91-
92-
This problem can be solved by using Dictionary, Two pointers.
93-
Error prone. Think more about different cases.
94-
95-
**Algorithm**
96-
Use dict save record for char. Use two pointers as a sliding window for substring problem. If the char at right pointer exists in the dict, just move the left pointer to skip the same previous char (be careful to compare if left pointer is already larger than the previous char index).
97-
98-
#### 28. Implement strStr()
99-
100-
This problem can be solve either by brute force or using hash method or KMP.
101-
102-
Similar Problem:
103-
*459.* Repeated Substring Pattern

0 commit comments

Comments
 (0)