python+算法+数据结构+面试题

一、单链表反转

#!/usr/bin/python
#递归实现单链表反转

class ListNode(object):
  def __init__(self,x):
    self.val=x
    self.next=None


def recurse(head,newhead):  #递归,head为原链表的头结点,newhead为反转后链表的头结点
  if head is None:
    return
  if head.next is None:
    newhead=head
  else :
    newhead=recurse(head.next,newhead)
    head.next.next=head
    head.next=None
  return newhead



if __name__ == '__main__':
    head=ListNode(1)       #测试代码
    p1=ListNode(2)       # 建立链表1->2->3->4->None
    p2=ListNode(3)
    p3=ListNode(4)
    head.next=p1
    p1.next=p2
    p2.next=p3
    newhead=None
    p=recurse(head,newhead)    #输出链表4->3->2->1->None
    while p:
      print(p.val)
      p=p.next

二、字符串倒序

#写一个函数, 输入一个字符串, 返回倒序排列的结果
#1).利用字符串本身的翻转

str = 'abcdef'
# def string_reverse(string):
#     return string[::-1]
#
# if __name__ =="__main__":
#     print(str)
#     print(string_reverse(str))

#2).把字符串变成列表,用列表的reverse函数
# def string_reverse2(string):
#     new_str = list(string)
#     new_str.reverse()
#     return ''.join(new_str)
# if __name__ =="__main__":
#     print(str)
#     print(string_reverse2(str))


#3).新建一个列表,从后往前取
# def string_reverse3(string):
#     new_str = []
#     for i in range(1,len(string)+1):
#         new_str.append(string[-i])
#
#     return ''.join(new_str)
#
# if __name__ =="__main__":
#     print(str)
#     print(string_reverse3(str))

#4).利用双向列表deque中的extendleft函数
# from collections import deque
#
# def string_reverse4(string):
#     d = deque()
#     d.extendleft(string)
#     return ''.join(d)
#
# if __name__ =="__main__":
#     print(str)
#     print(string_reverse4(str))

#5).递归
# def string_reverse5(string):
#     if len(string)<=1:
#         return string
#     else:
#         return string_reverse5(string[1:])+string[0]
#
# if __name__ =="__main__":
#     print(str)
#     print(string_reverse5(str))

三、list去重

# 1.按升序合并如下两个list, 并去除重复的元素
list1 = [2, 3, 8, 4, 9, 5, 6]

list2 = [5, 6, 10, 17, 11, 2]

list3 = list1 + list2

sort_list = sorted(list(set(list3)))

print(sort_list)

四、排序

1.冒泡排序

时间复杂度:O(n2)
#coding:utf-8
#!/usr/bin/python

def bubble_sort(list):
    for i in range(len(list)-1):
        for j in range(len(list)-i-1):
            if list[j]>list[j+1]:
                list[j],list[j+1] = list[j+1],list[j]
    return list

if __name__ == '__main__':
    list = [2,3,5,19,3,5,6,7]
    print(bubble_sort(list))

2.快速排序

时间复杂度:O(nlogn)

版本一:

#!/usr/bin/python3
#-*- coding:utf-8 -*-

def sub_sort(lists,low,high):
    key = lists[low]
    while low < high:
        while low < high and lists[high] >= key:
            high -= 1
            lists[low] = lists[high]
            print("===========")
            print("low=")
            print(low)
            print("high=")
            print(high)
            print(lists)
        while low < high and lists[high] < key:

            low += 1
            lists[high] = lists[low]
            print("===========")
            print("low=")
            print(low)
            print("high=")
            print(high)
            print(lists)
    lists[low] = key
    return low

def quick_sort(array, low, high):
    if low < high:
        key_index = sub_sort(array,low,high)
        #递归进行排序
        quick_sort(array, low, key_index)
        quick_sort(array, key_index+1, high)


if __name__ == "__main__":
    lists = [3, 5, 4, 2, 1, 6]
    print(lists)
    quick_sort(lists, 0, 5)

版本二(自己写):

# coding:utf-8
# /usr/bin/python
import time
import sys

def calc_time(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        print(start_time)
        print(end_time)
        print("%s waster time is %s"%(func.__name__, end_time - start_time))
        return result
    return wrapper



def quick_sort(data, left, right):
    if left < right:
        mid = partition(data, left, right)
        quick_sort(data, left, mid - 1)
        quick_sort(data, mid + 1, right)


def partition(data, left, right):
    tmp = data[left]
    while left < right:
        while left < right and data[right] >= tmp:
            right -= 1
        data[left] = data[right]

        while left < right and data[left] <= tmp:
            left += 1
        data[right] = data[left]
    data[left] = tmp
    return left

@calc_time
def quick_sort_x(data):
    return quick_sort(data, 0, len(data) - 1)


if __name__ == '__main__':
    sys.setrecursionlimit(10000)
    lists = list(range(1000))
    #print(lists)
    quick_sort_x(lists)
    #print(lists)

 

    原文作者:David在学习
    原文地址: https://blog.csdn.net/a1032818891/article/details/80683793
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