再来两道华为字符串上机题目

1、输入一个字符串,删除重复前面的字符,其余字符按原样顺序输出.
 * 例如:
 * 输入:input: “acbdfdffZZet”;
 * 输出:output:”acbdfZet”;
 * 实现函数:String(String input)

String delString(String input){
  
           String output = “”; //声明输出字符串
           int i,j;
           char a,b;
  
           for(i = 0; i < input.length(); i++){
                     a = input.charAt(i);                  //获取input中里面当前字符
                     for(j = 0; j < output.length(); j++){
                                b = output.charAt(j);       //获取output中的字符,两进行比对,发现有相同的,则跳出循环,进行下一轮循环
                                if(a == b){
                                            break;
                                }
                     }
   
                    if(j == output.length()){          //直到循环结束都没有找到相同的字符,则将该字符加到输出字母的尾部
                               output += a;
                    }
           }
           return output;
 }

2、尾号限行:输入一串车牌号码,以逗号分割开,车牌号码有以下要求,只要取后五位,如:AD123,12101,中国 的车牌号不可能全是字母。
 * 现在对尾号进行限制:尾号为1,6则周一限行,尾号为2,7则周二限行,尾号为3,8则周三限行 尾号为4,9则周四限行,尾号为5,0的周五限行.
 * 周六周日不限行。尾号不为数字,则看第4位是否是数字,如果第4位还不是 数字,继续看第3位,以此下去,直到找到有数字的时候止.
 *
 * 由于用户不熟悉系统,有可能输入错误车牌,如车牌不满5位或大于5位、车牌全是字母、没用逗号分割等,如有输入错误情况 一律返回error
 * 如输入没有问题则返回限行的车牌号,如没有,刚返回空.
 *
 * 例如: 输入:String theCarCard = “A1023,NB123,D12ZG”,int limitDate = 3;
 *       输出:A1023,NB123
 *      
 * String limitedCarCard(String input)

String limitedCarCard(String input, int limitedDate){
            int i = 0;
            String output = “”;
            String[] tempStr = input.split(“,”); //将字符串分割成5个一组的子字符串,然后存入字符串数组
  
            for(String s:tempStr){
                      if(s.matches(“[A-Z]{5}”)){   //如果连续出现5个大写字母,返回错误
                                 System.out.println(s);
                                 output = “error”;
                      }
                      if(s.length() != 5){    //如果车牌号长度不等于5,返回错误
                                 System.out.println(s);
                                 output = “error”;
                      }
                      if(!s.matches(“[A-Z0-9]+”)){  //如果是有非A-Z或0-9中间的符号,返回错误
                                 System.out.println(s);
                                 output = “error”;
                      }
            }

            for(String s:tempStr){
                     switch(limitedDate){
                                 case 1:for(i = 4; i >= 0; i–){
                                                        if(s.charAt(i) == ‘1’ || s.charAt(i) == ‘6’){ //尾号是1或6,则限行
                                                                   output += s+”,”;
                                                                   break;
                                                        }
                                              }
                                              break;
                                 case 2:for(i = 4; i >= 0; i–){
                                                        if(s.charAt(i) == ‘2’ || s.charAt(i) == ‘7’){ //尾号是2或7,则限行
                                                                   output += s+”,”;
                                                                   break;
                                                        }
                                               }
                                               break;
                                 case 3:for(i = 4; i >= 0; i–){
                                                        if(s.charAt(i) == ‘3’ || s.charAt(i) == ‘8’){ //尾号是3或8,则限行
                                                                   output += s+”,”;
                                                                   break;
                                                        }
                                              }
                                              break;
                                  case 4:for(i = 4; i >= 0; i–){
                                                        if(s.charAt(i) == ‘4’ || s.charAt(i) == ‘9’){ //尾号是4或9,则限行
                                                                   output += s+”,”;
                                                                   break;
                                                        }
                                               }
                                               break;
                                  case 5:for(i = 4; i >= 0; i–){
                                                        if(s.charAt(i) == ‘5’ || s.charAt(i) == ‘0’){ //尾号是5或0,则限行
                                                                   output += s+”,”;
                                                                   break;
                                                        }
                                              }
                                              break;
                                  default:break;
                                  }
                      }
                     //去掉最后一个逗号
                     output = output.replaceAll(“,$”,””);

                     return output;
          }

 

点赞