LeetCode解題筆記(3)

342. Power of Four

231. Power of Two

326. Power of Three


這三題都是類似的,大意是判斷一個整數是否是4/2/3的次方。

思路一:

一個通用的解法就是不停的除以4/2/3最後判斷餘數是否等於1

/**
 * @param {number} n
 * @return {boolean}
 */
var isPowerOfThree = function(n) {
    while(n && n%3 === 0){
        n /=3 ;
    }
    
    return n == 1;
};

思路二:

可以考慮一下數字的特點。

比如說2,如果一個數是2的次方,那麼它的二進制最高位肯定爲1,其餘位置爲0。其餘同理。

不過這要注意負數和0,直接返回false即可。(我竟然然後-3是3的-1次方。)

/**
 * @param {number} n
 * @return {boolean}
 */
var isPowerOfThree = function(n) {
   

 if(n<=0){  
        return false;  
    }
    
    n = n.toString(3).split("");
    
    var flag = 1;
    if(n[0] == "1"){
        for(var i = 1; i<n.length;i++){
            if(n[i] != "0"){
                flag = 0;
                break;
            }
        }
    }else{
        flag = 0;
    }
    
    return flag == 1;

};


35. Search Insert

PositionGiven a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.

Here are few examples.

[1,3,5,6], 5 → 2

[1,3,5,6], 2 → 1

[1,3,5,6], 7 → 4

[1,3,5,6], 0 → 0

題目大意即給定一個排序數組和一個數,返回這個數在這個排數數組中應插入的位置。例子也給的很詳細,幾種情況都囊括了。根據例子敲碼,遍歷一次數組即可

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var searchInsert = function(nums, target) {
    if(nums[nums.length - 1] < target){
        return nums.length;
    }
    else if(nums[0] > target){
        return 0;
    }
    else{
        for(var i = 0;i < nums.length;i++){
            if(nums[i] == target){
                return i;
            }   
            else if(nums[i] < target && i < (nums.length - 1) && nums[i+1] > target){
                return i+1;
            }
        }
    }
};

292. Nim Game

You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.

Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.

For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend

大意即:你和朋友玩Nim遊戲,桌上有一堆石頭,你和朋友輪流拿石頭,每次可拿走1-3個,拿到最後一顆石頭即贏了此次遊戲。你和朋友都非常聰明並且懂得遊戲的技巧。舉個例子,如果有四個石頭,先手必輸。
這個例子就是此題突破的重點了。

當桌上有1-3個石頭,先手贏;有四個,先手輸;有5-7個,先手贏(轉換成4個的問題)。以此類推。

解題突破主要是列子,加上之前好像有學長講過類似的問題~

/**
 * @param {number} n
 * @return {boolean}
 */
var canWinNim = function(n) {
    return (n % 4) != 0;
};


167. Two Sum II - Input array is sorted

這題是Two Sum排序版,這樣就可以用我上次說到的兩個指針的思路啦。

/**
 * @param {number[]} numbers
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function(numbers, target) {
    var low = 0;
    var high = numbers.length-1;
    while(low < high){
        if((numbers[low] + numbers[high]) == target){
            return [low+1,high+1];
        }else if((numbers[low] + numbers[high]) > target){
            high--;
        }else{
            low++;
        }
    }
};



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章