标签:去哪儿网2015校招软件研发笔试题杭州站 则输出-a.相反 则输出+a 如果字符a在str1中出现
1、String padString(String string,int minLength,char padChar);minLength可能是负数
public static void main(String[] args) { System.out.println(padString("7", 3, '0')); System.out.println(padString("2012", 3, '0')); } static String padString(String string,int minLength,char padChar){ int len = string.length(); if(minLength<0 || minLength<len){ return string; } StringBuffer sb = new StringBuffer(); int pads = minLength - len; while(pads-->0){ sb.append(padChar); } sb.append(string); return sb.toString(); }
public static void main(String[] args) { System.out.println(parser("-name jack -age 20 -address \"HangZhou ZheDa Road\"")); System.out.println(parser(" -name jack -age 20 -address \"HangZhou ZheDa Road\"")); System.out.println(parser("-name jack -age 20 -address \"HangZhou ZheDa Road\"")); System.out.println(parser("-name jack -age 20 -address \"HangZhou ZheDa Road \" ")); } static String parser(String str){ String params[] = str.split("-"); for(int i=0;i<params.length;i++){ params[i]=params[i].trim();//去掉前后空格 params[i]=params[i].replaceAll("\\s+", " ");//去掉中间空格 } StringBuffer sb = new StringBuffer(); sb.append("["); for(int i=0;i<params.length;i++){ if(params[i].length()!=0 && !params[i].equals("")){ sb.append("-"); sb.append(params[i]); if(i!=params.length-1){ sb.append(", "); } } } sb.append("]"); return sb.toString(); }
public static void main(String[] args) { System.out.println(dif("abcde","bcde")); System.out.println(dif("dabc","aabcef")); System.out.println(dif("abcdefe","aabcadef")); } static String dif(String str1, String str2) { int flag[] = new int[26]; StringBuffer sb = new StringBuffer(); str1 = str1.toLowerCase().trim(); str2 = str2.toLowerCase().trim(); for(int i=0;i<str1.length();i++){ flag[str1.charAt(i)-'a']--; } for(int i=0;i<str2.length();i++){ flag[str2.charAt(i)-'a']++; } for(int i=0;i<flag.length;i++){ // System.out.println("flag["+i+"]="+flag[i]); if(flag[i]<0){ int count = Math.abs(flag[i]); for(int j=0;j<count;j++){ sb.append("-"); sb.append((char)(i+'a')); sb.append(","); } }else if(flag[i]>0){ for(int j=0;j<flag[i];j++){ sb.append("+"); sb.append((char)(i+'a')); sb.append(","); } } } if(sb.lastIndexOf(",")==sb.length()-1) sb.deleteCharAt(sb.length()-1); return sb.toString(); }
标签:去哪儿网2015校招软件研发笔试题杭州站 则输出-a.相反 则输出+a 如果字符a在str1中出现
原文地址:http://blog.csdn.net/zheng0518/article/details/39529181