面试题(2)

来源:互联网 发布:网络推手公司哪个好 编辑:程序博客网 时间:2024/06/04 20:10

100以内所有可以加为100的整数的组合

import java.util.LinkedList; 

public class  

  public static void main(String[] args)  

    split(10, 0);  

   

  static LinkedList<Integer> list new LinkedList<Integer>();  

  public static void split(int n, int base)  

    if (n == 0)  

      System.out.println(list);  

      return;  

     

    for (int base 1; <= n; i++)  

      list.addLast(i);  

      split(n i, i);  

      list.removeLast();  

     

   

  

 

2个有序列表,将重复的部分合并为一个有序列表

public static void test()  

  int[] 2, 3, 4, 4, 4, 4, 7, 8, 8, 8, 8, 9, 100, 130, 150, 160 };  

  int[] 4, 6, 7, 7, 7, 7, 8, 8, 9, 10, 100, 130, 130, 140, 150 };  

  int 0, 0;  

  ArrayList<Integer> al new ArrayList<Integer>();  

  while (i a.length && (j b.length))  

    if (a[i] b[j])  

      i++;  

    else if (a[i] b[j])  

      j++;  

    else  

      al.add(a[i]);  

      i++;  

      j++;  

     

   

  System.out.println(al);  

 

任意数字字母的全排列组合问题

import java.util.Set;  

import java.util.TreeSet;  

public class  

  public static void perm(char[] n, int beg, int end)  

    if (beg == end)  

      addNumber(String.valueOf(n));  

    else  

      for (int beg; <= end; ++i)  

        swap(n, beg, i);  

        perm(n, beg 1, end);  

        swap(n, beg, i);  

       

     

   

  

  public static void swap(char[] n, int x, int y)  

    if (x == || n[x] == n[y])  

      return;  

     

    char temp n[x];  

    n[x] n[y];  

    n[y] temp;  

   

  public static void addNumber(String str)  

    set.add(str);  

   

  public static Set<String> set new TreeSet<String>();  

  public static void main(String args[])  

    char[] number new char[] '1', '2', '3', '4', '5' };  

    perm(number, 0, number.length 1);  

    System.out.println(set.size());  

    int cols 10;  

    for (String set)  

      System.out.print(s ");  

      if (cols-- == 1)  

        System.out.println();  

        cols 10;  

       

     

   

 

背包问题算法的JAVA实现

import java.util.Arrays;  

public class  

  public static void knapsack(int[] value, int[] weight, int capicity, int[][] m)  

    int value.length 1;  

    int jMax Math.min(weight[n] 1, capicity);  

    for (int 0; <= jMax; j++)  

      m[n][j] 0; // 当w[n]>j 有 m[n][j]=0  

    showArray(m);  

    // 后半部分设置为此物品的价值  

    // m[n][j] 表示只有w[n]物品,背包的容量为j时的最大价值  

    for (int weight[n]; <= capicity; j++)  

      m[n][j] value[n]; // 当w[n]<=j 有m[n][j]=v[n]  

    showArray(m);  

    // 递规调用求出m[][]其它值,直到求出m[0][c]  

    for (int 1; >= 1; i--)  

      jMax Math.min(weight[i] 1, capicity);  

      System.out.println(jMax);  

      for (int 0; <= jMax; k++)  

        m[i][k] m[i 1][k];  

      showArray(m);  

      for (int weight[i]; <= capicity; h++)  

        System.out.println(m[i+1][h]+" "+ m[i 1][h weight[i]]+" +value[i]+"("+h+","+weight[i]+","+value[i]+")");  

        m[i][h] Math.max(m[i 1][h], m[i 1][h weight[i]] value[i]);  

       

      showArray(m);  

     

    m[0][capicity] m[1][capicity];  

    if (capicity >= weight[0])  

      m[0][capicity] Math.max(m[0][capicity], m[1][capicity weight[0]] value[0]);  

    System.out.println("bestw =" m[0][capicity]);  

   

  

  public static void showArray(int[][] m)  

    for (int[] m)  

      System.out.println(Arrays.toString(a));  

     

    System.out.println("-------------------------------------------");  

   

  

  public static void traceback(int[][] m, int[] w, int c, int[] x) {// 根据最优值求出最优解  

    int w.length 1;  

    for (int 0; n; i++)  

      if (m[i][c] == m[i 1][c])  

        x[i] 0;  

      else  

        x[i] 1;  

        -= w[i];  

       

    x[n] (m[n][c] 0) 0;  

   

  

  public static void main(String[] args)  

    // 测试  

    int[] ww 8,5,4,3 };  

    int[] vv 10,7,5,4 };  

    int[][] mm new int[4][13];  

    knapsack(vv, ww, 12, mm);  

    int[] xx new int[ww.length];  

    traceback(mm, ww, 12, xx);  

    for (int 0; xx.length; i++)  

      System.out.println(xx[i]);  

   

 

按数字首位逐列打印

import java.util.Queue;  

import java.util.concurrent.LinkedBlockingQueue;  

public class  

  public static void main(String[] args)  

    int[] tArr 23, 2, 3, 234, 365, 564, 12, 13, 34, 35, 3453, 223, 6, 744354, 23131, 5657,  

        999, 877, 772, 877 };  

    // 10个队列  

    Queue<Integer>[] qs new Queue[10];  

    for (int 0; <= 9; i++)  

      qs[i] new LinkedBlockingQueue<Integer>();  

     

    for (int tArr)  

      qs[String.valueOf(a).charAt(0) '0'].add(a);  

     

    int maxSize 0;  

    for (int 0; <= 9; i++)  

      if (qs[i].size() maxSize)  

        maxSize qs[i].size();  

       

     

    Integer tmp;  

    // 开始显示  

    for (int row 0; row maxSize; row++)  

      for (int 0; <= 9; i++)  

        tmp qs[i].poll();  

        System.out.printf("%-7s", tmp == null "" tmp);  

       

      System.out.println();  

     

   

 

