Skip to content

Commit be9dca4

Browse files
authored
Merge branch 'neetcode-gh:main' into 9
2 parents 2228dea + 2d0e0cc commit be9dca4

File tree

81 files changed

+1985
-116
lines changed

Some content is hidden

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

81 files changed

+1985
-116
lines changed

README.md

+61-61
Large diffs are not rendered by default.

cpp/0014-longest-common-prefix.cpp

+24-55
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,28 @@
11
class Solution {
22
public:
3-
vector<vector<int>> res;
4-
5-
vector<vector<int>> fourSum(vector<int>& nums, int target) {
6-
7-
if(nums.size() < 4) return res;
8-
9-
vector<int>quad;
10-
sort(nums.begin() , nums.end());
11-
kSum(0,4,target,nums,quad);
12-
return res;
13-
}
14-
15-
16-
void kSum (int index , int k , long long target, vector<int> nums , vector<int>&q)
17-
{
18-
19-
if(k == 2)
20-
{
21-
twoSum(index , target, q , nums);
22-
return;
23-
}
24-
25-
for(int i = index ; i < nums.size() - k + 1; i++)
26-
{
27-
if(i > index && nums[i] == nums[i-1]) continue;
28-
q.push_back(nums[i]);
29-
kSum(i+1 , k-1 , target-nums[i] , nums , q);
30-
q.pop_back();
31-
}
32-
33-
}
34-
35-
void twoSum (int start,long long target,vector<int>&ans,vector<int>& nums)
36-
{
37-
int lo = start;
38-
int hi = nums.size()-1;
39-
40-
while(lo < hi)
41-
{
42-
int sum = nums[lo]+nums[hi];
43-
if(sum > target) hi--;
44-
else if (sum < target) lo++;
45-
46-
else
47-
{
48-
ans.insert(ans.end() , {nums[lo] , nums[hi]});
49-
res.push_back(ans);
3+
string longestCommonPrefix(vector<string>& strs) {
4+
string result = strs[0];
5+
int charIndex = 0;
506

51-
ans.pop_back();
52-
ans.pop_back();
53-
54-
lo++;
55-
while (lo < hi && nums[lo] == nums[lo - 1]) lo++;
56-
}
57-
}
58-
}
7+
//finding minimum string length - that could be max common prefix
8+
long maxCharIndex = strs[0].length();
9+
for (int i = 1; i < strs.size(); ++i) {
10+
if (strs[i].length() < maxCharIndex) {
11+
maxCharIndex = strs[i].length();
12+
}
13+
}
14+
15+
while (charIndex < maxCharIndex) {
16+
char prevChar = strs[0][charIndex];
17+
for (int i = 1; i < strs.size(); ++i) {
18+
if (prevChar == strs[i][charIndex]) {
19+
continue;
20+
}
21+
return result.substr(0, charIndex);
22+
}
23+
++charIndex;
24+
result += prevChar;
25+
}
26+
return result.substr(0, charIndex);
27+
}
5928
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int removeDuplicates(vector<int>& nums) {
4+
int left = 1;
5+
6+
for(int right = 1; right < nums.size(); right++){
7+
if(nums[right] != nums[right - 1]){
8+
nums[left] = nums[right];
9+
left++;
10+
}
11+
}
12+
13+
return left;
14+
}
15+
};

cpp/0027-Remove-element.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int removeElement(vector<int>& nums, int val) {
4+
int n=nums.size();
5+
int count=0;
6+
for(int i=0;i<n;i++)
7+
{
8+
if(nums[i]!=val)
9+
{
10+
swap(nums[i],nums[count]);
11+
count++;
12+
}
13+
}
14+
15+
return count;
16+
}
17+
};

cpp/0290-Word-Pattern.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
bool wordPattern(string pattern, string s) {
4+
stringstream ss(s);
5+
vector<string> sWords;
6+
string word;
7+
while(ss >> word){
8+
sWords.push_back(word);
9+
}
10+
11+
if(pattern.size() != sWords.size()){
12+
return false;
13+
}
14+
15+
unordered_map<char, string> patternMap;
16+
unordered_map<string, char> sMap;
17+
18+
for(int i = 0; i < pattern.size(); i++){
19+
if(patternMap.count(pattern[i]) && patternMap[pattern[i]] != sWords[i]){
20+
return false;
21+
}
22+
if(sMap.count(sWords[i]) && sMap[sWords[i]] != pattern[i]){
23+
return false;
24+
}
25+
26+
patternMap[pattern[i]] = sWords[i];
27+
sMap[sWords[i]] = pattern[i];
28+
}
29+
30+
return true;
31+
}
32+
};

