常见算法题目的考查:
1)模拟一个trim方法,去除字符串两端的空格。
2)将一个字符串进行反转。将字符串中指定部分进行反转。比如“abcdefg”反转为”abfedcg”
3)获取一个字符串在另一个字符串中出现的次数。 比如:获取“ ab”在 “abkkcadkabkebfkabkskab” 中出现的次数
4)获取两个字符串中最大相同子串。比如: str1 = "abcwerthelloyuiodef“;str2 = “cvhellobnm”
提示: 将短的那个串进行长度依次递减的子串与较长的串比较。
5)对字符串中字符进行自然顺序排序。 提示: 1字符串变成字符数组。 2对数组排序,择,冒泡,Arrays.sort(); 3将排序后的数组变成字符串。
package com.atguigu.exer;
import org.junit.Test;
import java.util.Arrays;
public class StringExer {
public String myTrim(String str) {
if (str != null) {
int start = 0;
int end = str.length() - 1;
while (start < end && str.charAt(start) == ' ') {
start++;
}
while (start < end && str.charAt(end) == ' ') {
end--;
}
if (str.charAt(start) == ' ') {
return "";
}
return str.substring(start, end + 1);
}
return null;
}
public String reverse1(String str, int start, int end) {
if(str != null){
char[] arr = str.toCharArray();
for (int i = start, j = end; i < j; i++, j--) {
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return new String(arr);
}
return null;
}
public String reverse2(String str, int start, int end) {
if(str != null) {
String newStr = str.substring(0, start);
for (int i = end; i >= start; i--) {
newStr += str.charAt(i);
}
newStr += str.substring(end + 1);
return newStr;
}
return null;
}
public String reverse3(String str, int start, int end) {
StringBuffer s = new StringBuffer(str.length());
s.append(str.substring(0, start));
for (int i = end; i >= start; i--) {
s.append(str.charAt(i));
}
s.append(str.substring(end + 1));
return s.toString();
}
public String reverse4(String str, int start, int end) {
if(str!=null){
StringBuilder builder = new StringBuilder(str.length());
builder.append(str.substring(0,start));
for (int i = end; i >= start ; i--) {
builder.append(str.charAt(i));
}
builder.append(str.substring(end + 1));
return builder.toString();
}
return null;
}
@Test
public void testReverse() {
String str = "abcdefg";
String str1 = reverse4(str, 2, 5);
System.out.println(str1);
}
public int getCount(String mainStr, String subStr) {
if (mainStr.length() >= subStr.length()) {
int count = 0;
int index = 0;
while ((index = mainStr.indexOf(subStr, index)) != -1) {
index += subStr.length();
count++;
}
return count;
} else {
return 0;
}
}
@Test
public void testGetCount() {
String str1 = "cdabkkcadkabkebfkabkskab";
String str2 = "ab";
int count = getCount(str1, str2);
System.out.println(count);
}
@Test
public void testMyTrim() {
String str = " a ";
String newStr = myTrim(str);
System.out.println("---" + newStr + "---");
}
public String getMaxSameSubString(String str1, String str2) {
if (str1 != null && str2 != null) {
String maxStr = (str1.length() > str2.length()) ? str1 : str2;
String minStr = (str1.length() > str2.length()) ? str2 : str1;
int len = minStr.length();
for (int i = 0; i < len; i++) {
for (int x = 0, y = len - i; y <= len; x++, y++) {
if (maxStr.contains(minStr.substring(x, y))) {
return minStr.substring(x, y);
}
}
}
}
return null;
}
public String[] getMaxSameSubString1(String str1, String str2) {
if (str1 != null && str2 != null) {
StringBuffer sBuffer = new StringBuffer();
String maxString = (str1.length() > str2.length()) ? str1 : str2;
String minString = (str1.length() > str2.length()) ? str2 : str1;
int len = minString.length();
for (int i = 0; i < len; i++) {
for (int x = 0, y = len - i; y <= len; x++, y++) {
String subString = minString.substring(x, y);
if (maxString.contains(subString)) {
sBuffer.append(subString + ",");
}
}
System.out.println(sBuffer);
if (sBuffer.length() != 0) {
break;
}
}
String[] split = sBuffer.toString().replaceAll(",$", "").split("\\,");
return split;
}
return null;
}
@Test
public void testGetMaxSameSubString() {
String str1 = "abcwerthelloyuiodef";
String str2 = "cvhellobnmiodef";
String[] strs = getMaxSameSubString1(str1, str2);
System.out.println(Arrays.toString(strs));
}
@Test
public void testSort() {
String str = "abcwerthelloyuiodef";
char[] arr = str.toCharArray();
Arrays.sort(arr);
String newStr = new String(arr);
System.out.println(newStr);
}
}
|