【LeetCode】Algorithm 61~70:66,67,69,70

前言

本系列博客爲平時刷LeetCode的記錄,每十道題一篇博客,持續更新,所有代碼詳見GitHub:https://github.com/roguesir/LeetCode-Algorithm

66. Plus One

introduction

Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
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.

Solution

class Solution(object):
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        digits = "".join(map(str, digits))
        if digits.isdigit():
            return map(int, list(str(int(digits) + 1)))

https://leetcode.com/submissions/detail/202038077/

67. Add Binary

Introduction

Given two binary strings, return their sum (also a binary string).
The input strings are both non-empty and contains only characters 1 or 0.
Example 1:

Input: a = "11", b = "1"
Output: "100"

Example 2:

Input: a = "1010", b = "1011"
Output: "10101"

Solution

class Solution(object):
    def addBinary(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        return bin(int(a,2)+int(b,2))[2:]

69. Sqrt(x)

Introduction

Implement int sqrt(int x).

Compute and return the square root of x, where x is guaranteed to be a non-negative integer.

Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.

Example 1:

Input: 4
Output: 2
Example 2:

Input: 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since 
             the decimal part is truncated, 2 is returned.

Solution

class Solution(object):
    def mySqrt(self, x):
        """
        :type x: int
        :rtype: int
        """
        import math
        if x >= 0:
            return int(math.sqrt(x))

https://leetcode.com/submissions/detail/202038710/

70. Climbing Stairs

Introduction

You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Note: Given n will be a positive integer.
Example 1:

Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. step + 1 step
2. steps

Example 2:

Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. step + 1 step + 1 step
2. step + 2 steps
3. steps + 1 step

Solution

實際上,這道題的結果是斐波那契數列,爲了簡介代碼,我首先想到的是使用lambda表達式,結果,竟然超時了。。。
Method 1
使用lambda表達式構建斐波那契函數。

class Solution(object):
    def climbStairs(self, n):
        """
        :type n: int
        :rtype: int
        """
        fib = lambda n:1 if n<=2 else fib(n-1)+fib(n-2)
        return fib(n+1)

https://leetcode.com/submissions/detail/202039687/
Method 2
直接使用斐波那契數列的通項公式進行計算。

class Solution(object):
    def climbStairs(self, n):
        """
        :type n: int
        :rtype: int
        """
        from math import sqrt
        return int(sqrt(5)/5*((((1+sqrt(5))/2))**(n+1)-(((1-sqrt(5))/2))**(n+1)))

https://leetcode.com/submissions/detail/202040369/
Method 3
遞歸構建斐波那契函數。

class Solution(object):
    def climbStairs(self, n):
        """
        :type n: int
        :rtype: int
        """
        a=1
        b=1
        def fib(n, a, b):
            while n != 0:
                a, b = b, a+b
                n -= 1
            return b
        return fib(n-1, a, b)

https://leetcode.com/submissions/detail/202222009/

  • 更新時間:2019-01-27
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章