Skip to content

Commit 3226dcf

Browse files
committed
js转ts
1 parent 2b50d25 commit 3226dcf

File tree

61 files changed

+29
-29
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+29
-29
lines changed

src/array/container-with-most-water.js src/array/container-with-most-water.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @param {number[]} heights
44
* @return {number}
55
*/
6-
export const maxArea = function (heights) {
6+
export const maxArea = function (heights:number[]):number {
77
let max = 0
88

99
let l = 0

src/array/count-number-of-nice-subarrays.js src/array/count-number-of-nice-subarrays.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @param {number} k
55
* @return {number}
66
*/
7-
export const numberOfSubarrays = function (nums, k) {
7+
export const numberOfSubarrays = function (nums:number[], k:number):number {
88
const odd = []
99
odd.push(-1)
1010

src/array/count-the-repetitions.js src/array/count-the-repetitions.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @param {string} s2
55
* @return {number} 返回可以变为s1的次数
66
*/
7-
export const includesInStr = (s1, s2) => {
7+
export const includesInStr = (s1:string, s2:string):number => {
88
let i = 0
99
while (s1.length >= s2.length) {
1010
for (let n = 0, len = s2.length; n < len; n++) {
@@ -26,7 +26,7 @@ export const includesInStr = (s1, s2) => {
2626
* @param {string} str
2727
* @param {number} time
2828
*/
29-
export const getStrCopyByNum = (str, time) => {
29+
export const getStrCopyByNum = (str:string, time:number) => {
3030
return str.repeat(time)
3131
}
3232

@@ -68,7 +68,7 @@ export const getStrCopyByNum = (str, time) => {
6868
* @param {number} n2
6969
* @return {number}
7070
*/
71-
export const getMaxRepetitions = function (s1, n1, s2, n2) {
71+
export const getMaxRepetitions = function (s1:string, n1:number, s2:string, n2:number):number {
7272
// 保存s2p的记录和对应的countS1,countS2
7373
const indexMap = new Map()
7474
let countS1 = 0

src/array/first-unique-character-in-a-string.js src/array/first-unique-character-in-a-string.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @param {string} s
33
* @return {character}
44
*/
5-
export const firstUniqChar = function (s) {
5+
export const firstUniqChar = function (s:string) {
66
const due = new Set()
77
const queue = new Set()
88
for (const n of s) {

src/array/house-robber.js src/array/house-robber.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @param {number[]} nums
33
* @return {number}
44
*/
5-
export const rob = function (nums) {
5+
export const rob = function (nums:number[]):number {
66
if (!nums) return 0
77
const len = nums.length
88
if (len === 0) return 0

src/array/jump-game-ii.js src/array/jump-game-ii.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @param {number[]} nums
33
* @return {number}
44
*/
5-
export const jump = function (nums) {
5+
export const jump = function (nums:number[]):number {
66
let step = 0 // 跳跃步数
77
let maxPosition = 0 // 最大位置
88
let lastJumpStepMax = 0 // 最后一次跳跃最大能跳的步数

src/array/jump-game.js src/array/jump-game.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @param {number[]} nums
33
* @return {boolean}
44
*/
5-
export const canJump = function (nums) {
5+
export const canJump = function (nums:number[]):boolean {
66
let maxLength = nums[0]
77

88
for (let n = 1, len = nums.length; n < len; n++) {

src/array/kids-with-the-greatest-number-of-candies.js src/array/kids-with-the-greatest-number-of-candies.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
* @param {number} extraCandies
44
* @return {boolean[]}
55
*/
6-
export const kidsWithCandies = (candies, extraCandies) => {
6+
export const kidsWithCandies = (candies:number[], extraCandies:number):boolean[] => {
77
return candies.map(n => n + extraCandies >= Math.max(...candies))
88
}

src/array/kth-largest-element-in-an-array.js src/array/kth-largest-element-in-an-array.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
export default (arr, k) => {
2+
export default (arr:number[], k:number) => {
33
// 这个方法未必是效率最差的
44
return arr.sort((a, b) => b - a)[k - 1]
55

src/array/kth-smallest-element-in-a-sorted-matrix.js src/array/kth-smallest-element-in-a-sorted-matrix.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
* @param {number} k
44
* @return {number}
55
*/
6-
export const kthSmallest = function (matrix, k) {
6+
export const kthSmallest = function (matrix:number[][], k:number):number {
77
return matrix.flat().sort((a, b) => a - b)[k - 1]
88
}

src/array/majority-element.js src/array/majority-element.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @param {number[]} nums
33
* @return {number}
44
*/
5-
export const majorityElement = function (nums) {
5+
export const majorityElement = function (nums:number[]):number {
66
if (nums.length === 1) return nums[0]
77
const tmp = nums.sort((a, b) => a - b)
88
const res = tmp[~~(nums.length / 2)]

src/array/maximum-and-minimum.js src/array/maximum-and-minimum.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @param matrix: an input matrix
33
* @return: nums[0]: the maximum,nums[1]: the minimum
44
*/
5-
export const maxAndMin = function (matrix) {
5+
export const maxAndMin = function (matrix:number[][]) {
66
if (matrix[0] === undefined || matrix[0][0] === undefined) return []
77
let max = matrix[0][0]
88
let min = matrix[0][0]

src/array/maximum-length-of-repeated-subarray.js src/array/maximum-length-of-repeated-subarray.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// A: [1, 2, 3, 2, 1]
33
// B: [3, 2, 1, 4, 7]
44

5-
const maxLength = function (A, B, addA, addB, len) {
5+
const maxLength = function (A:number[], B:number[], addA:number, addB:number, len:number) {
66
addA = (addA > 0) ? addA : 0
77
addB = (addB > 0) ? addB : 0
88
let result = 0
@@ -23,7 +23,7 @@ const maxLength = function (A, B, addA, addB, len) {
2323
* @param {number[]} B
2424
* @return {number}
2525
*/
26-
export const findLength = function (A, B) {
26+
export const findLength = function (A:number[], B:number[]):number {
2727
const ALen = A.length
2828
const BLen = B.length
2929
let result = 0

src/array/maximum-product-subarray.js src/array/maximum-product-subarray.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @param {number[]} nums
33
* @return {number}
44
*/
5-
export const maxProduct = function (nums) {
5+
export const maxProduct = function (nums:number[]):number {
66
let res = nums[0]
77
let prevMin = nums[0]
88
let prevMax = nums[0]

src/array/maximum-subarray.js src/array/maximum-subarray.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @param {number[]} nums
33
* @return {number}
44
*/
5-
export const maxSubArray = function (nums) {
5+
export const maxSubArray = function (nums:number[]):number {
66
let max = nums[0]
77
let tmp = 0
88
nums.forEach(n => { max = Math.max(tmp > 0 ? tmp += n : tmp = n, max) })

src/array/minimum-cost-for-tickets.js src/array/minimum-cost-for-tickets.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @param {number[]} costs
44
* @return {number}
55
*/
6-
export const mincostTickets = function (days, costs) {
6+
export const mincostTickets = function (days:number[], costs:number[]) {
77
// 动态规划:
88
// dp[i]: 从第i天开始,到最后一天所用的票价总和
99

src/array/partition-array.js src/array/partition-array.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// LintCode 31. 数组划分 https://www.lintcode.com/problem/partition-array/description
22

3-
export default (nums, k) => {
3+
export default (nums:number[], k:number) => {
44
if (nums !== null) {
55
if (nums.length === 0) return 0
66

src/array/product-of-array-except-self.js src/array/product-of-array-except-self.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @param {number[]} nums
33
* @return {number[]}
44
*/
5-
export const productExceptSelf = function (nums) {
5+
export const productExceptSelf = function (nums:number[]):number[] {
66
const ans = []
77
for (let i = 0, len = nums.length; i < len; i++) {
88
let tmpL = 1

src/array/remove-duplicates-from-sorted-array.js src/array/remove-duplicates-from-sorted-array.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// LeetCode 26. 删除排序数组中的重复项 https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
22
// LintCode 100. 删除排序数组中的重复数字 https://www.lintcode.com/problem/remove-duplicates-from-sorted-array/description
33

4-
export default (nums) => {
4+
export default (nums:number[]):number => {
55
if (!nums || (nums && nums.length === 0)) return 0
66
let i = 0
77
for (let n = 1, len = nums.length; n < len; n++) {

src/array/search-in-rotated-sorted-array.js src/array/search-in-rotated-sorted-array.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @param {number} target
44
* @return {number}
55
*/
6-
export const search = function (nums, target) {
6+
export const search = function (nums:number[], target:number):number {
77
// 参考 src/array/binary-search.js
88
let l = 0
99
let r = nums.length - 1

src/array/search-insert-position.js src/array/search-insert-position.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @param {number} target
44
* @return {number}
55
*/
6-
var searchInsert = function (nums, target) {
6+
var searchInsert = function (nums:number[], target:number):number {
77
const res = nums.findIndex(item => item === target)
88
if (res !== -1) return res
99

src/array/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof.js src/array/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @param {number[]} nums
33
* @return {number[]}
44
*/
5-
export const singleNumbers = function (nums) {
5+
export const singleNumbers = function (nums:number[]):number[] {
66
const ab = nums.reduce((a, b) => a ^ b)
77
const diff = ab & -ab
88
const num1 = nums.reduce((a, n) => n & diff ? a ^ n : a, 0)

src/array/single-number.js src/array/single-number.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
* @param {number[]} nums
33
* @return {number}
44
*/
5-
export var singleNumber = function (nums) {
5+
export var singleNumber = function (nums:number[]):number {
66
return nums.reduce((a, b) => a ^ b)
77
}

src/array/subarray-sums-divisible-by-k.js src/array/subarray-sums-divisible-by-k.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @param {number} K
44
* @return {number}
55
*/
6-
export const subarraysDivByK = function (A, K) {
6+
export const subarraysDivByK = function (A:number[], K:number):number {
77
const map = new Map([[0, 1]]); let sum = 0; let count = 0
88

99
A.forEach(n => {

src/math/median-of-two-sorted-arrays.js src/math/median-of-two-sorted-arrays.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @param {number[]} nums2
66
* @return {number}
77
*/
8-
export const findMedianSortedArrays = function (nums1, nums2) {
8+
export const findMedianSortedArrays = function (nums1:number[], nums2:number[]):number {
99
if (nums1.length > nums2.length) return findMedianSortedArrays(nums2, nums1)
1010

1111
const m = nums1.length; const n = nums2.length

src/math/powx-n.js src/math/powx-n.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @param {number} n
44
* @return {number}
55
*/
6-
export const myPow = function (x, n) { // 参考:快速幂 + 迭代 https://leetcode-cn.com/problems/powx-n/solution/powx-n-by-leetcode-solution/
6+
export const myPow = function (x:number, n:number) { // 参考:快速幂 + 迭代 https://leetcode-cn.com/problems/powx-n/solution/powx-n-by-leetcode-solution/
77
let ans = 1.0 // 答案
88

99
if (n < 0) { // 如果 n 为负
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)