# 一道简单的阶层面试题

privatestaticint factorial(int n){

int ret=1;

for(int i=2;i<=n;ret*=i++);

return ret;

}

publicstaticint combination1(int all,int select){

return factorial(all)/factorial(select)/factorial(all-select);

}

return factorial(all)/factorial(select)*factorial(all-select);

privatestaticint factorial(int n){

if (n<0)

thrownew InvalidParameterException();

int ret=1;

if (n==0 || n==1)

return ret;

else

for(int i=2;i<=n;ret*=i++);

return ret;

}

publicstaticint combination1(int all,int select){

if (select>all || all<=0 || select<=0)

thrownew InvalidParameterException();

if (select==all)

return 1;

return factorial(all)/factorial(select)/factorial(all-select);

}

publicstaticint combination2(int all,int select){

if (select>all || all<=0 || select<=0)

thrownew InvalidParameterException();

int ret=1;

int begin=0;

if (all==select)

return ret;

//1-begin之间的数被约分掉以简化计算

if (select<all-select)

begin=all-select+1;

else

begin=select+1;

for(int i=begin;i<=all;ret*=i++);    //计算分子

for(int i=all-begin+1;i>1;ret/=i--); //被分母除

return ret;

}

privatestatic Map<String,Integer> mapTable=new HashMap<String,Integer>();

……

publicstaticint combination3(int all,int select){

if (select>all || all<=0 || select<=0)

thrownew InvalidParameterException();

int ret=1;

int begin=0;

if (all==select){

return ret;

}

String tableKey=new String(all+","+select);

Integer tableValue=mapTable.get(tableKey);

if (tableValue!=null)

return tableValue.intValue();

if (select<all-select){

begin=all-select+1;

}

else{

begin=select+1;

}

for(int i=begin;i<=all;ret*=i++);

for(int i=all-begin+1;i>1;ret/=i--);

mapTable.put(tableKey, Integer.valueOf(ret));

return ret;

}

#### CSS并不简单--一道微信面试题的实践

2017-02-22 17:03:14

#### 一道简单而又不简单的面试题

2017-05-04 11:22:20

#### 一道常被人轻视的前端JS面试题

2016-02-21 23:30:48

#### 由一道面试题引起的疑问与思考

2008-02-05 22:53:00

#### web前端一道很“坑”的js面试题

2017-02-23 22:28:41

#### 微软经典面试题(附答案)

2012-04-10 18:21:31

#### 一道丧心病狂的java面试题

2018-03-07 00:00:00

#### 一道让我无地自容的百度面试题

2017-04-17 15:34:32

#### 又一道简单题

2016-08-23 16:50:12

#### 一道有意思的JS面试题

2017-03-02 11:33:11