Skip to content

Commit ef6fb5b

Browse files
authored
Added tasks 127-137
1 parent f194b2d commit ef6fb5b

File tree

16 files changed

+388
-0
lines changed

16 files changed

+388
-0
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
570570

571571
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
572572
|-|-|-|-|-|-
573+
| 0127 |[Word Ladder](src/main/ts/g0101_0200/s0127_word_ladder/solution.ts)| Hard | Top_Interview_Questions, String, Hash_Table, Breadth_First_Search | 41 | 95.63
573574

574575
#### Day 13 Graph Theory
575576

@@ -1025,6 +1026,8 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
10251026
| 0055 |[Jump Game](src/main/ts/g0001_0100/s0055_jump_game/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
10261027
| 0045 |[Jump Game II](src/main/ts/g0001_0100/s0045_jump_game_ii/solution.ts)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 1 | 84.27
10271028
| 0238 |[Product of Array Except Self](src/main/ts/g0201_0300/s0238_product_of_array_except_self/solution.ts)| Medium | Top_100_Liked_Questions, Array, Prefix_Sum, Big_O_Time_O(n^2)_Space_O(n) | 3 | 92.81
1029+
| 0134 |[Gas Station](src/main/ts/g0101_0200/s0134_gas_station/solution.ts)| Medium | Top_Interview_Questions, Array, Greedy | 0 | 100.00
1030+
| 0135 |[Candy](src/main/ts/g0101_0200/s0135_candy/solution.ts)| Hard | Array, Greedy | 2 | 96.15
10281031
| 0042 |[Trapping Rain Water](src/main/ts/g0001_0100/s0042_trapping_rain_water/solution.ts)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
10291032
| 0013 |[Roman to Integer](src/main/ts/g0001_0100/s0013_roman_to_integer/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Math | 3 | 94.58
10301033
| 0012 |[Integer to Roman](src/main/ts/g0001_0100/s0012_integer_to_roman/solution.ts)| Medium | String, Hash_Table, Math | 3 | 93.02
@@ -1110,6 +1113,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
11101113
| 0117 |[Populating Next Right Pointers in Each Node II](src/main/ts/g0101_0200/s0117_populating_next_right_pointers_in_each_node_ii/solution.ts)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Linked_List | 47 | 97.74
11111114
| 0114 |[Flatten Binary Tree to Linked List](src/main/ts/g0101_0200/s0114_flatten_binary_tree_to_linked_list/solution.ts)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Big_O_Time_O(N)_Space_O(N) | 0 | 100.00
11121115
| 0112 |[Path Sum](src/main/ts/g0101_0200/s0112_path_sum/solution.ts)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00
1116+
| 0129 |[Sum Root to Leaf Numbers](src/main/ts/g0101_0200/s0129_sum_root_to_leaf_numbers/solution.ts)| Medium | Depth_First_Search, Tree, Binary_Tree | 0 | 100.00
11131117
| 0124 |[Binary Tree Maximum Path Sum](src/main/ts/g0101_0200/s0124_binary_tree_maximum_path_sum/solution.ts)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 2 | 71.11
11141118
| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/ts/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/solution.ts)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 61 | 75.97
11151119

@@ -1138,6 +1142,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
11381142

11391143
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11401144
|-|-|-|-|-|-
1145+
| 0127 |[Word Ladder](src/main/ts/g0101_0200/s0127_word_ladder/solution.ts)| Hard | Top_Interview_Questions, String, Hash_Table, Breadth_First_Search | 41 | 95.63
11411146

11421147
#### Top Interview 150 Trie
11431148

@@ -1195,6 +1200,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
11951200
|-|-|-|-|-|-
11961201
| 0067 |[Add Binary](src/main/ts/g0001_0100/s0067_add_binary/solution.ts)| Easy | String, Math, Bit_Manipulation, Simulation | 0 | 100.00
11971202
| 0136 |[Single Number](src/main/ts/g0101_0200/s0136_single_number/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 1 | 78.27
1203+
| 0137 |[Single Number II](src/main/ts/g0101_0200/s0137_single_number_ii/solution.ts)| Medium | Array, Bit_Manipulation | 0 | 100.00
11981204

11991205
#### Top Interview 150 Math
12001206

@@ -1714,9 +1720,14 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
17141720
| 0141 |[Linked List Cycle](src/main/ts/g0101_0200/s0141_linked_list_cycle/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Data_Structure_I_Day_7_Linked_List, Udemy_Linked_List, Top_Interview_150_Linked_List, Big_O_Time_O(N)_Space_O(1) | 47 | 89.37
17151721
| 0139 |[Word Break](src/main/ts/g0101_0200/s0139_word_break/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Algorithm_II_Day_15_Dynamic_Programming, Dynamic_Programming_I_Day_9, Udemy_Dynamic_Programming, Top_Interview_150_1D_DP, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 4 | 76.41
17161722
| 0138 |[Copy List with Random Pointer](src/main/ts/g0101_0200/s0138_copy_list_with_random_pointer/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Programming_Skills_II_Day_14, Udemy_Linked_List, Top_Interview_150_Linked_List, Big_O_Time_O(N)_Space_O(N) | 49 | 72.42
1723+
| 0137 |[Single Number II](src/main/ts/g0101_0200/s0137_single_number_ii/solution.ts)| Medium | Array, Bit_Manipulation, Top_Interview_150_Bit_Manipulation | 0 | 100.00
17171724
| 0136 |[Single Number](src/main/ts/g0101_0200/s0136_single_number/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Data_Structure_II_Day_1_Array, Algorithm_I_Day_14_Bit_Manipulation, Udemy_Integers, Top_Interview_150_Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 1 | 78.27
1725+
| 0135 |[Candy](src/main/ts/g0101_0200/s0135_candy/solution.ts)| Hard | Array, Greedy, Top_Interview_150_Array/String | 2 | 96.15
1726+
| 0134 |[Gas Station](src/main/ts/g0101_0200/s0134_gas_station/solution.ts)| Medium | Top_Interview_Questions, Array, Greedy, Top_Interview_150_Array/String | 0 | 100.00
17181727
| 0131 |[Palindrome Partitioning](src/main/ts/g0101_0200/s0131_palindrome_partitioning/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(N\*2^N)_Space_O(2^N\*N) | 13 | 94.96
1728+
| 0129 |[Sum Root to Leaf Numbers](src/main/ts/g0101_0200/s0129_sum_root_to_leaf_numbers/solution.ts)| Medium | Depth_First_Search, Tree, Binary_Tree, Top_Interview_150_Binary_Tree_General | 0 | 100.00
17191729
| 0128 |[Longest Consecutive Sequence](src/main/ts/g0101_0200/s0128_longest_consecutive_sequence/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Union_Find, Top_Interview_150_Hashmap, Big_O_Time_O(N_log_N)_Space_O(1) | 34 | 90.07
1730+
| 0127 |[Word Ladder](src/main/ts/g0101_0200/s0127_word_ladder/solution.ts)| Hard | Top_Interview_Questions, String, Hash_Table, Breadth_First_Search, Graph_Theory_I_Day_12_Breadth_First_Search, Top_Interview_150_Graph_BFS | 41 | 95.63
17201731
| 0125 |[Valid Palindrome](src/main/ts/g0101_0200/s0125_valid_palindrome/solution.ts)| Easy | Top_Interview_Questions, String, Two_Pointers, Udemy_Two_Pointers, Top_Interview_150_Two_Pointers | 0 | 100.00
17211732
| 0124 |[Binary Tree Maximum Path Sum](src/main/ts/g0101_0200/s0124_binary_tree_maximum_path_sum/solution.ts)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Udemy_Tree_Stack_Queue, Top_Interview_150_Binary_Tree_General, Big_O_Time_O(N)_Space_O(N) | 2 | 71.11
17221733
| 0123 |[Best Time to Buy and Sell Stock III](src/main/ts/g0101_0200/s0123_best_time_to_buy_and_sell_stock_iii/solution.ts)| Hard | Array, Dynamic_Programming, Top_Interview_150_Multidimensional_DP | 5 | 85.07
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
127\. Word Ladder
2+
3+
Hard
4+
5+
A **transformation sequence** from word `beginWord` to word `endWord` using a dictionary `wordList` is a sequence of words <code>beginWord -> s<sub>1</sub> -> s<sub>2</sub> -> ... -> s<sub>k</sub></code> such that:
6+
7+
* Every adjacent pair of words differs by a single letter.
8+
* Every <code>s<sub>i</sub></code> for `1 <= i <= k` is in `wordList`. Note that `beginWord` does not need to be in `wordList`.
9+
* <code>s<sub>k</sub> == endWord</code>
10+
11+
Given two words, `beginWord` and `endWord`, and a dictionary `wordList`, return _the **number of words** in the **shortest transformation sequence** from_ `beginWord` _to_ `endWord`_, or_ `0` _if no such sequence exists._
12+
13+
**Example 1:**
14+
15+
**Input:** beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
16+
17+
**Output:** 5
18+
19+
**Explanation:** One shortest transformation sequence is "hit" -> "hot" -> "dot" -> "dog" -> cog", which is 5 words long.
20+
21+
**Example 2:**
22+
23+
**Input:** beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
24+
25+
**Output:** 0
26+
27+
**Explanation:** The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.
28+
29+
**Constraints:**
30+
31+
* `1 <= beginWord.length <= 10`
32+
* `endWord.length == beginWord.length`
33+
* `1 <= wordList.length <= 5000`
34+
* `wordList[i].length == beginWord.length`
35+
* `beginWord`, `endWord`, and `wordList[i]` consist of lowercase English letters.
36+
* `beginWord != endWord`
37+
* All the words in `wordList` are **unique**.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// #Hard #Top_Interview_Questions #String #Hash_Table #Breadth_First_Search
2+
// #Graph_Theory_I_Day_12_Breadth_First_Search #Top_Interview_150_Graph_BFS
3+
// #2025_04_07_Time_41_ms_(95.63%)_Space_63.50_MB_(82.78%)
4+
5+
function ladderLength(beginWord: string, endWord: string, wordList: string[]): number {
6+
const wordSet: Set<string> = new Set(wordList)
7+
if (!wordSet.has(endWord)) {
8+
return 0
9+
}
10+
let beginSet: Set<string> = new Set([beginWord])
11+
let endSet: Set<string> = new Set([endWord])
12+
const visited: Set<string> = new Set()
13+
let len = 1
14+
const wordLen = beginWord.length
15+
while (beginSet.size > 0 && endSet.size > 0) {
16+
if (beginSet.size > endSet.size) {
17+
[beginSet, endSet] = [endSet, beginSet]
18+
}
19+
const tempSet: Set<string> = new Set()
20+
for (const word of beginSet) {
21+
const chars = word.split('')
22+
for (let i = 0; i < wordLen; i++) {
23+
const oldChar = chars[i]
24+
for (let c = 97; c <= 122; c++) {
25+
chars[i] = String.fromCharCode(c)
26+
const nextWord = chars.join('')
27+
if (endSet.has(nextWord)) {
28+
return len + 1
29+
}
30+
if (!visited.has(nextWord) && wordSet.has(nextWord)) {
31+
tempSet.add(nextWord)
32+
visited.add(nextWord)
33+
}
34+
}
35+
chars[i] = oldChar
36+
}
37+
}
38+
beginSet = tempSet
39+
len++
40+
}
41+
return 0
42+
}
43+
44+
export { ladderLength }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
129\. Sum Root to Leaf Numbers
2+
3+
Medium
4+
5+
You are given the `root` of a binary tree containing digits from `0` to `9` only.
6+
7+
Each root-to-leaf path in the tree represents a number.
8+
9+
* For example, the root-to-leaf path `1 -> 2 -> 3` represents the number `123`.
10+
11+
Return _the total sum of all root-to-leaf numbers_. Test cases are generated so that the answer will fit in a **32-bit** integer.
12+
13+
A **leaf** node is a node with no children.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/02/19/num1tree.jpg)
18+
19+
**Input:** root = [1,2,3]
20+
21+
**Output:** 25
22+
23+
**Explanation:** The root-to-leaf path `1->2` represents the number `12`. The root-to-leaf path `1->3` represents the number `13`. Therefore, sum = 12 + 13 = `25`.
24+
25+
**Example 2:**
26+
27+
![](https://assets.leetcode.com/uploads/2021/02/19/num2tree.jpg)
28+
29+
**Input:** root = [4,9,0,5,1]
30+
31+
**Output:** 1026
32+
33+
**Explanation:** The root-to-leaf path `4->9->5` represents the number 495. The root-to-leaf path `4->9->1` represents the number 491. The root-to-leaf path `4->0` represents the number 40. Therefore, sum = 495 + 491 + 40 = `1026`.
34+
35+
**Constraints:**
36+
37+
* The number of nodes in the tree is in the range `[1, 1000]`.
38+
* `0 <= Node.val <= 9`
39+
* The depth of the tree will not exceed `10`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// #Medium #Depth_First_Search #Tree #Binary_Tree #Top_Interview_150_Binary_Tree_General
2+
// #2025_04_07_Time_0_ms_(100.00%)_Space_57.72_MB_(19.07%)
3+
4+
import { TreeNode } from "../../com_github_leetcode/treenode"
5+
6+
/**
7+
* Definition for a binary tree node.
8+
* class TreeNode {
9+
* val: number
10+
* left: TreeNode | null
11+
* right: TreeNode | null
12+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
13+
* this.val = (val===undefined ? 0 : val)
14+
* this.left = (left===undefined ? null : left)
15+
* this.right = (right===undefined ? null : right)
16+
* }
17+
* }
18+
*/
19+
function sumNumbers(root: TreeNode | null): number {
20+
let sum = 0
21+
function recurseSum(node: TreeNode | null, curNum: number): void {
22+
if (node === null) {
23+
return
24+
}
25+
const newNum = 10 * curNum + node.val
26+
if (node.left === null && node.right === null) {
27+
sum += newNum
28+
return
29+
}
30+
if (node.left !== null) {
31+
recurseSum(node.left, newNum)
32+
}
33+
if (node.right !== null) {
34+
recurseSum(node.right, newNum)
35+
}
36+
}
37+
recurseSum(root, 0)
38+
return sum
39+
}
40+
41+
export { sumNumbers }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
134\. Gas Station
2+
3+
Medium
4+
5+
There are `n` gas stations along a circular route, where the amount of gas at the <code>i<sup>th</sup></code> station is `gas[i]`.
6+
7+
You have a car with an unlimited gas tank and it costs `cost[i]` of gas to travel from the <code>i<sup>th</sup></code> station to its next <code>(i + 1)<sup>th</sup></code> station. You begin the journey with an empty tank at one of the gas stations.
8+
9+
Given two integer arrays `gas` and `cost`, return _the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return_ `-1`. If there exists a solution, it is **guaranteed** to be **unique**
10+
11+
**Example 1:**
12+
13+
**Input:** gas = [1,2,3,4,5], cost = [3,4,5,1,2]
14+
15+
**Output:** 3
16+
17+
**Explanation:**
18+
19+
Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
20+
Travel to station 4. Your tank = 4 - 1 + 5 = 8
21+
Travel to station 0. Your tank = 8 - 2 + 1 = 7
22+
Travel to station 1. Your tank = 7 - 3 + 2 = 6
23+
Travel to station 2. Your tank = 6 - 4 + 3 = 5
24+
Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
25+
Therefore, return 3 as the starting index.
26+
27+
**Example 2:**
28+
29+
**Input:** gas = [2,3,4], cost = [3,4,3]
30+
31+
**Output:** -1
32+
33+
**Explanation:**
34+
35+
You can't start at station 0 or 1, as there is not enough gas to travel to the next station.
36+
Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
37+
Travel to station 0. Your tank = 4 - 3 + 2 = 3
38+
Travel to station 1. Your tank = 3 - 3 + 3 = 3
39+
You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.
40+
Therefore, you can't travel around the circuit once no matter where you start.
41+
42+
**Constraints:**
43+
44+
* `gas.length == n`
45+
* `cost.length == n`
46+
* <code>1 <= n <= 10<sup>5</sup></code>
47+
* <code>0 <= gas[i], cost[i] <= 10<sup>4</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// #Medium #Top_Interview_Questions #Array #Greedy #Top_Interview_150_Array/String
2+
// #2025_04_07_Time_0_ms_(100.00%)_Space_66.31_MB_(63.11%)
3+
4+
function canCompleteCircuit(gas: number[], cost: number[]): number {
5+
let index = 0
6+
let total = 0
7+
let current = 0
8+
for (let i = 0; i < gas.length; i++) {
9+
const balance = gas[i] - cost[i]
10+
total += balance
11+
current += balance
12+
if (current < 0) {
13+
index = i + 1
14+
current = 0
15+
}
16+
}
17+
return total >= 0 ? index : -1
18+
}
19+
20+
export { canCompleteCircuit }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
135\. Candy
2+
3+
Hard
4+
5+
There are `n` children standing in a line. Each child is assigned a rating value given in the integer array `ratings`.
6+
7+
You are giving candies to these children subjected to the following requirements:
8+
9+
* Each child must have at least one candy.
10+
* Children with a higher rating get more candies than their neighbors.
11+
12+
Return _the minimum number of candies you need to have to distribute the candies to the children_.
13+
14+
**Example 1:**
15+
16+
**Input:** ratings = [1,0,2]
17+
18+
**Output:** 5
19+
20+
**Explanation:** You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
21+
22+
**Example 2:**
23+
24+
**Input:** ratings = [1,2,2]
25+
26+
**Output:** 4
27+
28+
**Explanation:** You can allocate to the first, second and third child with 1, 2, 1 candies respectively. The third child gets 1 candy because it satisfies the above two conditions.
29+
30+
**Constraints:**
31+
32+
* `n == ratings.length`
33+
* <code>1 <= n <= 2 * 10<sup>4</sup></code>
34+
* <code>0 <= ratings[i] <= 2 * 10<sup>4</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// #Hard #Array #Greedy #Top_Interview_150_Array/String
2+
// #2025_04_07_Time_2_ms_(96.15%)_Space_61.00_MB_(33.47%)
3+
4+
function candy(ratings: number[]): number {
5+
const n = ratings.length
6+
const candies: number[] = new Array(n).fill(1)
7+
for (let i = 0; i < n - 1; i++) {
8+
if (ratings[i + 1] > ratings[i]) {
9+
candies[i + 1] = candies[i] + 1
10+
}
11+
}
12+
for (let i = n - 1; i > 0; i--) {
13+
if (ratings[i - 1] > ratings[i] && candies[i - 1] < candies[i] + 1) {
14+
candies[i - 1] = candies[i] + 1
15+
}
16+
}
17+
return candies.reduce((sum, c) => sum + c, 0)
18+
}
19+
20+
export { candy }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
137\. Single Number II
2+
3+
Medium
4+
5+
Given an integer array `nums` where every element appears **three times** except for one, which appears **exactly once**. _Find the single element and return it_.
6+
7+
You must implement a solution with a linear runtime complexity and use only constant extra space.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [2,2,3,2]
12+
13+
**Output:** 3
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [0,1,0,1,0,1,99]
18+
19+
**Output:** 99
20+
21+
**Constraints:**
22+
23+
* <code>1 <= nums.length <= 3 * 10<sup>4</sup></code>
24+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
25+
* Each element in `nums` appears exactly **three times** except for one element which appears **once**.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// #Medium #Array #Bit_Manipulation #Top_Interview_150_Bit_Manipulation
2+
// #2025_04_07_Time_0_ms_(100.00%)_Space_59.11_MB_(24.16%)
3+
4+
function singleNumber(nums: number[]): number {
5+
let ones = 0
6+
let twos = 0
7+
for (const num of nums) {
8+
ones = (ones ^ num) & (~twos)
9+
twos = (twos ^ num) & (~ones)
10+
}
11+
return ones
12+
}
13+
14+
export { singleNumber }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// tslint:disable:no-magic-numbers
2+
import { ladderLength } from 'src/main/ts/g0101_0200/s0127_word_ladder/solution'
3+
import { expect, test } from 'vitest'
4+
5+
test('ladderLength', () => {
6+
expect(ladderLength('hit', 'cog', ['hot', 'dot', 'dog', 'lot', 'log', 'cog'])).toEqual(5)
7+
})
8+
9+
test('ladderLength2', () => {
10+
expect(ladderLength('hit', 'cog', ['hot', 'dot', 'dog', 'lot', 'log'])).toEqual(0)
11+
})

0 commit comments

Comments
 (0)