Skip to content

Commit 73ffaf9

Browse files
committed
Add new code in clear code war
1 parent d013307 commit 73ffaf9

13 files changed

+417
-83
lines changed
Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11

2+
/* O(n) runtime, O(1) space */
3+
24
public class Solution {
35
public bool IsPalindrome(string s) {
4-
if (s.Length == 0) return true;
56
int left = 0, right = s.Length - 1;
6-
while (left < right) {
7-
while (left < right && !char.IsLetterOrDigit(s[left])){
8-
left++;
9-
}
10-
if (left == right) break;
11-
while (left < right && !char.IsLetterOrDigit(s[right])) {
12-
right--;
13-
}
14-
if (left == right) break;
15-
if (char.ToLower(s[left]) != char.ToLower(s[right])) {
16-
return false;
17-
}
18-
left++;
19-
right--;
20-
7+
while (left < right)
8+
{
9+
while (left < right && !char.IsLetterOrDigit(s[left])) left++;
10+
while (left < right && !char.IsLetterOrDigit(s[right])) right--;
11+
if (char.ToLower(s[left]) != char.ToLower(s[right])) return false;
12+
left++; right--;
2113
}
2214
return true;
2315
}
24-
}
16+
}
17+
18+
/*
19+
Similar Questions or Next challenges:
20+
Palindrome Linked List
21+
Valid Palindrome II
22+
*/

CodeClearWar/1. Two Sum.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
to get the value, index.
1313
1414
That's it! Just do re-mapping, trade off is O(n) space vs O(n) time.
15-
15+
The solution is
16+
O(n) runtime, O(n) space
1617
*/
1718
public class Solution {
1819
public int[] TwoSum(int[] nums, int target) {
@@ -23,9 +24,7 @@ public int[] TwoSum(int[] nums, int target) {
2324
if (dict.ContainsKey(complement)) {
2425
return result = new int[] { dict[complement], i };
2526
}
26-
if (!dict.ContainsKey(cur)) {
27-
dict[cur] = i;
28-
}
27+
dict[cur] = i;
2928
}
3029
throw new Exception("No two sum solution");
3130
}

CodeClearWar/125. Valid Palindrome.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
/* O(n) runtime, O(1) space */
3+
4+
public class Solution {
5+
public bool IsPalindrome(string s) {
6+
int left = 0, right = s.Length - 1;
7+
while (left < right)
8+
{
9+
while (left < right && !char.IsLetterOrDigit(s[left])) left++;
10+
while (left < right && !char.IsLetterOrDigit(s[right])) right--;
11+
if (char.ToLower(s[left]) != char.ToLower(s[right])) return false;
12+
left++; right--;
13+
}
14+
return true;
15+
}
16+
}
17+
18+
/*
19+
Similar Questions or Next challenges:
20+
Palindrome Linked List
21+
Valid Palindrome II
22+
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* O(n) runtime, O(n) space:
2+
One simple approach is a two-pass solution: First pass to split the string by spaces into an
3+
array of words, then second pass to extract the words in reversed order.
4+
We can do better in one-pass. While iterating the string in reverse order, we keep track of
5+
a word’s begin and end position. When we are at the beginning of a word, we append it. */
6+
public class Solution {
7+
public string ReverseWords(string s) {
8+
if (s == null || s.Length == 0) return "";
9+
int j = s.Length;
10+
var str = new StringBuilder();
11+
for (int i = s.Length - 1; i >= 0; i--) {
12+
if (s[i] == ' ') {
13+
j = i;
14+
} else if (i == 0 || s[i - 1] == ' ') {
15+
if (str.Length != 0) {
16+
str.Append(' ');
17+
}
18+
str.Append(s.Substring(i, j - i));
19+
}
20+
}
21+
return str.ToString();
22+
}
23+
}
24+
25+
// use buildin trim and split
26+
public class Solution {
27+
public string ReverseWords(string s) {
28+
if (s == null || s.Length == 0 ) return "";
29+
string[] array = s.Split(' ');
30+
StringBuilder sb = new StringBuilder();
31+
for (int i = array.Length - 1; i >= 0; i--) {
32+
if (array[i].Trim() != "") {
33+
sb.Append(array[i]).Append(" ");
34+
}
35+
}
36+
// remove last " "
37+
return sb.ToString().TrimStart().TrimEnd();
38+
}
39+
}

CodeClearWar/167. Two Sum II - Input array is sorted.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public int[] TwoSum(int[] nums, int target) {
2020
}
2121
}
2222

