LeetCode--Add Binary、Sqrt(x)、Excel Sheet Column Title、Excel Sheet Column Number、Factorial Trailing Z

67. Add Binary

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"

给定两个二进制字符串,返回他们的和(用二进制表示)。

输入为非空字符串且只包含数字 1 和 0

思路:先使用int()函数将a, b转化为整数, 再整数相加后用bin()转化为二进制, 取’0b’之后的数字部分.

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)

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.

实现 int sqrt(int x) 函数。

计算并返回 x 的平方根,其中 x 是非负整数。

由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。

思路:使用二分查找

class Solution(object):
    def mySqrt(self, x):
        """
        :type x: int
        :rtype: int
        """
        l,r=0,x
        while l<=r:
            m=(l+r)//2
            if m*m<=x<(m+1)*(m+1):
                return m
            elif x<m*m:
                r=m-1
            else:
                l=m+1

168. Excel Sheet Column Title

Given a positive integer, return its corresponding column title as appear in an Excel sheet.

For example:

    1 -> A
    2 -> B
    3 -> C
    ...
    26 -> Z
    27 -> AA
    28 -> AB 
    ...

Example 1:

Input: 1
Output: "A"

Example 2:

Input: 28
Output: "AB"

Example 3:

Input: 701
Output: "ZY"

给定一个正整数,返回它在 Excel 表中相对应的列名称。

思路:A-Z对应1-26,当列标题进一位变成AA时,列对应的数字变成27。所以这个题本质上是一个10进制转26进制的问题,不过A对应的是1而不是0。用处理进制转换的一般思路,重复取模和除法即可。但是注意由于A对应1,所以Z之后是AA,这个转换不同于一般的进制转换。

class Solution(object):
    def convertToTitle(self, n):
        """
        :type n: int
        :rtype: str
        """
        res=''
        while n:
            res=chr((n-1)%26+65)+res
            n=(n-1)/26
        return res

171. Excel Sheet Column Number

Given a column title as appear in an Excel sheet, return its corresponding column number.

For example:

    A -> 1
    B -> 2
    C -> 3
    ...
    Z -> 26
    AA -> 27
    AB -> 28 
    ...

Example 1:

Input: "A"
Output: 1

Example 2:

Input: "AB"
Output: 28

Example 3:

Input: "ZY"
Output: 701

给定一个Excel表格中的列名称,返回其相应的列序号。

思路:从A-Z对应1-26,当列标题进一位变成AA时,列对应的数字变成27,这本质上是一个26进制转10进制的问题,不过A对应的是1而不是0。

class Solution(object):
    def titleToNumber(self, s):
        """
        :type s: str
        :rtype: int
        """
        sum=0
        for c in s:
            sum=sum*26+ord(c)-64
        return sum

172. Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!.

Example 1:

Input: 3
Output: 0
Explanation: 3! = 6, no trailing zero.

Example 2:

Input: 5
Output: 1
Explanation: 5! = 120, one trailing zero.

Note: Your solution should be in logarithmic time complexity.

给定一个整数 n,返回 n! 结果尾数中零的数量。

思路:所有的尾部的0可以看做都是2*5得来的,所以通过计算所有的因子中2和5的个数就可以知道尾部0的个数。实际上,2的个数肯定是足够的,所以只需计算5的个数即可。 
要注意,25=5*5是有两个5的因子;125=5*5*5有3个5的因子。比如,计算135!末尾0的个数。 
首先135/5 = 27,说明135以内有27个5的倍数;27/5=5,说明135以内有5个25的倍数;5/5=1,说明135以内有1个125的倍数。当然其中有重复计数,算下来135以内因子5的个数为27+5+1=33。
 

class Solution(object):
    def trailingZeroes(self, n):
        """
        :type n: int
        :rtype: int
        """
        res=0
        while n>0:
            n=n/5
            res+=n
        return res

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值