Google面试题专题5 - leetcode66. Plus One/326. Power of Three/883. Projection Area of 3D Shapes - Easy

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a786150017/article/details/84586259

66. Plus One

题目描述

给定一非空的数字数组,代表一非负整数。将该整数加1。

整数存储方式为:最高位数字存储在数组首个元素,数组中每个元素包含一个数字。假设无前导0。

例子
Example 1:

Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.

Example 2:

Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

思想
(法1 - 直接调库函数转成int计算)
(法2 - 模拟从低位到高位,按位计算)
Trick:在原数组上直接操作

解法1
直接调库函数int

class Solution(object):
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        num = 0
        for digit in digits:
            num = num * 10 + digit
        return [int(x) for x in str(num+1)]

解法2
从低位到高位,按位计算

class Solution(object):
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        digits[-1] += 1
        for i in range(len(digits)-1, 0, -1):
            if digits[i] > 9:
                digits[i] = 0
                digits[i-1] += 1
        if digits[0] > 9:
            digits[0] = 0
            return [1] + digits
        return digits

326. Power of Three

题目描述

给定一个整数,编写函数判断它是不是3的幂次方。

例子
Example 1:

Input: 27
Output: true

Example 2:

Input: 0
Output: false

Example 3:

Input: 45
Output: false

思想
(法1)
循环除以3进行判断,注意n<0的情况
(法2)
Trick:3的幂次方,质因子只包含3.整型范围内3的最大次幂是3^19=1162261467,必然可以整除n。

注意:math.log(243,3) = 4.999999999999999

解法1
循环除以3

class Solution(object):
    def isPowerOfThree(self, n):
        """
        :type n: int
        :rtype: bool
        """
        if n <= 0:
            return False
        while n % 3 == 0:
            n //= 3
        return n == 1

解法2

class Solution(object):
    def isPowerOfThree(self, n):
        """
        :type n: int
        :rtype: bool
        """
        return n > 0 and 1162261467 % n == 0

883. Projection Area of 3D Shapes

题目描述

在N*N的网格上,我们沿着x、y和z轴放置1*1*1的立方体。每个值v = grid [i][j]表示放置在网格单元(i, j)顶部的立方体塔。

我们观察这些立方体在xy、yz和zx平面的投影。返回投影的总面积。

例子
Example 1:

Input: [[2]]
Output: 5

Example 2:

Input: [[1,2],[3,4]]
Output: 17

思想
从顶面观察的面积 - 非零元素的个数;
从正面观察的面积 - 按列存储时,各列最大值之和;
从侧面观察的面积 - 按行存储时,各行最大值之和。

zip(*list) - 二维列表转置成tuple

解法

class Solution(object):
    def projectionArea(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        res = 0
        for i in range(len(grid)):
            maxRow = maxCol = 0
            for j in range(len(grid)):
                if grid[i][j]:
                    res += 1
                maxRow = max(maxRow, grid[i][j])
                maxCol = max(maxCol, grid[j][i])
            res += maxRow + maxCol
        return res

(利用Python语法特性)

class Solution(object):
    def projectionArea(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        res = len(grid) * len(grid)
        for row in grid:
            res += max(row)
            res -= row.count(0)
            
        for col in zip(*grid):
            res += max(col)
        return res

猜你喜欢

转载自blog.csdn.net/a786150017/article/details/84586259