Skip to content

Commit d16e59a

Browse files
committed
0622
1 parent ce89772 commit d16e59a

24 files changed

+754
-0
lines changed

sort_by_leetcode/math/__init__.py

Whitespace-only changes.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
3+
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
4+
5+
For example:
6+
7+
1 -> A
8+
2 -> B
9+
3 -> C
10+
...
11+
26 -> Z
12+
27 -> AA
13+
28 -> AB
14+
Credits:
15+
Special thanks to @ifanchu for adding this problem and creating all test cases.
16+
17+
"""
18+
19+
"""采用除余结合的思想,但是要考虑26%26 为0,所以我们可以先对n-1,再除以26,再加上'A',"""
20+
21+
22+
class Solution(object):
23+
def convertToTitle(self, n):
24+
"""
25+
:type n: int
26+
:rtype: str
27+
"""
28+
res = ''
29+
while n:
30+
res = chr((n - 1) % 26 + ord('A')) + res
31+
n = (n - 1) / 26
32+
return res
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
3+
Related to question Excel Sheet Column Title
4+
5+
Given a column title as appear in an Excel sheet, return its corresponding column number.
6+
7+
For example:
8+
9+
A -> 1
10+
B -> 2
11+
C -> 3
12+
...
13+
Z -> 26
14+
AA -> 27
15+
AB -> 28
16+
17+
"""
18+
"""168题逆向思维"""
19+
class Solution(object):
20+
def titleToNumber(self, s):
21+
"""
22+
:type s: str
23+
:rtype: int
24+
"""
25+
res = 0
26+
for num in s:
27+
res = res * 26 + (ord(num)-ord('A')+1)
28+
return res
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
3+
Given an integer n, return the number of trailing zeroes in n!.
4+
5+
Note: Your solution should be in logarithmic time complexity.
6+
7+
"""
8+
"""直接使用数学公式即可:0的个数= n/5 + n/(5^2)+.......
9+
"""
10+
class Solution(object):
11+
def trailingZeroes(self, n):
12+
zeroCnt = 0
13+
while n > 0:
14+
n = n/ 5
15+
zeroCnt += n
16+
17+
return zeroCnt
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
3+
Write an algorithm to determine if a number is "happy".
4+
5+
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
6+
7+
Example: 19 is a happy number
8+
9+
12 + 92 = 82
10+
82 + 22 = 68
11+
62 + 82 = 100
12+
12 + 02 + 02 = 1
13+
Credits:
14+
Special thanks to @mithmatt and @ts for adding this problem and creating all test cases.
15+
16+
"""
17+
18+
"""解法1,用列表保存出现过的数,当出现重复的数字的时候,说明出现了循环,循环有两种情况,一种不是hayyp数,循环的数必不是1,如果是happy数,那么会出现1的不断循环"""
19+
class Solution(object):
20+
def isHappy(self, n):
21+
"""
22+
:type n: int
23+
:rtype: bool
24+
"""
25+
c = set()
26+
while not n in c:
27+
c.add(n)
28+
n = sum([int(x) ** 2 for x in str(n)])
29+
return n==1
30+
31+
"""解法2,使用追赶法,快的指针每次前进两步,慢的指针每次只前进一步,当快的指针追上慢的指针即二者数字相同时,同样说明出现了
32+
循环,情况同上"""
33+
class Solution(object):
34+
def isHappy(self, n):
35+
"""
36+
:type n: int
37+
:rtype: bool
38+
"""
39+
slow = n
40+
quick = sum([int(x) ** 2 for x in str(n)])
41+
while quick != slow:
42+
quick = sum([int(x) ** 2 for x in str(quick)])
43+
quick = sum([int(x) ** 2 for x in str(quick)])
44+
slow = sum([int(x) ** 2 for x in str(slow)])
45+
return slow == 1
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
3+
Description:
4+
5+
Count the number of prime numbers less than a non-negative number, n.
6+
7+
"""
8+
9+
"""统计比n小的质数有多少,首先设置一个数组,全部质为true,然后遍历2-sqrt(n)的数,把他们的倍数所在的数组位置
10+
置为True这里为了避免重复,从i*i的位置开始,而不是从i*2的位置开始,因为i*2,i*3,i*n-1其实已经置为false了
11+
"""
12+
class Solution(object):
13+
def countPrimes(self, n):
14+
"""
15+
:type n: int
16+
:rtype: int
17+
"""
18+
if n < 3:
19+
return 0
20+
res = [True] * n
21+
res[0] = res[1] = False
22+
for i in range(2,int(math.sqrt(n)) + 1):
23+
res[i*i:n:i] = [False] * len(res[i*i:n:i])
24+
return sum(res)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
3+
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
4+
5+
You may assume the integer do not contain any leading zero, except the number 0 itself.
6+
7+
The digits are stored such that the most significant digit is at the head of the list.
8+
9+
"""
10+
"""利用了str和int的互相转换"""
11+
class Solution(object):
12+
def plusOne(self, digits):
13+
"""
14+
:type digits: List[int]
15+
:rtype: List[int]
16+
"""
17+
sum = 0
18+
for i in digits:
19+
sum = sum * 10 + i
20+
return [int(x) for x in str(sum+1)]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
3+
Given two binary strings, return their sum (also a binary string).
4+
5+
For example,
6+
a = "11"
7+
b = "1"
8+
Return "100".
9+
10+
"""
11+
12+
"""
13+
采用递归的思路,从末位开始相加,要分五种情况进行讨论
14+
1、a为空,返回b
15+
2、b为空,返回a
16+
3、a和b的末位都是1,递归的同时,末位添0,同时前面要再和'1'想加
17+
4、a和b的末位都是0,递归的同时,末位添0
18+
5、a和b的末位有一个为1,递归的同时,末位添1
19+
"""
20+
21+
22+
class Solution(object):
23+
def addBinary(self, a, b):
24+
"""
25+
:type a: str
26+
:type b: str
27+
:rtype: str
28+
"""
29+
if a == '':
30+
return b
31+
if b == '':
32+
return a
33+
if a[-1] == '1' and b[-1] == '1':
34+
return self.addBinary(self.addBinary(a[:-1], b[:-1]), '1') + '0'
35+
elif a[-1] == '0' and b[-1] == '0':
36+
return self.addBinary(a[:-1], b[:-1]) + '0'
37+
else:
38+
return self.addBinary(a[:-1], b[:-1]) + '1'
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
3+
Implement int sqrt(int x).
4+
5+
Compute and return the square root of x.
6+
7+
"""
8+
"""采用二分搜索的思路"""
9+
10+
class Solution(object):
11+
def mySqrt(self, x):
12+
"""
13+
:type x: int
14+
:rtype: int
15+
"""
16+
if x<=0:
17+
return 0
18+
else:
19+
left = 1
20+
right = x
21+
while left <= right:
22+
mid = (left+right) / 2
23+
a1 = mid*mid - x
24+
a2 = (mid-1)*(mid-1) -x
25+
if a1*a2 <= 0:
26+
break
27+
elif a1 * a2 >0 and a1<0:
28+
left = mid+1
29+
else:
30+
right = mid-1
31+
if a1 == 0:
32+
return mid
33+
else:
34+
return mid-1
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
3+
Reverse digits of an integer.
4+
5+
Example1: x = 123, return 321
6+
Example2: x = -123, return -321
7+
8+
click to show spoilers.
9+
10+
Note:
11+
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
12+
13+
"""
14+
15+
16+
"""
17+
这里用到的方法是经典的除余相结合,得到一个数的最后一位简单的方法就是余10,得到这个数的除最后一位之外的n-1位,简单的方法就是除10
18+
这里要注意的地方就是负数首先要先得到它的相反数在进行操作,最后再变为负数
19+
这个题还有一个注意的地方就是别的语言如c++,java中整数有溢出,而python的整数是没有溢出的,所以我们最好手动判断是否溢出
20+
"""
21+
22+
class Solution(object):
23+
def reverse(self, x):
24+
"""
25+
:type x: int
26+
:rtype: int
27+
"""
28+
n = x if x > 0 else -x
29+
res = 0
30+
while n:
31+
res = res * 10 + n % 10
32+
n = n / 10
33+
if res > 0x7fffffff:
34+
return 0
35+
return res if x > 0 else -res

0 commit comments

Comments
 (0)