
算法
yoyochina
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
一道算法题
package cn.lifx.test; public class OneToNineTest { public static void main(String[] args) { OneToNineTest test = new OneToNineTest(); test.findNum(); } public void findNum() ...2009-10-08 11:19:57 · 167 阅读 · 0 评论 -
Java堆排序
如下面代码所示(先建立大根堆,然后进行排序): public class HeapSort { public static void main(String[] args) { int[] a = {26, 5, 77, 1, 61, 11, 59, 15, 48, 19}; Sort(a); } public static void Sort(...2009-06-28 20:06:30 · 93 阅读 · 0 评论 -
电梯调度问题
这是《编程之美》上面的一道题,大体看过一点,早就忘了,昨天考试又遇到了,就想了下。下面的代码中第一个方法时间复杂度为O(n*n),第二个方法为O(n)。 public class FloorNum { public static void main(String[] args) { int[] arr = {3, 2, 7, 5, 9, 4, 6}; cal...2009-12-17 18:23:56 · 180 阅读 · 0 评论 -
动态规划之矩阵链乘法的Java实现
下面的代码是对《算法导论》(第二版)第十五章第二节内容的实现。这个算法的时间复杂度是O(n3)(n的3次方)。 下面的网址是网上很常见的一个c++的算法实现: http://blog.youkuaiyun.com/ujs_abc/archive/2008/02/01/2076876.aspx public class MatrixOrder2 { pri...2009-07-01 20:11:50 · 386 阅读 · 0 评论 -
动态规划之最长公共子序列的Java实现
这是算法导论动态规划一章讲的内容。 public class LCSProblem { public static void main(String[] args) { String[] x = {"", "A", "B", "C", "B", "D", "A", "B"}; String[] y = {"&2009-07-03 11:09:50 · 269 阅读 · 0 评论 -
一道经典的Google算法题
这是一道google的比较经典算法题,题目是:已经两个已经排好序的数组,找出两个数组合起来的中间大的数字。要求算法复杂度尽可能低。如:x数组:1,7,9,10,30 y数组:3,5,8,11 则中间大数为:8 下面是我自己想的算法。 public class GetMiddleValue { public static void mai...2009-07-04 12:23:06 · 187 阅读 · 0 评论 -
java之全排列
如下面代码: import java.util.ArrayList; public class FullSort { public static void main(String[] args) { String str = "2468"; FullSort fs = new FullSort(); ArrayList<Strin...原创 2010-01-11 16:28:32 · 124 阅读 · 0 评论 -
打印九九乘法表
如下代码: public class PrintJJTable { public static void main(String[] args) { PrintJJTable t = new PrintJJTable(); t.Display(); } public void Display() { for(int i=1; i<=9;...原创 2010-01-11 20:59:22 · 163 阅读 · 0 评论 -
java之金额大小写转换
代码如下,但是感觉不太好。 public class MoneyConvert { private final String[] str1 = {"元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿"}; private final String[] str2 = {"原创 2010-01-16 20:15:20 · 202 阅读 · 0 评论 -
判断有序数组中是否存在两个唯一的元素
对一个已排序的数组,判断里面是否有两个唯一的元素,即这两个元素跟其他元素都不一样。 public class Search { public static void main(String[] args) { int[] a = {3, 3, 3, 4, 4, 5, 6, 6, 6, 7, 7, 8, 8, 8, 9}; search(a); ...2009-08-04 15:18:01 · 302 阅读 · 0 评论 -
Java之基数排序
该基数排序算法先对低位进行排序,然后对高位排序,而对每一位排序时又采用了计数排序算法。代码如下: package cn.lifx.array.sort; public class RadixSort { public static void main(String[] args) { int[] a = {123, 963, 234, 546, 361, 798, ...2009-09-08 10:51:52 · 125 阅读 · 0 评论 -
Java之计数排序
至于啥叫计数排序就不说了,下面是Java的实现,计数排序就是不用进行元素的比较,效率较高,只是其适用范围有限(特殊条件)。 public class CountSort { public static void main(String[] args) { int[] a = {3, 1, 6, 0, 3, 0, 1, 5, 3, 6}; int max = ...2009-06-25 13:56:41 · 95 阅读 · 0 评论 -
计算数组中的最小值和最大值
下面代码中的第一种方法是最简单最容易想到的方法,但比较次数较多,所用时间较长。第二种方法的效率就高一些了。 public class GetMinMaxValue { public static void main(String[] args) { int[] a = {5, 2, 7, 16, 9, 64, 35, 79, 96, 28}; getMinM...2009-06-24 10:38:36 · 275 阅读 · 0 评论 -
Java判断一个整数是否为奇数
如下面的代码所示: public class isOdd { public static void main(String[] args) { int a = 3; int b = -3; //方法1 System.out.println((a % 2) != 0); System.out.println((b % 2) != 0); //方法2 ...2009-02-12 10:36:20 · 632 阅读 · 0 评论 -
两道算法题
1. 字符串截取,输入为一个字符串和一个数字,但是如果遇到汉字不能只截取半个汉字。 package cn.lifx.test; public class GetNBitChar { public static void main(String[] args) { String str1 = "中abc国人def伟大"; GetNBitChar test...2009-10-23 10:19:53 · 139 阅读 · 0 评论 -
数组循环右移和约瑟夫环问题
1. 把数组中的每个数循环右移n位,要求时间复杂度O(n),空间复杂度O(1) package cn.lifx.test; public class RightMove { public static void main(String[] args) { int n = 10; int m = 3; int[] arr = new int[n];...2009-10-28 16:32:41 · 171 阅读 · 0 评论 -
冒泡、选择、插入排序
冒泡排序,如下: public class BubbleSort { public static void main(String[] args) { int[] array = new int[]{29, 23, 56, 40, 12, 63, 89, 7, 96, 38}; System.out.println("Before sort:"); D...2009-04-27 08:52:18 · 94 阅读 · 0 评论 -
全排列
使用递归进行操作,如下代码: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class TotalPermutation { private static int size; private static int c...2009-04-29 10:51:07 · 103 阅读 · 0 评论 -
归并排序
如下代码: public class MergeSort { private static int[] array = new int[]{23, 6, 18, 20, 45, 31, 79, 3, 64, 82, 99, 1}; public static void main(String[] args) { Display(array, "Befor s...2009-05-01 20:33:10 · 91 阅读 · 0 评论 -
解析算术表达式
下面的是一个比较简单的情况,很多情况都没考虑,比如不是数字(如字母等),比如是多位数的运算(如23+456)等。 public class SuffixCompute { private static String suffix = ""; public static void main(String[] args) { Stack<String> ...2009-05-02 15:03:21 · 98 阅读 · 0 评论 -
希尔排序
通过公式h=3*h+1生成间隔序列1,4,13,40,121,364……,使用中通过h=(h-1)/3减小间隔进行计算(也就是间隔依次为……,364,121,40,12,4,1)。 public class ShellSort { public static void main(String[] args) { int[] array = new int[]...2009-05-04 10:39:11 · 74 阅读 · 0 评论 -
快速排序
快速排序的效率较高。如下代码: public class QuickSort { public static void main(String[] args) { int[] array = new int[12]; for(int i=0; i<array.length; i++) array[i] = (int)(Math.random() * ...2009-05-05 10:38:33 · 105 阅读 · 0 评论 -
打印七进制的加法表和乘法表
前几天,在看《什么是数学时:对思想和方法的基本研究(增订版)》时,书中讲到了非十进制的计算,并列出了七进制运算的加法表和乘法表,于是,就想着把它打印出来。结果,前两天忙就忘记了,今天又想起来了,就写了一下。 public class Test1 { public static final int N = 7; public static void main(String...2008-12-28 12:57:26 · 1125 阅读 · 0 评论 -
Java判断一个正整数是不是2的乘方
这是在http://okruby.com/ruby-wenzhai/200901/27_551.html上面看到的一个题目,就写了一下。暂时就只想到这样做了,也许还有更好的算法。 import java.util.Scanner; public class Test { public static void main(String[] args) { int temp ...2009-02-09 16:37:17 · 289 阅读 · 0 评论 -
查找字符串中第一个非重复字符
题目描述:编写一个高效函数,找到字符串中首个非重复字符。如"total"首个非重复字符为'o',"teeter"为'r'。(时间复杂度最好为o(n))。代码如下: package cn.lifx.test; import java.util.Hashtable; public class FindFirstUnique { private static final in...2009-09-08 12:17:07 · 287 阅读 · 0 评论