leetcode习题练习
day001 两数之和
#!user/bin/env python # -*- coding:utf-8 -*- #方法1 def Sum(nbs,tgt): len_nums = len(nbs) for i in range(len_nums): for j in range(i+1,len_nums): if nbs[i] + nbs[j] == tgt: return [i,j] print(Sum([2,7,11,15],9)) #方法2 二遍哈希表 def Sum(nums,tgt): len_num = len(nums) for i in range(len_num): dif = tgt - nums[i] if dif in nums[i+1:]: return [i,nums[i+1:].index(dif)+i+1] print(Sum([2,7,11,15],9)) #方法3 一遍哈希表 def Sum(nums,tgt): len_num = len(nums) for i in range(len_num): dif = tgt - nums[i] if dif in nums[:i]: return [nums[:i].index(dif),i] print(Sum([2,7,11,15],9))
day002 给定一个字符串,找出其中不含有重复字符的最长子串的长度
# global num # 求出字符串最长长度,一次,无迭代(代码正确),求出最大值,代码错误!而且题目理解错误,只要求出数字即可!!! # def func_str(x_li): # num = 0 # for i in range(len(x_li)): # li1.append(x_li[i]) # if num == 0: # num = num + 1 # continue # for j in range(len(li1)-1): # if x_li[i] == li1[j]: # return li1[:-1] #返回值形如['a','b','c'] # global num # li1 = [] # li = list(input('>>>')) # str_max = func_str(li) # print(''.join(str_max)) #以下错误代码 def func_str(x_li): num = 0 for i in range(len(x_li)): li1.append(x_li[i]) if num == 0: num = num + 1 continue for j in range(len(li1)-1): if x_li[i] == li1[j]: return li1[:-1] #返回值形如['a','b','c'] global num li1 = [] li = list(input('>>>')) str_max = func_str(li) print(''.join(str_max)) # 输入aaa 输出a s = len(str_max) while True: li1.clear() str_max1 = func_str(list(set(li)-set(str_max))) # print(li2) # li2 = func_str(li[s:-1]) if len(str_max1) > s: str_max = str_max1 print(''.join(str_max)) #reference code 14行精简代码!! def func_str(s): max_len = 0 if s is None or len(s) == 0: return max_len str_dict = {} one_max = 0 start = 0 for i in range(len(s)): if (s[i] in str_dict) and (str_dict[s[i]] >= start): #字典中的数要从start算起,并且s[i]存在于i以后的字典中 start = i print(str_dict[s[i]]) #测试用 one_max = i - start + 1 str_dict[s[i]] = i #字典不允许key,value重复 # print(str_dict[s[i]]) # print(str_dict[s[i]]) max_len = max(max_len, one_max) return max_len print(func_str('abc123abcefghi1231234567890zxcvbnmasdfghjkl'))
day003 整数反转,给出一个32位的有符号整数,你需要将这个整数中每位上的数字进行反转
str = input('>>>') li = [] for i in str: li.append(i) if li[0] is '-': li.pop(0) li.reverse() print(''.join(li),end='') print('-') else: li.reverse() print(li) for i in li: if i == '0': li.pop(int(i)) # print(li) elif int(i) is not 0: print('yes') print(''.join(li)) break
day004 最长回文字串:给定一个字符串s,找到s中最长的回文子串。假设s的最大长度为1000. 回文子串定义:如level,noon
#判断一个固定长度字符串是否回文的3中方法 方法1_字符串逆序法 def is_palindrome1(text): l = list(text) l.reverse() t1 = ''.join(l) if t1 == text: print 'the text is palindrome' else: print 'the text is not palindrome' 方法2_字符串逆序索引 def is_palindrome2(text): t1 = text[::-1] if t1 == text: print 'the text is palindrome' else: print 'the text is not palindrome' 方法3_传统法,s[len(s)-x-1] == s[x] ?? def is_palindrome3(text): r = True for x in range(len(text)): print x,text[x] if text[x] != text[len(text)-x-1]: print 1 r = False break if r == True: print 'the text is palindrome' else: print 'the text is not palindrome' #中心扩展法 代码好像有点问题,3.5revision! def huiwen(str): str_max = '' for i in range(2*len(str)-1): #偶数情况 如在12321中i==2,为偶数情况 if i % 2 == 0: start = end = i // 2 #/整除 while start >= 0 and end < len(str) and str[start] == str[end]: start -= 1 end += 1 #奇数情况 else: start = (i-1)//2 end = (i-1)//2 while start >= 0 and end <= len(str) and str[start] == str[end]: start -= 1 end += 1 if len(str) <= (end-start+1): str_max = str[start+1:end] return str_max print(huiwen('cbbd'))
本代码编写分两步,1向字符串中插入'#',字符串首尾+‘#’;2字符串中以每个元素为中心进行扩展,查找最大回文字符串。
def max_huiwen(string): li = [i for i in string] newli_list = '#' + '#'.join(li) + '#' max_str = 0 length = len(newli_list) for index in range(0,length): max_substr = get_length(newli_list,index) if max_substr > max_str: max_str = max_substr return max_str def get_length(s_li,index): length = len(s_li) submax_str = 0 for i in range(1,index+1): if index+i < length and s_li[index-i] == s_li[index+i]: submax_str = submax_str + 1 else: break return submax_str res = max_huiwen('13553143211234321123aaaaaaaaaaaaaaaaaa') print(res) day005 有两个有序数组,求他们的中位数,如num1=[1,3],num2=[2,4] 中位数是(2+3)/2=2.5;再如num1=[1,3],num2=[2] 中位数是2. num1 = [1,3,5,6,7,9,11,22] num2 = [2,4,5] def medium1(num1): # num = num1 + num2 # sorted(num) length1 = len(num1) if length1 % 2 == 0: #length1长度为偶数 res1 = (num1[length1//2] + num1[length1//2-1])/2 return res1 else: #length1为奇数 res1 = num1[(length1-1)//2] return res1 def medium2(num1): length2 = len(num2) if length2 % 2 == 0: #length2长度为偶数 res2 = (num2[length2//2] + num2[length2//2-1])/2 return res2 else: #length2为奇数 res2 = num2[(length2-1)//2] return res2 a = medium1(num1) b = medium1(num2) print((a+b)/2)
day005 将罗马字符转换为阿拉伯数字:算法,字符串左边小于右边,累加;左边大于右边,左边数字 - 2*右边数字
def func(roman_substr): res = 0 dic = { 'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000 } for i in range(0,len(roman_substr)): if i == 0 or dic[roman_substr[i]] <= dic[roman_substr[i-1]]: res += dic[roman_substr[i]] else: res += dic[roman_substr[i]] - 2*dic[roman_substr[i-1]] return res roman_str = ['DII','XV','MDCLXVI','XII','VIII','XCIX','XII'] for roman_substr in roman_str: print(func(roman_substr)) day007 输入的一个字符串和总行数,按照zigzag类型输出,如输入‘HKTSUOFFER’,3;输出HUEKTOFRSF(18行代码!) def zigzag(s, nRows): if s == '' or nRows <= 1: return s li = [] size = 2*nRows-2 for i in range(nRows): j = i #values of lines from this one if i == 0 or i == nRows-1: while j < len(s): li.append(s[j]) j +=size #algorithm1 for initial and end lines else: while j < len(s): li.append(s[j]) j += size-2*i #algorithm2 for uninitial and unend lines return li res = zigzag('HKSTUOFFER', 3) print((''.join(res)).upper())
day006 Regular Expression Matching, 规则1 '.'可以匹配任何字母,‘*’可以重复一次‘*’之前的一个字母;规则2 输入两个字符串a和b,如b能覆盖a,返回True,否则返回False. 如1输入’ab‘和‘a*’ 返回False. 如2输入'aa'和‘a*’ 返回True.
def judge_twostr(): str = input('输入原字符串>>>') str_li = [] str_str1 = '' s = input('输入想要匹配的字符串>>>') s_li = [] str_str2 = '' for i in range(len(str)): str_li.append(str[i]) for j in range(len(s)): s_li.append(s[j]) for k in range(len(s_li)): if s_li[k] == '.': #字符串中部遇到字符'.' s_li[k] = str_li[k] if s_li[k] == '*': #字符串中部遇到字符‘*’ s_li[k] = s_li[k-1] str_str1 += ''.join(str_li) #str_li和s_li转换为字符串 str_str2 += ''.join(s_li) #str_li 范围小 s_li范围大 if str_str2 in str_str1 or str_str1 == str_str2: print('T') else: print('F') if __name__ == '__main__': judge_twostr() # 复习字符串处理 def judge_twostr(): str = input('输入原字符串>>>') li_str = list(str) str_str1 = '' s = input('输入想要匹配的字符串>>>') li_s = list(s) str_str2 = '' for k in range(len(li_s)): if li_s[k] == '.': #字符串中部遇到字符'.' li_s[k] = li_str[k] if li_s[k] == '*': #字符串中部遇到字符‘*’ li_s[k] = li_s[k-1] str_str1 += ''.join(li_str) #str_li和s_li转换为字符串 str_str2 += ''.join(li_s) #str_li 范围小 s_li范围大 if str_str2 in str_str1 or str_str1 == str_str2: print('T') else: print('F') if __name__ == '__main__': judge_twostr()