Skip to content

Commit ee7c645

Browse files
authored
Added tasks 416-1143
1 parent 8257395 commit ee7c645

File tree

15 files changed

+744
-78
lines changed

15 files changed

+744
-78
lines changed

README.md

+39-16
Large diffs are not rendered by default.

src/main/ts/g0001_0100/s0001_two_sum/readme.md

+6-9
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,16 @@ You can return the answer in any order.
4343
## Solution
4444

4545
```typescript
46-
interface ITemp {
47-
[key: number]: number
48-
}
49-
5046
function twoSum(nums: number[], target: number): number[] {
51-
const temp: ITemp = {}
47+
const indexMap: Map<number, number> = new Map()
5248
for (let i = 0; i < nums.length; i++) {
53-
const tag = target - nums[i]
54-
if (temp[tag] >= 0) {
55-
return [temp[tag], i]
49+
const requiredNum: number = target - nums[i]
50+
if (indexMap.has(requiredNum)) {
51+
return [indexMap.get(requiredNum)!, i]
5652
}
57-
temp[nums[i]] = i
53+
indexMap.set(nums[i], i)
5854
}
55+
return [-1, -1]
5956
}
6057

6158
export { twoSum }

src/main/ts/g0001_0100/s0002_add_two_numbers/readme.md

+21-21
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,29 @@ import { ListNode } from '../../com_github_leetcode/listnode'
5454
* }
5555
*/
5656
function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
57-
const result = new ListNode(null)
58-
let next = result
59-
let carryNum = 0
60-
let currSum = 0
61-
let num1 = 0
62-
let num2 = 0
63-
64-
while (l1 !== null || l2 !== null) {
65-
// @ts-ignore
66-
num1 = l1 !== null ? l1.val : 0
67-
// @ts-ignore
68-
num2 = l2 !== null ? l2.val : 0
69-
currSum = (num1 + num2 + carryNum) % 10
70-
carryNum = Math.floor((num1 + num2 + carryNum) / 10)
71-
next.next = new ListNode(currSum)
72-
next = next.next
73-
l1 = l1 !== null ? l1.next : null
74-
l2 = l2 !== null ? l2.next : null
57+
const dummyHead: ListNode = new ListNode(0)
58+
let p: ListNode | null = l1
59+
let q: ListNode | null = l2
60+
let curr: ListNode | null = dummyHead
61+
let carry: number = 0
62+
while (p !== null || q !== null) {
63+
const x: number = p !== null ? p.val : 0
64+
const y: number = q !== null ? q.val : 0
65+
const sum: number = carry + x + y
66+
carry = Math.floor(sum / 10)
67+
curr.next = new ListNode(sum % 10)
68+
curr = curr.next
69+
if (p !== null) {
70+
p = p.next
71+
}
72+
if (q !== null) {
73+
q = q.next
74+
}
7575
}
76-
if (carryNum) {
77-
next.next = new ListNode(carryNum)
76+
if (carry > 0) {
77+
curr.next = new ListNode(carry)
7878
}
79-
return result.next
79+
return dummyHead.next
8080
}
8181

8282
export { addTwoNumbers }

src/main/ts/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md

+18-12
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,27 @@ Notice that the answer must be a substring, "pwke" is a subsequence and not a su
4444

4545
```typescript
4646
function lengthOfLongestSubstring(s: string): number {
47-
const hash: { [key: string]: number } = {}
48-
let maxLength = 0
49-
let length = 0
50-
let start = 0
47+
const lastIndices: number[] = new Array(256).fill(-1)
48+
let maxLen: number = 0
49+
let curLen: number = 0
50+
let start: number = 0
5151
for (let i = 0; i < s.length; i++) {
52-
const char = s[i]
53-
if (hash[char] !== undefined && hash[char] >= start) {
54-
start = hash[char] + 1
55-
length = i - start
52+
const cur: string = s.charAt(i)
53+
const charCode: number = cur.charCodeAt(0)
54+
if (lastIndices[charCode] < start) {
55+
lastIndices[charCode] = i
56+
curLen++
57+
} else {
58+
const lastIndex: number = lastIndices[charCode]
59+
start = lastIndex + 1
60+
curLen = i - start + 1
61+
lastIndices[charCode] = i
62+
}
63+
if (curLen > maxLen) {
64+
maxLen = curLen
5665
}
57-
length++
58-
hash[char] = i
59-
maxLength = Math.max(maxLength, length)
6066
}
61-
return maxLength
67+
return maxLen
6268
}
6369

6470
export { lengthOfLongestSubstring }

src/main/ts/g0001_0100/s0005_longest_palindromic_substring/readme.md

+23-20
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,33 @@ Given a string `s`, return _the longest_ _palindromic_ _substring_ in `s`.
3030

3131
```typescript
3232
function longestPalindrome(s: string): string {
33-
let max = 0
34-
let res = ''
33+
const newStr: string[] = new Array(s.length * 2 + 1)
34+
newStr[0] = '#'
3535
for (let i = 0; i < s.length; i++) {
36-
for (let j = i + 1; j <= s.length; j++) {
37-
if (s.slice(i, j).length > max && isPalindrome(s.slice(i, j))) {
38-
max = s.slice(i, j).length
39-
res = s.slice(i, j)
40-
}
41-
}
36+
newStr[2 * i + 1] = s.charAt(i)
37+
newStr[2 * i + 2] = '#'
4238
}
43-
return res
44-
}
45-
46-
const isPalindrome = (s: string): boolean => {
47-
let i = 0
48-
let j = s.length - 1
49-
while (i < j) {
50-
if (s[i] !== s[j]) {
51-
return false
39+
const dp: number[] = new Array(newStr.length)
40+
let friendCenter: number = 0
41+
let friendRadius: number = 0
42+
let lpsCenter: number = 0
43+
let lpsRadius: number = 0
44+
for (let i = 0; i < newStr.length; i++) {
45+
dp[i] =
46+
friendCenter + friendRadius > i ? Math.min(dp[2 * friendCenter - i], friendCenter + friendRadius - i) : 1
47+
while (i + dp[i] < newStr.length && i - dp[i] >= 0 && newStr[i + dp[i]] === newStr[i - dp[i]]) {
48+
dp[i]++
49+
}
50+
if (friendCenter + friendRadius < i + dp[i]) {
51+
friendCenter = i
52+
friendRadius = dp[i]
53+
}
54+
if (lpsRadius < dp[i]) {
55+
lpsCenter = i
56+
lpsRadius = dp[i]
5257
}
53-
i++
54-
j--
5558
}
56-
return true
59+
return s.substring((lpsCenter - lpsRadius + 1) / 2, (lpsCenter + lpsRadius - 1) / 2)
5760
}
5861

5962
export { longestPalindrome }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 416\. Partition Equal Subset Sum
5+
6+
Medium
7+
8+
Given a **non-empty** array `nums` containing **only positive integers**, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.
9+
10+
**Example 1:**
11+
12+
**Input:** nums = [1,5,11,5]
13+
14+
**Output:** true
15+
16+
**Explanation:** The array can be partitioned as [1, 5, 5] and [11].
17+
18+
**Example 2:**
19+
20+
**Input:** nums = [1,2,3,5]
21+
22+
**Output:** false
23+
24+
**Explanation:** The array cannot be partitioned into equal sum subsets.
25+
26+
**Constraints:**
27+
28+
* `1 <= nums.length <= 200`
29+
* `1 <= nums[i] <= 100`
30+
31+
## Solution
32+
33+
```typescript
34+
function canPartition(nums: number[]): boolean {
35+
let sums: number = 0
36+
for (const num of nums) {
37+
sums += num
38+
}
39+
if (sums % 2 === 1) {
40+
return false
41+
}
42+
sums /= 2
43+
const dp: boolean[] = new Array(sums + 1).fill(false)
44+
dp[0] = true
45+
for (const num of nums) {
46+
for (let sum = sums; sum >= num; sum--) {
47+
dp[sum] = dp[sum] || dp[sum - num]
48+
}
49+
}
50+
return dp[sums]
51+
}
52+
53+
export { canPartition }
54+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 437\. Path Sum III
5+
6+
Medium
7+
8+
Given the `root` of a binary tree and an integer `targetSum`, return _the number of paths where the sum of the values along the path equals_ `targetSum`.
9+
10+
The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2021/04/09/pathsum3-1-tree.jpg)
15+
16+
**Input:** root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
17+
18+
**Output:** 3
19+
20+
**Explanation:** The paths that sum to 8 are shown.
21+
22+
**Example 2:**
23+
24+
**Input:** root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
25+
26+
**Output:** 3
27+
28+
**Constraints:**
29+
30+
* The number of nodes in the tree is in the range `[0, 1000]`.
31+
* <code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code>
32+
* `-1000 <= targetSum <= 1000`
33+
34+
## Solution
35+
36+
```typescript
37+
function pathSum(root: TreeNode | null, targetSum: number): number {
38+
let count = 0
39+
let map = new Map<number, number>()
40+
41+
function dfs(node: TreeNode | null, currentSum: number): void {
42+
if (!node) return
43+
44+
currentSum += node.val
45+
if (currentSum === targetSum) count++
46+
47+
count += map.get(currentSum - targetSum) ?? 0
48+
49+
map.set(currentSum, map.get(currentSum) + 1 || 1)
50+
dfs(node?.left, currentSum)
51+
dfs(node?.right, currentSum)
52+
53+
//remove from hashmap
54+
map.set(currentSum, map.get(currentSum) - 1)
55+
if (map.get(currentSum) === 0) map.delete(currentSum)
56+
}
57+
58+
dfs(root, 0)
59+
return count
60+
}
61+
62+
export { pathSum }
63+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 438\. Find All Anagrams in a String
5+
6+
Medium
7+
8+
Given two strings `s` and `p`, return _an array of all the start indices of_ `p`_'s anagrams in_ `s`. You may return the answer in **any order**.
9+
10+
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "cbaebabacd", p = "abc"
15+
16+
**Output:** [0,6]
17+
18+
**Explanation:**
19+
20+
The substring with start index = 0 is "cba", which is an anagram of "abc".
21+
The substring with start index = 6 is "bac", which is an anagram of "abc".
22+
23+
**Example 2:**
24+
25+
**Input:** s = "abab", p = "ab"
26+
27+
**Output:** [0,1,2]
28+
29+
**Explanation:**
30+
31+
The substring with start index = 0 is "ab", which is an anagram of "ab".
32+
The substring with start index = 1 is "ba", which is an anagram of "ab".
33+
The substring with start index = 2 is "ab", which is an anagram of "ab".
34+
35+
**Constraints:**
36+
37+
* <code>1 <= s.length, p.length <= 3 * 10<sup>4</sup></code>
38+
* `s` and `p` consist of lowercase English letters.
39+
40+
## Solution
41+
42+
```typescript
43+
function findAnagrams(s: string, p: string): number[] {
44+
const map: number[] = new Array(26).fill(0)
45+
for (let i = 0; i < p.length; ++i) {
46+
map[p.charCodeAt(i) - 'a'.charCodeAt(0)]++
47+
}
48+
const res: number[] = []
49+
let i: number = 0
50+
let j: number = 0
51+
52+
while (i < s.length) {
53+
const idx: number = s.charCodeAt(i) - 'a'.charCodeAt(0)
54+
// Add the new character
55+
map[idx]--
56+
// If the length is greater than window's length, pop the left character in the window
57+
if (i >= p.length) {
58+
map[s.charCodeAt(j++) - 'a'.charCodeAt(0)]++
59+
}
60+
let finish: boolean = true
61+
62+
for (let k = 0; k < 26; k++) {
63+
// If it is not an anagram of string p
64+
if (map[k] !== 0) {
65+
finish = false
66+
break
67+
}
68+
}
69+
70+
if (i >= p.length - 1 && finish) {
71+
res.push(j)
72+
}
73+
i++
74+
}
75+
return res
76+
}
77+
78+
export { findAnagrams }
79+
```

0 commit comments

Comments
 (0)