Leetcode三题:#62、#78、#89

#62 Unique Paths

题目描述

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).
How many possible unique paths are there?
这里写图片描述
Above is a 7 x 3 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

Example 1:

Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Right -> Down
2. Right -> Down -> Right
3. Down -> Right -> Right

Example 2:

Input: m = 7, n = 3
Output: 28

题目大意

有一个n行m列的网格,机器人从左上角的格子出发,每次走一步,且只能向右走或者向下走,问走到右下角的格子有多少种方法。

解题思路

设左上角的格子坐标为(0, 0),以此类推,右下角格子坐标为(n-1, m-1),走到某个格子(i, j)的方法数为f(i, j),因为必定是从(i-1, j)或(i, j-1)走到这个格子的,所以它的方法数也只和f(i-1, j)和f(i, j-1)有关,由此得到递推关系:

f(i, j) = f(i - 1, j) + f(i, j - 1), 0 < i < n, 0 < j < m

因为第一行的每个格子和第一行的每个格子方法数都为1(只能一直向右走或向下走),所以边界条件为:

f(i, 0) = 1, 0 < i < n
f(0, j) = 1, 0 < j < m

因为每一行的状态都只跟上一行和当前行的状态有关,所以可以用一维数组代替二维数组储存方法数。下面附上通过的代码:

class Solution(object):
    def uniquePaths(self, m, n):
        """
        :type m: int
        :type n: int
        :rtype: int
        """
        row = [1] * m
        for i in range(n-1):
            for j in range(m-1):
                row[j+1] += row[j]
        return row[m-1]

#78 Subsets

题目描述

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

题目大意

给定一个集合,找出它的所有子集。

解题思路

一个集合中的每个元素,如果它在子集中出现,则记为1,否则记为0,每个元素的状态合在一起便构成一个01串。每一个子集都对应着这样一个01串,,如[1,3]对应101,而所有的子集恰好跟000···000到111···111的串一一对应(全0代表空集,全1代表全集),把它看成二进制的话,就是0到2^size-1,这样就有了这道题的解法,我们只需找出0到2^size-1相对应的子集即可。

class Solution(object):
    def subsets(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        subsets = []
        status = 0
        while status < pow(2, len(nums)):
            subset = []
            for i in range(len(nums)):
                if (status >> i) & 1:
                    subset.append(nums[i])
            subsets.append(subset)
            status += 1
        return subsets

#89 Gray Code

题目描述

The gray code is a binary numeral system where two successive values differ in only one bit.
Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.
For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:

00 - 0
01 - 1
11 - 3
10 - 2

Note:
For a given n, a gray code sequence is not uniquely defined.
For example, [0,2,3,1] is also a valid gray code sequence according to the above definition.
For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.

题目大意

按顺序列出所有长度为n的格雷码。

解题思路

格雷码满足任意两个相邻的代码只有一位二进制数不同,且最大数与最小数之间也仅一位数不同,它的规律是n+1位格雷码的集合的前一半是n位格雷码集合的顺序加前缀0,后一半是n位格雷码集合的逆序加前缀1,知道这个特性就可以用递归写出求n位格雷码集合的程序。

class Solution(object):
    def grayCode(self, n):
        """
        :type n: int
        :rtype: List[int]
        """
        if n == 0:
            return [0]
        grayList = self.grayCode(n - 1)
        for i in range(len(grayList)):
            grayList.append(grayList[pow(2, n-1) - i - 1] + pow(2, n-1))
        return grayList

猜你喜欢

转载自blog.csdn.net/weixin_36326096/article/details/80166545
今日推荐