zoukankan      html  css  js  c++  java
  • Delete False Elements

    Given an ArrayList<someObject> and a function " boolean test(someObject obj) " 

    Test every element in the array list, delete all element that returns F

    with minimum number of MOVE operation of elements

    tips : 

    1. MOVE - as for the feature of ArrayList, when we delete one element in the ArrayList, we should MOVE all other element behind it.

    2. Do Not have to keep element in the original order.

    hint: Something like the quick sort -- move all Ture element to the left and all False element to the right.

     1 public class deleteFElement {
     2     public static void main(String[] args) {
     3         ArrayList<Character> list = new ArrayList<Character>();
     4         list.add('a');
     5         list.add('b');
     6         list.add('2');
     7         list.add('t');
     8         list.add('4');
     9         list.add('3');
    10         list.add('p');
    11         int left = 0, right = list.size()-1;
    12         while (left <= right) {
    13             while (left <= right && test(list.get(left))) {
    14                 left++;
    15             }
    16             while (left <= right && !test(list.get(right))) {
    17                 right--;
    18             }
    19             if (left < right) {
    20                 Character tmp = list.get(left);
    21                 list.set(left, list.get(right));
    22                 list.set(right,  tmp);
    23             }
    24         }
    25         List<Character> list0 = list.subList(0, left);
    26         for (Character c : list0) {
    27             System.out.print(c+" ");
    28         }
    29         return;
    30     }
    31     private static boolean test(Character obj) {
    32         return obj < '0' || obj > '9';
    33     }
    34 }
  • 相关阅读:
    蛋疼的时候写三消游戏(三)
    Unity3d做的小游戏Demo《高铁惊魂》
    蛋疼的时候写三消游戏(四)
    Unity中调用http请求
    Unity中的单例
    Unity的IOS接入新浪微博
    Unity中实现热浪效果
    Unity开发摘要
    蛋疼的时候写三消游戏(二)
    蛋疼的时候写三消游戏(一)
  • 原文地址:https://www.cnblogs.com/joycelee/p/4516146.html
Copyright © 2011-2022 走看看