
算法与数据结构
Garvin Li
Dancing with data
展开
-
【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)
背景(以下背景资料转载自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool)DFS(Dpeth-first Search)顾名思义,就是深度搜索,一条路走到黑,再选新的路。记得上Algorithm的时候,教授举得例子就是说,DFS很像好奇的小孩,你给这个小孩几个盒子套盒子,好奇的小孩肯定会一个盒子打开后继续再在这个原创 2015-04-06 16:57:23 · 1783 阅读 · 0 评论 -
【LeetCode从零单排】No 114 Flatten Binary Tree to Linked List
题目Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \ 2原创 2015-03-16 14:57:08 · 1432 阅读 · 0 评论 -
【LeetCode从零单排】No104 Maximum Depth of Binary Tree
题目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 leaf node.代码/** * Definition for binary tree * p原创 2015-03-04 14:51:02 · 1652 阅读 · 0 评论 -
【LeetCode从零单排】No102 Binary Tree Level Order Traversal
题目 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 /原创 2015-03-03 16:22:03 · 1572 阅读 · 2 评论 -
【LeetCode从零单排】No100 Same Tree && No101 Symmetric Tree
题目1.same treeGiven two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.2.sym原创 2015-03-02 16:10:29 · 1515 阅读 · 0 评论 -
【LeetCode从零单排】No83 Remove Duplicates from Sorted List
题目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, return 1->2->3.代码 public static ListNode deleteD原创 2015-03-02 11:18:50 · 1464 阅读 · 0 评论 -
【LeetCode从零单排】No88.Merge Sorted Array
题目Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. T原创 2015-03-02 09:54:12 · 1400 阅读 · 0 评论 -
【LeetCode从零单排】No58.Length of Last Word
题目Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defined原创 2015-02-15 10:19:37 · 1479 阅读 · 0 评论 -
【LeetCode从零单排】No67.AddBinary
题目Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".代码public class Solution { public String addBinary(String a, String b) { S原创 2015-02-15 16:44:53 · 1322 阅读 · 0 评论 -
【LeetCode从零单排】No38.CountAndSay
题目The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then原创 2015-02-14 18:13:40 · 1415 阅读 · 0 评论 -
【LeetCode从零单排】No112 Path Sum
题目Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and sum =原创 2015-03-04 16:48:47 · 1665 阅读 · 0 评论 -
单链表问题(反转、是否有环、删除结尾第N个节点、合并两个sortlist、找到交点)
1.时间复杂度O(N),内存O(1)的效率下实现单链表的翻转public static TreeNode revers(TreeNode head){ TreeNode temp,first,second; first=head; second=head.next; while(second!=null){ temp=second.next; second.next=原创 2015-03-26 15:21:00 · 2264 阅读 · 0 评论 -
【LeetCode从零单排】No15 3Sum
题目Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c) must be原创 2015-03-20 16:39:36 · 1227 阅读 · 0 评论 -
【LeetCode从零单排】No121 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 (ie, buy one and sell one share of the stock), d原创 2015-04-01 09:16:04 · 1567 阅读 · 0 评论 -
【LeetCode从零单排】No96 Unique Binary Search Trees
题目Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \原创 2015-03-24 17:09:02 · 1604 阅读 · 0 评论 -
【LeetCode从零单排】No129 Sum Root to Leaf Numbers
题目Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the total sum of原创 2015-03-23 15:16:54 · 1301 阅读 · 0 评论 -
【LeetCode从零单排】No 191.Number of 1 Bits(考察位运算)
题目Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representation 0000000000原创 2015-03-13 15:50:15 · 4620 阅读 · 2 评论 -
【LeetCode从零单排】No.169 Majority Element(hashmap用法)
题目Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element alwa原创 2015-03-12 11:26:57 · 1504 阅读 · 0 评论 -
【LeetCode从零单排】No189 .Rotate Array
题目Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].Note:Try to come up as many solutions as you can, the原创 2015-03-12 16:12:35 · 1448 阅读 · 0 评论 -
【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters
题目Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. Fo原创 2015-03-17 16:33:47 · 1449 阅读 · 0 评论 -
【LeetCode从零单排】No118 Pascal's Triangle
题目Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]代码public class Solution { public原创 2015-03-09 15:10:31 · 1523 阅读 · 0 评论 -
【LeetCode从零单排】No.160 Intersection of Two Linked Lists
题目Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘原创 2015-03-10 14:28:15 · 1320 阅读 · 0 评论 -
【LeetCode从零单排】No28 Implement strStr()
题目Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.代码public class Solution { public int strStr(String haystack, String ne原创 2015-02-12 16:36:11 · 1400 阅读 · 0 评论 -
【LeetCode从零单排】No27.Remove Element
题目 Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.代码p原创 2015-02-12 16:34:25 · 1254 阅读 · 0 评论 -
【LeetCode从零单排】No21.MergeTwoSortedLists
题目 这道题是链表的简单应用,将两个有序链表合成一个有序链表。 思路是:表一,表二各取两个对象,分别指向current和next,进行交叉比较排序。Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nod原创 2015-02-11 11:18:30 · 1415 阅读 · 0 评论 -
【算法数据结构Java实现】递归的简单剖析及时间复杂度计算
1.理解 对于递归函数的理解,我觉得是比较重要的,因为很多大神能把递归函数用的惟妙惟肖,不光是他们的编程功力高深,更主要是能理解这个算法。比较直白的理解是,如果一个事件的逻辑可以表示成,f(x)=nf(x-1)+o(x)形式,那么就可以用递归的思路来实现。编写递归逻辑的时候要知道如下法则:1.要有基准 比如说,f(x)=f(x-1)+1,如果不加入基准,f(0)的值是多少,原创 2014-11-21 15:32:16 · 2416 阅读 · 0 评论 -
【算法数据结构Java实现】时间复杂度为O(n)的最大和序列
1.背景 最大序列和问题一直以来是一个比较经典的算法题,看到这个问题,有很多解题的办法。今天看到了一种时间复杂度只为O(n)的解题算法,在这里记录下。 思路很简单,比方说有P1,P2,P3,P4.....这样一个序列,我们从P1开始求和,比如说在P5时求和数小于零,就可以断定。第一种情况,最大序列在P1~P5之间,或者说在P6~Pn之间。因为如果P1原创 2014-11-21 20:11:30 · 3221 阅读 · 0 评论 -
【算法数据结构Java实现】欧几里得算法
1.背景 欧几里得算法是一个求最大因子的快速算法。如果m,n存在最大因子k,假设m=x*n+r,那么m和n可以整出k的话,r也肯定可以整除k 因为定理:如果M>N,则M mod N2.代码 package Algorithm_analysis;public class Euclid { public static void mai原创 2014-11-25 13:23:57 · 3628 阅读 · 0 评论 -
【Java设计模式】装饰模式
(转载请注明出处:http://blog.youkuaiyun.com/buptgshengod)1.背景 装饰模式就是为一个对象增添新的功能,在对象的基础上修饰。本文实现的功能是,对象a有方法show输出“This is a test”,经过装饰后输出“This is a test for decorate”。实质就是多个类通用一个接口,然后将要修饰的类定义在修饰类的构造函数里。2原创 2014-02-12 14:17:57 · 1727 阅读 · 0 评论 -
【算法与数据结构】中缀表达式转为后缀表达式
(转载请注明出处:http://blog.youkuaiyun.com/buptgshengod)1.题目介绍 中缀表达式是将运算符放在运算数中间的写法,如a+b*c。后缀表达式是将运算符放在运算数后面,如abc*+。2.代码实现部分 import java.util.Stack; public class Main { private String t原创 2013-12-16 11:32:26 · 2351 阅读 · 1 评论 -
【算法与数据结构】查找二叉树的实现
(转载请注明出处:http://blog.youkuaiyun.com/buptgshengod)1.题目介绍 二叉树是一种基本的数据结构。查找二叉树是一种方便与查找,删除,插入等功能的二叉树,它要求每个父节点的左分支小于父节点,右分支大于父节点。下面我们来实现下面这个查找二叉树。2.java代码实现public class BinaryTree { private N原创 2013-12-16 16:09:11 · 2167 阅读 · 0 评论 -
【算法与数据结构】一道检测inversion count的初级算法
(转载请注明出处:http://blog.youkuaiyun.com/buptgshengod)1.题目 这是一道检测inversion count的算法。它将检测输入序列中反序输入的个数,即检测其中有几对A[i] > A[j], i 比如输入4,3,2,1,输出应该为3+2+1=6.。 因为:1. 4比3,2,1大,但4在输入序列中是第一位,比3,2,1的inde原创 2013-12-14 19:16:04 · 2691 阅读 · 0 评论 -
【算法与数据结构】最大子序列和问题
(转载请注明出处:http://blog.youkuaiyun.com/buptgshengod)1.题目 给定一个数字序列,其中有正有负,确定最大子序列和。用穷举法最好的结果也是时间复杂度O(n²)。后来看到一个聪明的方法,直接使时间复杂度变为O(n)。2.解法(1)穷举法 把所有序列都算出来找到最大的。/* 最大序列和问题的求解,一组数列有正有负,找出其中原创 2013-12-12 16:23:57 · 2160 阅读 · 0 评论 -
【算法与数据结构】关于代码运行时间复杂度的计算方法
(转载请注明出处:http://blog.youkuaiyun.com/buptgshengod)1.背景知识 大O标记就不用我说了吧。O(n)这种时间复杂度的意义自己google吧。这里简单讲下从代码推算。2.具体案例 (1)案例一 int a=0; //第一行for(int i=0;i<=N;i++)//第二行{ a+=i原创 2013-12-12 14:57:39 · 2671 阅读 · 0 评论 -
【算法数据结构Java实现】折半查找
1.背景 以一个题目为例,一个整数x是一组按大小顺序排列好的数列中的一个数,我们要找到x在数列中的索引位置。比如按从小到大排列的数列:-3,-2,0,4,5,7,12,64我们要找到数字7的位置,如果是线性查找,时间复杂度是O(n),如果用折半查找的话,时间复杂度是O(log(n)),因为每次折半,计算量少一半,所以取对数。2.代码package Algorithm_analysis;原创 2014-11-24 22:07:21 · 2005 阅读 · 0 评论 -
【算法数据结构Java实现】Java实现单链表
1.背景 单链表是最基本的数据结构,仔细看了很久终于搞明白了,差不每个部分,每个链都是node的一个对象。需要两个参数定位:一个是index,表示对象的方位。另一个是node的对象。2.代码node类public class Node { protected Node next; protected int data; public Node(int data){原创 2014-12-02 17:02:19 · 1638 阅读 · 2 评论 -
【算法数据结构Java实现】Java实现动态规划(背包问题)
1.背景 追随着buptwusuopu大神的脚步,最近在研习动态规划。动态规划应该叫一种解决问题的思想,记得又一次去某公司面试就被问到了这个。 多于动态规划的理解,大致是这样的,从空集合开始,每增加一个元素就求它的最优解,直到所有元素加进来,就得到了总的最优解。 比较典型的应用就是背包问题,有一个重量一定的包,有若干件物品,他们各自有不同的重量和价值,怎样背原创 2015-02-05 16:25:44 · 6653 阅读 · 1 评论 -
【LeetCode从零单排】No26.Remove Duplicates from Sorted Array
题目 题目要求:去除sort int数组中的重复项。 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,原创 2015-02-10 11:16:00 · 1286 阅读 · 0 评论 -
【LeetCode从零单排】No19.RemoveNthNodeFromEndofList
题目 这是道链表的简单应用题目,删除从结尾数第n个节点。Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the原创 2015-02-09 17:02:44 · 1404 阅读 · 0 评论 -
【LeetCode从零单排】No70.ClimbingStairs
题目 爬楼梯问题,这是一道很有趣的问题。首先看题目:You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?这原创 2015-02-20 13:16:45 · 2122 阅读 · 0 评论