package com.zte.array1;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class Main2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// System.out.println(isUniqueChars("strr"));
// System.out.println(replaceSpaces("hh ll"));
// System.out.println(isPermutationOfPalindrome("abass"));
// System.out.println(oneEditAway("pale", "ple"));
// System.out.println(compress("aabcccccaa"));
System.out.println(isFlipedString("waterbottle", "erbottlewat"));
}
/*
* 1.判断字符是否唯一
*/
public static boolean isUniqueChars(String str) {
if (str.length() > 128)
return false;
else {
boolean char_set[] = new boolean[128];
for (int i = 0; i < str.length(); i++) {
if (char_set[str.charAt(i)] == true)
return false;
else
char_set[str.charAt(i)] = true;
}
}
return true;
}
/*
* 2.判断字符是否为字符重排
*/
public static boolean sort(String s, String t) {
char[] source = new char[s.length()];
char[] destion = new char[t.length()];
Arrays.sort(source);
Arrays.sort(destion);
String sor = source.toString();
String des = destion.toString();
if (sor.equals(des))
return true;
else
return false;
}
/*
* 3.url化 编写一种方法将字符串空格替换为%20
*/
public static String replaceSpaces(String str) {
StringBuilder strbuilder = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != ' ')
strbuilder.append("" + str.charAt(i));
else
strbuilder.append("%20");
}
return String.valueOf(strbuilder);
}
/*
* 4.给定一个字符串,判断是否为某个回文串的排列之一
*
*/
public static boolean isPermutationOfPalindrome(String phrase) {
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < 26; i++) {
map.put((char) ('a' + i), 0);
}
int sum = 0;
int j = 0;
for (int i = 0; i < phrase.length(); i++) {
j = map.get(phrase.charAt(i));
map.put(phrase.charAt(i), ++j);
}
for (int i : map.values()) {
if (i % 2 != 0)
sum++;
}
if (sum <= 1)
return true;
else
return false;
}
/*
* 5.判断一个字符串是否经过一次编辑可成为另一个字符串
*/
public static boolean oneEditAway(String first, String second) {
if (first.length() - second.length() > 1 || first.length() - second.length() < -1)
return false;
else {
if (first.length() == second.length()) {
int sum = 0;
for (int i = 0; i < first.length(); i++) {
if (first.charAt(i) != second.charAt(i)) {
++sum;
if (sum > 1)
return false;
}
}
return true;
} else {
if (first.length() > second.length()) {
for (int i = 0; i < second.length(); i++) {
if (first.charAt(i) != second.charAt(i)) {
if (first.charAt(i + 1) != second.charAt(i))
return false;
}
}
} else {
for (int i = 0; i < first.length(); i++) {
if (first.charAt(i) != second.charAt(i)) {
if (first.charAt(i) != second.charAt(i + 1))
return false;
}
}
}
}
}
return true;
}
/*
*
* 6. 字符串压缩
*/
public static String compress(String str) {
int length = str.length();
StringBuilder strbuilder = new StringBuilder();
int j = 1;
if (length > 1) {
for (int i = 0; i < length - 1; i++) {
if (str.charAt(i) == str.charAt(i + 1)) {
j++;
} else {
strbuilder.append("" + str.charAt(i) + j);
j = 1;
}
if (i == length - 2) {
if (str.charAt(length - 1) != str.charAt(length - 2))
strbuilder.append("" + str.charAt(length - 1) + 1);
else
strbuilder.append("" + str.charAt(i) + j);
}
}
String str1 = String.valueOf(strbuilder);
if (str1.length() < str.length())
return str1;
else
return str;
} else
return str;
}
/*
* 7.旋转矩阵
*
*/
public static boolean rotate(int[][] matrix) {
if (matrix.length == 0 || matrix.length != matrix[0].length)
return false;
int n = matrix.length;
for (int layer = 0; layer < n / 2; layer++) {
int first = layer;
int last = n - 1 - layer;
for (int i = first; i < last; i++) {
int offset = i - first;
int top = matrix[first][i];// 存储上边
matrix[first][i] = matrix[last = offset][first];// 左边移到上边
matrix[last - offset][first] = matrix[last][last - offset];// 下边移到左边
matrix[last][last - offset] = matrix[i][last];// 右边移到下边
matrix[i][last] = top;
}
}
return false;
}
/*
* 8.0矩阵
*/
public static void setZeros(int[][] matrix) {
boolean[] row = new boolean[matrix.length];
boolean[] col = new boolean[matrix[0].length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == 0) {
row[i] = true;
col[j] = true;
}
}
}
// 置空行
for (int i = 0; i < matrix.length; i++) {
if (row[i]) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = 0;
}
}
}
// 置空列
for (int i = 0; i < matrix[0].length; i++) {
for (int j = 0; j < matrix.length; j++) {
if (col[i]) {
matrix[j][i] = 0;
}
}
}
}
/*
* 9.字符传轮转
*/
public static boolean isFlipedString(String s1, String s2) {
int length = s1.length();
char[] ch1 = s1.toCharArray();
char[] ch2 = s2.toCharArray();
char[] ch3 = new char[length];
String str1 = "";
if (s1.length() != s2.length())
return false;
else {
if (s1.length() > 0) {
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (j - i < 0)
ch3[j] = ch2[length + j - i];
else
ch3[j] = ch2[j - i];
}
str1 = String.valueOf(ch3);
if (s1.equals(str1)) {
return true;
}
}
} else {
return true;
}
}
return false;
}
}
程序员面试金典字符串数组
最新推荐文章于 2022-10-09 12:44:28 发布