
算法研究
文章平均质量分 78
ljiabin
在IT的世界里不断成长!
展开
-
双调排序程序解析(可对任意长度的序列排序)
上一篇文章中给出了可对任意长度的序列进行排序的双调排序的程序实现,这篇文章将对程序进行解析,通过与归并排序的对比有助于对程序的理解。首先回顾一下归并排序的程序实现:public class MergeSorter{ private int[] a; private final static boolean ASCENDING=true; // sorting d原创 2013-03-03 19:15:52 · 3358 阅读 · 0 评论 -
浙大PAT_1038:Recover the Smallest Number 解题报告
题目:Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given {32, 321, 3214, 0229, 87}, we can recover many numbers such like 32-321-3214-022原创 2013-03-04 21:09:12 · 2392 阅读 · 0 评论 -
n!=2^k的双调排序网络
排序网络n!=2^k的双调排序网络经典的双调排序网络的设计是用来解决输入长度n=2^k的情况。下面提出了一种对于任意n的双调排序网络算法,它的正确性来自于0-1原理。问题:对于长度为n=2^k的双调排序网络包含如表1所示的三块。左边两块是双调排序网络,把原序列均分为两半,分别按升序和降序排列;右边一块是双调合并网络,归并两半有序序列,得到整个有序序列。图1:双调排序(2^k翻译 2013-03-03 13:50:47 · 8712 阅读 · 1 评论 -
浙大PAT_1044:Shopping in Mars 解题报告
题目大意:给出长度为n的一个整数序列,找出其中所有和为m的子序列(连续的),如果没有任何子序列的和等于m,那么找出大于m的最小和。如长度为8的序列3,2,1,5,4,6,8,7,找出和为15的子序列有三个:1~5,4~6,7~8。这道题思路并不难,一般都能想到通过遍历来解决,但是如果不注意细节是很难拿到满分的。以下是大家通常能够写出的解决方法:#include using nam原创 2013-03-04 15:14:19 · 2114 阅读 · 5 评论 -
【LeetCode】Candy 解题报告
There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least on原创 2014-09-04 22:46:47 · 6028 阅读 · 2 评论 -
【LeetCode】Triangle 解题报告
【题目】Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [原创 2014-09-18 10:55:15 · 1854 阅读 · 0 评论 -
POJ_1007:DNA Sorting解题报告
大致题意:排列多个DNA序列,按照每个序列的“有序程度”。如果一个序列已经按照字母顺序排好了,那么这个序列有序程度最高,如AACCGGTT。反之,如果一个序列越无序,那么它的有序程度越低,如TGTCAA。解题思路:计算每个序列的“逆序数”,即反序字母对的个数,如ATGC的逆序数是3,因为TG,TC,GC都是逆序的。然后按照每个序列的逆序数排序,逆序数越大说明这个序列越无序。#include原创 2014-08-21 15:44:53 · 2172 阅读 · 0 评论 -
【TopCoder SRM 157 DIV 1】 Salary 解题报告
本来想把原题给贴过来的,后来看到人家有版权保护,在网上搜了一下,好像也没有人在博客中贴过原题,为避免麻烦,就不copy人家原题了。【题目大意】公司按照每小时 wage 元给员工发工资,员工上班打卡记录一个时间,下班记录一个时间,时间格式为 "hh:mm:ss"。其中,18:00:00 到 23:59:59 和 00:00:00 到 05:59:59 之间算加班时间,工资为1.5倍。计算工资原创 2014-08-26 15:31:56 · 1217 阅读 · 0 评论 -
【LeetCode】Gas Station 解题报告
【题目】There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from sta原创 2014-09-10 09:37:43 · 7000 阅读 · 0 评论 -
【LeetCode】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.Fin原创 2014-09-22 17:14:49 · 3427 阅读 · 0 评论 -
【LeetCode】Clone Graph 解题报告
【题目】Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ's undirected graph serialization:Nodes are labeled uniquely.We use # as a separa原创 2014-09-11 21:08:40 · 2778 阅读 · 0 评论 -
【LeetCode】Word Break 解题报告
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, givens = "leetcode",dict = ["leet"原创 2014-08-31 19:50:14 · 1825 阅读 · 0 评论 -
【LeetCode】Reorder List 解题报告
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder it to原创 2014-08-29 22:52:58 · 5168 阅读 · 1 评论 -
【LeetCode】Reverse Words in a String 解题报告
【题意】给定一个字符串,把里面的单词逆序输出来。例如:给定 s = "the sky is blue",返回 "blue is sky the"。注意:接收的字符串开头和结尾可能有空格,但是返回时开头和结尾不能有空格;返回时每两个单词之间只能有一个空格。【Java代码】public class Solution { public String reverseWords(原创 2014-08-28 17:19:55 · 1646 阅读 · 0 评论 -
【LeetCode】Insertion Sort List 解题报告
Sort a linked list using insertion sort.【题意】用插入排序对一个链表进行排序。/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) {原创 2014-08-29 20:42:30 · 1906 阅读 · 0 评论 -
【LeetCode】Evaluate Reverse Polish Notation 解题报告
【题意】逆波兰表达式,又叫后缀表达式。例如:["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6【思路】用一个栈存储操作数,遇到操作数直接压入栈内,遇到操作符就把栈顶的两个操作数拿出来运算一下,然后把运算结果放入栈内。【Jav原创 2014-08-28 20:57:36 · 2614 阅读 · 0 评论 -
【LeetCode】LRU Cache 解题报告
题外话:才连续写了几篇博客,博客排名竟然就不再是“千里之外”了,已经进入2万名之内了。再接再厉,加油!Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.get(key)原创 2014-08-31 16:51:39 · 2496 阅读 · 0 评论 -
【LeetCode】Max Points on a Line 解题报告
【题意】求二维平面上n个点中,最多共线的点数。【思路】比较直观的方法是,三层循环,以任意两点划线,判断第三个点是否在这条直线上。【Java代码】/** * Definition for a point. * class Point { * int x; * int y; * Point() { x = 0; y = 0; } * Po原创 2014-08-28 22:05:38 · 8041 阅读 · 2 评论 -
【LeetCode】Word Break II 解题报告
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.Return all such possible sentences.For example, givens = "原创 2014-09-02 16:25:40 · 3291 阅读 · 0 评论 -
【LeetCode】Copy List with Random Pointer 解题报告
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list./** * Definition for singly-lin原创 2014-09-04 17:01:53 · 4744 阅读 · 0 评论 -
【LeetCode】Set Matrix Zeroes 解题报告
今天看到优快云博客的勋章换了图表,同时也增加显示了博客等级,看起来都听清新的,感觉不错!【题目】Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did yo原创 2014-10-24 10:17:51 · 5009 阅读 · 0 评论 -
【LeetCode】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原创 2014-10-26 10:39:52 · 1413 阅读 · 1 评论 -
【LeetCode】【Solution】Maximum Product Subarray
【题目】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 contiguous subarray [2,3] has th原创 2014-11-11 17:00:08 · 1662 阅读 · 1 评论 -
【LeetCode】Pascal's Triangle & II 解题报告
杨辉三角,分别求前n行和第n行。【求杨辉三角前n行】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,原创 2014-10-20 17:45:38 · 3948 阅读 · 1 评论 -
【LeetCode】Sort List 解题报告(对链表进行归并排序)
【题目】Sort a linked list in O(n log n) time using constant space complexity.【归并排序】原创 2014-11-13 11:16:55 · 915 阅读 · 0 评论 -
【LeetCode】4Sum 解题报告
【题目】Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:原创 2014-10-31 10:38:01 · 1841 阅读 · 0 评论 -
【LeetCode】Add Two Numbers 解题报告
【题目】You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it原创 2014-10-26 14:03:49 · 11085 阅读 · 3 评论 -
【LeetCode】Longest Palindromic Substring 解题报告
DP、KMP什么的都太高大上了,自己想了个朴素的遍历方法。【题目】Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palin原创 2014-10-26 15:17:47 · 5067 阅读 · 0 评论 -
【LeetCode】String to Integer (atoi) 解题报告
这道题在LeetCode OJ上难道属于Easy,但是通过率却比较低,究其原因是需要考虑的情况比较低,很少有人一遍过吧。【题目】Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge,原创 2014-10-27 16:19:38 · 15745 阅读 · 2 评论 -
【LeetCode】ZigZag Conversion 解题报告
【题目】The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H N原创 2014-10-26 16:49:26 · 27215 阅读 · 5 评论 -
【LeetCode】Integer to Roman 和 Roman to Integer 解题报告
【题目】Given a roman numeral, convert it to an integer. Or, Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.【罗马数字】1~9: {"I", "II", "III原创 2014-10-10 20:28:24 · 23527 阅读 · 4 评论 -
【LeetCode】Sort Colors 解题报告
【题目】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 the int原创 2014-10-13 20:51:44 · 5624 阅读 · 1 评论 -
【LeetCode】Palindrome Partitioning II 解题报告
【题目】Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.For example, given s = "aab原创 2014-11-16 16:00:19 · 4011 阅读 · 1 评论 -
【LeetCode】Palindrome Partitioning 解题报告
【题目】Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = "aab",Return [原创 2014-11-16 16:05:58 · 2821 阅读 · 0 评论 -
【LeetCode】Balanced Binary Tree 解题报告
【题目】Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never d原创 2014-10-12 20:15:29 · 6489 阅读 · 0 评论 -
【LeetCode】3Sum Closest 解题报告
【题目】Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would ha原创 2014-10-30 16:57:05 · 9971 阅读 · 5 评论 -
【LeetCode】3Sum 解题报告
这道题凭我现有知识实在解答不上来,只好网上搜索解法,才发现 K Sum 是一类问题,但是网上没有比较简洁的代码,我想对于初学者来说,可能还是想先看看这道题怎么解,然后才有兴趣去看其扩展吧。【题目】Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Fin原创 2014-10-30 16:00:34 · 38001 阅读 · 7 评论 -
【LeetCode】Generate Parentheses 解题报告
【题目】Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()原创 2014-10-16 15:26:03 · 2424 阅读 · 0 评论 -
【论文研读】事件与时间序列的关联
最近学习了一篇论文,是KDD 2014的,题目为“Correlating Events with Time Series for Incident Diagnosis“,论文作者是微软研究院的。为了讲解,做了一个PPT,希望能对大家有点帮助。(这个PPT被老板逼得我改了好多遍,花了好多时间)不过,”天下没有白做的努力“,期间也查阅收获了不少知识,下面的”t-test“是统计学领原创 2014-11-04 16:20:29 · 2225 阅读 · 1 评论 -
【LeetCode】Find Minimum in Rotated Sorted Array 解题报告
【题目】Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.You may assume no duplicate原创 2014-10-20 10:33:26 · 5834 阅读 · 5 评论