Skip to content

Commit 22dcf55

Browse files
authored
Merge branch 'neetcode-gh:main' into main
2 parents ab53081 + 1939100 commit 22dcf55

File tree

73 files changed

+2196
-177
lines changed

Some content is hidden

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

73 files changed

+2196
-177
lines changed

README.md

Lines changed: 29 additions & 29 deletions
Large diffs are not rendered by default.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* struct ListNode *next;
6+
* };
7+
*/
8+
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
9+
struct ListNode *currA = headA;
10+
struct ListNode *currB = headB;
11+
12+
while (currA && currB){
13+
if (currA == currB){
14+
return currA;
15+
}
16+
currA = currA->next;
17+
currB = currB->next;
18+
19+
if (!currA && currB){
20+
currA = headB;
21+
}
22+
if (!currB && currA){
23+
currB = headA;
24+
}
25+
}
26+
return NULL;
27+
}

c/0234-palindrome-linked-list.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* struct ListNode *next;
6+
* };
7+
*/
8+
bool isPalindrome(struct ListNode* head){
9+
10+
// find middle of linked list
11+
struct ListNode *slow = head, *fast = head;
12+
while (fast && fast->next){
13+
slow = slow->next;
14+
fast = fast->next->next;
15+
}
16+
// reverse second half
17+
struct ListNode* prev = NULL;
18+
while (slow){
19+
struct ListNode* nxt = slow->next;
20+
slow->next = prev;
21+
prev = slow;
22+
slow = nxt;
23+
}
24+
25+
// compare left and right
26+
struct ListNode *left=head, *right=prev;
27+
while (right){
28+
if (left->val != right->val){
29+
return false;
30+
}
31+
left = left->next;
32+
right = right->next;
33+
}
34+
return true;
35+
36+
}

