1
1
import type { ListNode } from "./utils.ts" ;
2
+
2
3
/**
4
+ * https://leetcode.com/problems/two-sum/
5
+ *
3
6
* Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
4
7
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
5
8
*
@@ -46,6 +49,11 @@ export function twoSum(nums: number[], target: number): number[] {
46
49
return [ ] ;
47
50
}
48
51
52
+ /**
53
+ * https://leetcode.com/problems/palindrome-number/
54
+ * @param x
55
+ * @returns
56
+ */
49
57
export function palindromeNumber ( x : number ) : boolean {
50
58
const num = x ;
51
59
if ( x < 0 ) return false ;
@@ -59,6 +67,11 @@ export function palindromeNumber(x: number): boolean {
59
67
return num === ans ;
60
68
}
61
69
70
+ /**
71
+ * https://leetcode.com/problems/roman-to-integer/
72
+ * @param s
73
+ * @returns
74
+ */
62
75
export function romanToInteger ( s : string ) : number {
63
76
const multipliers : { [ key : string ] : number } = {
64
77
IV : 4 ,
@@ -95,6 +108,11 @@ export function romanToInteger(s: string): number {
95
108
} , 0 ) ;
96
109
}
97
110
111
+ /**
112
+ * https://leetcode.com/problems/longest-common-prefix/
113
+ * @param strs
114
+ * @returns
115
+ */
98
116
export function longestCommonPrefix ( strs : string [ ] ) : string {
99
117
let prefix = strs [ 0 ] ;
100
118
@@ -111,6 +129,11 @@ export function longestCommonPrefix(strs: string[]): string {
111
129
return prefix ;
112
130
}
113
131
132
+ /**
133
+ * https://leetcode.com/problems/valid-parentheses/
134
+ * @param s
135
+ * @returns
136
+ */
114
137
export function validParentheses ( s : string ) : boolean {
115
138
const bracketsMap : { [ key : string ] : string } = {
116
139
")" : "(" ,
@@ -132,6 +155,12 @@ export function validParentheses(s: string): boolean {
132
155
return ! openBracketsStack . length ;
133
156
}
134
157
158
+ /**
159
+ * https://leetcode.com/problems/merge-two-sorted-lists/
160
+ * @param list1
161
+ * @param list2
162
+ * @returns
163
+ */
135
164
export function mergeTwoSortedLists (
136
165
list1 : ListNode | null ,
137
166
list2 : ListNode | null ,
@@ -148,6 +177,11 @@ export function mergeTwoSortedLists(
148
177
}
149
178
}
150
179
180
+ /**
181
+ * https://leetcode.com/problems/remove-duplicates-from-sorted-array/
182
+ * @param nums
183
+ * @returns
184
+ */
151
185
export function removeDuplicatesFromSortedArray ( nums : number [ ] ) : number {
152
186
let i = 0 ;
153
187
for ( let j = 0 ; j < nums . length ; j ++ ) {
@@ -159,12 +193,24 @@ export function removeDuplicatesFromSortedArray(nums: number[]): number {
159
193
return i ;
160
194
}
161
195
196
+ /**
197
+ * https://leetcode.com/problems/remove-element/
198
+ * @param nums
199
+ * @param val
200
+ * @returns
201
+ */
162
202
export function removeElement ( nums : number [ ] , val : number ) : number {
163
203
let j = 0 ;
164
204
for ( const n of nums ) if ( n !== val ) nums [ j ++ ] = n ;
165
205
return j ;
166
206
}
167
207
208
+ /**
209
+ * https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/
210
+ * @param haystack
211
+ * @param needle
212
+ * @returns
213
+ */
168
214
export function findTheIndexOfTheFirstOccurrenceInAString (
169
215
haystack : string ,
170
216
needle : string ,
@@ -188,6 +234,12 @@ export function findTheIndexOfTheFirstOccurrenceInAString(
188
234
return result ;
189
235
}
190
236
237
+ /**
238
+ * https://leetcode.com/problems/search-insert-position/
239
+ * @param nums
240
+ * @param target
241
+ * @returns
242
+ */
191
243
export function searchInsertPosition ( nums : number [ ] , target : number ) : number {
192
244
let min = 0 ;
193
245
let max = nums . length - 1 ;
0 commit comments