23+
24+
2325
/*
2426
Next challenges:
2527
Find the Duplicate Number
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
O(n) runtime, O(1) space – In-place reverse:
3+
Let us indicate the i th word by w i and its reversal as w i ′. Notice that when you reverse a
4+
word twice, you get back the original word. That is, (w i ′)′ = w i .
5+
The input string is w 1 w 2 … w n . If we reverse the entire string, it becomes w n ′ … w 2 ′ w 1 ′.
6+
Finally, we reverse each individual word and it becomes w n … w 2 w 1 . Similarly, the same
7+
result could be reached by reversing each individual word first, and then reverse the
8+
entire string.
9+
*/
10+
11+
public class Solution {
12+
public void ReverseWords(char[] str) {
13+
if (str == null || str.Length <= 1) return;
14+
Reverse(str, 0, str.Length - 1);
15+
int l = 0;
16+
for (int r = 0; r < str.Length; r++) {
17+
if (r + 1 == str.Length || str[r + 1] == ' ') {
18+
Reverse(str, l, r);
19+
l = r + 2;
20+
}
21+
}
22+
}
23+
private void Reverse(char[] str, int left, int right) {
24+
while (left < right) {
25+
var temp = str[left];
26+
str[left] = str[right];
27+
str[right] = temp;
28+
left++;
29+
right--;
30+
}
31+
}
32+
}
33+
34+
/*
35+
Similar Questions
36+
Reverse Words in a String
37+
Rotate Array
38+
*/