cpp/0344-Reverse-String.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
void reverseString(vector<char>& s) {
4+
int left = 0, right = s.size() - 1;
5+
6+
while (left < right){
7+
swap(s[left], s[right]);
8+
9+
left++;
10+
right--;
11+
}
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public int val;
5+
* public TreeNode left;
6+
* public TreeNode right;
7+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
8+
* this.val = val;
9+
* this.left = left;
10+
* this.right = right;
11+
* }
12+
* }
13+
*/
14+
public class Solution {
15+
public IList<int> InorderTraversal(TreeNode root) {
16+
// return InorderTraversalRecursive(root).ToList();
17+
return InorderTraversalIterative(root).ToList();
18+
}
19+
20+
// Time: O(n)
21+
// Space: O(n)
22+
private IEnumerable<int> InorderTraversalRecursive(TreeNode node) {
23+
if (node == null) {
24+
return Enumerable.Empty<int>();
25+
}
26+
27+
return InorderTraversalRecursive(node.left)
28+
.Append(node.val)
29+
.Concat(InorderTraversalRecursive(node.right));
30+
}
31+
32+
// Time: O(n)
33+
// Space: O(n)
34+
private IEnumerable<int> InorderTraversalIterative(TreeNode node) {
35+
List<int> result = new();
36+
Stack<TreeNode> stack = new();
37+
while (node != null || stack.Any()) {
38+
while (node != null) {
39+
stack.Push(node);
40+
node = node.left;
41+
}
42+
node = stack.Pop();
43+
result.Add(node.val);
44+
node = node.right;
45+
}
46+
return result;
47+
}
48+
}

go/0002-add-two-numbers.go.txt

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
2+
var dummy = new(ListNode)
3+
var l3 **ListNode = &dummy
4+
var carry int
5+
list1 := l1
6+
list2 := l2
7+
8+
for (list1 != nil) || (list2 != nil) {
9+
l3 = &((*l3).Next)
10+
*l3 = new(ListNode)
11+
var sum int
12+
13+
if list1 != nil {
14+
sum += list1.Val;
15+
list1 = list1.Next
16+
}
17+
18+
if list2 != nil{
19+
sum += list2.Val
20+
list2 = list2.Next
21+
}
22+
23+
(*l3).Val = (sum + carry)%10
24+
carry = (sum + carry)/10
25+
}
26+
27+
if(carry > 0){
28+
l3 = &((*l3).Next)
29+
*l3 = new(ListNode)
30+
(*l3).Val = carry
31+
}
32+
33+
return dummy.Next
34+
}

go/0054-spiral-matrix.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var visited = 200 // -100 <= matrix[i][j] <= 100
2+
3+
func spiralOrder(matrix [][]int) []int {
4+
n, m := len(matrix[0]), len(matrix)
5+
x, y, dx, dy := 0, 0, 1, 0
6+
ans := make([]int, 0)
7+
for i := 0; i < m*n; i++ {
8+
if !(0 <= x+dx && x+dx < n) || !(0 <= y+dy && y+dy < m) || matrix[y+dy][x+dx] == visited {
9+
dx, dy = -dy, dx
10+
}
11+
ans = append(ans, matrix[y][x])
12+
matrix[y][x] = visited
13+
x, y = x+dx, y+dy
14+
}
15+
return ans
16+
}

go/0077-combinations.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func backtrack(n, k, start int, arr []int, ans *[][]int) {
2+
if len(arr) == k {
3+
comb := make([]int, k)
4+
copy(comb, arr)
5+
*ans = append(*ans, comb)
6+
return
7+
}
8+
for i := start; i <= n-k+(len(arr)+1); i++ {
9+
arr = append(arr, i)
10+
backtrack(n, k, i+1, arr, ans)
11+
arr = arr[:len(arr)-1]
12+
}
13+
}
14+
15+
func combine(n int, k int) [][]int {
16+
ans := [][]int{}
17+
backtrack(n, k, 1, []int{}, &ans)
18+
return ans
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func sortedArrayToBST(nums []int) *TreeNode {
10+
if nums == nil || len(nums) == 0 {
11+
return nil
12+
}
13+
mid := len(nums) / 2
14+
return &TreeNode{
15+
Val: nums[mid],
16+
Left: sortedArrayToBST(nums[:mid]),
17+
Right: sortedArrayToBST(nums[mid+1:]),
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a Node.
3+
* type Node struct {
4+
* Val int
5+
* Left *Node
6+
* Right *Node
7+
* Next *Node
8+
* }
9+
*/
10+
11+
func connect(root *Node) *Node {
12+
populate(root)
13+
return root
14+
}
15+
16+
func populate(node *Node) {
17+
if node == nil {
18+
return
19+
}
20+
if node.Left == nil {
21+
return
22+
}
23+
24+
node.Left.Next = node.Right
25+
if node.Next != nil {
26+
node.Right.Next = node.Next.Left
27+
}
28+
populate(node.Left)
29+
populate(node.Right)
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
8+
func getIntersectionNode(headA, headB *ListNode) *ListNode {
9+
a, b := headA, headB
10+
for a != b {
11+
if a == nil {
12+
a = headB
13+
} else {
14+
a = a.Next
15+
}
16+
if b == nil {
17+
b = headA
18+
} else {
19+
b = b.Next
20+
}
21+
}
22+
return a
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
func findKthLargest(nums []int, k int) int {
2+
sort.Ints(nums)
3+
return nums[len(nums)-k]
4+
}

go/0263-ugly-number.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func isUgly(n int) bool {
2+
if n <= 0 {
3+
return false
4+
}
5+
6+
primes := []int{2, 3, 5}
7+
for _, p := range primes {
8+
for n%p == 0 {
9+
n /= p
10+
}
11+
}
12+
return n == 1
13+
}

0 commit comments

Comments
 (0)