11.盛水最多的容器 15.三数之和 26.删除有序数组中的重复项 27.移除元素 75.颜色分类 88.合并两个有序数组 167.两数之和2-输入有序数组 581.最短无序连续子数组 2486.追加字符以获得子序列
11.盛水最多的容器
var maxArea = function ( height) { let left = 0 ; let right = height. length - 1 ; let ans = 0 ; while ( left < right) { let width = right - left; if ( height[ left] > height[ right] ) { val = width * height[ right] ; ans = Math. max ( ans, val) ; right - = 1 ; } else { val = width * height[ left] ; ans = Math. max ( ans, val) ; left + = 1 ; } } console. log ( ans) ;
} ;
maxArea ( [ 1 , 8 , 6 , 2 , 5 , 4 , 8 , 3 , 7 ] ) ;
15.三数之和
var threeSum = function ( nums) { nums. sort ( ( a, b) = > a - b) ; let ans = [ ] ; for ( let i = 0 ; i < nums. length; i++ ) { let startNum = nums[ i] ; let left = i + 1 ; let right = nums. length - 1 ; if ( startNum > 0 ) continue ; if ( i > 0 && startNum === nums[ i - 1 ] ) continue ; while ( left < right) { let total = startNum + nums[ left] + nums[ right] ; if ( total < 0 ) { left + = 1 ; } else if ( total > 0 ) { right - = 1 ; } else { ans. push ( [ startNum, nums[ left] , nums[ right] ] ) ; while ( left < right && nums[ left] === nums[ left + 1 ] ) left + = 1 ; while ( left < right && nums[ right] === nums[ right - 1 ] ) right - = 1 ; left + = 1 ; right - = 1 ; } } } return ans;
} ;
threeSum ( [ - 1 , 0 , 1 , 2 , - 1 , - 4 ] ) ;
26.删除有序数组中的重复项
var removeDuplicates = function ( nums) { let left = 0 ; for ( let right = 1 ; right < nums. length; right++ ) { if ( nums[ left] !== nums[ right] ) { left + = 1 ; nums[ left] = nums[ right] ; } } console. log ( left + 1 ) ; return left + 1 ;
} ;
removeDuplicates ( [ 0 , 0 , 1 , 1 , 1 , 2 , 2 , 3 , 3 , 4 ] ) ;
27.移除元素
var removeElement = function ( nums, val) { let i = 0 ; for ( let j = 0 ; j < nums. length; j++ ) { if ( nums[ j] !== val) { nums[ i] = nums[ j] ; i + = 1 ; } } return i;
} ;
removeElement ( [ 3 , 2 , 2 , 3 ] , 3 ) ;
75.颜色分类
88.合并两个有序数组
var merge = function ( nums1, m, nums2, n) { let firstPoint = m - 1 ; let secondPoint = n - 1 ; while ( firstPoint >= 0 && secondPoint >= 0 ) { if ( nums1[ firstPoint] > nums2[ secondPoint] ) { nums1[ firstPoint + secondPoint + 1 ] = nums1[ firstPoint] ; firstPoint - = 1 ; } else { nums1[ firstPoint + secondPoint + 1 ] = nums2[ secondPoint] ; secondPoint - = 1 ; } } if ( secondPoint >= 0 ) { for ( let i = 0 ; i < secondPoint; i++ ) { nums1[ i] = nums2[ i] ; } }
} ;
167.两数之和2-输入有序数组
581. 最短无序连续子数组
var findUnsortedSubarray = function ( nums) { let len = nums. length; let left = 0 ; let right = len - 1 ; while ( left < len && nums[ left] <= nums[ left + 1 ] ) left++ ; while ( right >= 0 && nums[ right - 1 ] <= nums[ right] ) right-- ; let min = Infinity; let max = - Infinity; for ( let i = left; i <= right; i++ ) { min = Math. min ( min, nums[ i] ) ; max = Math. max ( max, nums[ i] ) ; } while ( nums[ left] > min) left-- ; while ( nums[ right] < max) right++ ; console. log ( left, right) ; return left < right ? right - left - 1 : 0 ;
} ;
console. log ( findUnsortedSubarray ( [ 2 , 6 , 4 , 8 , 10 , 9 , 15 ] ) ) ;
console. log ( findUnsortedSubarray ( [ 1 , 3 , 2 , 2 , 2 ] ) ) ;
2486. 追加字符以获得子序列
var appendCharacters = function ( s, t) { const n = t. length; let i = 0 ; for ( let j = 0 ; j < s. length; j++ ) { if ( s[ j] === t[ i] ) { i++ ; } } console. log ( n - i) ; return n - i;
} ;
appendCharacters ( "coaching" , "coding" ) ;