3. 无重复字符的最长子串

var lengthOfLongestSubstring = function(s) {
    let arr = [];
    let max = 0
    for (let i = 0; i < s.length; i++) {
        let index = arr.indexOf(s[i])
        if (index !== -1) {
            arr.splice(0, index + 1)
        }
        arr.push(s[i])
        max = Math.max(arr.length, max)
    }
    return max
};

6. Z 字形变换

var convert = function(s, numRows) {
    if (numRows === 1) return s // 首先还是,只有一行直接返回
    let arr = new Array(numRows).fill('') // 创建一个数组,存储每行
    let n = numRows * 2 - 2
    for (let i = 0; i < s.length; i++) {
        i % n < numRows ? arr[i % n] += s[i] : arr[n - i % n] += s[i]
    }
    return arr.join('') // 拼接返回
};

8. 字符串转换整数 (atoi)

var myAtoi = function(str) {
    let result = str.trim().match(/^[-|+]{0,1}[0-9]+/)
    if (result != null) {
        if (result[0] > (Math.pow(2, 31) - 1)) {
            return Math.pow(2, 31) - 1
        }
        if (result[0] < Math.pow(-2, 31)) {
            return Math.pow(-2, 31)
        }
        return result[0]
    }
    return 0
};

49. 字母异位词分组

var groupAnagrams = function(strs) {
    const len = strs.length,
        ans = new Map()
    for (let i = 0; i < len; i++) {
        let asc = strs[i].split('').sort().join()
        if (ans.has(asc)) {
            ans.get(asc).push(strs[i])
        } else {
            ans.set(asc, [strs[i]])
        }

    }
    return Array.from(ans.values())
}

179. 最大数

var largestNumber = function(nums) {
    if (Number(nums.join(''))) {
        return nums.map(n => String(n)).sort((a, b) => (b + a) - (a + b)).join('')
    }
    return '0'
};

299. 猜数字游戏

var getHint = function(secret, guess) {
    let guessList = guess.split('')
    let secretList = secret.split('')
    let A = 0
    let B = 0
    for (let i = 0; i < secretList.length; i++) {
        if (secretList[i] == guess[i]) {
            A++
            guessList[i] = '-'
            secretList[i] = '+'
        }
    }
    for (let i = 0; i < secretList.length; i++) {
        let index = guessList.indexOf(secretList[i])
        if (index > -1) {
            B++
            guessList[index] = '-'
        }
    }
    return `${A}A${B}B`
};

524. 通过删除字母匹配到字典里最长单词

var findLongestWord = function(s, dictionary) {
    let res = ''
    for (let i = 0; i < dictionary.length; i++) {
        if (isSubsequence(s, dictionary[i])) {
            res = !res ? dictionary[i] : getRight(res, dictionary[i])
        }
    }

    function getRight(oldVal, newVal) {
        if (oldVal.length > newVal.length) {
            return oldVal
        } else if (oldVal.length < newVal.length) {
            return newVal
        } else {
            return oldVal < newVal ? oldVal : newVal
        }
    }

    function isSubsequence(s, t) {
        let i = 0
        let j = 0
        while (i < s.length) {
            if (s[i] == t[j]) {
                j++
            }
            if (j == t.length) {
                return true
            }
            i++
        }
        return false
    }
    return res
};

539. 最小时间差

var findMinDifference = function(timePoints) {
    if (timePoints.length > 1440) return 0 // 将时间转成分钟,24H=1440min,如果参数长度超过1440,说明有重复,直接返回0

    let times = []
    for (let i = 0; i < timePoints.length; i++) {
        let hour = Number(timePoints[i].split(':')[0])
        let min = Number(timePoints[i].split(':')[1])
        times[i] = hour * 60 + min
    }

    times.sort((a, b) => a - b)
    let min = 1440 - times[times.length - 1] + times[0] // 先把头尾差值当做 min
    for (let i = 1; i < times.length; i++) {
        min = Math.min(times[i] - times[i - 1], min)
    }
    return min
};

609. 在系统中查找重复文件

var findDuplicate = function(paths) {
    let dicMap = {}
    for (let i = 0; i < paths.length; i++) {
        let dirs = paths[i].split(' ')
        let root = dirs[0] + '/'
        for (let j = 1; j < dirs.length; j++) {
            let index = dirs[j].indexOf('(')
            let txt = dirs[j].substring(0, index)
            let cont = dirs[j].substring(index + 1, dirs[j].length - 1)
            if (!dicMap[cont]) {
                dicMap[cont] = []
            }
            dicMap[cont].push(root + txt)
        }
    }
    let res = []
    for (const key in dicMap) {
        if (dicMap[key].length > 1) {
            res.push(dicMap[key])
        }
    }
    return res
};

648. 单词替换

var replaceWords = function(dictionary, sentence) {
    let sentArr = sentence.split(' ')
    for (let i = 0; i < sentArr.length; i++) {
        for (let j = 0; j < dictionary.length; j++) {
            if (sentArr[i].startsWith(dictionary[j])) {
                sentArr[i] = dictionary[j]
            }
        }
    }
    return sentArr.join(' ')
};