cpp/0206-reverse-linked-list.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Given the head of a singly linked list, reverse list & return
33
Ex. head = [1,2,3,4,5] -> [5,4,3,2,1], head = [1,2] -> [2,1]
44
5-
Maintain prev, curr, & next pointers, iterate thru & reverse
5+
Maintain prev, curr pointers, iterate thru & reverse
66
77
Time: O(n)
88
Space: O(1)
@@ -18,24 +18,24 @@
1818
* ListNode(int x, ListNode *next) : val(x), next(next) {}
1919
* };
2020
*/
21-
class Solution {
21+
class Solution
22+
{
2223
public:
23-
ListNode* reverseList(ListNode* head) {
24-
if (head == NULL || head->next == NULL) {
24+
ListNode *reverseList(ListNode *head)
25+
{
26+
if (head == NULL || head->next == NULL)
2527
return head;
26-
}
27-
28-
ListNode* prev = NULL;
29-
ListNode* curr = head;
30-
ListNode* next = curr->next;
31-
32-
while (curr != NULL) {
33-
next = curr->next;
28+
29+
ListNode *prev = NULL;
30+
ListNode *curr = head;
31+
32+
while (curr != NULL)
33+
{
34+
ListNode *temp = curr->next;
3435
curr->next = prev;
3536
prev = curr;
36-
curr = next;
37+
curr = temp;
3738
}
38-
3939
return prev;
4040
}
4141
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
typedef unsigned long long ll;
2+
class Solution {
3+
public:
4+
bool solve(string & s , ll last , int index,int cnt){
5+
6+
if(index >= s.size()) return cnt > 1;
7+
8+
ll num = 0;
9+
bool ret = false;
10+
11+
for(int i = index ; i < s.size() ; i++){
12+
num = num * 10;
13+
num += (s[i] - '0');
14+
15+
if(last == -1 || last == num + 1){
16+
ret |= solve(s , num , i + 1 , cnt + 1);
17+
}else if(last != -1 && num >= last)break;
18+
}
19+
20+
return ret;
21+
}
22+
bool splitString(string s) {
23+
return solve(s,-1,0,0);
24+
}
25+
};

csharp/0179-largest-number.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class Solution {
2+
public string LargestNumber(int[] nums)
3+
{
4+
if(nums.All(_ => _ == 0)) return "0";
5+
6+
var s = nums.Select(_ => _.ToString()).ToList();
7+
8+
s.Sort((a, b) => (b+a).CompareTo(a+b));
9+
10+
return string.Concat(s);
11+
}
12+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
public class Solution {
2+
public int MaxProduct(string s) {
3+
if(s == null || s.Length < 2)
4+
return 0;
5+
if(s.Length == 2)
6+
return 1;
7+
8+
int n = s.Length;
9+
int total = 1 << n;
10+
11+
List<(int, int)> possible = new List<(int, int)>();
12+
13+
for(int i = 0; i < total; i++) {
14+
StringBuilder sb = new StringBuilder();
15+
16+
for(int j = 0; j < n; j++) {
17+
if((i & (1 << j)) != 0) {
18+
sb.Append(s[j]);
19+
}
20+
}
21+
22+
if(IsPalindrome(sb.ToString())) {
23+
possible.Add((i, sb.Length));
24+
}
25+
}
26+
27+
int ans = 0;
28+
for(int i = 0; i < possible.Count; i++) {
29+
int bitmask = possible[i].Item1;
30+
int count = possible[i].Item2;
31+
for(int j = i + 1; j < possible.Count; j++) {
32+
int bitmask2 = possible[j].Item1;
33+
int count2 = possible[j].Item2;
34+
if((bitmask & bitmask2) == 0)
35+
ans = Math.Max(ans, count * count2);
36+
}
37+
}
38+
return ans;
39+
}
40+
41+
private bool IsPalindrome(string s){
42+
int i = 0;
43+
int j = s.Length - 1;
44+
while(i < j) {
45+
if(s[i++] != s[j--])
46+
return false;
47+
}
48+
return true;
49+
}
50+
}

go/0179-largest-number.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func largestNumber(nums []int) string {
2+
ans, s := "", make([]string, len(nums))
3+
for i, num := range nums { s[i] = strconv.Itoa(num) }
4+
sort.Slice(s, func(a, b int) bool { return s[a] + s[b] > s[b] + s[a] })
5+
if s[0] == "0" { return "0" }
6+
for _, v := range s { ans += v }
7+
return ans
8+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
func maxProduct(s string) (res int) {
2+
dp := make([]int, 1<<len(s))
3+
mask := (1 << len(s)) - 1
4+
palindromeSize := func(s string, mask int) (res int) {
5+
i, j := 0, len(s)
6+
for i <= j {
7+
if mask&(1<<i) == 0 {
8+
i++
9+
} else if mask&(1<<j) == 0 {
10+
j--
11+
} else if s[i] != s[j] {
12+
return 0
13+
} else {
14+
if i == j {
15+
res++
16+
} else {
17+
res += 2
18+
}
19+
i++
20+
j--
21+
}
22+
}
23+
return res
24+
}
25+
26+
max := func(a, b int) int {
27+
if a > b {
28+
return a
29+
}
30+
return b
31+
}
32+
33+
for m := 1; m <= mask; m++ {
34+
dp[m] = palindromeSize(s, m)
35+
}
36+
37+
for m1 := mask; m1 > 0; m1-- {
38+
if dp[m1]*(len(s)-dp[m1]) <= res {
39+
continue
40+
}
41+
for m2 := mask ^ m1; m2 > 0; m2 = (m2 - 1) & (mask ^ m1) {
42+
res = max(res, dp[m1]*dp[m2])
43+
}
44+
}
45+
return
46+
}

java/0020-valid-parentheses.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@ public boolean isValid(String s) {
2828
class Solution {
2929
public boolean isValid(String s) {
3030
Stack<Character> brackets = new Stack<>();
31-
Map<Character, Character> bracketLookup = new HashMap<>();
31+
Map<Character, Character> bracketLookup = new HashMap<>(3);
3232

3333
bracketLookup.put(')', '(');
3434
bracketLookup.put('}', '{');
3535
bracketLookup.put(']', '[');
3636

37-
for (char c : s.toCharArray()) {
37+
for (int i = 0; i < s.length(); i++) {
38+
char c = s.charAt(i);
3839
if (bracketLookup.containsKey(c)) {
39-
if (brackets.size() != 0 && brackets.peek() == bracketLookup.get(c)) {
40+
if (!brackets.isEmpty() && bracketLookup.get(c).equals(brackets.peek())) {
4041
brackets.pop();
4142
} else {
4243
return false;
@@ -46,7 +47,6 @@ public boolean isValid(String s) {
4647
}
4748
}
4849

49-
if (brackets.size() == 0) return true;
50-
return false;
50+
return brackets.isEmpty();
5151
}
5252
}

0 commit comments

Comments
 (0)