随笔分类 - 算法
摘要:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array[−2,1,−3,4,−1,2,1,...
阅读全文
摘要:You are climbing a stair case. It takesnsteps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb...
阅读全文
摘要:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in or...
阅读全文
摘要:Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3,
阅读全文
摘要:Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointe...
阅读全文
摘要:给定一个二叉树,以集合方式返回其中序/先序方式遍历的所有元素。有两种方法,一种是经典的中序/先序方式的经典递归方式,另一种可以结合栈来实现非递归Given a binary tree, return theinordertraversal of its nodes' values.For examp...
阅读全文
摘要:Given two binarytrees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and...
阅读全文
摘要:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?注意,链表循环并不是尾指针和头指针相同,可能是在中间某一段形成一个环路,所以不能只判...
阅读全文
摘要:Reverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321本地注意正负号判断比较关键,实现部分可能不是最优的,按照自己的想法实现:设ret = 1;每次对x进行取余mod,然后ret ...
阅读全文
摘要:Given a binary tree,find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest lea...
阅读全文
摘要:Given an array of integers, every element appearstwiceexcept for one. Find that single one.Note:Your algorithm should have a linear runtime complexity...
阅读全文
摘要:Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array[2,3,-2,4],the...
阅读全文
摘要:算法的时间复杂度,是刚开始接触算法和数据结构时的概念,在真正使用的时候有时候常常忘记它的推导公式。最近准备校招,把二叉树、排序、查找等这些经典的算法复习了一遍,这次把这些都整理成博客以便以后查看,复习计划接近尾声,这两天老是不在状态,学习图的时候有点晕乎乎,今天反过头来把时间复杂度的求解法整理一...
阅读全文
摘要:二叉排序数或者是一棵空树,或者是一棵具有以下性质的二叉树:(1)若它有左子树,则左子树上所有结点的数据均小于根结点的数据。(2)若它有右子树,则右子树上所有结点的数据均大于根结点的数据。(3)左、右子树本身又各是一棵二叉排序树。这样,在查找的时候,从根节点开始,若查找的元素小于根节点则查找其左子树,...
阅读全文
摘要:•顺序查找 从线性表的一端开始,依次将每个记录的关键字与给定值进行比较,若某个记录的关键字等于给定值,表示查找成功,返回记录序号;若将线性表中所有记录都比较完,仍未找到关键字与给定值相等的记录,则表示查找失败,返回一个失败值。•折半查找 又称为二分查找。这种查找方法要求查找表的数据是线性结构保存...
阅读全文
摘要:前面几篇博客分别罗列和总结了近期学习的几种排序算法,今天总结一下。#排序算法#【1】概述、冒泡排序、选择排序#排序算法#【2】直接插入排序、希尔排序#排序算法#【3】堆排序#排序算法#【4】快速排序#排序算法#【5】合并排序 排序算法有很多,在实际使用的时候需要根据情况选择合适的相应算法。每种...
阅读全文
摘要:ok,尘埃落定。期待工作的气息,越来越浓烈...继续更新博客,排序算法的最后一个——合并排序 合并排序(Merge Sort)是将两个或多个有序表合并成一个有序表。也称为二路合并。 合并排序的基本思想是:对于两个有续表合并初始时,把含有n个节点的待排序序列看做由n个长度为1的有序子表所组成麻...
阅读全文
摘要:快速排序法是对冒泡排序的一种改进,本来是要和冒泡排序写在一个文章里的,不过前两天刚开始在递归调用的时候没有完全理解,昨天晚上google了一把发现原来自己理解错了,我看的这个教材没有写清楚,今天早上调试了一把终于成功。 快速排序算法的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其...
阅读全文
摘要:堆是一个完全二叉树,树中每个结点对应于原始数据的一个记录,并且每个结点应满足以下条件:非叶结点的数据大于或等于其左、右孩子结点的数据(若是按从大到小的顺序排序,则要求非叶结点的数据小于或等于其左、右孩子结点的数据)。由堆的定义可看出,其根结点为最大值,堆排序就是利用这一特点进行的。堆排序过程包括...
阅读全文
摘要:直接插入排序法 插入排序的算法描述是一种简单直观的排序算法。它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。插入排序在实现上,在从后向前扫描过程中,需要反复把已排序元素逐步向后移动,为最新元素提供插入空间。 核心代码://直接插入排序法void I...
阅读全文

浙公网安备 33010602011771号