![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
数据结构—算法
一米半
这个作者很懒,什么都没留下…
展开
-
冒泡排序
#!/usr/bin/env python3# -*- coding: utf-8 -*-' 冒泡排序 n个 从 0 到 n-1 把最大的排上去, 在从 0到 n-2排出最大,直到全部排序。'__author__ = 'Zhang Shuai's = [23, 423, 23, 42, 54, 23, 12]def maopao(s): i = len(s)-1 whi...原创 2017-06-01 19:34:23 · 170 阅读 · 0 评论 -
二分查找
#!/usr/bin/env python3# -*- coding: utf-8 -*-' a test module '__author__ = 'Zhang Shuai'a = [2, 34, 45, 56, 57, 68, 78, 82, 86, 90]def binary_search(a, x, i, j): if i > j: return "don't原创 2017-07-16 16:47:12 · 262 阅读 · 0 评论 -
最少找零问题
假设硬币金额为1,3,9,10,14等,找零19,如果正常找零用贪心算法,我们需要14,3,1,1,4个硬币,但是其实最优解为10,9两个硬币。 所以问题可以考虑成:min(coin(n-i)+1),其中n为需要找的总额,i为各个硬币的面值,i必须小于等于n采取计算,coin(n)代表总额为n最少要找的硬币数,当n-i等于0是,需要找0个硬币。from functools import wraps原创 2017-07-14 13:16:34 · 782 阅读 · 0 评论 -
python 斐波那契数列缓存方式实现
#!/usr/bin/env python3# -*- coding: utf-8 -*-' a test module '__author__ = 'Zhang Shuai'from functools import wrapsimport timedef memory(func): l = {} @wraps(func) def wrap(n):原创 2017-07-13 16:06:15 · 1061 阅读 · 0 评论 -
python 实现堆
堆:是一个完全二叉树,有大根堆跟小根堆的区别。 大根堆最大数在根节点(即最上方),他的子树也都是大根堆。小根堆同理,最小数在根节点。 堆排序:只用把根节点取出,再把最后一个节点放入根节点的位置,然后进行排序,排序的方式是(比如大根堆),比较新的根节点跟它的两个子节点的大小,如果比它们其中一个小就跟它互换位置(如果比两个子节点都小则跟最小的那个换),然后去跟新的子节点比较(即递归)。 可以看一...原创 2018-03-22 22:46:30 · 1489 阅读 · 0 评论 -
斐波那契 矩阵计算
原理: import numpy as npclass Fibo(): def __init__(self): self.T = np.array([[1],[0]]) self.A = np.array([[1,1],[1,0]]) def compute(self,num): if num ==1: ...原创 2018-04-02 10:44:27 · 637 阅读 · 0 评论 -
归并排序
算法思想就是,把数组每次拆成两半,然后递归继续拆,知道数组长度<=1,这样时间复杂度为o(logn), 然后两两合并,比较大小,然后装进临时数组里面,因为两个数组里面的元素只要比较一次,所以时间复杂度为o(n),所以整体的时间复杂度为o(nlogn)。 代码:class MergeSort(): def __init__(self,s): self.mer...原创 2018-04-11 18:07:24 · 139 阅读 · 0 评论 -
买卖股票的最佳时机 II
转自:https://www.cnblogs.com/peterzone/p/9097509.html 题目:给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。示例 1:输入: [7,1,5,3,6,4] ...转载 2018-06-07 09:56:47 · 294 阅读 · 0 评论 -
Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock),...原创 2018-06-07 10:26:36 · 141 阅读 · 0 评论 -
栈 python
#!/usr/bin/env python3# -*- coding: utf-8 -*-' a test module '__author__ = 'Zhang Shuai'class Node(): def __init__(self,value=None,next=None): self.next = next self.value = value原创 2017-11-08 10:41:02 · 264 阅读 · 1 评论 -
0-1背包问题 python
原理:当前物品重量wi大于当前背包容量w的时候,直接去前一个物品在背包容量为w时的最大价值,否则取前一个物品在w时的最大价值 和 前一个物品在w-wi的最大价值加上当前物品vi的价值 中最大的价值。#!/usr/bin/env python3# -*- coding: utf-8 -*-' a test module '__author__ = 'Zhang Shuai'...原创 2017-07-03 10:18:17 · 3625 阅读 · 0 评论 -
leetcode:Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it原创 2017-09-06 23:32:29 · 242 阅读 · 0 评论 -
快排
#!/usr/bin/env python3# -*- coding: utf-8 -*-' 快排 选取s[0]为哨兵,i等于0,j等于n-1,从n-1开始j--找到比s[0]小的插入s[i],然后i++ 找到比s[0]大的插入到s[j],直到 i=j,然后对两侧继续循环排序'__author__ = 'Zhang Shuai'def quick_sort(s, l, r):原创 2017-06-01 19:37:52 · 352 阅读 · 0 评论 -
选择排序
#!/usr/bin/env python3# -*- coding: utf-8 -*-' a test module '__author__ = 'Zhang Shuai's = [2, 4, 45, 23, 65, 234, 3, 123, 455, 26, 87, 96, 34]def xuanze(L): for i in range(len(L)):原创 2017-06-19 15:45:09 · 193 阅读 · 0 评论 -
python实现二叉树,前序遍历,中序遍历,后续遍历
#!/usr/bin/env python3# -*- coding: utf-8 -*-' a test module '__author__ = 'Zhang Shuai'class Node(): def __init__(self, data=-1, lchild=None, rchild=None): self._data = data ...原创 2017-07-01 20:34:41 · 1354 阅读 · 0 评论 -
链表倒置
class Node(): def __init__(self, data=None, next=None): self.data = data self.next = nextroot = Node(1, Node(2, Node(3, Node(4, Node(5, None)))))def reverse(root): if root ==N原创 2017-07-14 13:49:46 · 236 阅读 · 0 评论 -
数据结构 两个链表合并-python
#!/usr/bin/env python3# -*- coding: utf-8 -*-' a test module '__author__ = 'Zhang Shuai'class Node(): def __init__(self, d, p): self.next = p self.data = da = Node(1, Node(5, Nod原创 2017-06-27 17:28:07 · 876 阅读 · 1 评论 -
最长公共子序列和最长公共子串
子串必须是连续的,子序列可以不连续。 如1234和124,最长子串是12,最长子序列是124。 最长公共子序列:只求子序列长度,先根据两个串建立一个矩阵,如果sub1[i-1] == sub2[j-1],相当于 c[i][j] = c[i-1][j-1] +1,如果不等,则想当于c[i][j] = max(c[i][j-1],c[i-1][j]),当i=0或j=0时c取0。#!/usr/bin原创 2017-07-16 21:16:28 · 232 阅读 · 0 评论 -
在一个有序数列中找到第一个比x大的数的位置
#!/usr/bin/env python3# -*- coding: utf-8 -*-' a test module '__author__ = 'Zhang Shuai'def binary_search(L, x): i = 0 j = len(L)-1 res = j if(L[j]<=x): print( -1...原创 2017-07-18 19:27:04 · 4205 阅读 · 0 评论 -
leetcode:Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same ele原创 2017-09-06 22:18:24 · 250 阅读 · 0 评论 -
Best Time to Buy and Sell Stock III
解题思路来自:https://blog.csdn.net/u012501459/article/details/46514309 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profi...原创 2018-06-09 17:40:47 · 214 阅读 · 0 评论