
Java/C 算法锦集
huoyin
这个作者很懒,什么都没留下…
展开
-
JAVA斐波那契非递归算法
public class Fibonacci { public static int fibo2(int n) { if(n==1 || n==2) { return n; } int f1=1, f2=1, sum=0; for(int i=2; i sum = f1 + f2;原创 2007-05-19 17:47:00 · 1695 阅读 · 2 评论 -
数组面试题之子数组(subarray)
1. QUESTION: Given an array, subarray size k and a number ssum, find the number of subarrays of size k that sum up to ssum.ANSWER:1) sum the first k element from array. 2) Start a loop from k+1原创 2012-02-16 20:56:54 · 1427 阅读 · 0 评论 -
数组面试题之统计
question: Count smaller elements on right sideeg : [4,12,5,6,1,34,3,2] o/p [3,5,3,3,0,2,1,0]原创 2012-02-28 08:05:32 · 514 阅读 · 0 评论 -
单链表算法面试题
question: find the middle element in one traversalanswer: use two pointer with one step and two steps, when the two steps pointer ends, the location where one step pointer runs is the answerques原创 2012-01-21 11:39:22 · 1164 阅读 · 0 评论 -
程序员面试之杂题
QUESTION:Is is possible to write a program to print number 1 to 100 without using loop. if yes how many way and how?answer: 1)using recursiveQUESTION: How many ways two variables can be原创 2012-03-09 14:13:59 · 542 阅读 · 0 评论 -
数组面试题之序列
1. QUESTION: Given an int array which might contain duplicates, find the largest subset of it which form a sequence.Eg. {1,6,10,4,7,9,5}then ans is 4,5,6,7 answer1. Create an array 'TempArray原创 2012-02-16 21:33:46 · 700 阅读 · 0 评论 -
程序员之数学面试题
question:Fibonacci sequence and optimization question:check a number whether is Power of 2answer:return (n & (n-1) == 0) question:Implement an algorithm to generate all prime numb原创 2012-01-27 09:45:26 · 1237 阅读 · 0 评论 -
Java如何获得当前目录代码示例
import java.io.*;public class Test { public static void main(String[] args) { System.out.println("Current Working Dir: " + new File("").getAbsolutePath());原创 2012-03-29 18:13:20 · 558 阅读 · 0 评论 -
数组面试题之矩阵
1. QUESTION: Given an n x n matrix, where every row and column is sorted in increasing order. Given a number x, how to decide whether this x is in the matrix. The designed algorithm should have linear原创 2012-02-16 21:27:12 · 1414 阅读 · 0 评论 -
面试题之树与图
question: GIven a binary tree, print the tree according to the level.eg010203040506070809101112131415proceed further to find the mirror image of alternate level010203070605040809原创 2012-01-28 19:43:28 · 941 阅读 · 0 评论 -
程序员面试之设计题
question:Implement a connection poolanswer:Create a ConnectionPool class. A Properties class can be used as the initial parameter for the constructor of the ConnectionPool class. The properties ma原创 2012-01-27 11:14:26 · 970 阅读 · 0 评论 -
脑筋急转弯
question:Given 25 horses, find the best 3 horses with minimum number of races. Each race can have only 5 horses. You don't have a timer.answer:7 races questions:there are 81 horses. you ne原创 2012-01-26 17:11:09 · 1016 阅读 · 0 评论 -
JAVA斐波那契递归算法
public class Fibonacci { //时间复杂度为2的n次方 public static int fibo1(int n) { if (n == 0) { return 0; } if (n == 1) { return 1; } return fibo1(n - 1) + fibo1(n - 2); }}原创 2007-05-19 17:37:00 · 3362 阅读 · 2 评论 -
JAVA归并排序算法
<br />import java.util.*;public class MergeSort { public static void merge(int[] a, int low, int mid, int high) { System.out.printf("low=%d,mid=%d,high=%d/n",low, mid, high); int left_size = mid-low+1; int[] left = new int[left_size]; f原创 2010-12-05 20:44:00 · 657 阅读 · 0 评论 -
Java插入排序递归算法(insert sorting recursively)
<br />import java.util.*;public class InsertSort { public static void sort(int[] a, int m) { if(m==0) { return; } sort(a, m-1); int k = a[m]; while(m>0 && k<a[m-1]) { a[m]=a[m-1]; m--; } a[m]=k; } public stati原创 2010-12-05 21:48:00 · 1352 阅读 · 0 评论 -
Java二分法(Binary Search)查找递归算法与迭代算法
<br />public class BinarySearch { public static int search(int[] a, int k) { int index = -1; int low = 0; int high = a.length-1; while(low<high) { int mid = low + (high-low)/2; if(k==a[mid]) { index = mid; break; } els原创 2010-12-06 20:02:00 · 2782 阅读 · 0 评论 -
算法导论-Inversion Pair算法
import java.util.*;public class InversionPair { public static int inversion(int[] a, int low, int mid, int high) { int count = 0; int left_size = mid-low+2; int[] left = new int[left_size]; for(int i=0; i原创 2010-12-07 21:23:00 · 1342 阅读 · 0 评论 -
单链表逆置递归算法(C语言)
<br />#include <stdio.h>typedef int Element;typedef struct LinkListType{ Element data; struct LinkListType * next;} LinkList ;//reverse a link list recursivelyLinkList * reverse(LinkList * pre, LinkList * cur){ if(!cur)原创 2010-12-08 22:50:00 · 4762 阅读 · 1 评论 -
JAV打印字符串中最大的回文子串
import java.util.*;/** * 打印字符串中最大的回文子串 * 时间复杂度O(n power 3),空间复杂度O(1) * @author HUOYIN * @version 1.0 2010-10-28 上午11:19:14 * */public class EchoString { public static void main(String[] args) { Scanner scan = new Scanner(System.i原创 2010-10-28 13:30:00 · 1867 阅读 · 0 评论 -
名企面试题之泰波那契数列Tribonacci,其定义式为T(n)=T(n-1)+T(n-2)+T(n-3)
import java.util.concurrent.TimeUnit;/** * 泰波那契数列 (Tribonacci Number) 即把斐波那契数列 (Fibonacci Number) 的概念推广至三个数。 * T(0)=1, T(1)=1, T(2)=2, T(n)=T(n-1)+T(n-2)+T(n-3), 用最优方式求T(n) * * 文中的三个算法的时间复杂度都是线性的,网上有log(n)级的通过矩阵乘法求解, * 自己还没有看明白,等以后有机会在研究吧原创 2010-11-17 23:32:00 · 6488 阅读 · 0 评论 -
如何设计一个模式是所有的排序方法都是稳定的
这个模式可以被设计为每个元素加入其位置索引信息,在比较操作时同时也比较位置信息即可。 附加空间:每个元素可以用lg(n)位来表示,共需O(nlg(n)) 附加时间: 最差的是所有元素都相等,渐进时间不变因为每个迭代增加了常量时间。原创 2011-06-24 11:16:00 · 1403 阅读 · 0 评论 -
数组面试算法题
1. question: Given an integer array of which both first half and second half are sorted. Write a function to merge the two parts to create one single sorted array in place [do not use any extra space]原创 2012-01-25 07:56:12 · 3659 阅读 · 0 评论