
algorithm
iteye_15479
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
用两个栈实现一个队列的功能?
用两个栈,栈A作为入队,栈B作为出队。 enqueue(){ 将入队数据压到A的栈顶;}dequeue(){ if B 为空; if A 不为空; 弹出A数据到B中,然后弹出B的一个数据作为出队数据; else 队列空; else 弹出B的一个数据作为...原创 2010-01-04 10:37:43 · 110 阅读 · 0 评论 -
Little-Known Awesome Algorithms: Fenwick Trees – Rapidly Find Cumulative Frequen
[url]http://www.swageroo.com/wordpress/little-known-awesome-algorithms-fenwick-range-trees-rapidly-find-cumulative-frequency-sums/[/url]This is to solve range query problems. Questions can be li...原创 2012-09-28 17:28:50 · 124 阅读 · 0 评论 -
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:[code="java"]package alg;import java.util.ArrayList;import j...原创 2012-10-16 17:01:33 · 91 阅读 · 0 评论 -
怎样生成全排列?
我前面写过一种方法生成全排列,现在看用DP的方法解决。参考[url="http://standalone.iteye.com/blog/757948"]How to generate permutations[/url] 看前面的那种解法。DP的思路就是生成N个数的全排列,先考虑生成前面N-1个数字的全排列,然后把最后一个数字插入上一步每个结果的每个缝隙中,形成最后的结果。用pe...原创 2012-10-29 11:25:23 · 155 阅读 · 0 评论 -
找零钱问题
假设有25美分,10美分,5美分,1美分的硬币足够多,假设有N美分钱,问你怎么用这些硬币表示?用perl重新做这个问题,前面用[url="http://standalone.iteye.com/admin/blogs/1670164"]java[/url]做过[code="java"]use strict;use warnings;my $count = 0;s...原创 2012-10-29 15:59:42 · 152 阅读 · 0 评论 -
算法:去除字符串中的重复字母
问题:去除字符串中的重复字符,不能使用额外空间(1或者2个变量除外)。很简单的练手题,但是发现很难写正确。[code="java"]public class DuplicateChar { //return length of the final string public static int removeDuplicateChar(char []str){...原创 2012-11-06 22:50:13 · 1011 阅读 · 0 评论 -
Rotation of String
Problem: Assume you have a method isSubstring which checks if one word is a substring ofanother. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call t...原创 2012-11-06 23:29:24 · 164 阅读 · 0 评论 -
stack with min operation
How would you design a stack which, in addition to push and pop, also has a functionmin which returns the minimum element? Push, pop and min should all operate inO(1) time.[code="java"]pub...原创 2012-11-06 23:37:05 · 158 阅读 · 0 评论 -
怎么不开根号算平方根?
写一个函数,不用开根号算平方根。这是网上一个题目,一开始一筹莫展,看了答案恍然大悟,就是用二分法去逼近。[code="java"]const double error = 0.000000001f;double findSqrt(double t){ double high = t; double low = 0; whil...原创 2012-12-24 22:55:41 · 608 阅读 · 0 评论 -
[leetcode] Palindrome Partitioning II
http://leetcode.com/onlinejudge#question_132Question:Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrom...原创 2013-04-11 16:45:54 · 125 阅读 · 0 评论 -
[leetcode] Palindrome Partition
http://leetcode.com/onlinejudge#question_131Question:Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of...原创 2013-04-12 10:25:27 · 306 阅读 · 0 评论 -
[leetcode] Count and Say
http://leetcode.com/onlinejudge#question_38Question: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...原创 2013-04-12 14:05:02 · 129 阅读 · 0 评论 -
[leetcode] word ladder II
http://leetcode.com/onlinejudge#question_126Q: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: Only one let...2013-04-15 07:35:45 · 8422 阅读 · 0 评论 -
[leetcode] sqrt(x)
I have talked about this question in my previous posts.http://leetcode.com/onlinejudge#question_69Q:Implement int sqrt(int x).Compute and return the square root of x.Solution:[code...2013-04-15 07:44:01 · 131 阅读 · 0 评论 -
[leetcode] word ladder
Q: Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each inter...2013-04-25 15:05:24 · 140 阅读 · 0 评论 -
[leetcode] find median of two sorted arrays
[url]http://leetcode.com/onlinejudge#question_4[/url]Solution: a little ugly.[code="java"]public class Solution { public double findMedianSortedArrays(int A[], int B[]) { /...2013-04-26 10:55:02 · 131 阅读 · 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 p...原创 2012-09-06 15:31:05 · 167 阅读 · 0 评论 -
Lock Free Queue Implementation
One consumer, one producer problem:[url="http://www.drdobbs.com/parallel/writing-lock-free-code-a-corrected-queue/210604448?pgno=1"]Lock Free Queue[/url]Basically add one a separator in a list...原创 2012-09-05 16:29:31 · 156 阅读 · 0 评论 -
nvidia面试题
八道题目,第一题有六个小题,一道小题0.5分,剩下的七道题目一题1分。全英文试卷,要求英文作答。 1. (1)what is an abstract class (2)myclass a;myclass b;b=a; myclass a;myclass b=a;区别 (3)stl有哪三类? (4)容器有哪几种? (5)virtual destruction作用? (6)...原创 2010-01-04 14:34:21 · 530 阅读 · 0 评论 -
How to generate permutations
The following algorithm generates the next permutation lexicographically after a given permutation. It changes the given permutation in-place.Find the largest index k such that ...原创 2010-09-07 23:10:08 · 193 阅读 · 0 评论 -
记一次面试
今天参加了一个phone interview,有几道题自己还记着。1. c++析构函数为啥是虚函数。2. 四个cast。3. 类初始化列表。编译器为什么要按照声明中的顺序初始化?4. 一个空类,编译器帮你实现了哪些default的函数。4. IQ题。假设三个事件A,B,C,给你两枚硬币,如果根据扔硬币的结果得到独立概率的一件事情。...原创 2010-09-20 15:10:18 · 100 阅读 · 0 评论 -
sort n integers of range 0~n^2-1 in O(n) time
This is one problem introduced in "Introduction to Algorithm".We can use radix-sort to solve it. package alg;import java.util.ArrayList;import java.util.Iterator;/* * This example sh...原创 2011-06-24 17:22:17 · 432 阅读 · 0 评论 -
Check if the two rectangles intersect
Given the bottom left and top right coordinates of the two rectangles, check whether the two rectangles intersect or not.原创 2011-06-29 14:58:22 · 106 阅读 · 0 评论 -
Last Survival
There are 100 people forming a circle and they are numbered from 1 to 100. They decide to kill each other using a sword, in following manner. At any point, person numbered n having a s...原创 2011-06-30 16:02:28 · 114 阅读 · 0 评论 -
计算数组元素的乘积
Given an array a[i], your task is to generate an array b[i], b[i] is the product of the numbers in the array except the i th number. My solution: DP problem? Define two functions:f(i)=a...原创 2010-11-03 20:56:17 · 444 阅读 · 0 评论 -
How to delete a node in a LinkList ?
If only given the pointer to the node to be deleted? Note the node is not the first one nor the last. 如果在单链表中删除一个指定的结点? 需要注意的是不是头尾结点。 这到题其实就是个小技巧,没有告诉前面的一个结点怎么做删除呢?答案就是把后面结点的内容拷贝过来!这个问题的前提是结点不...原创 2010-07-06 21:22:52 · 108 阅读 · 0 评论 -
how to reverse bits in a byte?
Given a 8-bit byte, assume its bits are b8b7...b1.Provide an algorithm to reverse the bit sequence.Result should be b1b2b3...b8.An simple answer:Suppose the byte is c.c=((c>>1) & 0x55)...原创 2012-07-03 17:59:44 · 177 阅读 · 0 评论 -
[leetcode] Decode ways
[url]http://leetcode.com/onlinejudge#question_91[/url]Decode WaysJun 25 '121292 / 5011A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B...2013-05-02 21:47:39 · 123 阅读 · 0 评论 -
Find all paths with sum as a given value in a binary three
Problem:microsoft-interview-questions 57 AnswersGiven 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 ...原创 2012-09-01 11:40:37 · 105 阅读 · 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 representationSolution from CareerCup book:[code="java"]public...原创 2012-09-01 15:14:32 · 134 阅读 · 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 o...原创 2012-09-02 16:56:25 · 114 阅读 · 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:...原创 2012-09-03 11:34:00 · 122 阅读 · 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:____________________________________...原创 2012-09-03 14:54:56 · 330 阅读 · 0 评论 -
[leetcode] Balanced Binary Tree
Check if a binary tree is balanced or not. This solution is from the discuss board. Much better than mine.[code="java"]/** * Definition for binary tree * public class TreeNode { * int ...原创 2013-04-28 14:08:57 · 133 阅读 · 0 评论