java算法每日一练
来源:互联网 发布:淘宝整点秒杀是真的吗 编辑:程序博客网 时间:2024/05/22 12:44
描述:有一组数(43,14,6,9,3,27,38,19,5等)
排序:将最大的放在第一位,最小放在第二位,剩下的最大的放在第三位,最小的放在第四位,以此类推
输出排序结果
作为一个新手,首先看到题目感觉有点难,他不像一般的大小排序可以调用工具类直接排序。
仔细想想其实也不是那么的复杂:
1.难点1:本身是个无序的组合,如果是个排序好的数组,或许就没有那么难了
2.排完序后,只要按照最后一个放第一位,原来的第一位放在后面,下面的以此类推。
int[] a = new int[]{43,14,6,9,3,27,38,19,5}; Arrays.sort(a); for (int i = 0,j=a.length-1; i <= j;) { System.out.println(a[j--]); if(j==i) { System.out.println(a[i]); } if(j>i) { System.out.println(a[i++]); } }
按照上的思路很快就能实现功能了。
由此,复习一下最近用到的排序,最近的一个有个优先级排序的功能,具体的掠过:
功能描述大概是这样的:按照规则对对象进行优先级排序,相同的优先级需要随机返回。
下面的列子中People中的leavl是对象的优先级的体现。
public class People{public People(String name,String leavl,String id){this.id = id;this.name = name;this.leavl = leavl;}private String name;public String getName(){return name;}public void setName(String name){this.name = name;}public String getLeavl(){return leavl;}public void setLeavl(String leavl){this.leavl = leavl;}public String getId(){return id;}public void setId(String id){this.id = id;}private String leavl;private String id;}
public static void main(String[] args){People people1 = new People("cxm",1,"1");People people2 = new People("zxc",1,"2");People people3 = new People("zxz",2,"3");People people4 = new People("zxx",4,"4");People people5 = new People("zxv",2,"5");People people6 = new People("zxb",3,"6");People people7 = new People("zxn",4,"7");People people8 = new People("zxm",3,"8");List<People> list = new ArrayList<People>();list.add(people1);list.add(people2);list.add(people3);list.add(people4);list.add(people5);list.add(people6);list.add(people7);list.add(people8);Collections.sort(list, new Comparator<People>(){public int compare(People arg0, People arg1){return arg0.getLeavl().compareTo(arg1.getLeavl());}});Set<Integer> set = new HashSet<Integer>();set.add(people1.getLeavl());set.add(people2.getLeavl());set.add(people3.getLeavl());set.add(people4.getLeavl());set.add(people5.getLeavl());set.add(people6.getLeavl());set.add(people7.getLeavl());set.add(people8.getLeavl());Map<Integer ,List<People>> map = new HashMap<Integer, List<People>>();List<People> newList = null;for(Integer i : set){newList = new ArrayList<People>();for(People people : list){if(i.equals(people.getLeavl())){newList.add(people);}}map.put(i, newList);}List<People> returnList = new ArrayList<People>();for(Map.Entry<Integer, List<People>> entry :map.entrySet() ){returnList.addAll(getNewOrderList(entry.getValue()));}for(People people : returnList){System.out.println("name "+people.getName()+" leavl "+people.getLeavl() +" id "+people.getId());}}private static List<People> getNewOrderList(List<People> value){Random rand = new Random();int listIndex = rand.nextInt(value.size());List<People> returnList = new ArrayList<People>();returnList.add(value.get(listIndex));value.remove(listIndex);returnList.addAll(value);return returnList;}
public static void main(String[] args){People people1 = new People("cxm",1,"1");People people2 = new People("zxc",1,"2");People people3 = new People("zxz",2,"3");People people4 = new People("zxx",4,"4");People people5 = new People("zxv",2,"5");People people6 = new People("zxb",3,"6");People people7 = new People("zxn",4,"7");People people8 = new People("zxm",3,"8");List<People> list = new ArrayList<People>();list.add(people1);list.add(people2);list.add(people3);list.add(people4);list.add(people5);list.add(people6);list.add(people7);list.add(people8);Collections.sort(list, new Comparator<People>(){public int compare(People arg0, People arg1){return arg0.getLeavl().compareTo(arg1.getLeavl());}});Set<Integer> set = new HashSet<Integer>();set.add(people1.getLeavl());set.add(people2.getLeavl());set.add(people3.getLeavl());set.add(people4.getLeavl());set.add(people5.getLeavl());set.add(people6.getLeavl());set.add(people7.getLeavl());set.add(people8.getLeavl());Map<Integer ,List<People>> map = new HashMap<Integer, List<People>>();List<People> newList = null;for(Integer i : set){newList = new ArrayList<People>();for(People people : list){if(i.equals(people.getLeavl())){newList.add(people);}}map.put(i, newList);}List<People> returnList = new ArrayList<People>();for(Map.Entry<Integer, List<People>> entry :map.entrySet() ){returnList.addAll(getNewOrderList(entry.getValue()));}for(People people : returnList){System.out.println("name "+people.getName()+" leavl "+people.getLeavl() +" id "+people.getId());}}private static List<People> getNewOrderList(List<People> value){Random rand = new Random();int listIndex = rand.nextInt(value.size());List<People> returnList = new ArrayList<People>();returnList.add(value.get(listIndex));value.remove(listIndex);returnList.addAll(value);return returnList;}
上面运用的排序:
1.
Collections.sort(list, new Comparator<People>()
实现了优先级的排序
2.
Random rand = new Random();int listIndex = rand.nextInt(value.size());List<People> returnList = new ArrayList<People>();returnList.add(value.get(listIndex));value.remove(listIndex);returnList.addAll(value);
这里最终使用的那个值可能是People中的Id,实现了同一个优先级的随机返回。
测试的结果如下:
name zxc leavl 1 id 2 name cxm leavl 1 id 1 name zxc leavl 1 id 2name cxm leavl 1 id 1 name zxc leavl 1 id 2 name cxm leavl 1 id 1name zxv leavl 2 id 5 name zxz leavl 2 id 3 name zxz leavl 2 id 3name zxz leavl 2 id 3 name zxv leavl 2 id 5 name zxv leavl 2 id 5name zxm leavl 3 id 8 name zxb leavl 3 id 6 name zxm leavl 3 id 8name zxb leavl 3 id 6 name zxm leavl 3 id 8 name zxb leavl 3 id 6name zxn leavl 4 id 7 name zxn leavl 4 id 7 name zxn leavl 4 id 7name zxx leavl 4 id 4 name zxx leavl 4 id 4 name zxx leavl 4 id 4
- java算法每日一练
- 算法每日一练
- java每日一练
- java每日一练
- java每日一练
- java每日一练
- java每日一练
- java每日一练
- java每日一练
- java每日一练
- java每日一练
- java每日一练
- java每日一练
- java每日一练
- java每日一练
- java每日一练
- java每日一练
- java每日一练
- Perfect Permutation
- 树状数组
- 男人凭什么三十而立——用好自己的钱
- php 数字转换时间
- Android SDCard的容量大小
- java算法每日一练
- source insight配置
- android.widget.ImageView用法详解
- Spring AOP 的 Hello World
- Servlet中的过滤器(拦截器)Filter与监听器Listener的作用和区别
- 结构体详解
- SPEC组织发布Specviewperf 11综合测试软件
- 油田合并(dfs实现)
- java求集合的子集算法