![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法笔记
文章平均质量分 61
Peggy_Chang
人工智能挖坑者
展开
-
堆和堆排序
堆与队列基本概念普通队列: FIFO 优先队列:有优先级的队列,可以用堆来实现。 二叉堆:最大堆MaxHeap满足父节点的值大于任一子节点,最小堆MinHeap反之。且二叉堆必须是一个完全二叉树,这个性质让它可以用数组来实现。堆的基本存储由于有完全二叉树的性质,我们把它们按层序遍历的方式编号,根节点是1(不是0!),以此类推。设某节点的编号为k,则它的parent = k/...原创 2018-03-14 23:15:32 · 191 阅读 · 0 评论 -
算法笔记-快速排序(Quick Sort)之Duplicate Keys
问题描述: 当数组中有很多重复的元素(Dupicate Keys)时,采用3-way partitioning,即把array分成三部分,一部分是小于duplicate keys的,一部分是等于duplicate keys,还有一部分是大于duplicate keys。当然,一轮partition只能让一种duplicate keys归位,如果其他数字也有duplicate的情况,则需要进行下一...原创 2018-03-05 21:46:48 · 574 阅读 · 0 评论 -
[Leetcode] 88. Merge Sorted Array
Description: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) t...原创 2018-03-12 11:07:35 · 356 阅读 · 0 评论 -
[Leetcode] 75. Sort Colors
Description: Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use t...原创 2018-03-11 21:00:16 · 188 阅读 · 0 评论 -
算法笔记-快速排序(Quick Sort)之Selection
Quick-Selection问题描述: Goal: 给了一个有N个元素的数组,找出最大的第K个元素。(或返回最大or最小的K个) Ex: Min(k = 0), Max(k = N-1), Medium(k = N/2)思路: 给数组Partition(如果是返回最大or最小的K个元素不一定要排序,因为没要求返回排好序的K个数,但Partition用的是Quick Sort的),找到第...原创 2018-03-04 12:46:37 · 208 阅读 · 0 评论 -
BST/二叉树层序遍历
利用队列的FIFO特性来实现层序遍历。初始化:创建队列q,root入队。只要队列不为空:存队首元素,把队首元素pop出来,打印队首元素。判断它是否有左孩子,如果有,则左孩子入队;判断是否有右孩子,如果有,则右孩子入队(先判断左孩子再右孩子是因为这里一层中遍历假定从左到右)。有点像利用栈的前序遍历,但是栈是LIFO,因此假如要进行前序遍历,则入栈的时候是先判断并入右孩子”go” comm...原创 2018-03-17 15:00:17 · 641 阅读 · 0 评论 -
[Leetcode]144, 94, 145二叉树前中后序遍历的非递归实现
144. Binary Tree Preorder TraversalDescription: Given a binary tree, return the preorder traversal of its nodes’ values.For example: Given binary tree [1,null,2,3], 1 \ 2 / ...原创 2018-03-17 14:26:20 · 252 阅读 · 0 评论 -
算法笔记-快速排序(Quick Sort)
看了教科书上的快排,又对比了“Algorithm 4th”里的快排,感觉后者更加简洁清晰,而且成功cover极端情况,故决定采用“Algorithm 4th”里面的快排代码。 算法思路: 一轮 QuickSort Partition有两个phase: phase 1:重复步骤直到i, j指针交叉从左向右扫描i指针(只要满足a[i]<a[lo],当然,一开始把a[lo]作为pivo...原创 2018-03-03 23:30:28 · 292 阅读 · 0 评论 -
[Leetcode] 80. Remove Duplicates from Sorted Array II
Description: Follow up for “Remove Duplicates”: What if duplicates are allowed at most twice? 由于follow up Problem 26,因此还是要求in-place。Example: Given sorted array nums = [1,1,1,2,2,3], 仍旧是排好序的数组 ...原创 2018-03-11 10:46:16 · 147 阅读 · 0 评论 -
[Leetcode] 26. Remove Duplicates from Sorted Array
Description: Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this b...原创 2018-03-11 00:36:05 · 138 阅读 · 0 评论 -
[Leetcode] 27. Remove Element
Description: Given an array and a value, remove all instances of that value in-place and return the new length.Do not allocate extra space for another array, you must do this by modifying the input...原创 2018-03-10 17:13:26 · 148 阅读 · 0 评论 -
链表--穿针引线
链表基础原创 2018-03-15 19:23:34 · 260 阅读 · 0 评论