力扣 – 数据结构二周计划
第一天 – 数组
function containsDuplicate ( nums: number[ ] ) : boolean {
let hash: object = { }
for ( let i = 0 ; i< nums. length; i++ ) {
if ( ! hash[ nums[ i] ] ) {
hash[ nums[ i] ] = true
} else {
return true
}
}
return false
} ;
function maxSubArray ( nums: number[ ] ) : number {
let max: number = nums[ 0 ]
let len: number = nums. length
for ( let i = 1 ; i< len; i++ ) {
nums[ i] += Math. max ( nums[ i- 1 ] , 0 )
max = Math. max ( nums[ i] , max)
}
return max
} ;
第二天 – 数组
function twoSum ( nums: number[ ] , target: number) : number[ ] {
let map = new Map ( )
let len = nums. length
for ( let i = 0 ; i< len; i++ ) {
let rest = target - nums[ i]
if ( map. has ( rest) ) {
return [ map. get ( rest) , i]
}
map. set ( nums[ i] , i)
}
} ;
function merge ( nums1: number[ ] , m: number, nums2: number[ ] , n: number) : void {
let len: number = nums1. length- 1
m--
n--
while ( n>= 0 ) {
if ( nums1[ m] > nums2[ n] ) {
nums1[ len-- ] = nums1[ m-- ]
} else {
nums1[ len-- ] = nums2[ n-- ]
}
}
} ;
function merge ( nums1: number[ ] , m: number, nums2: number[ ] , n: number) : void {
nums1. splice ( m, nums1. length- m, ... nums2)
nums1. sort ( ( a, b) => a- b)
} ;
第三天 – 数组
var intersect = function ( nums1, nums2) {
if ( nums1. length > nums2. length) {
return intersect ( nums2, nums1)
}
const hashmap = new Map ( )
for ( let num of nums1) {
let count = hashmap. get ( num) || 0
hashmap. set ( num, count+ 1 )
}
let res = [ ] , index = 0
for ( let num of nums2) {
let count = hashmap. get ( num) || 0
if ( count > 0 ) {
res[ index++ ] = num
count--
if ( count > 0 ) {
hashmap. set ( num, count)
} else {
hashmap. delete ( num)
}
}
}
return res
} ;
var intersect = function ( nums1, nums2) {
nums1. sort ( ( a, b) => a- b)
nums2. sort ( ( a, b) => a- b)
let res = [ ] , index= 0 , left= 0 , right= 0
let len1 = nums1. length, len2 = nums2. length
while ( left< len1 && right < len2) {
if ( nums1[ left] < nums2[ right] ) {
left++
} else if ( nums1[ left] > nums2[ right] ) {
right++
} else {
res[ index++ ] = nums1[ left]
left++
right++
}
}
return res
} ;
var maxProfit = function ( prices) {
let min = prices[ 0 ]
let max = 0
let len = prices. length
for ( let i = 1 ; i< len; i++ ) {
let cur = prices[ i]
min = Math. min ( cur, min)
if ( cur > min ) max = Math. max ( cur - min, max)
}
return max
} ;
第四天 – 数组
var matrixReshape = function ( mat, r, c) {
let m = mat. length, n = mat[ 0 ] . length
if ( m * n != r * c) return mat
let res = [ ]
mat = mat. flat ( )
for ( let i = 0 ; i< r; i++ ) {
res. push ( mat. splice ( 0 , c) )
}
return res
} ;
function generate ( numRows: number) : number[ ] [ ] {
let res = [ ]
for ( let i = 0 ; i< numRows; i++ ) {
const row = new Array ( i+ 1 ) . fill ( 1 )
for ( let j = 1 ; j< row. length- 1 ; j++ ) {
row[ j] = res[ i- 1 ] [ j- 1 ] + res[ i- 1 ] [ j]
}
res. push ( row)
}
return res
} ;
var generate = function ( numRows) {
let r = Array ( numRows)
r[ 0 ] = [ 1 ]
for ( let i= 1 ; i< numRows; i++ ) {
r[ i] = [ ]
r[ i- 1 ] . reduce ( ( p, c) => {
r[ i] . push ( p+ c)
return c
} , 0 )
r[ i] . push ( 1 )
}
return r
} ;
第五天 – 数组
var isValidSudoku = function ( board) {
let set = new Set ( ) ;
for ( let i = 0 ; i < 9 ; i++ ) {
for ( let j = 0 ; j < 9 ; j++ ) {
const number = board[ i] [ j] ;
if ( number === '.' ) continue ;
const colStr = ` ${ number} c ${ i} ` ;
const rowStr = ` ${ number} r ${ j} ` ;
const sectionStr = ` ${ number} s( ${ Math. floor ( i/ 3 ) } , ${ Math. floor ( j/ 3 ) } )` ;
if ( set . has ( colStr) || set . has ( rowStr) || set . has ( sectionStr) ) return false ;
set . add ( colStr) ;
set . add ( rowStr) ;
set . add ( sectionStr) ;
}
}
return true ;
} ;
var setZeroes = function ( matrix) {
let r = matrix. length
let d = matrix[ 0 ] . length
let res = [ ]
for ( let i = 0 ; i< r; i++ ) {
if ( matrix[ i] . indexOf ( 0 ) !== - 1 ) {
matrix[ i] . forEach ( ( item, index, arr) => {
if ( item == 0 ) res. push ( index)
arr[ index] = 0
} )
}
}
let len = res. length
for ( let i = 0 ; i< r; i++ ) {
for ( let j = 0 ; j< len; j++ ) {
matrix[ i] [ res[ j] ] = 0
}
}
} ;
第六天 – 字符串
var firstUniqChar = function ( s) {
let len = s. length
for ( let i= 0 ; i< len; i++ ) {
if ( s. indexOf ( s[ i] ) === s. lastIndexOf ( s[ i] ) ) {
return i
}
}
return - 1
} ;
var canConstruct = function ( ransomNote, magazine) {
let hash = { }
for ( let i of magazine) {
hash[ i] = ( hash[ i] || 0 ) + 1
}
for ( let i of ransomNote) {
if ( hash[ i] ) {
hash[ i] --
} else {
return false
}
}
return true
} ;
var isAnagram = function ( s, t) {
if ( s. length !== t. length) return false
let hash = { }
for ( let i of s) {
hash[ i] = ( hash[ i] || 0 ) + 1
}
for ( let i of t) {
if ( hash[ i] ) {
hash[ i] --
}
else return false
}
return true
} ;
第七天 – 链表
var hasCycle = function ( head) {
let node = head
while ( node) {
if ( node. flag) return true
node. flag = true
node = node. next
}
return false
} ;
var hasCycle = function ( head) {
try {
JSON . stringify ( head)
return false
} catch {
return true
}
} ;
var mergeTwoLists = function ( list1, list2) {
let head = new ListNode ( - 1 )
let preHead = head
while ( list1 && list2) {
if ( list1. val < list2. val)
{
preHead. next = list1
list1 = list1. next
}
else {
preHead. next = list2
list2 = list2. next
}
preHead = preHead. next
}
preHead. next = list1 === null ? list2: list1
return head. next
} ;
var removeElements = function ( head, val) {
if ( head == null ) return head
let newList = new ListNode ( 0 , head)
let cur = newList
while ( cur. next) {
if ( cur. next. val == val) {
cur. next = cur. next. next
} else {
cur = cur. next
}
}
return newList. next
} ;
第八天 — 链表
var reverseList = function ( head) {
if ( ! head) return head
let node = head
let pre = null , temp
while ( node) {
temp = node. next
node. next = pre
pre = node
node = temp
}
return pre
} ;
var reverseList = function ( head) {
if ( head == null || head. next == null ) {
return head;
}
const newHead = reverseList ( head. next) ;
head. next. next = head;
head. next = null ;
return newHead;
} ;
var deleteDuplicates = function ( head) {
if ( ! head) return head
let node = head
while ( node. next ) {
if ( node. val === node. next. val) {
node. next = node. next. next
} else {
node = node. next
}
}
return head
} ;
第九天 — 栈 / 队列
var isValid = function ( s) {
if ( ! s) return true
let len = s. length
let str = {
'{' : '}' ,
'(' : ')' ,
'[' : ']'
}
let stack = [ ]
for ( let i = 0 ; i< len; i++ ) {
const cur = s[ i]
if ( cur == '(' || cur == '[' || cur == '{' ) stack. push ( str[ cur] )
else {
if ( ! stack. length || stack. pop ( ) !== cur) return false
}
}
return ! stack. length
} ;
var MyQueue = function ( ) {
this . MyQueue = [ ]
} ;
MyQueue. prototype. push = function ( x) {
this . MyQueue. push ( x)
} ;
MyQueue. prototype. pop = function ( ) {
return this . MyQueue. shift ( )
} ;
MyQueue. prototype. peek = function ( ) {
return this . MyQueue[ 0 ]
} ;
MyQueue. prototype. empty = function ( ) {
return this . MyQueue. length == 0
} ;
第十天 — 树
var preorderTraversal = function ( root) {
if ( ! root) return [ ]
let res = [ ]
let queue = [ root]
while ( queue. length) {
let temp = queue. pop ( )
res. push ( temp. val)
if ( temp. right) queue. push ( temp. right)
if ( temp. left) queue. push ( temp. left)
}
return res
} ;
var preorderTraversal = function ( root, res= [ ] ) {
if ( ! root) return [ ]
res. push ( root. val)
preorderTraversal ( root. left, res)
preorderTraversal ( root. right, res)
return res
} ;
var inorderTraversal = function ( root) {
if ( ! root) return [ ]
let res = [ ]
let stack = [ ]
while ( stack. length || root) {
while ( root) {
stack. push ( root)
root = root. left
}
let temp = stack. pop ( )
res. push ( temp. val)
root = temp. right
}
return res
} ;
var inorderTraversal = function ( root, res = [ ] ) {
if ( ! root) return [ ]
if ( root. left) inorderTraversal ( root. left, res)
res. push ( root. val)
if ( root. right) inorderTraversal ( root. right, res)
return res
} ;
var postorderTraversal = function ( root) {
if ( ! root) return [ ]
let res = [ ]
let stack = [ root]
while ( stack. length) {
let temp = stack. pop ( )
if ( temp. left) stack. push ( temp. left)
if ( temp. right) stack. push ( temp. right)
res. push ( temp. val)
}
return res. reverse ( )
} ;
var levelOrder = function ( root) {
if ( ! root) return [ ]
let res = [ ]
let stack = [ root]
let count = 0
while ( stack. length) {
res[ count] = [ ]
let countNum = stack. length
while ( countNum-- ) {
let temp = stack. shift ( )
res[ count] . push ( temp. val)
if ( temp. left) stack. push ( temp. left)
if ( temp. right) stack. push ( temp. right)
}
count++
}
return res
} ;
第十一天 — 树
var levelOrder = function ( root) {
if ( ! root) return [ ]
let count = 0
let node = [ root]
let res = [ ]
while ( node. length) {
res[ count] = [ ]
let countNum = node. length
while ( countNum-- ) {
let temp = node. shift ( )
res[ count] . push ( temp. val)
if ( temp. left) node. push ( temp. left)
if ( temp. right) node. push ( temp. right)
}
count++
}
return res
} ;
var maxDepth = function ( root) {
let max = 0
const dfs = ( root, res = 1 ) => {
if ( ! root) return
if ( ! root. left && ! root. right) max = Math. max ( res, max)
if ( root. left) dfs ( root. left, res+ 1 )
if ( root. right) dfs ( root. right, res+ 1 )
}
dfs ( root)
return max
} ;
var maxDepth = function ( root) {
return root == null ? 0 : Math. max ( maxDepth ( root. left) , maxDepth ( root. right) ) + 1
} ;
var isSymmetric = function ( root) {
if ( ! root) return true
const isMirror = ( left, right) => {
if ( ! left && ! right) return true
if ( left && right && left. val === right. val
&& isMirror ( left. left, right. right)
&& isMirror ( left. right, right. left)
) return true
return false
}
return isMirror ( root. left, root. right)
} ;
第十二天 — 树
var invertTree = function ( root) {
if ( root == null ) return root
let stack = [ root]
while ( stack. length) {
let node = stack. shift ( )
let temp = node. left
node. left = node. right
node. right = temp
if ( node. left) {
stack. push ( node. left)
}
if ( node. right) {
stack. push ( node. right)
}
}
return root
} ;
var inverTree = function ( root) {
if ( ! root) return null
return {
val: root. val,
left: inverTree ( root. right) ,
right: inverTree ( root. left)
}
}
var hasPathSum = function ( root, targetSum) {
if ( ! root) return false
let stack = [ [ root, root. val] ]
while ( stack. length) {
let [ node, sum] = stack. pop ( )
if ( ! node. left && ! node. right && sum == targetSum) return true
if ( node. right) stack. push ( [ node. right, node. right. val + sum] )
if ( node. left) stack. push ( [ node. left, node. left. val + sum] )
}
return false
} ;
var hasPathSum = function ( root, targetSum) {
if ( ! root) return false
let final = false
const dfs = ( node, res) => {
if ( ! node. left && ! node. right && res == targetSum) final = true
if ( node. left) dfs ( node. left, res + node. left. val)
if ( node. right) dfs ( node. right, res + node. right. val)
}
dfs ( root, root. val)
return final
} ;
第十三天 — 树
var searchBST = function ( root, val) {
if ( ! root) return null
if ( root. val === val) return root
return root. val > val? searchBST ( root. left, val) : searchBST ( root. right, val)
} ;
var insertIntoBST = function ( root, val) {
if ( ! root) return new TreeNode ( val)
if ( root. val < val) {
if ( ! root. right) root. right = new TreeNode ( val)
else insertIntoBST ( root. right, val)
} else {
if ( ! root. left) root. left = new TreeNode ( val)
else insertIntoBST ( root. left, val)
}
return root
} ;
第十四天 — 树
var isValidBST = function ( root, min= - Infinity , max= Infinity ) {
if ( ! root) return true
if ( root. val <= min || root. val >= max) return false
return isValidBST ( root. left, min, root. val) && isValidBST ( root. right, root. val, max)
} ;
var isValidBST = function ( root) {
let res = [ ]
const dfs = ( root) => {
if ( ! root) return false
root. left && dfs ( root. left)
res. push ( root. val)
root. right && dfs ( root. right)
}
dfs ( root)
let len = res. length
for ( let i = 1 ; i< len; i++ ) {
if ( res[ i- 1 ] >= res[ i] ) return false
}
return true
} ;
var findTarget = function ( root, k) {
let res = [ ]
const dfs = ( root) => {
if ( ! root) return false
res. push ( root. val)
if ( root. left) dfs ( root. left)
if ( root. right) dfs ( root. right)
}
dfs ( root)
let hash = { }
for ( let i = 0 ; i < res. length; i++ ) {
let rest = k - res[ i]
if ( hash[ rest] ) return true
hash[ res[ i] ] = true
}
return false
} ;
var lowestCommonAncestor = function ( root, p, q) {
if ( root. val > p. val && root. val > q. val) return lowestCommonAncestor ( root. left, p, q)
if ( root. val < p. val && root. val < q. val) return lowestCommonAncestor ( root. right, p, q)
return root
} ;