算法与数据结构
文章平均质量分 70
Peggy_Chang
人工智能挖坑者
展开
-
从入门到精通我是这样学习算法的
这篇文章讲了什么? 我这些年学习数据结构和算法的总结。一些不错的算法书籍和教程。算法的重要性。初学第一次接触数据结构是在大二下学期的数据结构课程。然而这门课程并没有让我入门——当时自己正忙于倒卖各种MP3和耳机,对于这些课程根本就不屑一顾——反正最后考试划个重点也能过,于是这门整个计算机专业本科最重要的课程就被傻逼的我直接忽略过去了。直到大三我才反应过来转载 2015-12-25 11:28:07 · 488 阅读 · 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 · 147 阅读 · 0 评论 -
[Leetcode] 771. Jewels and Stones
Description: You’re given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how m...原创 2018-02-23 23:47:38 · 307 阅读 · 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 · 134 阅读 · 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 · 145 阅读 · 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 · 288 阅读 · 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 · 250 阅读 · 0 评论 -
BST/二叉树层序遍历
利用队列的FIFO特性来实现层序遍历。初始化:创建队列q,root入队。只要队列不为空:存队首元素,把队首元素pop出来,打印队首元素。判断它是否有左孩子,如果有,则左孩子入队;判断是否有右孩子,如果有,则右孩子入队(先判断左孩子再右孩子是因为这里一层中遍历假定从左到右)。有点像利用栈的前序遍历,但是栈是LIFO,因此假如要进行前序遍历,则入栈的时候是先判断并入右孩子”go” comm...原创 2018-03-17 15:00:17 · 639 阅读 · 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 · 206 阅读 · 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 · 185 阅读 · 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 · 355 阅读 · 0 评论 -
[Leetcode] 215. Kth Largest Element in an Array
Description: Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example, Given [3,2,1,5,6,4] and k = 2...原创 2018-03-05 19:14:14 · 148 阅读 · 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 · 569 阅读 · 0 评论 -
排序算法大总结
选择排序思路:从最左边的元素开始,逐个遍历,找到这一趟中的最小值,存到minIndex里面,最后这一趟结束,将它和这一趟开始遍历的位置i上的元素swap,直到最后一个位置上也就位。相当于选择出每一趟最小的元素,然后把它放到这一趟开始的地方。基础版:void selectionSort(int arr[], int n){ for(int i = 0 ; i &...原创 2018-03-13 21:51:59 · 236 阅读 · 0 评论 -
[Leetcode] 125. Valid Palindrome
Description: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example, “A man, a plan, a canal: Panama” is a palindrome. “race a ca...原创 2018-03-13 23:53:15 · 163 阅读 · 0 评论 -
[Leetcode] 561. Array Partition I
Description: Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as l...原创 2018-02-23 19:48:23 · 143 阅读 · 0 评论 -
[Leetcode] 1. Two Sum
Description Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the ...原创 2018-02-23 19:36:46 · 148 阅读 · 0 评论 -
递归算法详细分析--C
转自:http://www.cnblogs.com/zhangqqqf/archive/2008/09/12/1289730.html C通过运行时堆栈支持递归函数的实现。递归函数就是直接或间接调用自身的函数。许多教科书都把计算机阶乘和菲波那契数列用来说明递归,非常不幸我们可爱的著名的老潭老师的《C语言程序设计》一书中就是从阶乘的计算开始的函数递归。导致读过这本经书的同学们,看到阶乘计转载 2015-12-26 20:27:05 · 463 阅读 · 0 评论 -
4.10编程笔记
最近在学文件,编一个商品记录的代码,两百多行,尽管过程非常艰辛,但是同时也收获了很多~~~ int main(int argc, char*argv[])是什么意思?什么时候用?请举个例子,谢谢这是C/C++的一重要函数,叫主函数。无论程序多复杂,代码中必须有这么一个函数,也只能有一个这样的函数;程序执行时就是从这个函数进入的。由于问得比较笼统,如果你想知道详细情原创 2016-04-11 00:53:55 · 390 阅读 · 0 评论 -
字符串匹配算法
主要根据《算法导论》总结了以下几种字符串匹配算法朴素字符串匹配算法KMP算法Rabin-Karp算法有限状态自动机原创 2017-01-04 16:31:30 · 326 阅读 · 0 评论 -
树与二叉树的概念
树与二叉树的概念原创 2017-01-07 00:37:26 · 397 阅读 · 0 评论 -
二叉搜索树
二叉搜索树原创 2017-01-07 11:10:50 · 266 阅读 · 0 评论 -
二叉树的遍历
一般二叉树的遍历:先序、中序、后序、按层序遍历求叶子结点个数求二叉树的深度原创 2017-01-07 16:29:14 · 409 阅读 · 0 评论 -
哈希表
概述哈希函数的基本构造方法处理冲突的常用方法哈希表的建立、查找及其ASL分析原创 2017-01-07 21:03:33 · 577 阅读 · 0 评论 -
内部排序
内部排序原创 2017-01-08 16:35:44 · 415 阅读 · 0 评论 -
Algorithm 4th 1.1 基础编程模型
Algorithm 4th 1.1 基础编程模型原创 2017-10-31 20:21:11 · 330 阅读 · 0 评论 -
[Leetcode] 283. Move Zeroes
Description: Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.原创 2018-03-07 10:03:17 · 158 阅读 · 0 评论 -
[Leetcode] 167. Two Sum II - Input array is sorted
Description: Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should return indices of th...原创 2018-03-08 09:08:48 · 144 阅读 · 0 评论 -
[Leetcode] 345. Reverse Vowels of a String
Description: Write a function that takes a string as input and reverse only the vowels of a string.Example 1: Given s = “hello”, return “holle”.Example 2: Given s = “leetcode”, return “leotcede...原创 2018-03-14 19:35:50 · 204 阅读 · 0 评论 -
堆和堆排序
堆与队列基本概念普通队列: FIFO 优先队列:有优先级的队列,可以用堆来实现。 二叉堆:最大堆MaxHeap满足父节点的值大于任一子节点,最小堆MinHeap反之。且二叉堆必须是一个完全二叉树,这个性质让它可以用数组来实现。堆的基本存储由于有完全二叉树的性质,我们把它们按层序遍历的方式编号,根节点是1(不是0!),以此类推。设某节点的编号为k,则它的parent = k/...原创 2018-03-14 23:15:32 · 189 阅读 · 0 评论 -
链表--穿针引线
链表基础原创 2018-03-15 19:23:34 · 254 阅读 · 0 评论 -
[Leetcode] 344. Reverse String
Description: Write a function that takes a string as input and returns the string reversed.Example: Given s = “hello”, return “olleh”.考虑空字符串,也属于reversed string.解法一: 基本思想:对撞指针。 思路:从头从尾分别同时遍历,...原创 2018-03-14 09:56:38 · 152 阅读 · 0 评论