JAVA去掉一个已经排好序的数组的重复数字,尽量快

import java.util.Arrays;  

public class Test  

       public static void main(String args[])  

    int[] arr 1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 6, 6, 7, 8, 9, 9, 10, 11, 11,  

        11, 12, 12, 13, 14, 14, 15 }; // 预设数据数组  

    int index 1; // 保存最后一个不重复的位置  

    int last arr[0];  

    for (int 1; arr.length; i++)  

      if (arr[i] != last)  

        arr[index] arr[i];  

        last arr[index];  

        index++;  

       

     

    int[] rtn new int[index];  

    System.arraycopy(arr, 0, rtn, 0, index);  

    System.out.println(Arrays.toString(rtn));  

   

 

按照字母排序

1. import java.util.Arrays;  

2. import java.util.Comparator;  

3.   class  

4.   private static final int MASK 0xFFDF; // 用来把小写变成大写  

5.   public static int compare(String o1, String o2)  

6.     int length1 o1.length();  

7.     int length2 o2.length();  

8.     int length length1 length2 length2 length1;  

9.     int c1, c2;  

10.    int d1, d2;  

11.    for (int 0; length; i++)  

12.      c1 o1.charAt(i);  

13.      c2 o2.charAt(i);  

14.      d1 c1 MASK;  

15.      d2 c2 MASK;  

16.      if (d1 d2)  

17.        return 1;  

18.      else if (d1 d2)  

19.        return -1;  

20.      else  

21.        if (c1 c2)  

22.          return 1;  

23.        else if (c1 c2)  

24.          return -1;  

25.         

26.       

27.     

28.    if (length1 length2)  

29.      return 1;  

30.    else if (length1 length2)  

31.      return -1;  

32.     

33.    return 0;  

34.    

35.   

36.  public static void sortByPOPO(String[] args)  

37.    String tmp;  

