[日常刷题]leetcode D27

版权声明:希望各位多提意见多多互相交流哦~ https://blog.csdn.net/wait_for_taht_day5/article/details/82955593

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.

Example:

Input: 4
Output: false 
Explanation: 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.

Solution in C++:

关键点:

  • 思考窘境

思路:

  • 自己拿到的时候开始没什么思路,后来想着4为什么会输的时候就有点思路了,石头为4个的时候比较特别,就是我即不能全部拿完,也无法使剩下的石头对方拿不完。但是我没有继续深入思考,倍数的情况也是这样的窘境。直接看了解析。这个就像数学的推理过程一样,假设n-1成立,推n。
bool canWinNim(int n) {
        return (n % 4 != 0);
    }

303. Range Sum Query - Immutable

Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
Example:

Given nums = [-2, 0, 3, -5, 2, -1]

sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3

Note:

  1. You may assume that the array does not change.
  2. There are many calls to sumRange function.

Solution in C++:

关键点:

  • 数组不变可预处理

思路:

  • 我最初想到的就是暴力解法了,这几天解题的AC率在不断提高,因为我这几天的调试能力有所提高,都会测测边缘问题,但是这个问题,我好像测试边缘问题有点多余,不过题目没有说明界限问题。测试就不知道如何抛出bug。但是发现暴力解法完全没考虑。还有一个很奇怪的点就是,暴力解法有时候会AC有时会T掉,这也让我想到原来刷题为什么跟别人解法一样别人就能过自己过不了的原因了。
  • 这里提一个解析里面的思路,就是通过数组去存储sum[0-i]的值,每次去提取就好。这里代码里面sumArray[i+1]存储0-i的和,所以sum[i-j] = sumArray[j+1] - sumArray[i]就行。
class NumArray {
private:
    vector<int> sumArray;
public:
    NumArray(vector<int> nums) {
        size_t size = nums.size();
        sumArray.push_back(0);
        for(int i = 0; i < size; ++i)
            sumArray.push_back(sumArray[i] + nums[i]);
    }
    
    int sumRange(int i, int j) {
        
        return sumArray[j + 1] - sumArray[i];
    }
};

/**
 * Your NumArray object will be instantiated and called as such:
 * NumArray obj = new NumArray(nums);
 * int param_1 = obj.sumRange(i,j);
 */

326. Power of Three

Given an integer, write a function to determine if it is a power of three.
Example 1:

Input: 27
Output: true

Example 2:

Input: 0
Output: false

Example 3:

Input: 9
Output: true

Example 4:

Input: 45
Output: false

Follow up:

Could you do it without using any loop / recursion?

Solution in C++:

关键点:

  • 3及其幂特点

思路:

  • 可能陷入了之前刷的power of two的思维里面,看到位运算行不通就不知道其他方法了,就只能暴力了,没想到最后看性能暴力的效率也不错,虽然对比最好的方法是差了点。
  • 然后看解析之后再介绍几个不错的思路。
  • 1.进制转换(这个还蛮通用的)转换成对应幂的进制,然后就可以像power of two那样的方法判断了。
  • 2.数学性质。3的幂取底之后为整数。对数公式。不推荐,感觉精度和选择的底数有很大关系。
  • 3.整型极限。因为输入的是整型变量,所以对于3^n存在一个极限,并且3是一个素数,所以可以采用看是否可以被最大3的幂次方整除的方式来判断。

方法一:进制转换

bool isPowerOfThree(int n) {
       if (n <= 0)
           return false;
       
        int trans = 0;
        while(n)
        {
            if (n % 3 == 1)
            {
                if (trans == 1)
                    return false;
                ++trans;
            } else if ( n % 3 != 0 )
                return false;
            n /= 3;
            
        }
       if (trans == 1)
           return true;
       else
           return false;
    }

方法二:数学性质,对数公式

bool isPowerOfThree(int n) {
        if (n <= 0)
            return  false;
        double i = log10(n) / log10(3);
        int j = (int)i;
        return (abs(i - j) < pow(10,-18));
    }

方法三:整型极限

bool isPowerOfThree(int n) {
        int limit = pow(3,floor(log10(n)/log10(3)));
        return n > 0 && limit % n == 0;
    }

342. Power of Four

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
Example 1:

Input: 16
Output: true

Example 2:

Input: 5
Output: false

Solution in C++:

关键点:

  • 4 -> 0100

思路:

  • 4的幂和2的幂类似,采用位运算的方式,只不过判断标准除了只有1个1之外还有1出现的位置在偶数位上。
 bool isPowerOfFour(int num) {
        if (num <= 0)
            return false;
        
        long long bit = 1;
        int i = 0;
        int sum = 0;
        
        while(i < 32)
        {
            if (bit & num)
            {
                ++sum;
                if (i % 2)
                    return false;
            }
            ++i;
            bit <<=1;
        }
        
        if (sum > 1)
            return false;
        else
            return true;
    }

小结

今天一个是收获了锻炼大脑,采用数学归纳法的方式去推理,另外一个是学会掌握缓存的思想;再就是关于不同的power of n问题的思考。

知识点

  • 数学归纳法
  • 缓存思想
  • power of n(进制、对数、整型极限[素数])

猜你喜欢

转载自blog.csdn.net/wait_for_taht_day5/article/details/82955593