小E学Python PART2 Leetcode_Math
写的都很繁琐而且可能有BUG,要继续修修补补,望各位不吝赐教:)
easy_part
2019.06.01 儿童节来写代码吧:)
T7. Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
class Solution:
def reverse(self, x: int) -> int:
s=str(abs(x))
s=int(s[::-1])
if s in range ((-2)**31,2**31-1):
if x<0:
return (-1*s)
else:
return s
else:
return 0
总结:
要注意数据的范围!
Runtime: 40 ms, faster than 85.48% of Python3 online submissions for Reverse Integer.
Memory Usage: 13.3 MB, less than 48.79% of Python3 online submissions for Reverse Integer.
T9. Palindrome Number
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
Input: 121
Output: true
Example 2:
Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Follow up:
Coud you solve it without converting the integer to a string?
class Solution:
def isPalindrome(self, x: int) -> bool:
def length(x):
if (int(x/10)==0):
return 0
else:
sum=0
while(int(x/10)!=0):
sum+=1
x=x/10
return sum
num=length(x)+1
if x<0:
return False
elif num==1:
return True
elif ((num!=1)and(x%10==0)):
return False
else:
passi=0
for i in range(num//2):
a=int(x/(10**i)%10)
b=int(x/(10**(num-1-i))%10)
if (a==b):
passi+=1
a=0
b=0
if(passi==(num//2)):
return True
else:
return False
总结:
没用str()的我仿佛是个傻子,啥都不会了?,这写的也太繁琐了吧
Runtime: 132 ms, faster than 38.75% of Python3 online submissions for Palindrome Number.
Memory Usage: 13.3 MB, less than 44.22% of Python3 online submissions for Palindrome Number.
2019.06.02
T69. 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.
class Solution:
def mySqrt(self, x: int) -> int:
import math
return (int(math.sqrt(x)))
总结:
刚开始看题目,有点傻了:)
Runtime: 36 ms, faster than 96.77% of Python3 online submissions for Sqrt(x).
Memory Usage: 13.2 MB, less than 66.65% of Python3 online submissions for Sqrt(x).
T168. 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”
class Solution:
def convertToTitle(self, n: int) -> str:
import math
alpha2=''
num2=1
sum=int(math.log(n,26))
if sum>1:
if n<=26**sum+26**(sum-1):
sum=sum-1
elif (sum==1)and(n==26):
sum=sum-1
else:
sum=sum
for i in range(sum,-1,-1):
if i>0:
for j in range(1,27):
if (n-j*(26**i)>=1):
num2=j
else:
break
else:
num2=n
alpha1=chr(64+num2)
alpha2=alpha1+alpha2
n=n-(26**i)*num2
return alpha2[::-1]
总结:
四次才AC,要哭出来了,做一个27进制:)
Runtime: 32 ms, faster than 94.29% of Python3 online submissions for Excel Sheet Column Title.
Memory Usage: 13.2 MB, less than 42.97% of Python3 online submissions for Excel Sheet Column Title.
2019.06.03
T171. 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
class Solution:
def titleToNumber(self, s: str) -> int:
length=len(s)
sumn=0
s=s[::-1]
for i in range(length):
num=(ord(s[i])-64)*(26**i)
sumn+=num
return sumn
总结:
字符串翻转:s=s[::-1]
Runtime: 40 ms, faster than 89.55% of Python3 online submissions for Excel Sheet Column Number.
Memory Usage: 13.3 MB, less than 29.32% of Python3 online submissions for Excel Sheet Column Number.
T27. Remove Element
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
Example 1:
Given nums = [3,2,2,3], val = 3,
Your function should return length = 2, with the first two elements of nums being 2.
It doesn’t matter what you leave beyond the returned length.
Example 2:
Given nums = [0,1,2,2,3,0,4,2], val = 2,
Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
Note that the order of those five elements can be arbitrary.
It doesn’t matter what values are set beyond the returned length.
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy)
int len = removeElement(nums, val);
// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
while 1:
try:
nums.remove(val)
except:
break
return len(nums)
总结:
踩坑了:remove只会删除list中的第一个指定元素lol
Runtime: 32 ms, faster than 97.23% of Python3 online submissions for Remove Element.
Memory Usage: 13.3 MB, less than 20.25% of Python3 online submissions for Remove Element.
T172. 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.
class Solution:
def trailingZeroes(self, n: int) -> int:
num_5=1
sum_5=0
while(n>=5**num_5):
sum_5+=n//5**num_5
num_5+=1
return sum_5
总结:
第一次写得超级复杂,Time Limit Exceeded lol
Runtime: 36 ms, faster than 89.09% of Python3 online submissions for Factorial Trailing Zeroes.
Memory Usage: 13.2 MB, less than 41.64% of Python3 online submissions for Factorial Trailing Zeroes.