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 }
  • 相关阅读:
    Java实现web页面内容抓取
    Java写入文件的几种方法及性能对比
    Java实现导出excel
    win10系统安装VMware虚拟机软件以及linux系统
    oracle11g安装教程
    oracle怎么建立本地连接
    工厂模式
    计算一个字符串中每个字符出现的次数
    MySql多表查询
    如何查看MySql的sql语句性能
  • 原文地址:https://www.cnblogs.com/joycelee/p/4516146.html
Copyright © 2011-2022 走看看