leetcode:一行代码就能解决的智力题

看了这些题,总觉得自己智商不够了······

第一题:

注意,你们双方都是很聪明的,意思是这里会互相博弈,当然可以用BP但是就很复杂了。

思考一下,如果你要赢,你最后要拿1~3颗石头,那么你就要给对手去拿4颗石头,如果要给对手拿4颗石头,那么要给你拿5~7颗石头,于是就要给对手拿8颗石头~ 恍然大悟:

    class Solution
    {
        private bool canWinNim(int n)
        {
            return n % 4 != 0;
        }
    }

拿到4的倍数个石子,必输。

第二题:

 分析:

  class Solution
    {
        private bool stoneGame(int [] piles)
        {
            return true;
        }
    }

 第三题:

 这题就不解释了,直接给出最简洁的方法:

    class Solution
    {
        private int bulbSwith(int n)
        {
            return (int)Math.Sqrt(n);
        }
    }

转自微信公众号:labuladong

发布了58 篇原创文章 · 获赞 7 · 访问量 3744

猜你喜欢

转载自blog.csdn.net/xy_learning/article/details/103043028
今日推荐