String算法考察小练习(康师傅较优解法)
1 package day2_9; 2 3 import java.util.Arrays; 4 5 /** 6 * @Author Tianhao 7 * @create 2021-02-09-15:05 8 */ 9 public class StringExer { 10 11 12 13 //实现和trim()一样的作用,去掉收尾空格 14 public String myTrim(String str) { 15 16 int start = 0; 17 int end = 0; 18 char[] chars = str.toCharArray(); 19 for (int i = 0; i < chars.length; i ) { 20 if (!String.valueOf(chars[i]).equals(" ")) { 21 start = i; 22 break; 23 } 24 } 25 for (int i = chars.length -1; i >= 0; i--) { 26 if (!String.valueOf(chars[i]).equals(" ")) { 27 end = i; 28 break; 29 } 30 } 31 return str.substring(start, end 1); 32 } 33 34 35 //实现数组的反转,将字符串中指定部分进行反转 36 //比如“abcdefg",将其中的cdef反转后,输出为abfedcg 37 //方式一: 两个数组的元素赋值 38 // public String myReverse(String src,int start, int end) { 39 // if (src!=null) { 40 // String substring = src.substring(start, end 1); 41 // char[] chars = substring.toCharArray(); 42 // int len = chars.length; 43 // char[] newChars = new char[len]; 44 // for (int i = 0,j=len-1; i < len; i ,j--) { 45 // newChars[j] = chars[i]; 46 // } 47 // String newS = new String(newChars); 48 // return src.substring(0,start) newS src.substring(end 1); 49 // } 50 // return null; 51 // } 52 53 54 55 //方式二: 数组内元素的对调 56 // public String myReverse(String src,int start, int end) { 57 // if (src!=null) { 58 // char[] chars = src.toCharArray(); 59 // char tmp; 60 // for (int i = start, j = end; i < j; i , j--) { 61 // tmp = chars[i]; 62 // chars[i] = chars[j]; 63 // chars[j] = tmp; 64 // } 65 // return new String(chars); 66 // } 67 // return null; 68 // } 69 70 //方式三 拼接 71 // public String myReverse(String src,int start, int end) { 72 // if (src!=null) { 73 // //第一部分 74 // String target = src.substring(0, start); 75 // //第二部分 76 // for (int i = end; i >= start ; i--) { 77 // target = src.charAt(i); 78 // } 79 // //第三部分 80 // target = src.substring(end 1); 81 // return target; 82 // } 83 // return null; 84 // } 85 86 //方式三 使用StringBuffer/StringBuilder 替换String 87 public String myReverse(String src, int start, int end) { 88 if (src!=null) { 89 //注意选择适合的构造器(考虑到src字符串长度可能超过16) 90 StringBuilder builder = new StringBuilder(src.length()); 91 builder.append(src.substring(0, start)); 92 for (int i = end; i >= start ; i--) { 93 builder.append(src.charAt(i)); 94 } 95 builder.append(src.substring(end 1)); 96 97 return new String(builder); 98 } 99 return null;100 }101 102 103 104 //获取一个字符串在另一个字符串中出现的次数105 //方式一106 // public int myContainsCount(String src,String target) {107 // int srcLen = src.length();108 // int targetLen = target.length();109 // int count = 0;110 // int index;111 // if (srcLen>=targetLen) {112 // for (; ; ) {113 // index = src.indexOf(target);114 // if (index == -1) {115 // break;116 // }117 // count ;118 // src = src.substring(index targetLen);119 // }120 //121 // return count;122 // }123 // return 0;124 // }125 126 //获取一个字符串在另一个字符串中出现的次数127 //方式一128 public int myContainsCount(String src, String target) {129 int srcLen = src.length();130 int targetLen = target.length();131 int count = 0;132 int index = 0;133 if (srcLen>=targetLen) {134 //关键方法indexOf(String target,int index)135 //从自定索引开始查询目标字符串出现的索引位置136 while ((index = src.indexOf(target,index) )!= -1) {137 count ;138 index = targetLen;139 }140 return count;141 }142 return 0;143 }144 145 146 //获取两个字符串中最大相同子串147 //第一种方式148 // public String myLongSubstring(String one, String two) {149 // if (one != null && two != null) {150 // //max 长的字符串,min 短的字符串151 // String max;152 // String min;153 // if (one.length() >= two.length()) {154 // max = one;155 // min = two;156 // } else {157 // max = two;158 // min = one;159 // }160 // String sub;161 // String targetSub = "没有相同的子串";162 // int subLen = 0;163 // int index;164 // for (int i = 0; i < min.length(); i ) {165 // for (int j = min.length(); j > i; j--) {166 // sub = min.substring(i, j);167 // index = max.indexOf(sub);168 // if (index != -1) {169 // if (subLen < sub.length()) {170 // targetSub = sub;171 // subLen = sub.length();172 // }173 // }174 // }175 // }176 // return targetSub;177 // }178 // return null;179 // }180 181 //获取两个字符串中最大相同子串182 //第二种方式:183 public String myLongSubstring(String one, String two) {184 if (one != null && two != null) {185 //找出较长串和较短串186 String max = (one.length() >= two.length()) ? one : two;187 String min = (one.length() < two.length()) ? one : two;188 int length = min.length();189 for (int i = 0; i < length; i ) {190 //较短串的子串逐渐减短,看较长串是否包含191 for (int j = 0,k=length-i; k <= length; j ,k ) {192 String sub = min.substring(j, k);193 if (max.contains(sub)) {194 return sub;195 }196 }197 }198 }199 return null;200 }201 202 203 //拓展:获取两个字符串中最大相同子串并且是满足要求的所有最大相同子串204 public String[] myAllLongSubstring(String one, String two) {205 if (one != null && two != null) {206 StringBuilder builder = new StringBuilder();207 //找出较长串和较短串208 String max = (one.length() >= two.length()) ? one : two;209 String min = (one.length() < two.length()) ? one : two;210 int length = min.length();211 for (int i = 0; i < length; i ) {212 //较短串的子串逐渐减短,看较长串是否包含213 for (int j = 0,k=length-i; k <= length; j ,k ) {214 String sub = min.substring(j, k);215 if (max.contains(sub)) {216 builder.append(sub ",");217 }218 }219 220 if (builder.length() != 0) {221 break;222 }223 }224 //将最后一个,更换为"",以,切分为String[]225 return builder.toString().replaceAll(",$","")226 .split("\\,");227 }228 return null;229 }230 231 //对字符串中字符进行自然顺序排序232 public String mySort(String str) {233 char[] chars = str.toCharArray();234 Arrays.sort(chars);235 String newStr = new String(chars);236 return newStr;237 }238 239 }
测试
1 package day2_9; 2 3 import org.junit.Test; 4 import org.junit.runner.RunWith; 5 6 import java.util.Arrays; 7 8 import static org.junit.Assert.*; 9 10 /**11 * @Author Tianhao12 * @create 2021-02-09-15:1213 */14 15 public class StringExerTest {16 StringExer stringExer = new StringExer();17 18 @Test19 public void myTrim() {20 String s = stringExer.myTrim(" wang tian ");21 System.out.println(s);22 }23 24 @Test25 public void myReverse() {26 String s = stringExer.myReverse("abcdefg", 2,5);27 System.out.println(s);28 }29 30 @Test31 public void myContainsCount() {32 int count = stringExer.myContainsCount("xiaoqiang and xiaohong", "ng");33 System.out.println(count);34 35 }36 37 38 @Test39 public void myLongSubstring() {40 String target = stringExer.myLongSubstring("xiaoqiang and xiaohong", "hong is female,qiang is male.");41 System.out.println(target);42 43 }44 45 @Test46 public void myAllLongSubstring() {47 String[] target = stringExer.myAllLongSubstring("xiaoming and xiaohong", "hongis female,mingis male.");48 System.out.println(Arrays.toString(target));49 50 }51 52 @Test53 public void mySort() {54 String str = stringExer.mySort("yienhaodlcjkf");55 System.out.println(str);56 }57 58 59 //补充知识点60 @Test61 public void test() {62 String s = "wang tian";63 char[] chars = s.toCharArray();64 //数组工具类Arrays的复制方法copyOf(xx[],newLength)65 char[] chars1 = Arrays.copyOf(chars, chars.length-3);66 System.out.println(Arrays.toString(chars));67 System.out.println(Arrays.toString(chars1));68 69 70 //基本数据类型与String的转换71 //第一种方式:String.valueOf(xxx)72 int i = 1;73 System.out.println(String.valueOf(i));74 char c = 'w';75 System.out.println(String.valueOf(c));76 77 //第二种方式:基本数据对应的包装类.toString()78 //下面其实也是String.valueOf(int)的内部实现79 System.out.println(Integer.toString(i));80 81 //对于char型,有第三种方式:82 //下面是valueOf(char)的内部实现83 char[] charArr = {c};84 String s1 = new String(charArr);85 System.out.println(s1);86 87 88 89 }90 }
赞 (0)