leetcode-other

来源:互联网 发布:手游抢激活码软件 编辑:程序博客网 时间:2024/05/17 23:52

9. Palindrome Number

判断一个数是不是回文数

#每次提取头尾两个数,判断它们是否相等,判断后去掉头尾两个数。class Solution(object):    def isPalindrome(self, x):        """        :type x: int        :rtype: bool        """        if x < 0:            return False        digits = 1        while x/digits >= 10:            digits *= 10        while digits > 1:            right = x%10            left = x//digits            if left != right:                return False            x = (x%digits) // 10            digits /= 100        return True#考虑生成一个反转整数,通过比较反转整数和原整数是否相等来判断回文class Solution(object):    def isPalindrome(self, x):        """        :type x: int        :rtype: bool        """        if x < 0:            return False        tmp = x        y = 0        while tmp:            y = y*10 + tmp%10            tmp = tmp/10        return y == x

191.numbers of 1 bits

给一个无符号数,求其二进制中1的个数
第一种 方法转二进制后计算1的个数

class Solution(object):    def hammingWeight(self, n):        """        :type n: int        :rtype: int        """        return bin(n).count('1')

第二种 通过移位操作,一位一位的判定是否是数字1。

class Solution(object):    def hammingWeight(self, n):        """        :type n: int        :rtype: int        """        count = 0        while n:            count += n&1            n >>= 1        return count

172. Factorial Trailing Zeroes

# Given an integer n, return the number of trailing zeroes in n!.#返回n的阶乘尾部0的个数,25有两个5,125有3个5,其他方法在计算时需要注意这个问题import mathclass Solution(object):    def trailingZeroes(self, n):        """        :type n: int        :rtype: int        """        res = 0        while n > 0:            n = n/5            res += n        return res

263.Ugly Number

判断一个数的因子是不是只有2,3,5.

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

400.Nth digit

给定一个无穷整数序列1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, … 求序列的第n位数字。

分析可以得出一位有9个数字,二位数有90个数字,三位数有900个数,依次类推. 因此可以每次增加一位数字,看n是否还在这个范围内.例如给定n = 150,首先一位有9个数字,所以位数可以+1,这样n-9 = 141. 然后2位的数字有2*90= 180,大于141,所以目标数字肯定是2位的.然后求具体落在哪个数字.可以用10+(141-1)/2 = 80求出.再求具体落在哪一位上,
可以用(141-1)%2=0求出为第0位,即8.如此即可.

class Solution(object):    def findNthDigit(self, n):        """        :type n: int        :rtype: int        """        for i in range(9):            d = 9 * 10 ** i            if n <= d * (i + 1): break            n -= d * (i + 1)        n -= 1        return int(str(10**i + n / (i + 1))[n % (i + 1)])

405. Convert a Number to Hexadecimal

class Solution(object):    def toHex(self, num):        ans = []        hexs = '0123456789abcdef'        if num < 0: num += 0x100000000        while num:            ans.append(hexs[num % 16])            num = num/16        return ''.join(ans[::-1]) if ans else '0'

441 Arranging Coins

def arrangeCoins(n):    """    :type n: int    :rtype: int    """    i=1    while i+(i*(i-1)) / 2 <= n:        i=i+1    return i-1print(arrangeCoins(5))#解一元二次方程:x^2 + x = 2 * n 解得:x = sqrt(2 * n + 1 / 4) - 1 /2class Solution(object):    def arrangeCoins(self, n):        """        :type n: int        :rtype: int        """        order = sqrt(2.0 * floor(n) + 0.25) - 0.5        return int(order)

507. Perfect Number

判断一个数的所有因数之和是否是这个数本身 比如:1+2+4+7+14=28

class Solution(object):    def checkPerfectNumber(self, num):        """        :type num: int        :rtype: bool        """        if num < 2: return False        divisor_sum = 1        for i in range(2,int(math.sqrt(num))+1):            if num%i == 0:                divisor_sum += i                divisor_sum += (num/i)        return num == divisor_sumclass Solution(object):    def checkPerfectNumber(self, num):        """        :type num: int        :rtype: bool        """        total, div = 1, 2        while div * div <= num:            if num % div == 0:                total += div                if div * div != num:                    total += num / div            div += 1        return num > 1 and total == num

633. Sum of Square Numbers

一个数是否为其他两个数的平方和

1.

import mathclass Solution(object):    def judgeSquareSum(self, c):        """        :type c: int        :rtype: bool        """        a = set()        for i in range(0,int(math.sqrt(c))+1):            a.add(i**2)        for j in a:            if (c - j) in a:                return True        return False

2.由于a*a+b*b=c,所以只需要从0到sqrt(c)遍历,求得c-i*i的剩余值,并对其进行开方,如果开方后结果的平方等于开方前结果,则为true,因为sqrt()函数保留整数位,所以不能开方的数结果只是整数位,乘方回去必然不等于开方前的数,本题不排除一个数使用两次的情况。

原创粉丝点击