做过的笔试题

1.有一数组S[] = {good,apple,fix,king,soft,god,food},要求将这个数组按照首字母分成多个数组,使其成为{apple},{fix,food},{good,god}这种,每一个数组内都是这样有序的。

View Code
 1 import java.util.ArrayList;
 2 
 3 
 4 public class strings {
 5 
 6     public ArrayList<ArrayList<String>> get(String[] s){
 7         ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>();
 8         for(int i=0;i<s.length;i++){
 9             ArrayList<String> array1 = null;
10             if(null!=s[i]){
11                 array1= new ArrayList<String>();
12                 array1.add(s[i]);
13                 char a=s[i].toString().charAt(0);
14                 for(int j=i+1;j<s.length;j++){
15                     if(null!=s[j]&&a==s[j].toString().charAt(0)){
16                         array1.add(s[j]);
17                         s[j]=null;
18                     }
19                 }
20                 s[i]=null;
21                 if(null!=array1){
22                     array.add(array1);
23                 }
24             }
25             
26         }
27         return array;
28     }
29     /**
30      * @param args
31      */
32     public static void main(String[] args) {
33 
34         String[] s = {"asdf","fsadf","as","fsfs"}; 
35         strings ab=new strings();
36         ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>();
37         array = ab.get(s);
38         for (ArrayList<String> a:array) {
39             for(String t: a){
40                 System.out.println(t);
41             }
42         }
43     }
44 
45 }

采取双ArrayList形式,不知道还有没有其他改进形式,欢迎讨论

******************************************************************************************

2.完成函数,将一个数组顺序移动循环右移n位。并写出其时间复杂度和空间复杂度。

View Code
 1 import java.util.LinkedList;
 2 
 3 
 4 public class MArray {
 5 
 6     public void Move(int [] array,int n){
 7         int t = n%array.length;
 8         int[] tempArray = new int[t];
 9         for(int i=array.length-1;!(i<0);i--){
10             if((i+t)>(array.length-1)){
11                 tempArray[i+t-array.length] = array[i];
12             }else{
13                 array[i+t]=array[i];
14             }
15         }
16         for(int i=0;i<t;i++){
17             array[i]=tempArray[i];
18         }
19     }
20     
21     public void Move2(int [] array,int n){
22         int t = n%array.length;
23         while(t>0){
24             int temp=0;
25             for(int i=array.length-1;i>0;--i){
26                 if(i==(array.length-1)){
27                     temp = array[0];
28                     array[0] = array[i];
29                     array[i] = temp;
30                 }else{
31                     temp = array[i+1];
32                     array[i+1] = array[i];
33                     array[i] = temp;
34                 }
35             }
36             --t;
37         }
38     }
39     
40     public void Move3(int[] array,int n){
41         int t = n%array.length;
42         LinkedList <Integer> queue = new LinkedList<Integer>();
43         for(int i=t;i>0;i--){
44             queue.offer(array[array.length-i]);
45         }
46         for(int i=0;i<array.length-t;i++){
47             queue.offer(array[i]);
48         }
49         for(int i=0;i<array.length;i++){
50             array[i] = queue.poll();
51         }
52     }
53     
54     /**
55      * @param args
56      */
57     public static void main(String[] args) {
58 
59         int[] array = {1,2,3,4,5,6,7,8,9,0};
60         int n = 3;
61         MArray ma = new MArray();
62         ma.Move3(array, n);
63         for(int i:array){
64             System.out.println(i);
65         }
66 
67     }
68 
69 }

方法一:延长数组n位,n为移动的位数,将数组向右移动,然后将延长的n项,覆盖原数组的前n项。时间复杂度o(n),n为数组长度,空间复杂度为o(n),n为移动位数。

方法二:定义一个变量,将原数组每次右移以为,共完成n次。时间复杂度o(n^2),空间复杂度o(1)。

方法三:定义一个队列,先将欲移动的n项按原数组顺序入队列,然后将剩下的N-n项按原数组顺序入队列,然后,全部出队列。时间复杂度o(n),空间复杂度o(n)。

******************************************************************************************

3.有一个表testa如下图所示

查询总成绩大于135的人       查询每门课程的最高分

第一问:SELECT sname FROM testa GROUP BY sname HAVING SUM(score)>135

 在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用。

