Q:两个有序数组合并成一个有序数组
def merge_sort(a, b):
ret = []
i = j = 0
while len(a) >= i + 1 and len(b) >= j + 1:
if a[i] <= b[j]:
ret.append(a[i])
i += 1
else:
ret.append(b[j])
j += 1
if len(a) > i:
ret += a[i:]
if len(b) > j:
ret += b[j:]
return ret
if __name__ == '__main__':
a = [1,3,4,6,7,78,97,190]
b = [2,5,6,8,10,12,14,16,18]
print(merge_sort(a, b))
Q:如何判断一串数字是回文数字
A:算法:
首先我们要处理一些特殊情况,负数不可能是回文数,首先要禁止负数进入。
然后再考虑如何反转数字,例如1221,首先1221%10就可以得到最后一位数字1,然后将1221/10得到122,再次122%10得到倒数第二位数字2,最后用1*10+2=12就得到了我们反转后的数字12,用12和原数字的前两位对比即可判断该数字是不是回文数。
还存在一个问题:我们怎么知道自己反转了一半呢?
我们将原始数字除以 10,然后给反转后的数字乘上 10,所以,当原始数字小于反转后的数字时,就意味着我们已经处理了一半位数的数字。
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x<0 or (x % 10 == 0 and x != 0): return False
y=0
while(x>y):
y=x%10+y*10
x /= 10;
return x==y or x==y/10
Q:冒泡排序
A:冒泡排序的基本思想就是让小的数逐渐‘浮上来’。也就是说:
第一次冒泡:将最小的数调换到最前面;
第二次冒泡:将第二小的数调换到最小的数的后面,也就是数组中的第二位;
第三次冒泡,将第三小的数调换到数组中的第三位;
… …
# 冒泡排序
def bubble_sort(nums):
# 每次冒泡,将最大的元素冒到最后面
# 第一次是前n个元素,最大的元素冒到最后
# 第二次是前n-1个元素,最大的元素冒到倒数第二个位置
# ... ...
n = len(nums)
for i in range(n-1):
for j in range(0,n-i-1):
if nums[j]>nums[j+1]:
nums[j], nums[j+1] = nums[j+1],nums[j]
return nums
时间复杂度: O(n^2),实际上是n-1 + n-2 + n-3 + …,所以是平方的量级。
空间复杂度: O(l),没有借助额外空间。
Q:快速排序
A:
def quick_sort(alist, start, end):
"""
快速排序
:param alist:
:return:
"""
# n = len(alist)
if start >= end:
return
mid = alist[start]
low = start
high = end
while low < high:
# 这里用alist[high] >= mid 而不是alist[high]>mid是为了把所有和中间值相等的都移动到一遍,而不是移来移去
while low < high and alist[high] >= mid:
high -= 1
alist[low] = alist[high]
while low < high and alist[low] < mid:
low += 1
alist[high] = alist[low]
# while low < high:
# while low < high:
# if alist[high] < mid:
# alist[low] = alist[high]
# # 这时候应该移动low了
# break
# # 这个else就是循环移动
# else:
# high -= 1
# while low < high:
# if alist[low] > mid:
# alist[high] = alist[low]
# # 这时候应该移动high了
# break
# else:
# low += 1
alist[low] = mid
quick_sort(alist, start, low)
quick_sort(alist, low+1, end)
Q:求一个数字的阶乘
A:
def fun(n):
if n < 0 :
return -1
if n == 0 :
return 1
return n * fun(n-1)
print(fun(6))
Q:洗牌,52张,从有序输入到无序输出
A:
import random
def func(lst):
l = len(lst)
if l <= 1:
return lst
i =0
while l>1:
p = int(random.random()*l)
lst[i],lst[i+p] = lst[i+p],lst[i]
i += 1
l -= 1
return lst
l = [1,2,3,4,5,6,7,8,9]
print(func(l))
Q:一个数组nums,目标值target,写一个程序,求nums中的两个元素加和等于target,并输出索引值
A:
def func(lst):
l = len(lst)
i = 0
for i in range(l):
num1 = lst[i]
j=i+1
for j in range(j,l):
if num1+lst[j] == 5:
print(i,j)
lst = [1,2,3,3,3,3,3,3]
func(lst)
Q:一个字符串str=’±q23’,取出数字
A:
import re
totalCount = "+_q123"
totalCount = re.sub("\D", "", totalCount)
print(totalCount)
Q:输出一个文件中,出现频率最高的前n个单词
A:
转载https://blog.csdn.net/mydistance/article/details/85220979
Q:查看一个文件夹下文件的个数
A:
import os
for dirpath, dirnames, filenames in os.walk("./data/NDBPic_Ehance"):
file_count = 0
for file in filenames:
file_count = file_count + 1
print(dirpath,file_count)
Q:多个字典相加
A:
一、适用合并两个字典(key不能相同否则会被覆盖),简单,好用。
A = {'a': 11, 'b': 22}
B = {'c': 48, 'd': 13}
#update() 把字典B的键/值对更新到A里
A.update(B)
print(A)
二、适用多种场合,多字典存在相同key需要合并相加的场景比较适用
def sum_dict(a,b):
temp = dict()
# python3,dict_keys类似set; | 并集
for key in a.keys()| b.keys(): #根据业务需求修改下面方法,
temp[key] = sum([d.get(key, 0) for d in (a, b)])
return temp
def test():
#python3使用reduce需要先导入
from functools import reduce
#[a,b,c]列表中的参数可以2个也可以多个,自己尝试。
return print(reduce(sum_dict,[a,b,c]))
a = {'a': 1, 'b': 2, 'c': 3}
b = {'a':1,'b':3,'d':4}
c = {'g':3,'f':5,'a':10}
test()
三、解包法和第一种效果(限制)一样,B吃掉A。
A = {'a': 11, 'b': 22}
B = {'a': 48, 'b': 13}
print({**A,**B})