Skip to content

Commit f2bef8b

Browse files
committed
update --52
1 parent b1de1be commit f2bef8b

File tree

6 files changed

+123
-0
lines changed

6 files changed

+123
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding:utf-8 -*-
2+
class Solution:
3+
def Sum_Solution(self, n):
4+
# write code here
5+
return self.sumN(n)
6+
7+
def sum0(self, n):
8+
return 0
9+
10+
def sumN(self,n):
11+
fun = {False:self.sum0,True: self.sumN}
12+
return n + fun[not not n](n - 1)
13+
14+
# -*- coding:utf-8 -*-
15+
class Solution:
16+
def Sum_Solution(self, n):
17+
# write code here
18+
return n and self.Sum_Solution(n - 1) + n
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- coding:utf-8 -*-
2+
class Solution:
3+
def Add(self, num1, num2):
4+
# write code here
5+
while num2 != 0:
6+
temp = num1 ^ num2
7+
num2 = (num1 & num2) << 1
8+
num1 = temp & 0xFFFFFFFF
9+
return num1 if num1 >> 31 == 0 else num1 - 4294967296
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding:utf-8 -*-
2+
class Solution:
3+
def StrToInt(self, s):
4+
# write code here
5+
flag = False
6+
if not s or len(s) < 1:
7+
return 0
8+
num = []
9+
numdict = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
10+
for i in s:
11+
if i in numdict.keys():
12+
num.append(numdict[i])
13+
elif i == '+' or i == '-':
14+
continue
15+
else:
16+
return 0
17+
ans = 0
18+
if len(num) == 1 and num[0] == 0:
19+
flag = True
20+
return 0
21+
for i in num:
22+
ans = ans*10 + i
23+
if s[0] == '-':
24+
ans = 0 - ans
25+
return ans
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding:utf-8 -*-
2+
class Solution:
3+
# 这里要特别注意~找到任意重复的一个值并赋值到duplication[0]
4+
# 函数返回True/False
5+
def duplicate(self, numbers, duplication):
6+
# write code here
7+
if not numbers or len(numbers) < 0:
8+
return False
9+
for i in numbers:
10+
if i < 0 or i > len(numbers) - 1:
11+
return False
12+
for i in range(len(numbers)):
13+
while numbers[i] != i:
14+
if numbers[i] == numbers[numbers[i]]:
15+
duplication[0] = numbers[i]
16+
return True
17+
else:
18+
idx = numbers[i]
19+
numbers[i],numbers[idx] = numbers[idx],numbers[i]
20+
return False
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding:utf-8 -*-
2+
class Solution:
3+
def multiply(self, A):
4+
# write code here
5+
if not A or len(A) <= 0:
6+
return
7+
length = len(A)
8+
lis = [1] * length
9+
for i in range(1,length):
10+
lis[i] = lis[i-1] * A[i-1]
11+
temp = 1
12+
for i in range(length-2,-1,-1):
13+
temp = temp * A[i+1]
14+
lis[i] *= temp
15+
return lis
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -*- coding:utf-8 -*-
2+
class Solution:
3+
# s, pattern都是字符串
4+
def match(self, s, pattern):
5+
# 如果s与pattern都为空,则True
6+
if len(s) == 0 and len(pattern) == 0:
7+
return True
8+
# 如果s不为空,而pattern为空,则False
9+
elif len(s) != 0 and len(pattern) == 0:
10+
return False
11+
# 如果s为空,而pattern不为空,则需要判断
12+
elif len(s) == 0 and len(pattern) != 0:
13+
# pattern中的第二个字符为*,则pattern后移两位继续比较
14+
if len(pattern) > 1 and pattern[1] == '*':
15+
return self.match(s, pattern[2:])
16+
else:
17+
return False
18+
# s与pattern都不为空的情况
19+
else:
20+
# pattern的第二个字符为*的情况
21+
if len(pattern) > 1 and pattern[1] == '*':
22+
# s与pattern的第一个元素不同,则s不变,pattern后移两位,相当于pattern前两位当成空
23+
if s[0] != pattern[0] and pattern[0] != '.':
24+
return self.match(s, pattern[2:])
25+
else:
26+
# 如果s[0]与pattern[0]相同,且pattern[1]为*,这个时候有三种情况
27+
# pattern后移2个,s不变;相当于把pattern前两位当成空,匹配后面的
28+
# pattern后移2个,s后移1个;相当于pattern前两位与s[0]匹配
29+
# pattern不变,s后移1个;相当于pattern前两位,与s中的多位进行匹配,因为*可以匹配多位
30+
return self.match(s, pattern[2:]) or self.match(s[1:], pattern[2:]) or self.match(s[1:], pattern)
31+
# pattern第二个字符不为*的情况
32+
else:
33+
if s[0] == pattern[0] or pattern[0] == '.':
34+
return self.match(s[1:], pattern[1:])
35+
else:
36+
return False

0 commit comments

Comments
 (0)