forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
47 changed files
with
1,129 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
func sortArray(nums []int) []int { | ||
sort.Ints(nums) | ||
return nums | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
func getConcatenation(nums []int) []int { | ||
n := len(nums) | ||
ans := make([]int, 2*n) //lets make an array of int type and 2*n size | ||
i := 0 | ||
for i < n { //implementing for loop for the given condition | ||
ans[i] = nums[i] | ||
ans[i+n] = nums[i] | ||
i++ | ||
} | ||
return ans | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 49 additions & 48 deletions
97
javascript/0080-remove-duplicates-from-sorted-array-ii.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,61 @@ | ||
/** | ||
* Linear | ||
* https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ | ||
* Time O(n) | Space O(1) | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
* https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ | ||
* | ||
* Time O(n) | Space O(1) | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var removeDuplicates = function(nums) { | ||
let current = nums[0]; | ||
let sameElCount = 0; | ||
let current = nums[0]; | ||
let sameElCount = 0; | ||
|
||
for(let i = 0; i < nums.length; i++) { | ||
if(current === nums[i]) { | ||
sameElCount++; | ||
} | ||
if(current !== nums[i]) { | ||
current = nums[i]; | ||
sameElCount = 1; | ||
} | ||
if(sameElCount > 2) { | ||
nums.splice(i,1); | ||
i--; | ||
} | ||
for(let i = 0; i < nums.length; i++) { | ||
if(current === nums[i]) { | ||
sameElCount++; | ||
} | ||
if(current !== nums[i]) { | ||
current = nums[i]; | ||
sameElCount = 1; | ||
} | ||
if(sameElCount > 2) { | ||
nums.splice(i,1); | ||
i--; | ||
} | ||
} | ||
}; | ||
|
||
|
||
/** | ||
* Two pointer | ||
* Time O(n^2) | Space O(1) | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
* Two pointer | ||
* Time O(n^2) | Space O(1) | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var removeDuplicates2 = function(nums) { | ||
|
||
let current = nums[0]; | ||
let sameElCount = 0; | ||
let current = nums[0]; | ||
let sameElCount = 0; | ||
|
||
|
||
for(let i = 0; i < nums.length; i++) { | ||
if(current === nums[i]) { | ||
sameElCount++; | ||
} | ||
if(current !== nums[i]) { | ||
current = nums[i]; | ||
sameElCount = 1; | ||
} | ||
if(sameElCount > 2) { | ||
let left = i; | ||
let right = i+1; | ||
let count = 1; | ||
while(nums[left] === nums[right]) { | ||
count++; | ||
right++; | ||
} | ||
nums.splice(left, count); | ||
i--; | ||
} | ||
for(let i = 0; i < nums.length; i++) { | ||
if(current === nums[i]) { | ||
sameElCount++; | ||
} | ||
if(current !== nums[i]) { | ||
current = nums[i]; | ||
sameElCount = 1; | ||
} | ||
return nums.length; | ||
}; | ||
if(sameElCount > 2) { | ||
let left = i; | ||
let right = i+1; | ||
let count = 1; | ||
while(nums[left] === nums[right]) { | ||
count++; | ||
right++; | ||
} | ||
nums.splice(left, count); | ||
i--; | ||
} | ||
} | ||
return nums.length; | ||
}; |
37 changes: 37 additions & 0 deletions
37
javascript/0116-populating-next-right-pointers-in-each-node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* BFS | ||
* Time O(n) | Space O(1) | ||
* | ||
* https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ | ||
* // Definition for a Node. | ||
* function Node(val, left, right, next) { | ||
* this.val = val === undefined ? null : val; | ||
* this.left = left === undefined ? null : left; | ||
* this.right = right === undefined ? null : right; | ||
* this.next = next === undefined ? null : next; | ||
* }; | ||
*/ | ||
/** | ||
* @param {Node} root | ||
* @return {Node} | ||
*/ | ||
|
||
var connect = function(root) { | ||
|
||
let currentNode = root; | ||
let nextLevelNode = root && root.left; | ||
|
||
while(currentNode && nextLevelNode) { | ||
currentNode.left.next = currentNode.right; | ||
if(currentNode.next) { | ||
currentNode.right.next = currentNode.next.left; | ||
} | ||
currentNode = currentNode.next; | ||
if(!currentNode) { | ||
currentNode = nextLevelNode; | ||
nextLevelNode = currentNode.left; | ||
} | ||
} | ||
|
||
return root; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Time O(log(N)) | Space O(1) | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var findPeakElement = function(nums) { | ||
let [l, r] = [0, nums.length - 1]; | ||
let mid = null; | ||
while (l <= r){ | ||
mid = (l + r) >> 1; | ||
if (mid < nums.length - 1 && nums[mid] < nums[mid+1]){ | ||
l = mid + 1; | ||
} | ||
else if (mid > 0 && nums[mid] < nums[mid-1]) { | ||
r = mid - 1; | ||
} | ||
else { | ||
break; | ||
} | ||
} | ||
return mid; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* @param {string} s | ||
* @return {string[]} | ||
*/ | ||
var findRepeatedDnaSequences = function (s) { | ||
const seen = new Set(); | ||
const res = new Set(); | ||
const arr = Array.from(s); | ||
|
||
for (let l = 0; l < arr.length - 9; l++) { | ||
const sequence = s.slice(l, l + 10); | ||
|
||
if (seen.has(sequence)) { | ||
res.add(sequence); | ||
} else { | ||
seen.add(sequence); | ||
} | ||
} | ||
|
||
return Array.from(res); | ||
}; |
Oops, something went wrong.