[日常刷题]leetcode D27

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(进制、对数、整型极限[素数])
Python 是一种流行的高级编程语言,因其简洁易读的语法和广泛的应用领域而受到开发者喜爱。LeetCode 是一个在线编程平台,专门用于算法和技术面试的准备,提供了大量的编程题目,包括数据结构、算法、系统设计等,常用于提升程序员的编程能力和解决实际问题的能力。 在 Python 中刷 LeetCode 题目通常涉及以下步骤: 1. **注册账户**:首先在 LeetCode 官网 (https://leetcode.com/) 注册一个账号,这样你可以跟踪你的进度和提交的代码。 2. **选择语言**:登录后,在个人主页设置中选择 Python 作为主要编程语言。 3. **学习和理解题目**:阅读题目描述,确保你理解问题的要求和预期输出。题目通常附有输入示例和预期输出,可以帮助你初始化思考。 4. **编写代码**:使用 Python 编写解决方案,LeetCode 提供了一个在线编辑器,支持实时预览和运行结果。 5. **测试和调试**:使用给出的测试用例来测试你的代码,确保它能够正确地处理各种边界条件和特殊情况。 6. **提交答案**:当代码完成后,点击 "Submit" 提交你的解法。LeetCode 会自动运行所有测试用例并显示结果。 7. **学习他人的代码**:如果遇到困难,可以查看社区中的其他优秀解法,学习他人的思路和技术。 8. **反复练习**:刷题不是一次性的事情,通过反复练习和优化,逐渐提高解题速度和代码质量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值