[日常刷题]leetcode D23

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

118. Pascal’s Triangle

Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.
在这里插入图片描述
In Pascal’s triangle, each number is the sum of the two numbers directly above it.
Example:

Input: 5
Output:
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

Solution in C++:

关键点:

  • 杨辉三角性质

思路:

  • 一个vector用来存上一行,一个用来计算下一行。另外需要注意的就是几个特殊值的情况,比如等于0的情况,然后就是每行的最后一个数为1。
vector<vector<int>> generate(int numRows) {
        vector<vector<int>> result;
        
        if (numRows == 0)
            return result;
        
        vector<int> tmp={1};
        result.push_back(tmp);
        

        for(int i = 1; i < numRows; ++i)
        {
            vector<int> next = {1};
            size_t size = tmp.size();
            
            for(int j = 0; j < size - 1;++j)
            {
                next.push_back(tmp[j] + tmp[j+1]);
            }
            next.push_back(1);
            result.push_back(next);
            tmp.swap(next);
        }
        
        return result;
    }

119. Pascal’s Triangle II

Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal’s triangle.

Note that the row index starts from 0.
在这里插入图片描述
In Pascal’s triangle, each number is the sum of the two numbers directly above it.
Example:

Input: 3
Output: [1,3,3,1]

Follow up:

Could you optimize your algorithm to use only O(k) extra space?

Solution in C++:

关键点:

  • 杨辉三角性质 or 公式(k从0开始)

思路:

  • 还是延续上一题的方法,一个数组上一行一个数组用于求当前行,这样的时间复杂度为0(k),因为行号为k的杨辉三角数组大小为k+1
vector<int> getRow(int rowIndex) {
       
       vector<int> preRow;
       
       
       preRow.push_back(1);
       
       for (int i = 0; i < rowIndex; ++i)
       {
           vector<int> currRow={1};
           for(int j = 0; j < i; ++j)
           {
               currRow.push_back(preRow[j] + preRow[j+1]);
           }
           currRow.push_back(1);
           preRow.swap(currRow);
       }
       
       return preRow;
   }

231. Power of Two

Given an integer, write a function to determine if it is a power of two.

Example 1:

Input: 1
Output: true 
Explanation: 20 = 1

Example 2:

Input: 16
Output: true
Explanation: 24 = 16

Example 3:

Input: 218
Output: false

Solution in C++:

关键点:

  • 判断1的位数

思路:

  • 这题的思路发展还是蛮有趣的,最开始就是暴力的想法,如果最后能够除以2能够除到2就是,当然这里是要首先把1排除的。然后稍微优化一点,就是当为偶数的时候才进行上述操作。后来根据2的幂的规律发现,这样的数,在二进制的表示中,始终只有一个位会有数字1,所以最后就变成了判断数字n中1的位数问题。这个问题在前面刷过的题中做过,但是我忘记了。。。 所以看到别人采用n & (n-1)的方式判断的时候才让我意识到做过。很秒的方法。下面还是贴上我自己的做法。
bool isPowerOfTwo(int n) {
        
        int nums = 0;
        long long bit = 1;
        for(; bit <= n; bit = bit << 1)
        {
            if (bit & n)
                ++nums;
        }
        
        if (nums > 1)
            return false;
        
        return nums;
    }

小结

今天收获还不错,感觉思维又得到了锻炼,在自己不断不满足于暴力解法的思路创新下。除此之外,让我发现自己还没有总结一些让自己碰出思想火花的东西,然后就是做过的题还是需要定期复习,好的题需要做笔记,摘抄出来,趁着现在做的题量还不是太大。

知识点

  • 杨辉三角
  • n中1的位数

猜你喜欢

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