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
别人的解法:
竖式计算:
FFT:
https://leetcode.com/problems/multiply-strings/discuss/17755/Share-a-code-with-FFT
关于FFT https://blog.csdn.net/enjoy_pascal/article/details/81478582