zoukankan      html  css  js  c++  java
  • 每日一题 为了工作 2020 0416 第四十五题

    /**
     * 问题:字符串的调整与替换
     * 给定一个字符类型的数组chas[],其中只含有数字字符和"*"字符,要求把所有的"*"
     * 字符移动到chas的左边,数字字符移动到chas的右边,不得改变数字字符的顺序。
     * 要求:时间复杂度为O(N),空间复杂度为O(1)。
     */
    

      

    public class Modify {
    
        public static char[] modify(char chas[]){
            if (chas == null || chas.length == 0){
                return null;
            }
            int len = chas.length;
            char newArray[] = new char[len];
            for (int i= chas.length-1 ; i > 0 ; i--){
                if (chas[i] != '*'){
                    newArray[len - 1] = chas[i];
                    len --;
                }
            }
            for (int i = 0; i < newArray.length; i++){
                if (newArray[i] == 0){
                    newArray[i] = '*';
                }
            }
            return newArray;
        }
    
        public static void main(String[] args) {
            char chas[]=new char[]{'*','1','3','*','5','*'};
            showCharArray(chas);
            char[] result = modify(chas);
            showCharArray(result);
        }
        public static void showCharArray(char array[]){
            for (int i = 0 ; i < array.length ; i++){
                System.out.print(array[i] + "	");
            }
            System.out.println();
        }
    }
    

      

    *运行结果

     

  • 相关阅读:
    iOS -一些常用的方法
    handoff了解
    UIlabel
    扩展运行机制
    github -- fork提交项目
    iOS
    AppDelegate解析
    KVC
    KVO
    xcode升级后, 插件失效修复
  • 原文地址:https://www.cnblogs.com/walxt/p/12712148.html
Copyright © 2011-2022 走看看