CodeClearWar/189. Rotate Array.cs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
Approach #4 Using Reverse [Accepted]
3+
Algorithm
4+
5+
This approach is based on the fact that when we rotate the array k times, k elements from
6+
the back end of the array come to the front and the rest of the elements from the front shift backwards.
7+
8+
In this approach, we firstly reverse all the elements of the array. Then, reversing the
9+
first k elements followed by reversing the rest n-k elements gives us the required result.
10+
11+
"double reverse tech"
12+
Complexity Analysis
13+
14+
Time complexity : O(n). nn elements are reversed a total of three times.
15+
16+
Space complexity : O(1). No extra space is used.
17+
*/
18+
public class Solution {
19+
public void Rotate(int[] nums, int k) {
20+
if (nums == null || nums.Length == 0 || k % nums.Length == 0) return;
21+
k %= nums.Length;
22+
Reverse(nums, 0, nums.Length - 1);
23+
Reverse(nums, 0, k - 1);
24+
Reverse(nums, k, nums.Length - 1);
25+
}
26+
private void Reverse(int[] nums, int start, int end) {
27+
while (start < end) {
28+
var temp = nums[start];
29+
nums[start] = nums[end];
30+
nums[end] = temp;
31+
start++;
32+
end--;
33+
}
34+
}
35+
}
36+
37+
/*
38+
Approach #1 Brute Force [Time Limit Exceeded]
39+
The simplest approach is to rotate all the elements of the array in k steps by rotating
40+
the elements by 1 unit in each step.
41+
O(n*k) runtime, O(1) sapce. No extra space is used.
42+
*/
43+
public class Solution {
44+
public void Rotate(int[] nums, int k) {
45+
int temp, previous;
46+
for (int i = 0; i < k; i++) {
47+
previous = nums[nums.Length - 1];
48+
for (int j = 0; j < nums.Length; j++) {
49+
temp = nums[j];
50+
nums[j] = previous;
51+
previous = temp;
52+
}
53+
}
54+
}
55+
}
56+
57+
/*
58+
Approach #2 Using Extra Array [Accepted]
59+
We use an extra array in which we place every element of the array at its
60+
correct position i.e. the number at index ii in the original array is placed
61+
at the index (i+k). Then, we copy the new array to the original one.
62+
63+
O(n) runtime. One pass is used to put the numbers in the new array. And another pass
64+
to copy the new array to the original one.
65+
66+
O(n) space. Another array of the same size is used.
67+
68+
*/
69+
70+
public class Solution {
71+
public void Rotate(int[] nums, int k) {
72+
int[] a = new int[nums.Length];
73+
for (int i = 0; i < nums.Length; i++) {
74+
a[(i + k) % nums.Length] = nums[i];
75+
}
76+
for (int i = 0; i < nums.Length; i++) {
77+
nums[i] = a[i];
78+
}
79+
}
80+
}
81+
82+
/*
83+
Approach #3 Using Cyclic Replacements [Accepted]
84+
Complexity Analysis
85+
Time complexity : O(n). Only one pass is used.
86+
Space complexity : O(1). Constant extra space is used.
87+
*/
88+
public class Solution {
89+
public void Rotate(int[] nums, int k) {
90+
k = k % nums.Length;
91+
int count = 0;
92+
for (int start = 0; count < nums.Length; start++) {
93+
int current = start;
94+
int prev = nums[start];
95+
do {
96+
int next = (current + k) % nums.Length;
97+
int temp = nums[next];
98+
nums[next] = prev;
99+
prev = temp;
100+
current = next;
101+
count++;
102+
} while (start != current); // current will finally meet start, the least common multiple of k and n
103+
}
104+
}
105+
}
106+
107+
/*
108+
Similar Questions
109+
Rotate List
110+
Reverse Words in a String II
111+
*/
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Similar Problem:
3+
459. Repeated Substring Pattern
4+
*/
5+
public class Solution {
6+
public int StrStr(string haystack, string needle) {
7+
if (haystack == null || needle == null) {
8+
return -1;
9+
}
10+
int m = haystack.Length, n = needle.Length;
11+
if (n == 0) return 0;
12+
if (m < n) return -1;
13+
for (int i = 0; i < m - n; i++) {
14+
int j = 0;
15+
for (j = 0; j < n; j++) {
16+
if (haystack[i + j] != needle[j])
17+
break;
18+
}
19+
if (j == n) return i;
20+
}
21+
return -1;
22+
}
23+
}
24+
25+
/*
26+
Next challenges:
27+
Shortest Palindrome
28+
Repeated Substring Pattern
29+
*/

LeetcodeCShaprDotNetCore/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace LeetcodeCShaprDotNetCore {
44
class Program {
55
static void Main(string[] args) {
6-
6+
var s = new int[]{ 1, 0, -1, 0, -2, 2 };
7+
var res = Solution.FourSum(s, 0);
8+
Console.ReadLine();
79
}
810

911
}

LeetcodeCShaprDotNetCore/Solution.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,19 @@
44

55
namespace LeetcodeCShaprDotNetCore
66
{
7-
7+
public class Solution
8+
{
9+
public bool IsPalindrome(string s)
10+
{
11+
int left = 0, right = s.Length - 1;
12+
while (left < right)
13+
{
14+
while (left < right && !char.IsLetterOrDigit(s[left])) left++;
15+
while (left < right && !char.IsLetterOrDigit(s[right])) right--;
16+
if (char.ToLower(s[left]) != char.ToLower(s[right])) return false;
17+
left++; right--;
18+
}
19+
return true;
20+
}
21+
}
822
}

0 commit comments

Comments
 (0)