java面试宝典读书笔记(五)-涉及数据结构

来源:互联网 发布:生活的真相 知乎 编辑:程序博客网 时间:2024/06/06 00:27


1、存储结构:1)随机存取:可以随意直接存取任意一个元素,如内存可以通过地址直接访问任意一个空间。2)顺序存取:从前往后逐个访问像链表结构。3)索引存取:是为某个关键字建立索引表。4)散列存储:建立散列表相当于一种索引。链式存储是顺序存储。

2、面试题:有1千万个有重复的短信,以文本的形式保存,一条一行也有重复。请5分钟内中出重复最多的10条短信。

1)哈希表的方法:分若干组,进行边扫描边建立散列表。2)采用从小到大排序的办法。(可以从单个字搜索比较,找出重复的top10,然后两个字以此类推。对于字数多的除了hash算法,可以选择只抽取头。中和尾部进行粗判加快查找还要做以标记,从各组备选的top10比较得出结果)。3)采用内存映射。

3、面试题:有20个数组,每个数组有500个元素,并且是有序排列好的,现在在这20*500个数中找出排名前500的数
(从数组末端起始)从20个数组中各取一个数,并记录每个数的来源数组,建立一个含20个元素的大根堆。此时堆顶就是最大的数,取出堆顶元素,并从堆顶元素的来源数组中取下一个数加入堆,再取最大值,一直这样进行500次即可。复杂度500xlog(20)。

4、面试题:辗转相除法时间复杂度:欧几里德算法基于数论等式gcd(a,b)=gcd(b,a mod b)其时间复杂度O(longn)

5、==与equals:

class test {public static void main(String[] args) {String s1="abc";String s2=s1;String s5="abc";String s3=new String("abc");String s4=new String("abc");System.out.println(s1==s5);//trueSystem.out.println(s1==s2);//trueSystem.out.println(s1.equals(s2));//trueSystem.out.println(s3==s4);//falseSystem.out.println(s1.equals(s4));//trueSystem.out.println(s3.equals(s4));//true}  }

==判断两个变量是否指向同一个地址,equals判断两个object是否一样(String字符串“abc”放在常量池)

6、写一个函数,在给定的整数数组中找出支配者是多少,如果一个数组中没有支配者,“支配着” 是在数组中出现频率超过一半的整数,例如{3, 2, 3, 3, 0, 2, 3, 4, 3},其中3出现5次,5除以9大于0.5,所以3是支配者。

public class Data{       public static void main(String[] args) {          int[] ints = {3, 2, 3, 3, 0, 2, 3, 4, 3};          int j = judge(ints);               if(j == -1){              System.out.println("No dominator");          }else{              System.out.println("The dominator is : " + ints[j]);          }      }      private static int judge(int[] ints) {          Arrays.sort(ints);          int counter = 1;          for(int i=0; i< (ints.length-1); i++){              if(ints[i] == ints[i+1]){                  counter++;                  if((((double)counter) / ints.length) > 0.5){                      return i ;                  }              }else{                  counter = 1;              }                    }          return -1;      }  }  

7、约瑟环问题:30个人十五个教徒,十五个非教徒,站成环数到九离开。

class test {public static void main(String[] args) {boolean[] use=new boolean[30];for(int i=0;i<use.length;i++){use[i]=true;}int left=use.length;int countnum=0;int index=0;while(left>1){countnum++;if(countnum==9){countnum=0;use[index]=false;left--;}index++;if(index==use.length)index=0;}for(int i=0;i<use.length;i++){System.out.println(i+"="+use[i]+" ");}}  }

8、单例模式

public class SingletonTest {      private SingletonTest() {      }      private static final SingletonTest instance = new SingletonTest();      public static SingletonTest getInstancei() {          return instance;      }    }
饱汉式

public class SingletonTest {    private SingletonTest() {       }       // 定义一个SingletonTest类型的变量(不初始化,注意这里没有使用final关键字)    private static SingletonTest instance=null;       public static SingletonTest getInstance() {           if (instance == null)               instance = new SingletonTest();           return instance;       }   } 
9、简述Struts下的模式
MVC模式(Model、View、controller)在MVC模式中,一个应用被划分成了模型(Model)、视图(View)和控制器(Controller)三个部分。模型是应用程序的主体部分,模型表示业务数据或者业务逻辑,视图是应用程序相关的部分是用户看到并与之交互的界面,控制器就是根据用户的输入控制用户界面数据显示和更新的model对象状态,mvc实现功能和显示的分离,同时提高了应用系统的可谓护性、可扩展性、可移植性和组件的可服用性。


0 0
原创粉丝点击