38.    for (int 0; args.length; i++)  

39.      for (int 1; args.length; j++)  

40.        if (compare(args[i], args[j]) 0)  

41.          tmp args[i];  

42.          args[i] args[j];  

43.          args[j] tmp;  

44.         

45.       

46.     

47.    // [Ad, aC, Bc, During, day, Hello, little, man]  

48.    System.out.println(Arrays.toString(args));  

49.    

50.   

51.  public static void main(String[] args)  

52.    String[] strs "Bc", "Ad", "aC", "Hello", "X man", "little", "During",  

53.        "day" };  

54.   

55.    sortByPOPO(strs);  

56.   

57.    

58. }  

用二分查找法判断任意整数在任意整数数组里面是否存在,若存在就返回它在数组中的索引位置,不存在返回-1

1. public class Test  

2.   public int binarySearch(int[] dataset, int data, int beginIndex, int endIndex)  

3.     int midIndex (beginIndex endIndex) 2;  

4.     if (data dataset[beginIndex] || data dataset[endIndex] || beginIndex endIndex)  

5.       return -1;  

6.     if (data dataset[midIndex])  

7.       return binarySearch(dataset, data, beginIndex, midIndex 1);  

8.     else if (data dataset[midIndex])  

9.       return binarySearch(dataset, data, midIndex 1, endIndex);  

10.    else  

11.      return midIndex;  

12.     

13.    

14.  public int binarySearch(int[] dataset, int data)  

15.    int beginIndex 0;  

16.    int endIndex dataset.length 1;  

17.    int midIndex -1;  

18.    if (data dataset[beginIndex] || data dataset[endIndex] || beginIndex endIndex)  

19.      return -1;  

20.    while (beginIndex <= endIndex)  

21.      midIndex (beginIndex endIndex) 2;  

22.      if (data dataset[midIndex])  

23.        endIndex midIndex 1;  

24.      else if (data dataset[midIndex])  

25.        beginIndex midIndex 1;  

26.      else  

27.        return midIndex;  

28.       

29.     

30.    return -1;  

31.    

32.   

33.  public static void main(String args[])  

34.    Test new Test();  

35.    int[] ids 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };  

36.    System.out.println(t.binarySearch(ids, 8, 0, ids.length-1));  

37.    System.out.println(t.binarySearch(ids, 8));  

38.    

39. }  

输入任意一个整数,返回它的阶乘

1. public class Test  

2.   public long factorial(int n)  

3.     // 递归  

4.     if (n == 1)  

5.       return 1;  

6.      

7.     return factorial(n 1);  

8.    

9.   

10.  public long factorial2(int n)  

11.    // 非递归  

12.    long multi 1;  

13.    for (int 2; <= n; i++)  

14.      multi *= i;  

15.     

16.    return multi;  

17.    

18.   

19.  public static void main(String args[])  

20.    Test new Test();  

21.    System.out.println(t.factorial(12));  

22.    System.out.println(t.factorial2(12));  

23.    

24. }  

能够判断任意一个整数是否素数

1. public class Test  

2.   public boolean isPrimes(int n)  

3.     for (int 2; <= Math.sqrt(n); i++)  

4.       if (n == 0)  

5.         return false 

6.        

7.      

8.     return true 

9.    

10.   

11.  public static void main(String args[])  

12.    Test new Test();  

13.    System.out.println(t.isPrimes(987));  

14.    

15. }  

给定一个java.util.Date对象,如何转化为”2007-3-22 20:23:22”格式的字符串

1. import java.text.SimpleDateFormat;  

2. import java.util.Date;  

3. public class Test  

4.   public String dateToStr(java.util.Date date)  

5.     SimpleDateFormat sdf new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  

6.     String str sdf.format(date);  

7.     return str;  

8.    

9.   

10.  public static void main(String args[])  

11.    Test new Test();  

12.    t.dateToStr(new Date());  

13.    

14. }  

 

原创粉丝点击