1. 程式人生 > >演算法中常見問題總結

演算法中常見問題總結

十、演算法部分

1、說明生活中遇到的二叉樹,用java實現二叉樹 這是組合設計模式。 我有很多個(假設10萬個)資料要儲存起來,以後還需要從儲存的這些資料中檢索是否存在某個資料,(我想說出二叉樹的好處,該怎麼說呢?那就是說別人的缺點),假如存在陣列中,那麼,碰巧要找的數字位於99999那個地方,那查詢的速度將很慢,因為要從第1個依次往後取,取出來後進行比較。平衡二叉樹(構建平衡二叉樹需要先排序,我們這裡就不作考慮了)可以很好地解決這個問題,但二叉樹的遍歷(前序,中序,後序)效率要比陣列低很多,原理如下圖:
程式碼如下: package com.huawei.interview;
public class Node {        public int value;        public Node left;        public Node right;        public void store(int value)        {               if(value<this.value)               {                      if(left == null)                      {                             left = new Node();
                            left.value=value;                      }                      else                      {                             left.store(value);                      }               }               else if(value>this.value)               {                      if(right == null)
                     {                             right = new Node();                             right.value=value;                      }                      else                      {                             right.store(value);                      }                                  }        }        public boolean find(int value)        {                    System.out.println("happen " + this.value);               if(value == this.value)               {                      return true;               }               else if(value>this.value)               {                      if(right == null) return false;                      return right.find(value);               }else               {                      if(left == null) return false;                      return left.find(value);               }        }        public  void preList()        {               System.out.print(this.value + ",");               if(left!=null) left.preList();               if(right!=null) right.preList();        }        public void middleList()        {               if(left!=null) left.preList();               System.out.print(this.value + ",");               if(right!=null) right.preList();                }        public void afterList()        {               if(left!=null) left.preList();               if(right!=null) right.preList();               System.out.print(this.value + ",");                }             public static void main(String [] args)        {               int [] data = new int[20];               for(int i=0;i<data.length;i++)               {                      data[i] = (int)(Math.random()*100) + 1;                      System.out.print(data[i] + ",");               }               System.out.println();               Node root = new Node();               root.value = data[0];               for(int i=1;i<data.length;i++)               {                      root.store(data[i]);               }               root.find(data[19]);               root.preList();               System.out.println();               root.middleList();               System.out.println();                            root.afterList();        } } -----------------又一次臨場寫的程式碼--------------------------- import java.util.Arrays; import java.util.Iterator; public class Node {        private Node left;        private Node right;        private int value;        //private int num;        public Node(int value){               this.value = value;        }        public void add(int value){               if(value > this.value)               {                      if(right != null)                             right.add(value);                      else                      {                             Node node = new Node(value);                                                       right = node;                      }               }               else{                      if(left != null)                             left.add(value);                      else                      {                             Node node = new Node(value);                                                       left = node;                      }                                  }        }        public boolean find(int value){               if(value == this.value) return true;               else if(value > this.value){                      if(right == null) return false;                      else return right.find(value);               }else{                      if(left == null) return false;                      else return left.find(value);                                  }        }        public void display(){               System.out.println(value);               if(left != null) left.display();               if(right != null) right.display();        }        /*public Iterator iterator(){        }*/        public static void main(String[] args){               int[] values = new int[8];               for(int i=0;i<8;i++){                      int num = (int)(Math.random() * 15);                      //System.out.println(num);                      //if(Arrays.binarySearch(values, num)<0)                      if(!contains(values,num))                             values[i] = num;                      else                             i--;               }               System.out.println(Arrays.toString(values));               Node root  = new Node(values[0]);               for(int i=1;i<values.length;i++){                      root.add(values[i]);               }               System.out.println(root.find(13));               root.display();        }        public static boolean contains(int [] arr, int value){               int i = 0;               for(;i<arr.length;i++){                      if(arr[i] == value) return true;               }               return false;        }

2、第1個人10,第2個比第1個人大2歲,依次遞推,請用遞迴方式計算出第8個人多大?

package cn.itcast; import java.util.Date; public class A1 {     public static void main(String [] args)     {         System.out.println(computeAge(8));     }     public static int computeAge(int n)     {         if(n==1) return 10;         return computeAge(n-1) + 2;     } }     public static void toBinary(int n,StringBuffer result)     {         if(n/2 != 0)             toBinary(n/2,result);         result.append(n%2);          }

3、排序都有哪幾種方法?請列舉。用JAVA實現一個快速排序。

public class QuickSort { /** * 快速排序 * @param strDate * @param left * @param right */ public void quickSort(String[] strDate,int left,int right){ String middle,tempDate; int i,j; i=left; j=right; middle=strDate[(i+j)/2]; do{ while(strDate[i].compareTo(middle)<0&& i<right) i++; //找出左邊比中間值大的數 while(strDate[j].compareTo(middle)>0&& j>left) j--; //找出右邊比中間值小的數 if(i<=j){ //將左邊大的數和右邊小的數進行替換 tempDate=strDate[i]; strDate[i]=strDate[j]; strDate[j]=tempDate; i++; j--; } }while(i<=j); //當兩者交錯時停止 if(i<right){ quickSort(strDate,i,right);//從 } if(j>left){ quickSort(strDate,left,j); } } /** * @param args */ public static void main(String[] args){ String[] strVoid=new String[]{"11","66","22","0","55","22","0","32"}; QuickSort sort=new QuickSort(); sort.quickSort(strVoid,0,strVoid.length-1); for(int i=0;i<strVoid.length;i++){ System.out.println(strVoid[i]+" "); } } }

4、金額轉換,阿拉伯數字的金額轉換成中國傳統的形式如:(¥1011)->(一千零一拾一元整)輸出。

去零的程式碼:     return sb.reverse().toString().replaceAll("零[拾佰仟]","零").replaceAll("零+萬","萬").replaceAll("零+元","元").replaceAll("零+","零"); public class RenMingBi {     /**      * @param args add by zxx ,Nov 29, 2008      */     private static final char[] data = new char[]{             '零','壹','貳','叄','肆','伍','陸','柒','捌','玖'         };     private static final char[] units = new char[]{         '元','拾','佰','仟','萬','拾','佰','仟','億'     };     public static void main(String[] args) {         // TODO Auto-generated method stub         System.out.println(                 convert(135689123));     }     public static String convert(int money)     {         StringBuffer sbf = new StringBuffer();         int unit = 0;         while(money!=0)         {             sbf.insert(0,units[unit++]);             int number = money%10;             sbf.insert(0, data[number]);             money /= 10;         }         return sbf.toString();     } }

5、從類似如下的文字檔案中讀取出所有的姓名,並打印出重複的姓名和重複的次數,並按重複次數排序:

1,張三,28 2,李四,35 3,張三,28