1.两数之和
import numpy as np
import math
nums = [3,2,4]
target = 6
第一个版本:两个for循环的嵌套(时间过长)
ans = []
l = len(nums)
for i_index in range(l):
for j_index in range(i_index+1,l):
if nums[i_index] + nums[j_index] == target:
ans.append([i_index,j_index])
for i in ans:
print(i)
第二个版本:减少for循环(不能用numpy)
nums_array = np.array(nums)
for i_index in range(len(nums_array)):
j = target - nums_array[i_index]
j_index = list(np.where(nums_array == j)[0])
if j_index != []:
j_index = j_index[0]
nums_array[i_index] = float('inf')
nums_array[j_index] = float('inf')
print([i_index,j_index])
第三个版本:不使用numpy(通过版)
for i_index in range(len(nums)):
j = target - nums[i_index]
if j in nums:
for j_index in range(len(nums)):
if nums[j_index] == j:
print([i_index,j_index])
nums[i_index] = float('inf')
nums[j_index] = float('inf')
2.两数相加(未通过)
第一个版本:直接在list里面计算
l1_list = [2,4,3]
l2_list = [5,6,4]
l1_length = len(l1_list)
l2_length = len(l2_list)
l1_num = 0
for i in range(l1_length):
l1_num += l1_list[l1_length-i-1] * pow(10,i)
l2_num = 0
for i in range(l2_length):
l2_num += l2_list[l2_length-i-1] * pow(10,i)
ans = l1_num+l2_num
ans_list = []
for i in list(str(ans)):
ans_list.insert(0,int(i))
第二个版本:加入类
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def addTwoNumbers(self, l1, l2):
""" :type l1: ListNode :type l2: ListNode :rtype: ListNode """
l1_list = l1.val
l2_list = l2.val
l1_length = len(l1_list)
l2_length = len(l2_list)
l1_num = 0
for i in range(l1_length):
l1_num += l1_list[l1_length-i-1] * pow(10,i)
l2_num = 0
for i in range(l2_length):
l2_num += l2_list[l2_length-i-1] * pow(10,i)
ans = l1_num+l2_num
ans_list = []
for i in list(str(ans)):
ans_list.insert(0,int(i))
return ListNode(ans_list)
7. 反转整数
第一个版本:硬怼
class Solution:
def reverse(self, x):
""" :type x: int :rtype: int """
def num_reverse(x_list):
x_reverse = []
for i in x_list:
x_reverse.insert(0,i)
ans = ''
for i in x_reverse:
ans += i
return ans
def judge_scale(num):
if num >= -2**31 and num <= (2**31 - 1):
return num
else:
return 0
x = judge_scale(x)
x_list = list(str(x))
if x_list[0] != '-':
ans = num_reverse(x_list)
else:
x_list = x_list[1:]
ans = '-' + num_reverse(x_list)
ans_int = int(ans)
ans_int = judge_scale(ans_int)
return ans_int
第二个版本:使用reserve()方法
class Solution:
def reverse(self, x):
""" :type x: int :rtype: int """
def judge_scale(num):
if num >= -2**31 and num <= (2**31 - 1):
return num
else:
return 0
def list_str(x_list):
ans = ''
for i in x_list:
ans += i
return ans
x = judge_scale(x)
x_list = list(str(x))
if x_list[0] != '-':
x_list.reverse()
ans = list_str(x_list)
else:
x_list = x_list[1:]
x_list.reverse()
ans = '-' + list_str(x_list)
ans_int = int(ans)
ans_int = judge_scale(ans_int)
return ans_int
第三个版本:使用切片的方法
class Solution:
def reverse(self, x):
""" :type x: int :rtype: int """
def judge_scale(num):
if num >= -2**31 and num <= (2**31 - 1):
return num
else:
return 0
def list_str(x_list):
ans = ''
for i in x_list:
ans += i
return ans
x = judge_scale(x)
x_list = list(str(x))
if x_list[0] != '-':
x_reverse = x_list[::-1]
ans = list_str(x_reverse)
else:
x_list = x_list[1:]
x_reverse = x_list[::-1]
ans = '-' + list_str(x_reverse)
ans_int = int(ans)
ans_int = judge_scale(ans_int)
return ans_int
3. 无重复字符的最长子串
class Solution:
def lengthOfLongestSubstring(self, s):
""" :type s: str :rtype: int """
string_list = list(s)
l = len(string_list)
if l != 0:
max_sub_list = 1
for i in range(l):
for j in range(i + max_sub_list,l):
if i != j:
sub_list = string_list[i:j+1]
sub_list_length = len(sub_list)
if sub_list_length > len(set(sub_list)):
break
elif sub_list_length > max_sub_list:
max_sub_list = sub_list_length
else:
max_sub_list = 0
return max_sub_list
5.最长回文字符串
class Solution:
def longestPalindrome(self, s):
""" :type s: str :rtype: str """
import numpy as np
mylist = list(s)
myset = set(s)
mycount = [(i,len(np.where(np.array(mylist) == i)[0])) for i in myset]
anslen = 0
if len(myset) == 1:
mysubs = s
elif len(s) != len(myset):
for (i,j) in mycount:
if j == 1:
continue
else:
index = list(np.where(np.array(mylist) == i)[0])
for m in index:
for n in range(len(index),index.index(m),-1):
subs = mylist[m:index[n-1]+1]
subs_copy = subs.copy()
subs.reverse()
if subs == subs_copy:
newanslen = len(subs)
if anslen <= newanslen:
anslen = newanslen
mysubs = ''
for mys in subs_copy:
mysubs += mys
continue
continue
else:
try:
mysubs = s[0]
except IndexError:
mysubs = s
return mysubs
9.回文数
第一版本:硬怼直接型
class Solution:
def isPalindrome(self, x):
""" :type x: int :rtype: bool """
if x < 0:
return False
else:
x_list = list(str(x))
x_reverse = list(reversed(x_list))
if x_reverse == x_list:
return True
else:
return False
第二个版本:只反转数值的一半
class Solution:
def isPalindrome(self, x):
""" :type x: int :rtype: bool """
if x < 0:
#return False
print(False)
else:
x_list = list(str(x))
x_half_len = len(x_list) / 2
if x_half_len == int(x_half_len):
x_half_reverse = list(reversed(x_list[:int(x_half_len)]))
x_leave = x_list[int(x_half_len):]
else:
x_half_reverse = list(reversed(x_list[:int(x_half_len - 0.5)]))
x_leave = x_list[int(x_half_len + 0.5):]
if x_half_reverse == x_leave:
#return True
print(True)
else:
#return False
print(False)
第三个版本:只反转一半,且不转换成字符串的方式进行
class Solution:
def isPalindrome(self, x):
""" :type x: int :rtype: bool """
if x < 0:
return False
elif x < 10:
return True
else:
div = x
reverse = 0
mod = 0
i = 1
while div > reverse and div >= 10:
div, mod = divmod(div, 10)
if mod == 0 and i == 1:
return False
else:
reverse = reverse * 10 + mod
i += 1
if reverse == div:
return True
elif (reverse - mod )/ 10 == div:
return True
else:
return False
4. 两个排序数组的中位数
class Solution:
def findMedianSortedArrays(self, nums1, nums2):
""" :type nums1: List[int] :type nums2: List[int] :rtype: float """
total = sorted(nums1+(nums2))
total_length = len(total)
div, mod = divmod(total_length, 2)
if mod == 0:
middel = sum(total[div-1:div+1])/2
else:
middel = total[div]
return middel