SELECT SUM(score) from testa group by class

第二问:SELECT class,sname,MAX(score) FROM testa GROUP BY class

******************************************************************************************

4.将一个二叉树按中序输出的顺序,转换成一个双向链表,树和链表的节点结构如下图:

解法:

 1 package BinaryTree2Array;
 2 
 3 import java.util.LinkedList;
 4 
 5 public class BinaryTree2Array {
 6     
 7     public LinkedList<Node> getArray(Node root){
 8         Node currentNode = root;
 9         Node parrentNode = null;
10         LinkedList<Node> stack = new LinkedList<Node>();
11         LinkedList<Node> result = new LinkedList<Node>();
12         while (currentNode!=null) {
13             if(null!=currentNode.left){
14                 stack.push(currentNode);
15                 currentNode = currentNode.left;
16             }else{
17                 if(stack.size()>0){
18                     parrentNode = stack.pop();
19                     result.offer(currentNode);
20                     result.offer(parrentNode);
21                     currentNode = parrentNode.right;
22                 }else{
23                     currentNode = null;
24                 }
25             }
26             
27         }
28         return result;
29     }
30 
31     /**
32      * @param args
33      */
34     public static void main(String[] args) {
35         // TODO Auto-generated method stub
36 
37         Node g = new Node(null, null, 7);
38         Node f = new Node(null, null, 6);
39         Node e = new Node(null, null, 5);
40         Node d = new Node(null, null, 4);
41         Node c = new Node(f, g, 3);
42         Node b = new Node(d, e, 2);
43         Node a = new Node(b, c, 1);
44         Node temp = null;
45         
46         BinaryTree2Array bTree2Array = new BinaryTree2Array();
47         LinkedList<Node> rs = bTree2Array.getArray(a);
48         for(int i=0;i<rs.size()-1;i++){
49             rs.get(i).right=rs.get(i+1);
50             rs.get(i+1).left=rs.get(i);
51         }
52         temp = rs.peek();
53         while (temp!=null) {
54             System.out.print(temp.value);
55             temp=temp.right;
56         }
57     }
58 
59 }
60 
61 class Node{
62     Node left;
63     Node right;
64     int value;
65     public Node(Node left,Node right,int value){
66         this.left = left;
67         this.right = right;
68         this.value = value;
69     }
70 }

******************************************************************************************

5.找出100以内所有质数

质数定义:所谓质数就是只能被1和它本身整除的数那么对于某一个数a,可以试着让它除以a-1......2,如果有任意一次除法的余数为零,这个数a就不是质数。1既不是质数,也不是合数。

 1 public static void test4() {
 2          boolean bool;
 3          for (int i = 3; i < 100; i+=2) {
 4              bool = true;
 5              for (int j = 3; j <= Math.sqrt(i); j++) {
 6                  if (i % j == 0) {
 7                      bool = false;
 8              break;
 9                  }
10              }
11              if (bool)
12                  System.out.print(i + " ");
13          }
14      }

 

 

******************************************************************************************

6.取出字符串中的数字,并求其和。例如,贾屌生于1988年12月23日。取出其中的数字,算起和得2023

public class testString {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String str = "aaa17ffff10fsfsfsf11";
        String[] array = str.split("\\D");
        int sum = 0;
        for(String s : array){
            if(!s.equals("") ){
                sum += Integer.valueOf(s);
            }
        }
        System.out.println(sum);
    }

}

 

******************************************************************************************

7.一个数组中的一个元素出现的次数,超过整个数组的长度的1/2。求这个元素

 1 public class FindMost {
 2 
 3     public int find(int[] array){
 4         int result = 0;
 5         int temp = 0;
 6         for(int i=0;i<array.length;++i){
 7             if(temp == 0){
 8                 result =array[i];
 9                 temp++;
10             }else{
11                 if(result==array[i]){
12                     temp++;
13                 }else{
14                     temp--;
15                 }
16             }
17         }
18         return result;
19     }
20     public static void main(String[] args) {
21 
22         int[] array = {1,15,31,15,1,51,15,1,34,1,1,1,1};
23         FindMost fm = new FindMost();
24         System.out.println(fm.find(array));
25 
26     }
27 }

posted on 2012-10-28 20:06  chlde2500  阅读(169)  评论(0)    收藏  举报

导航