1. 程式人生 > >LeetCode刷題(四)

LeetCode刷題(四)

LeetCode 是個著名的程式設計題庫,裡邊有很多面試、演算法題目,多刷刷對提高程式設計水平很有幫助,避免因為天天寫業務程式碼而停滯不前。最近玩了下挺有意思的,決定慢慢的刷起來,在這裡記錄一下我的刷題過程。

相關資料:

備註:

  • 先從簡單的開始刷,簡單記錄每道題的題目,解法和思路。
  • 因為之前並沒有什麼演算法基礎,所以很多解法可能並不是最優解。
  • 先把重點放在解題,故有些變數名並不嚴謹,但程式碼其他方面都儘可能遵循了規範,並使用ES6+的新特性。

題目

思路

ASCII,分情況判斷。

解法

/**
 * @param {string} word
 * @return {boolean}
 */
var detectCapitalUse = function(word) {
    const firstA = word.charCodeAt(0) >= 65 && word.charCodeAt(0) <= 90;
    let list = word.split('');
    list.shift();
    const allA = list.every(i => {
        return i.charCodeAt(0) >= 65 && i.charCodeAt(0) <= 90;
    });
    const alla = list.every(i => {
        return i.charCodeAt(0) >= 97 && i.charCodeAt(0) <= 122;
    });
    if (firstA && !allA && !alla) {
        return false;
    } else if (!firstA && !alla) {
        return false;
    } else {
        return true;
    }
};

題目

思路

取較長的陣列和較短的陣列,以提高效能。長陣列加分隔號變為字串。短陣列遍歷每項加分隔號查詢是否存在。最後去重。

解法

/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number[]}
 */
var intersection = function(nums1, nums2) {
    const shortList = nums1.length > nums2.length ? nums2: nums1;
    const longList = nums1.length > nums2.length ? nums1: nums2;
    const longStr = '|' + longList.join('|') + '|';
    let result = shortList.filter(i => {
        return longStr.indexOf(`|${i}|`) >= 0;
    });
    return Array.from(new Set(result));
};

題目

思路

迴圈findNums,找到每個元素在nums中的index,獲取nums中,index右側的元素組成的陣列。在其中找到第一個符合題意的元素,找不到就給-1。

解法

/**
 * @param {number[]} findNums
 * @param {number[]} nums
 * @return {number[]}
 */
var nextGreaterElement = function(findNums, nums) {
    let resultList = [];
    findNums.forEach((item, index) => {
        const numsIndex = nums.findIndex(i => {
            return i === item;
        });
        const copyList = nums.map(n => n);
        copyList.splice(0, numsIndex);
        let result = copyList.find(c => {
            return c > item;
        });
        result = result ? result : -1;
        resultList.push(result);
    });
    return resultList;
};

題目

反轉整數

思路

判斷x是否>=0,大於零則直接反轉,小於零則去掉負號-,然後反轉。取最大最小值,最後得出結果時溢位則返回0。

解法

/**
 * @param {number} x
 * @return {number}
 */
var reverse = function(x) {
    const max = Math.pow(2,31) - 1;
    const min = Math.pow(-2,31);
    const a = x >= 0;
    let result = 0;
    if (a) {
        result = +(x + '').split('').reverse().join('');
    } else {
        let absList = (x + '').split('');
        absList.splice(0, 1);
        absList.reverse();
        const str = '-' + absList.join('');
        result = +str;
    }
    return result < max && result > min ? result : 0;
};

題目

求眾數

思路

轉map,key為數字,value為出現次數。遍歷map獲取符合條件的眾數。

解法

/**
 * @param {number[]} nums
 * @return {number}
 */
var majorityElement = function(nums) {
    let map = {};
    nums.forEach(i => {
        map[i] !== undefined ? map[i]++ : map[i]=1;
    });
    let result = 0;
    for (i in map) {
        if (map[i] > nums.length / 2) {
            result = i;
        }
    }
    return +result;
};