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 }
  • 相关阅读:
    解决svn Authorization failed错误
    jQuery切换事件
    jQuery学习笔记
    EAS开发
    JavaScript第二课
    JavaScript学习第一课
    EAS常用工具类
    [转]OpenBLAS项目与矩阵乘法优化
    [转]mmap和madvise的使用
    [转]C++赋值运算符重载函数(operator=)
  • 原文地址:https://www.cnblogs.com/joycelee/p/4516146.html
Copyright © 2011-2022 走看看