算法
文章平均质量分 59
二次元中毒者
众多IT民工中的一员。爱好动漫,电影。闲的时候喜欢喝喝咖啡,看看书,散散步。新的开始,开始学着热爱C#,热爱码工的生活~ Life Pattern:coding-> read paper->coding->read paper---->
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
LeetCode N阶乘的尾数为0的个数
原题如下 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 分析,N的阶乘 n!=5^m * 2^k * ... ,因此只要我们找出m和k,并且取最小值就是末尾0的个数。 进一步原创 2015-01-27 10:19:25 · 2154 阅读 · 0 评论 -
二叉树遍历算法,前序
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Soluti原创 2014-10-14 13:27:00 · 419 阅读 · 0 评论 -
Binary Tree Level Order Traversal, 二叉树层级遍历
[LeetCode] Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3原创 2014-10-14 13:00:10 · 1270 阅读 · 0 评论 -
最后一个字的长度。
给定一个字符串,包含大小写英文字母,计算最右一个字的长度。原创 2014-10-14 14:04:49 · 677 阅读 · 0 评论 -
链表排序
对链表进行合并算法排序,基本s原创 2014-11-19 11:31:28 · 412 阅读 · 0 评论 -
判断链表是否循环,找出循环点
判断链表是否循环,找出循环点。 例子:原创 2014-10-19 02:32:15 · 688 阅读 · 0 评论 -
两个链表相加
给定两个链表,每个节点保存一位数字,shu'zi原创 2014-10-18 21:33:52 · 550 阅读 · 0 评论 -
Leetcode 平衡树判别
判断一颗二叉树是否平衡。 /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Soluti原创 2014-10-20 17:42:18 · 445 阅读 · 0 评论 -
Leetcode, 组合序列生成
给定一个n和k,给出c_{n}^k的所有组合。 一下是C++实现。原创 2014-10-20 18:43:38 · 745 阅读 · 0 评论 -
Heap 排序
Heap排序想必大家都知道。原创 2014-10-30 18:59:09 · 693 阅读 · 0 评论 -
maxSum
最大字段和问题: 给定有n个整数组成的序列a_1, ..., a_n, 求该序列\sum_{k=i}^j a_k。 或许最先想到的爆破发,遍历所有的子序列而找出最大值。然而这个时间复杂度太高O(n^3)。当然可以用一些技巧而时间复杂度降到n^2。 如果了解合并排序(Merge sort),我们也可以发现该问题和merge sort很相像。 我们把一个大数组均匀的分成两半,这样最大子原创 2015-01-28 10:25:22 · 380 阅读 · 0 评论 -
0-1背包问题
0-1背包问题是很经典的一个题目。 具体细节再次不叙了。 本文给出动态编程解法。 public class Knapsack { public static void main(String[] args) { int c = 10; int[] weights = { 2, 2, 6, 5, 4 }; int[] prices = { 6, 3, 5, 4, 6 };原创 2015-01-29 10:36:30 · 459 阅读 · 0 评论 -
反转栈
给定一个堆栈【1, 2, 3】,反转为【3, 2, 1】 import java.util.Stack; public class ReverseStack { public static void main(String[] args) { Stack stack = new Stack(); for (int i = 0; i < 10; i++) { st原创 2015-01-31 16:53:05 · 417 阅读 · 0 评论 -
合并排序算法 (Merge Sort)优化
Merge Sort很受欢迎 ,也经常出现在面试过程中。 之前写过一个Merge sort的一个算法(算法和大部分算法教程一样),本文对Merge sort做了些优化。 测试显示比之前的版本速度提高不少。 Merge Sort的主要思想是把一个大数组分解成小数组,分别对小数组进行sort,然后把sort好的两个小数组合并起来。 在合并的时候一般需要额外的空间来从放中间值。因此,分配空间的次原创 2015-01-17 09:48:40 · 2872 阅读 · 0 评论 -
Shell Sort
Shell sort是对插入排序的扩展,但是比插入排序速度快很多。 做了一个有趣的对比试验,比较shell sort和merge sort。 先把需要排序的数组分为大(>1M),中(1K 假如我们的所要排序的数组相对不是特别大,shell sort是个不错的选择。 另外,shell sort不需要额外分配空间(merge需要分配额外空间)。 一下是shell sort 的Java 实现。原创 2015-01-17 10:11:29 · 620 阅读 · 0 评论 -
基数排序
基数排序的基本思想是,先通过个位数字排序,让后根据十位数排序,,重复以上知道排序结束。 读Java算法教材,上面通过Queue来保存中间值,然而优先序列加入新元素需要log(n)的复杂度,总觉得应该有数组来代替。 其次,好多实现代码都需要给定最大的位数,要单独计算最大位数的话需要O(n)的复杂度,为什么不增加一个计数器来搞定呢? 一下是Java实现版本,目前只支持任意正整数排序。如果有更好的原创 2015-02-04 14:39:51 · 417 阅读 · 0 评论 -
LeetCode 二叉树的最小深度
计算二叉树的最小深度。最小深度定义为从root原创 2014-10-20 19:13:13 · 5171 阅读 · 0 评论 -
LeetCode DNA重复序列
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA. Wri原创 2015-02-12 09:41:57 · 2912 阅读 · 1 评论 -
单独数问题
Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using e原创 2014-10-09 12:41:23 · 495 阅读 · 0 评论 -
最大公约数算法
最大公约数算法是算法教材的第一个例子 java版本如下: package art.program; //Algorithm of the greatest common divisor public class GCD { public static void main(S原创 2011-08-05 16:50:31 · 420 阅读 · 0 评论 -
排列生成算法实现java
排列生成算法是常用的算法之一,详细的介绍请查考《组合数学》教材。1.序数法序数思想来源于数字的表示形式对于任何给定小于10的m次方的正整数N,我们可以表示为如下形式:N= a0×10^0+...+a(m-1)×10^(m-1)也就是说N可以唯一的表示为(a0,..., a(m-1原创 2011-07-24 00:37:18 · 911 阅读 · 0 评论 -
最小编辑距离
最小编辑距离是用来衡量两个字符串的相似度 比如从“cabc”到“abc”,我们删除最前面的‘c’即可。 对于字符串的操作主要包括三类:删除,插入和替换(替换可以看作是删除和插入的组合) 我们设置删除和插入的代价为1,那么替换的代价则为2. 详细参考《自然语言处理总理》5.6最小编辑距离。 java实现版本 public class MED{ public static v原创 2011-07-31 15:09:47 · 2969 阅读 · 0 评论 -
LeetCode 对称树解法
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the f原创 2014-10-20 07:44:54 · 638 阅读 · 0 评论 -
LeetCode 从排序好的列表中删除重复元素
从排序好的列表中删除重复元素 java 版本如下原创 2014-10-23 22:49:43 · 536 阅读 · 0 评论 -
LeetCode 从链表中删除倒数第N个节点
从单链表中删除倒数第N个节点,要求之遍历一遍lia原创 2014-10-23 11:24:12 · 3692 阅读 · 0 评论 -
波兰式解析Evaluate Reverse Polish Notation
本文介绍波兰式解析算法。 Leet原创 2014-10-08 22:01:25 · 458 阅读 · 0 评论 -
旋转列表
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. 翻译如下原创 2014-10-09 12:18:30 · 434 阅读 · 0 评论 -
最深二叉树 (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.原创 2014-10-09 12:51:07 · 684 阅读 · 0 评论 -
相同树判断
Given 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. 判断两颗二叉树原创 2014-10-09 13:05:53 · 354 阅读 · 0 评论 -
旋转整数
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 j原创 2014-10-09 13:21:15 · 622 阅读 · 0 评论 -
LeetCode 循环链表判断
判断一个单链表是否循环. Java version /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = n原创 2014-11-14 23:03:54 · 819 阅读 · 0 评论 -
LeetCode 循环链表II
判断一个链表是否有循环,如果有返回第一相遇节点,否则返回NULL。 jie'ti原创 2014-11-14 23:29:08 · 549 阅读 · 0 评论 -
优先序列
堆经常用来做优先队列( priorit原创 2014-10-30 20:32:11 · 1716 阅读 · 0 评论 -
Max Points on a line ,在二维平面寻找共线的最多点
LeetCode 上面的题目。原创 2014-10-13 19:42:45 · 819 阅读 · 0 评论
分享