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 }
  • 相关阅读:
    openpyxl读取Excel数据
    查找xml中的接口名及涉及表名并输出
    sqlalchemy 简介
    linux文件查看
    网页的MVC模式简介
    python 最小二乘拟合,反卷积,卡方检验
    生成随机图片验证码
    图形界面
    requests(第三方模块) 请求、登录、下载网页
    ( 转 ) 什么是 JWT -- JSON WEB TOKEN
  • 原文地址:https://www.cnblogs.com/joycelee/p/4516146.html
Copyright © 2011-2022 走看看