您的位置:首页 > 职场人生

循环删除指定索引位置一道面试题算法

2008-10-29 20:25 134 查看

      前段时间朋友面试碰到过这样题,要在指定的短时间内实现起来有一定困难,网上也看到类似这样算法题,今天跟朋友同事一起讨论一下,现在把我的一些方法与想法实现代码贴出来,参考参考...先做个记录了

 

 

/**
*created by zxb
*date  2008-10-29 - 上午12:48:10
*zxb 开源测试项目 test算法研究
*to do TODO
*JDK5.0
**/
package com.test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
/**
* 题如下:指定500个人,编号从1-500,现在从1编号开始点名,当点到序号为7时,
* 让该人出列,后续接着继续开始点名从1开始,再次点到为7时,让该人出列,
* 一直点名直到最后只有一个为止,当点名到最后时,从新开始循环点名,
* 剩下的最后一个人的编号是多少?(一面试题,还是有点挑战性,抛砖引玉了)
* 解题思路:构造LinkedList<Integer>进行批量删除等的操作
* 循环设置出列点位置值为0,然后删除该点数据,循环至小于数据组长度小于7时,
* 重新调整开始点名数据组,始终把第一位点名的数字<人>放在实际所因位置也是第一位的位置
*  按照索引位置增量删除一个 因为每删除一个数据组少掉一个数
*
*
* 451
*@author zxb
*/
public class FiveHundred {

private  LinkedList<Integer> l=null;
private List<Integer> list=null;
private static final int MAX=7;//指定点名的出列的编号数 固定范围内进行循环删除指定的索引位置
private int count=0;   //多少个轮回次数
private static final int SUM=500;//待删除指定索引位置的数据组(1--500)
private static int size=0;//队列的动态个数
public FiveHundred(){
list=new ArrayList<Integer>();
for (int i = 1; i <= SUM; i++) {
list.add(Integer.valueOf(i));
}
l=new LinkedList<Integer>(list);
int m= l.size()%MAX;
if(m==0){
count=l.size()/MAX;
}else{
count=l.size()/MAX+1;
}
}
public void change(){
List<Integer> l_2=new ArrayList<Integer>();
int j=1;//叠加 根据后面补充
List<Integer> l_3=null;
//过滤掉为0的元素
int s1=l.size();
for (int i = 0; i < s1; i++) {
if(l.contains(new Integer(0))){
l.remove(l.indexOf(new Integer("0")));
}
}
//点名出列的临时设置为0 并且设置点名之后队伍列的数据组
int s2=l.size();
for (int i = 0; i < s2; i++) {
if(i==MAX*j-1 && j<=count){//
l.set(MAX*j-1, new Integer("0"));
j++;
}else{
l_2.add(l.get(i));
}
}
//截取数据从最后一个0开始往后  list数据
int lastIndexZero=l.lastIndexOf(new Integer("0"));
int size=l.size();
if(lastIndexZero<size-1){
l_3=l.subList(lastIndexZero+1, size);
}
if(l_2!=null && l_3!=null)
l=reverseList(l_2,l_3);
}
//反调过来进行 重新排序
public  LinkedList<Integer> reverseList(Collection<Integer> c1,Collection<Integer> c2){
LinkedList<Integer> l=new LinkedList<Integer>();
c1.removeAll(c2);
l.addAll(c2);
l.addAll(c1);
return l;
}

//处理特殊的情况,当剩下的个数小于指定出列的索引位置时 这里即 小于7时
@SuppressWarnings("unchecked")
public  void specialList(){
System.out.println(l);
int size=l.size();
List l1=new ArrayList<Integer>();
List l2=new ArrayList();
int count=0;
while(size!=1){
if((MAX-1) % size==0){
l.remove((MAX-1) % size);
}else{
//后补叠加替换
l.remove((MAX-1) % size);
l1=l.subList((MAX-1) % size, l.size());
LinkedList<Integer> l3=new LinkedList<Integer>();
l3=(LinkedList)l.clone();
l2=(l3.subList(0, (MAX-1) % size));
count=l1.size();
//使用删除Collection的删除方法 容易引起java.util.ConcurrentModificationException
//remove(),clear(); iterator.remove()
for (int i = 0; i < count; i++) {
l.set(i, (Integer) l1.get(i));
}
int s1=l2.size();
for (int i = 0; i < s1; i++) {
l.set(count+i, (Integer) l2.get(i));
}
}
size--;
System.out.println(l);

}/**/
System.out.println("获取到最后一个数字==="+l.get(0));
}

//程序启动测试
public static void main(String[] args) {
FiveHundred fh=new FiveHundred();
long lo1=System.currentTimeMillis();
size=fh.l.size();
do{
fh.change();
size--;
}while(size>1);
fh.specialList();
long lo2=System.currentTimeMillis();
System.out.println("计算花费时间==== "+(lo2-lo1)+" 毫秒");
}
}

 

 

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