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 }
  • 相关阅读:
    一生中常用工具
    Visual Studio2005 + Visual SourceSafe 2005 实现团队开发、
    如何正确处理SQL SERVER日志文件
    Oracle SQL精妙SQL语句讲解
    Asp.Net 备份和恢复SQL SERVER 数据库
    学习中
    oracle函数[单行字符串函数]
    个人博客大收集
    UML站点
    ASP.NET(c#)常用类函数
  • 原文地址:https://www.cnblogs.com/joycelee/p/4516146.html
Copyright © 2011-2022 走看看