ITeye博客

渴死的火烈鸟

standalone · 共 356 篇文章

perl的数组操作

生成连续的一列数字或字母: my @numbers = (1..100); my @chars = (a..z); 添加或者删除元素 shift: 移除数组的第一个元素; unshift: 添加一个元素到数组的最后; push: 添加一个元素到数组的第一个元素; pop:去除数组的最后一个元素。 |Functio|Definition| |push(@array, Element)| Adds to the end of an array| |pop(@array)|Removes the last element of the array| |unshift(@array, Element…

2012-10-29 11:43 · perl · 评论(0)

怎样生成全排列?

我前面写过一种方法生成全排列,现在看用DP的方法解决。 参考 How to generate permutations 看前面的那种解法。 DP的思路就是生成N个数的全排列,先考虑生成前面N-1个数字的全排列,然后把最后一个数字插入上一步每个结果的每个缝隙中,形成最后的结果。用perl比较好操纵数组,写起来的程序比较简单。(可惜这个博客不支持perl语法高亮啊) use strict; use warnings; sub permutation { my (@array) = @_; my $length = scalar @array; if($length == 1){ #如果数组就一个…

2012-10-29 11:25 · perl · algorithm · 评论(0)

Hadoop 2.0 代码分析---MapReduce

本文参考hadoop的版本: hadoop-2.0.1-alpha-src 参考: http://www.cnblogs.com/biyeymyhjob/archive/2012/08/16/2640733.html 和参考的文章一样,还是考虑具体一个MR的job怎么执行的,这个例子如下: // Create a new Job Job job = new Job(new Configuration()); job.setJarByClass(MyJob.class); // Specify various job-specific parameters job.setJobName("my…

2012-10-25 18:27 · hadoop · 评论(1)

Print all binary search trees

Problem: Given numbers 1,2,3...N, print all binary search trees that can be constructed with these N numbers. Solution: package alg; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class AllBST { class Node<T extends Comparable<?>> { Node<T> left, right; T da…

2012-10-16 17:01 · java · algorithm · 评论(0)

Little-Known Awesome Algorithms: Fenwick Trees – Rapidly Find Cumulative Frequen

http://www.swageroo.com/wordpress/little-known-awesome-algorithms-fenwick-range-trees-rapidly-find-cumulative-frequency-sums/ This is to solve range query problems. Questions can be like below Imagine that we are creating an analytics tool for a financials brokerage, where orders are coming in all …

2012-09-28 17:28 · algorithm · 评论(0)

What's the difference between a jQuery object and a DOM element?

http://http://stackoverflow.com/questions/6942193/whats-the-difference-between-a-jquery-object-and-a-dom-element-difference-betw I got an answer from above. HTML != DOM != Javascript != jQuery, but they're all closely related. The browser receives an HTML document from a web server, which is just t…

2012-09-28 17:26 · javascript · 评论(0)

find the subtree with max sum

Problem: Given a binary tree, each node has an integer value attached (can be negative), write code to find the subtree with the max sum. My solution: Basically this is a recursive problem, for each subtree result, you should remember its maxSum and corresponding root node. So I use a struct SubTre…

2012-09-18 15:04 · java · 评论(0)

Lock Free Stack

This example is copied from book "Java Concurrency in Practice". From this example we can learn that the lock free queue code is not correct for MPMC (multi-producer-multi-consumer) in my previous post. Actually, lock free means you should guarantee the atomicity of updating a pointer. When you add…

2012-09-06 15:31 · algorithm · 评论(0)

Learn Ruby

First I should say rubymonk is a great web site for ruby learners. I just have some time this afternoon so I dived my self into this language and found it quite amazing! Look at this code snippet below. Besides a variable parameter list, it's also possible to add naming parameters to your method ar…

2012-09-05 18:02 · ruby · 评论(0)

Lock Free Queue Implementation

One consumer, one producer problem: Lock Free Queue Basically add one a separator in a list then nodes between separator and last is owned by consumer, the node between first and separator should be collected by producer and producer only append new nodes to the last. This problem is also described…

2012-09-05 16:29 · algorithm · 评论(0)

8-Queen Problem

Problem: Write an algorithm to print all ways of arranging eight queens on a chess board so that none of them share the same row, column or diagonal. My Code: package alg; import java.util.Stack; public class EightQueens { public static final int N = 8; static class Queen { public int x, y; Queen(i…

2012-09-03 14:54 · algorithm · 评论(0)

Number of ways to represent money

Problem: Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents) and pennies (1 cent), write code to calculate the number of ways of representing n cents. My code: package alg; import java.util.Stack; public class Coins { public final int coin[] = {25, 10, 5, 1}; void p…

2012-09-03 11:34 · algorithm · 评论(0)

Find all valid parentheses

Problem: Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-pairs of parentheses. 给定一个正整数N,打印所有可能的N对括号序列,例子如下。 EXAMPLE: input: 3 (e.g., 3 pairs of parentheses) output: ()()(), ()(()), (())(), ((())) My Code: public class Parenthese { public static void pr…

2012-09-02 16:56 · java · algorithm · 评论(0)

Find the previous and next nearest number with same 1 bits

Problem: Given an integer, print the next smallest and next largest number that have the same number of 1 bits in their binary representation Solution from CareerCup book: public static boolean GetBit(int n, int index) { return ((n & (1 << index)) > 0); } public static int SetBit(int n, int index, …

2012-09-01 15:14 · java · algorithm · 评论(0)

Find all paths with sum as a given value in a binary three

Problem: microsoft-interview-questions 57 Answers Given a value and a binary search tree. Print all the paths(if there exists more than one) which sum up to that value. It can be any path in the tree. It doesn't have to be from the root. My code: I also pasted my code at http://www.careercup.com/qu…

2012-09-01 11:40 · algorithm · 评论(0)