Skip to content

Commit 7639cc7

Browse files
committed
Merge branch 'main' of https://github.com/neetcode-gh/leetcode into mirvin/move-python-sols-to-dir
2 parents 1ba7c0f + 8bad0fb commit 7639cc7

File tree

85 files changed

+2069
-46
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+2069
-46
lines changed

125-Valid-Palindrome.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
class Solution:
22
def isPalindrome(self, s: str) -> bool:
3-
l, r = 0, len(s) - 1
4-
while l < r:
5-
while l < r and not self.alphanum(s[l]):
6-
l += 1
7-
while l < r and not self.alphanum(s[r]):
8-
r -= 1
9-
if s[l].lower() != s[r].lower():
3+
l = 0
4+
r = len(s)-1
5+
while l<r:
6+
if s[l].lower()==s[r].lower():
7+
l+=1
8+
r-=1
9+
continue
10+
11+
elif not (65<=ord(s[l])<=90 or 97<=ord(s[l])<=122 or 48<=ord(s[l])<=57):
12+
l+=1
13+
elif not (65<=ord(s[r])<=90 or 97<=ord(s[r])<=122 or 48<=ord(s[r])<=57):
14+
r-=1
15+
else:
1016
return False
11-
l += 1
12-
r -= 1
1317
return True
14-
15-
# Could write own alpha-numeric function
16-
def alphanum(self, c):
17-
return (ord('A') <= ord(c) <= ord('Z') or
18-
ord('a') <= ord(c) <= ord('z') or
19-
ord('0') <= ord(c) <= ord('9'))

261-Graph-Valid-Tree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Problem is free on Lintcode
1+
# Problem is free on Lintcode https://www.lintcode.com/problem/178/
22
class Solution:
33
"""
44
@param n: An integer

csharp/1-Two-Sum.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class Solution {
2+
public int[] TwoSum(int[] nums, int target) {
3+
Dictionary<int, int> indices = new Dictionary<int, int>();
4+
5+
for (int i = 0; i < nums.Length; i++) {
6+
var diff = target - nums[i];
7+
if (indices.ContainsKey(diff)) {
8+
return new int[] {indices[diff], i};
9+
}
10+
indices[nums[i]] = i;
11+
}
12+
return null;
13+
}
14+
}
15+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class Solution {
2+
public int MaxProfit(int[] prices) {
3+
int maxProfit = 0;
4+
int minPrice = prices[0];
5+
6+
for (int i = 1; i < prices.Length; i++) {
7+
int currPrice = prices[i];
8+
minPrice = Math.Min(minPrice, currPrice);
9+
maxProfit = Math.Max(maxProfit, currPrice - minPrice);
10+
}
11+
return maxProfit;
12+
}
13+
}

csharp/125-Valid-Palindrome.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class Solution {
2+
public bool IsPalindrome(string s) {
3+
int left = 0;
4+
int right = s.Length - 1;
5+
6+
while (left < right) {
7+
if (!char.IsLetterOrDigit(s[left])) {
8+
left++;
9+
} else if (!char.IsLetterOrDigit(s[right])) {
10+
right--;
11+
} else {
12+
if (char.ToLower(s[left]) != char.ToLower(s[right])) {
13+
return false;
14+
}
15+
left++;
16+
right--;
17+
}
18+
}
19+
return true;
20+
}
21+
}

csharp/155-Min-Stack.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class MinStack {
2+
private Stack<int> stack;
3+
private Stack<int> minStack;
4+
5+
public MinStack() {
6+
stack = new Stack<int>();
7+
minStack = new Stack<int>();
8+
}
9+
10+
public void Push(int val) {
11+
stack.Push(val);
12+
int min = Math.Min(val, minStack.Count != 0 ? minStack.Peek() : val);
13+
minStack.Push(min);
14+
}
15+
16+
public void Pop() {
17+
stack.Pop();
18+
minStack.Pop();
19+
}
20+
21+
public int Top() {
22+
return stack.Peek();
23+
}
24+
25+
public int GetMin() {
26+
return minStack.Peek();
27+
}
28+
}

csharp/167-Two-Sum-II.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Solution {
2+
public int[] TwoSum(int[] numbers, int target) {
3+
// Using 2 pointers. Since sorted, if l+r > target, decrease r.
4+
// Else if l+r < target, increase l. Else, result is found.
5+
int left = 0, right = numbers.Length - 1;
6+
7+
while (left < right) {
8+
int sum = numbers[left] + numbers[right];
9+
if (sum > target) {
10+
right--;
11+
} else if (sum < target) {
12+
left++;
13+
} else {
14+
return new int[] {left + 1, right + 1};
15+
}
16+
}
17+
18+
return new int[0];
19+
}
20+
}

csharp/20-Valid-Parentheses.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Solution {
2+
public bool IsValid(string s) {
3+
var stack = new Stack<char>();
4+
var pairs = new Dictionary<char, char>() {
5+
[')'] = '(',
6+
[']'] = '[',
7+
['}'] = '{'
8+
};
9+
10+
foreach (char c in s) {
11+
if (!pairs.ContainsKey(c)) {
12+
stack.Push(c);
13+
} else if (stack.Count == 0 || stack.Pop() != pairs[c]) {
14+
return false;
15+
}
16+
}
17+
18+
return stack.Count == 0;
19+
}
20+
}

csharp/242-Valid-Anagram.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Solution {
2+
public bool IsAnagram(string s, string t) {
3+
if (s.Length != t.Length) return false;
4+
if (s == t) return true;
5+
6+
Dictionary<char, int> sCounts = new Dictionary<char, int>();
7+
Dictionary<char, int> tCounts = new Dictionary<char, int>();
8+
9+
for (int i = 0; i < s.Length; i++) {
10+
sCounts[s[i]] = 1 + (sCounts.ContainsKey(s[i]) ? sCounts[s[i]] : 0);
11+
tCounts[t[i]] = 1 + (tCounts.ContainsKey(t[i]) ? tCounts[t[i]] : 0);
12+
}
13+
14+
foreach (char c in sCounts.Keys) {
15+
int tCount = tCounts.ContainsKey(c) ? tCounts[c] : 0;
16+
if (sCounts[c] != tCount) return false;
17+
}
18+
return true;
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class Solution {
2+
public int LengthOfLongestSubstring(string s) {
3+
int leftPointer = 0, rightPointer = 0, maxLength = 0;
4+
HashSet<int> chars = new HashSet<int>();
5+
6+
while (rightPointer < s.Length) {
7+
char currChar = s[rightPointer];
8+
if (chars.Contains(currChar)) { // Move left pointer until all duplicate chars removed
9+
chars.Remove(s[leftPointer]);
10+
leftPointer++;
11+
} else {
12+
chars.Add(currChar);
13+
maxLength = Math.Max(maxLength, rightPointer - leftPointer + 1);
14+
rightPointer++;
15+
}
16+
}
17+
return maxLength;
18+
}
19+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy