二分查找,给出一个已经排好序的列表,注意是已经排好序的,查找指定元素在列表中的位置
def binary_search(order_list,item):
low = 0
high = len(order_list)-1
while high-low>1:
middle = low+high//2
if order_list[middle]>item:
high = middle-1
if order_list[middle]
# -*- coding: utf-8 -*-
#选择排序
#选择排序,主要思想,找到数组中最小的元素,然后往新数组里追加,时间复杂度O(n^2)
def min_index(list):
min_index = 0
min_value = list[min_index]
for index,value in enumerate(list):
if value
另一种选择排序的写法
def swap(lyst,i,j):
temp = lyst[i]
lyst[i]=lyst[j]
lyst[j]=temp
def selectsort(lyst):
i=0
while i
def bubbleSort(lyst):
n = len(lyst)
while n>1:
i = 1
while i
# 另一种写法
def bubble_sort(list):
if len(list)>2:
max_index = len(list)
while max_index:
for index in range(1,max_index):
if list[index]
#快速排序,递归算法 O(nlogn)
# -*- coding: utf-8 -*-
#递归快速排序
def quicksort(list):
if len(list)<2:
return list #基线条件,为空或者只包含一个元素的数组是有序的
midpivot = list[0]#递归条件
lessbeforemidpivot = [i for i in list[1:] if i<=midpivot]#小于基准值的元素组成的子数组
biggeraftermidpivot = [i for i in list[1:] if i > midpivot]#大于基准值的元素组成的子数组
finallylist = quicksort(lessbeforemidpivot)+[midpivot]+quicksort(biggeraftermidpivot)
return finallylist
print quicksort([2,4,6,7,1,2,5])
# -*- coding: utf-8 -*-
#广度优先搜索
from collections import deque
graph = {}
graph["you"] = ["alice",'bob',"calm"]
graph["alice"] = ["peggym"]
graph["bob"] = ["anuj","peggym"]
graph["peggym"] = ["anuj"]
graph["anuj"] = ["peggym"]
# print type(graph)
def person_is_seller(persion):
if "m" in persion:
return True
def search(name):
search_queue = deque()
# print type(search_queue)
search_queue += graph[name]
# print type(search_queue)
# print (search_queue)
searched = []
while search_queue:
person = search_queue.popleft()
if person not in searched:
if person_is_seller(person):
print person+" is a mango seller!"
searched.append(person)
else:
search_queue += graph[person]
searched.append(person)
# print (search_queue)
return False
search("you")
# -*- coding: utf-8 -*-
#狄克斯特拉算法
# 同时存储邻居和前往邻居的开销
graph = {}
graph["start"] = {}
graph["start"]["a"] = 6
graph["start"]["b"] = 2
graph["a"] = {}
graph["a"]["fin"] = 1
graph["b"] = {}
graph["b"]["a"] = 3
graph["b"]["fin"] = 5
graph["fin"] = {}
# 从开始处到每个节点的开销散列表
infinity = float("inf")
costs = {}
costs["a"] = 6
costs["b"] = 2
costs["fin"] = infinity #终点视为无穷大
#存储父节点的散列表
parents = {}
parents["a"] = "start"
parents["b"] = "start"
parents["fin"] = None
# 记录处理过的节点的数组
processed = []
# 找出开销最低的节点
def find_lowest_cost_node(costs):
lowest_cost = float("inf")
lowest_cost_node = None
for node in costs:#遍历所有节点
cost = costs[node]#获取节点消费
if cost new_cost: #如果单独到达a和fin的开销大于从b走的开销
costs[n] = new_cost #就更新到达这一点的开销的为更小的从b走的记录代替直接走到a的原来的记录
parents[n] = node #将a的父节点设为b节点
processed.append(node)
node = find_lowest_cost_node(costs)
贪心法,又称貪心演算法、貪婪演算法、或稱貪婪法,是一种在每一步选择中都采取在当前状态下最好或最优(即最有利)的选择,从而希望导致结果是最好或最优的算法
# -*- coding: utf-8 -*-
#贪婪算法
# 包含需要覆盖的州的列表,用集合表示,不包含重复元素
states_needed = set(["mt","wa","or","id","nv","ut","ca","az"])
#可供选择的广播台清单,用散列表表示
stations = {}
stations["kone"] = set(["id","nv","ut"])
stations["ktwo"] = set(["wa","id","mt"])
stations["kthree"] = set(["or","nv","ca"])
stations["kfour"] = set(["nv","ut"])
stations["kfive"] = set(["ca","az"])
print stations
# 使用一个集合来存储最终的广播台
final_stations = set()
while states_needed:
best_station = None
states_covered = set()#被选中的广播台覆盖的州,遍历完一遍,没有清空states_needed的话就会重走一遍,重置states_covered
# 遍历出每个台覆盖的州
for station, states in stations.items():
print "遍历到的台%s" %station
covered = states_needed & states #看这个台覆盖的州和需要覆盖的州里面重合的
print "这个台覆盖的州和需要覆盖的州里面重合的%s" %covered
if len(covered)>len(states_covered):
print "重合的比上一个包含的states_covered多的时候"
best_station = station
print "上次states_covered为%s" % states_covered
states_covered = covered
print "更新states_covered为%s" %states_covered
states_needed -= states_covered
print "还没覆盖的州%s"%states_needed
final_stations.add(best_station)
print "已经选出的台%s" % best_station
print final_stations
#定义栈结构,根据栈的后进先出特性,增加辅助栈,来存储当前状态下数据栈中的最小、最大元素。
class Stack(object):
def __init__(self):
self.data = []
self.minValue = []
self.maxValue = []
def push(self,data):
self.data.append(data)
if len(self.minValue)==0:
self.minValue.append(data)
else:
if data <= self.minValue[-1]:
self.minValue.append(data)
if len(self.maxValue)==0:
self.maxValue.append(data)
else:
if data>=self.maxValue[-1]:
self.maxValue.append(data)
def pop(self):
if len(self.data)==0:
return None
else:
temp = self.data.pop()
if temp == self.minValue[-1]:
self.minValue.pop()
if temp == self.maxValue[-1]:
self.maxValue.pop()
return temp
def min(self):
if len(self.data)==0:
return None
else:
return self.minValue[-1]
def max(self):
if len(self.data)==0:
return None
else:
return self.maxValue[-1]
def show(self):
print("stack data")
for data in self.data:
print(data)
print("min",self.min())
print("max",self.max())
if __name__ == "__main__":
s = Stack()
s.push(2)
s.push(1)
s.show()
s.push(4)
s.push(3)
s.push(2)
s.show()
s.pop()
s.show()
s.pop()
s.show()
leetcode习题的答案
python3版本 easy level
https://leetcode.com/problems/two-sum/description/
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
dict = {}
for i in range(len(nums)):
x = nums[i]
if target-x in dict:
return (dict[target-x],i)
dict[x] = i
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
dict_temp = dict()
for index, value in enumerate(nums):
if target - value in dict_temp:
return [dict_temp[target - value], index]
dict_temp[value] = index
https://leetcode.com/problems/reverse-integer/description/
class Solution:
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
fu = None
new_str_x = str(x)
if new_str_x[0]=="-":
fu = "-"
new_str_x = new_str_x[1:]
reversal = new_str_x[::-1]
finallres = fu+reversal if fu else reversal
finallres = int(finallres)
if finallres >= 2147483648 or finallres<= -2147483648:
return 0
return finallres
class Solution:
def reverse(self, x: int) -> int:
fu = ''
str_x = str(x)
if str_x.startswith('-'):
str_x = str_x[1:]
fu = '-'
str_x = str_x[::-1]
if int(str_x)>=pow(2,31):
return 0
return int(fu+str_x)
https://leetcode.com/problems/palindrome-number/description/
class Solution:
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x<0:
return False
elif x%10==0 and x>0:
return False
right = 0
while x>right:
right = right*10+x%10
x=x//10
if x==right or x==right//10:
return True
else:
return False
class Solution:
def isPalindrome(self, x: int) -> bool:
if x < 0:
return False
if len(str(x))<3:
if len(str(x))==1:
return True
if len(str(x))==2:
if str(x)[0]==str(x)[1]:
return True
index = len(str(x)) // 2
if len(str(x))%2:
if str(x)[0:index] == str(x)[index+1:][::-1]:
return True
if str(x)[0:index] == str(x)[index:][::-1]:
return True
return False
https://leetcode.com/problems/roman-to-integer/description/
class Solution:
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
map_dict = dict(I=1,V=5,X=10,L=50,C=100,D=500,M=1000)
map_dict_new = dict(IV=4,IX=9,XL=40,XC=90,CD=400,CM=900)
sum_list=[]
for i in map_dict_new:
if i in s:
s = s.replace(i,"")
sum_list.append(map_dict_new.get(i))
if s:
for j in s:
sum_list.append(map_dict.get(j))
return sum(sum_list)
更好的解题思路:将罗马数字转换成对应的整数。首先将罗马数字翻转,从小的开始累加,如果遇到CM(M-C=1000-100=900)这种该怎么办呢?因为翻转过来是MC,M=1000先被累加,所以使用一个last变量,把M记录下来,如果下一个数小于M,那么减两次C,然后将C累加上,这个实现比较巧妙简洁。
class Solution:
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
numerals = {"M": 1000, "D": 500, "C": 100, "L": 50, "X": 10, "V": 5, "I": 1}
sum = 0
s = s[::-1]
last=None
for x in s:
if last and numerals[x]
https://leetcode.com/problems/longest-common-prefix/description/
class Solution:
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if len(strs) == 0:
return ''
if len(strs) == 1:
return strs[0]
minstrslen = 9999
index = 0
for i in range(0, len(strs)):
if len(strs[i]) < minstrslen:
minstrslen = len(strs[i])
index = i
ShortestString = strs[index]
list = [0 for i in range(len(ShortestString))]
for i in range(0, len(ShortestString)):
for j in range(0, len(strs)):
if strs[j][i] == ShortestString[i]:
list[i] += 1
Prefix = ''
for i in range(0, len(ShortestString)):
# 有多少个数,在这一位具有相同前缀,如果有len(strs)在这一位具有相同前缀,则这一位一定是公共前缀子串
if list[i] == len(strs):
Prefix += ShortestString[i]
else:
break
return Prefix
https://leetcode.com/problems/valid-parentheses/
class Solution:
# @return a boolean
def isValid(self, s):
stack = []
for i in range(len(s)):
if s[i] == "(" or s[i] == "{" or s[i] == "[":
stack.append(s[i])
if s[i] == ")":
if stack == [] or stack.pop() != "(":
return False
if s[i] == "}":
if stack == [] or stack.pop() != "{":
return False
if s[i] == "]":
if stack == [] or stack.pop() != "[":
return False
if stack:
return False
else:
return True
https://leetcode.com/problems/merge-two-sorted-lists
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if l1 == None:
return l2
if l2 == None:
return l1
dummy = ListNode(0)
tmp = dummy
while l1 and l2:
if l1.val <= l2.val:
tmp.next = l1
l1 = l1.next
tmp = tmp.next
else:
tmp.next = l2
l2 = l2.next
tmp = tmp.next
if l1 == None:
tmp.next = l2
if l2 == None:
tmp.next = l1
return dummy.next
https://leetcode.com/problems/remove-duplicates-from-sorted-array/
class Solution:
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 0:
return 0
else:
j=0
for i in range(len(nums)):
if nums[i] != nums[j]:
j=j+1
nums[i],nums[j] = nums[j],nums[i]
return j+1
https://leetcode.com/problems/remove-element/
class Solution:
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
j=len(nums)-1
for i in range(len(nums)-1,-1,-1):
if nums[i] == val:
nums[i],nums[j] = nums[j],nums[i]
j=j-1
return j+1
https://leetcode.com/problems/remove-element/
class Solution:
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
key = None
needle_length = len(needle)
haystack_length = len(haystack)
if needle_length==0:
return 0
if haystack == needle:
return 0
if needle in haystack:
for i in range(len(needle)):
for j in range(len(haystack)):
if needle[i]==haystack[j]:
if needle == haystack[j:int(j+needle_length)]:
key = j
return key
if key == 0:
return 0
if key is None:
return -1
or
class Solution:
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
return haystack.find(needle)
https://leetcode.com/problems/search-insert-position/
class Solution:
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
def func(nums_list,zk):
if len(nums_list)==1:
if target > nums_list[0]:
zk.append(nums.index(nums_list[0])+1)
return zk
if target < nums_list[0]:
zk.append(nums.index(nums_list[0])-1)
return zk
middle_index = len(nums_list)//2
before_list = nums_list[0:middle_index]
after_list = nums_list[middle_index:]
if nums_list[middle_index] > target:
func(before_list,zk)
if nums_list[middle_index] < target:
func(after_list,zk)
return zk[0]
zk = []
if target in nums:
return nums.index(target)
elif target > nums[-1]:
return len(nums)
elif target < nums[0]:
return 0
else:
return func(nums,zk)
https://leetcode.com/problems/length-of-last-word/
给定一个仅包含大小写字母和空格 ’ ’ 的字符串,返回其最后一个单词的长度。
如果不存在最后一个单词,请返回 0 。
说明:一个单词是指由字母组成,但不包含任何空格的字符串。
class Solution:
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
a = s.split(" ")
if len(a)<1:
return 0
else:
i = -1
if a[i]:
return len(a[i])
else:
while not a[i]:
if i<=-len(a):
return 0
i = i-1
if a[i]:
return len(a[i])
# 精简方法
class Solution:
def lengthOfLastWord(self, s):
return len(s.split()[-1]) if s.split() else 0
https://leetcode.com/problems/plus-one/
给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。
最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。
你可以假设除了整数 0 之外,这个整数不会以零开头。
class Solution:
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
digits = reversed(digits)
temp=None
for k,v in enumerate(digits):
if k==0:
temp = v
else:
temp = temp+(pow(10,k)*v)
temp = temp+1
temp = [int(i) for i in str(temp)]
return temp
https://leetcode.com/problems/add-binary/
给定两个二进制字符串,返回他们的和(用二进制表示)。
输入为非空字符串且只包含数字 1 和 0。
class Solution:
def addBinary(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
temp = 0
for index,value in enumerate(reversed(a)):
if value == "1":
temp+=pow(2,int(index))
for index,value in enumerate(reversed(b)):
if value == "1":
temp+=pow(2,int(index))
return bin(temp)[2:]
https://leetcode.com/problems/sqrtx/
实现 int sqrt(int x) 函数。
计算并返回 x 的平方根,其中 x 是非负整数。
由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。
class Solution:
def mySqrt(self, x):
"""
:type x: int
:rtype: int
"""
import math
return math.floor(math.sqrt(x))
class Solution:
def mySqrt(self, x):
"""
:type x: int
:rtype: int
"""
return int(x**0.5)
二分法
class Solution:
def mySqrt(self, x):
"""
:type x: int
:rtype: int
"""
low = 0
high = x
mid = int((low+high)/2)
while low<=high:
if mid*mid == x:
return mid
elif mid * mid >x:
high = mid-1
else:
low = mid+1
mid = int((low+high)/2)
return mid
https://leetcode.com/problems/climbing-stairs/
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
注意:给定 n 是一个正整数。
class Solution:
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
a=0
b=1
while n>=1:
a,b=b,a+b
n-=1
return b
https://leetcode.com/problems/remove-duplicates-from-sorted-list/
给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
示例 1:
输入: 1->1->2
输出: 1->2
示例 2:
输入: 1->1->2->3->3
输出: 1->2->3
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplicates(self, head: 'ListNode') -> 'ListNode':
if not head:
return head
temp_list= [head.val]
new_line = ListNode(head.val)
new_line_head = new_line
while head and head.next:
if head.next.val not in temp_list:
temp_list.append(head.next.val)
new_line.next = head.next
new_line = new_line.next
head = head.next
else:
head.next = head.next.next
return new_line_head
更好的方法
class Solution:
def deleteDuplicates(self, head: 'ListNode') -> 'ListNode':
if not head or not head.next:
return head
p = head
while head.next:
if head.val == head.next.val:
head.next = head.next.next
else:
head= head.next
return p
https://leetcode.com/problems/remove-duplicates-from-sorted-list/
给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组。
说明:
初始化 nums1 和 nums2 的元素数量分别为 m 和 n。
你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素。
示例:
输入:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3
输出: [1,2,2,3,5,6]
注意是修改原nums1而不是返回新的
class Solution:
def merge(self, nums1: 'List[int]', m: 'int', nums2: 'List[int]', n: 'int') -> 'None':
"""
Do not return anything, modify nums1 in-place instead.
"""
while m
https://leetcode.com/problems/same-tree/
给定两个二叉树,编写一个函数来检验它们是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
采用递归方法,首先判断两个根节点的是否相同,如果相同,递归判断根的左右子树,有相同的继续递归,直到没有了就整个都相同,发现有不同的就返回false
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSameTree(self, p: 'TreeNode', q: 'TreeNode') -> 'bool':
if not p and not q:return True
if p and q and p.val == q.val:
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right,q.right)
return False