当前位置: 代码迷 >> J2SE >> 一些面试题求准确答案解决思路
  详细解决方案

一些面试题求准确答案解决思路

热度:207   发布时间:2016-04-24 18:02:51.0
一些面试题求准确答案
1,写出连接池的伪代码实现,要求实现getConnection()方法,并且有最大最小连接数。

2,写一个方法求一个字符串里面的每个字符和它的个数,如 “aabbcccerrrajjjb”;

3,狗叫了,主人醒了,小偷吓跑了,主人和小偷是被动的并且还有可扩展性,用什么样的设计模式并写出代码。

4,一个队列最多的元素为M,怎么用表达式表达出来,是个选择题,选项不记得了。

5,用非递归方法实现下列函数的值。
  f(1)=1
  f(2)=2
  f(n)=f(n-1)+f(n-2)

------解决方案--------------------
5.
非递归的菲波那契(Fibonacci)数列算法相信递归的 菲波那契(Fibonacci) 数列 算法程序大家都会写,在计算比较小的数字的时候没什么问题,但是随着数字增大,运算复杂度则成2的指数级(相乘)倍数递增。不信的话,大家可以写个递归的算法,算一下 Fibonacci(50)看看要多久,再看看Fibonacci(40),Fibonacci(30),差别大的一P。呵呵,,,,
闲极无聊,这里弄个基于加法的算法,复杂度为O(n)
  
//优化后的Fibonacci数列算法,非递归!!!!

long Fibonacci(int n)

{

long coef1=1,coef2=1,coef_tmp=0;

int j=0;

if(n<3)

return 1;

j=n-1;

while(j>2)

{

coef_tmp=coef1;

coef1=coef1+coef2;

coef2=coef_tmp;

j--;

}

return coef1+coef2;

}

//递归算法,参考

long Recur_Fibonacci(int n)

{

if (n>1) {

return Recur_Fibonacci(n-1)+Recur_Fibonacci(n-2);

}

if (n==1) {

return 1;

}

if (n==0) {

return 0;

}

return 0;

}

 

////

 

试了一下,效果还是不错的。
http://calmet.spaces.live.com/blog/cns!3C0C35EFD6F5F063!250.entry
------解决方案--------------------
Java code
1.//普通的连接类public class ConnectionManager {        /**     * 得到数据库连接     *      * @return 数据库连接对象     * @throws ClassNotFoundException     * @throws SQLException     */    public static Connection getCon() {        try {            String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";            Class.forName(driver);            return DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=zf", "sa", "sa");        } catch (ClassNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return null;    }        /**     * 关闭所有连接     * @param con 数据库连接     * @param ps      * @param rs     */    public static void closeAll(Connection con, PreparedStatement ps,            ResultSet rs) {        try {            if (rs != null) {                rs.close();            }            if (ps != null) {                ps.close();            }            if (con != null && !con.isClosed()) {                con.close();            }        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}//代理的连接对象public class ConnectionAdvice implements InvocationHandler {        //获取真实的对象    private Connection realCon;        public ConnectionAdvice(Connection realCon) {        // TODO Auto-generated constructor stub        this.realCon=realCon;    }    public Object invoke(Object proxy, Method method, Object[] args)            throws Throwable {        // TODO Auto-generated method stub        //获取方法名        String methodName=method.getName();                //判断是否为关闭的方法        if("close".equals(methodName)){            System.out.println("代理该方法");            //将代理对象放到池子中            ConnectionPool.restore((Connection)proxy);            return null;        }else{            //如果不是则用原有连接执行原有的方法            return method.invoke(realCon, args);        }            }}//代理工厂通过真实对象创建代理对象public class ProxyFactory {        //通过真实的对象获取代理对象    public static Connection create(Connection realCon){        //通过代理类Proxy        Connection proxy=(Connection) Proxy.newProxyInstance(                //类加载器                realCon.getClass().getClassLoader(),                 //获取它的所有接口                realCon.getClass().getInterfaces(),                //调用处理器                new ConnectionAdvice(realCon));        return proxy;    }}//连接池public class ConnectionPool {        //用LinkedList集合存取连接    private static List<Connection> conList=new LinkedList<Connection>();        //最大连接数    private static final int Total=10;        private static int curNum=5;//最小连接数        static{        for (int i = 0; i < 5; i++) {            //获取代理的连接            Connection proxy=ProxyFactory.create(ConnectionManager.getCon());            //添加到集合中            conList.add(proxy);        }    }        //从池子中获取连接的方法    public static synchronized Connection getCon(){        Connection proxyCon=null;        //如果集合中有连接        if(conList.size()>0){            if(conList.size()<=5){                //从集合中获取一个连接                proxyCon=conList.get(0);                //从集合中删除该连接                conList.remove(0);                return proxyCon;            }else{                curNum-=5;                for (int i = 0; i < 5; i++) {                    conList.remove(0);                }                proxyCon=conList.get(0);                //从集合中删除该连接                conList.remove(0);                return proxyCon;            }        }else{            //如果集合中没有连接了            //当前总数小于最大连接数,则增加5            if(curNum<Total){                curNum+=5;                for (int i = 0; i < 5; i++) {                    //获取代理的连接                    Connection proxy=ProxyFactory.create(ConnectionManager.getCon());                    //添加到集合中                    conList.add(proxy);                }                proxyCon=conList.get(0);                //从集合中删除该连接                conList.remove(0);                return proxyCon;            }else{                //如果等于最大连接数,则抛出异常让用户等待                try {                    throw new Exception("连接已经满,请等待!");                } catch (Exception e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                return null;            }        }            }        //放回到池子中!    public static synchronized void restore(Connection proxyCon){        conList.add(proxyCon);    }    }2.    //写一个方法求一个字符串里面的每个字符和它的个数    public void show(String s){        if(s!=null){            for (int i = 0; i < s.length(); i++) {                System.out.println(s.substring(i, i+1));            }            System.out.println("它的个数为:"+s.length());        }else{            System.out.println("为NULL");        }    }3.不会4.没看明白5.    /**     * 用非递归方法实现下列函数的值。       f(1)=1       f(2)=2       f(n)=f(n-1)+f(n-2)     */    public int fun(int n){                        if(n<=0)            return -1;//表示异常        if(n==1)            return 1;        if(n==2)            return 2;                int[] num=new int[n];        num[0]=1;        num[1]=2;        //从第三个数开始计算        for (int i = 2; i < num.length; i++) {            num[i]=num[i-1]+num[i-2];        }        return num[n-1];    }
  相关解决方案