
算法
Chauncy__xu
要努力,要有趣
展开
-
59. 螺旋矩阵 II Spiral Matrix II(Python3)
1. 题目给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。示例 1:输入:n = 3输出:[[1,2,3],[8,9,4],[7,6,5]]示例 2:输入:n = 1输出:[[1]]2. 解题思路与官方的答案不太一样,我还是用的昨天的递归的思路,就是一圈一圈地考虑这个矩阵地元素,一直向内递归,用递归非常需要注意的就是递归的终止条件from typing import Listclass Solu原创 2021-03-16 22:37:11 · 218 阅读 · 0 评论 -
54. 螺旋矩阵 Spiral Matrix (Python3)
1. 题目给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。示例 1:输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]输出:[1,2,3,6,9,8,7,4,5]示例 2:输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]输出:[1,2,3,4,8,12,11,10,9,5,6,7]提示:m == matrix.lengthn == matrix[i].length原创 2021-03-15 21:00:16 · 256 阅读 · 0 评论 -
706. Design HashMap
'''今天的题目是设计一个哈希表'''class MyHashMap: def __init__(self): """ Initialize your data structure here. """ self.hash_buckets = 1001 self.hash_table = [[] for _ in range(1001)] def hash(self, key):原创 2021-03-14 22:08:22 · 124 阅读 · 0 评论 -
705. 设计哈希集合 Design HashSet
'''题目要求设计的一个哈希集合'''class MyHashSet: def __init__(self): """ Initialize your data structure here. """ self.buckets = 1001 # 这里用了最简单的求哈希值的方法 # 就是用一个数对一个质数取模,这个质数设置为1001 self.a_list = [[] for _ i原创 2021-03-13 22:45:49 · 136 阅读 · 0 评论 -
Python实现树结构的两种方式
实现树结构的经典方法是:嵌套列表法这里把我踩的坑记录一下'''使用嵌套列表法实现树的基本功能'''def BinaryTree(r): return [r, [], []]def insertLeft(root, newBranch): t = root.pop(1) if len(t) > 1: # 如果左子树非空,就把原来根节点上的左子树作为新的根的左节点的左子树 root.insert(1, [newBranch, t, [原创 2021-03-12 19:20:41 · 7036 阅读 · 0 评论 -
LeetCode算法题:基本计算器 Basic Calculator II
题目:Given a string s which represents an expression, evaluate this expression and return its value.The integer division should truncate toward zero.Example 1:Input: s = “3+2*2”Output: 7class Solution: def calculate(self, s: str) -> int:原创 2021-03-11 21:20:40 · 209 阅读 · 0 评论 -
LeetCode算法题:基本计算器 Basic Calculator
题目描述:Given a string s representing an expression, implement a basic calculator to evaluate it.Example 1:Input: s = “1 + 1”Output: 2Example 2:Input: s = " 2-1 + 2 "Output: 3Example 3:Input: s = “(1+(4+5+2)-3)+(6+8)”Output: 23Constraints:1 <=原创 2021-03-10 16:56:49 · 260 阅读 · 0 评论 -
求最短路的Dijkstra算法matlab源代码
直接给出代码function [cost,path]=Dijkstra(graph,sou,des)% 无向图的最短路径算法%获得城市的个数[~,num_city]=size(graph);%路径向量用于存放最优路径path_g=zeros(1,num_city);path_g(1,:)=graph(sou,:);min_index=sou;min_value=0;path(1,1)=min_index;count=2;all_min_index(1,1)=min_index;a原创 2020-05-29 15:04:00 · 1099 阅读 · 0 评论 -
分治之求逆序对数
分治求逆序对数用分治求逆序对数,其思想与归并排序差不多,具体的我们可以看一下伪代码第一步还是把一个大的数组进行分割,直至不能再分为止,这样的话总的逆序对数就可以分为三部分,左边数组的逆序对数、右边数组的逆序对数、左右数组交叉形成的逆序数对数。所以最后的结果是把他们三个加起来。第一步的伪代码:Count_Sort(A) Divide A into two sub_array L and...原创 2020-01-06 23:05:24 · 1063 阅读 · 1 评论 -
分治之归并排序
归并排序体现了比较典型的分治思想该算法理解起来比较简单,总共可以分为两步来看,第一步就是把一个大的数组分为两个规模小一点的数组,并且递归进行,把数组不断减小,直到只剩下一个元素。贴一下伪代码:Merge_Sort(A,left,right) p=(left+right)/2 Mergr_Sort(A.left,p) Merge_Sort(A,p,right) M...原创 2020-01-06 22:12:33 · 210 阅读 · 0 评论 -
分治之快速排序
分治之快速排序复习了一下快速排序,又忘了,这次加深一下印象。首先看一下快排的实现过程 利用分治的思想,把一个数字一分为二,接着对子数组进行同样的操作,直到数组小到我们可以轻易进行排序为止。贴一下伪代码Quick_sort(A,left,right) if left<right then p=Partition(A,left,right) ...原创 2020-01-06 21:40:10 · 191 阅读 · 0 评论 -
分治:旋转数组找最小元素
Suppose an array sorted in ascending order is rotated at some pivot unknown to you be- forehand. (i.e., [0, 1, 2, 4, 5, 6, 7] is an ascending array, then it might be rotated and become [4, 5, 6, 7, 0,...原创 2019-12-23 00:04:41 · 149 阅读 · 0 评论 -
求两个字符串的最长公共子序列(LCS)
求两个字符串的最长公共子序列(Longest common subsequence)Given two sequences.Find the length of the longest common subsequence(LCS)Note: A subsequence is different from a substring ,for the former need NOT be cons...原创 2019-12-10 22:04:09 · 1125 阅读 · 0 评论 -
动态规划之棋盘(简单版)
动态规划之棋盘(简单版)题目描述: 一个机器人从4*4的棋盘的左上角走到右下角,每次走一个,且只能向下或向右,并且要求不能连续向下或向右走,每个格子有自己的权值,请问怎样走权值总和最小?题目分析:这个题目是一个简单版本,因为题目要求了机器人不能连续向下或者向右走,所以机器人走的时候不会偏离对角线两步,我们看图:所以机器人一直在蓝色区域里走,所以我们维持一个一维数组保存走到每一步的最小...原创 2019-12-02 00:30:39 · 757 阅读 · 0 评论 -
贪心算法之股票买卖问题(Best Time to Buy and Sell Stocks)
这道题目应该是股票买卖问题里面比较简单的一道问题描述: Problem Description 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 profit. You may com...原创 2019-11-28 12:21:35 · 783 阅读 · 1 评论 -
贪心算法之猴子吃香蕉(Monkeys and Bananas)
贪心算法之猴子吃香蕉(Monkeys and Bananas) 这道题是比较简单的贪心算法的题,首先我们来看题目:问题描述**Problem Description**Given an array of size n that has the following specifications:Each element in the array contains either a monk...原创 2019-11-28 11:28:27 · 5082 阅读 · 1 评论