9.28 练手 腾讯50题 9.回文数 43.字符串数字相乘

Leetcode 9. Palindrome Number

思路是先转换成字符串然后两边双指针检测,代码如下:

class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        if x<0:
            return False
        if x <10:
            return True
        x = str(x)
        left = 0
        right = len(x)-1
        while(left < right):
            if(x[left] != x[right]):
                return False
            left += 1 
            right -= 1
        return True

后来想了想还有这个倒转数组方法:

class Solution:
    def isPalindrome(self, x: int) -> bool:
        x=str(x)
        if x[::-1]==x:
            return True
        else:
            return False

不转字符串的解法:

class Solution:
    def isPalindrome(self, x: int) -> bool:
        orig = x
        back_x = 0
        while x > 0:
            back_x = (back_x * 10) + (x % 10)
            x = x // 10
        return orig == back_x

 


 

Leetcode 43. Multiply Strings

 

想法是暴力解法,直接一个一个数相乘相加,代码如下

class Solution(object):
    def multiply(self, num1, num2):
        """
        :type num1: str
        :type num2: str
        :rtype: str
        """
        res = 0
        num1 = num1[::-1]
        num2 = num2[::-1]
        for i in range(len(num1)):
            rate1 = 10 ** i
            for  j in range(len(num2)):
                rate2 = 10 ** j    
                res += int(num1[i]) * int(num2[j]) * rate1 * rate2       
        return str(res)

复杂度 O(mn),就是非常慢hhh

别人的解法:
竖式计算:

https://leetcode.com/problems/multiply-strings/discuss/17605/Easiest-JAVA-Solution-with-Graph-Explanation

FFT:
https://leetcode.com/problems/multiply-strings/discuss/17755/Share-a-code-with-FFT

关于FFT https://blog.csdn.net/enjoy_pascal/article/details/81478582

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值