1.反转问题
1.1 反转字符串
原题:力扣344.
要求原地修改。
public void reverseString(char[] s) {if (s == null || s.length() == 0) {return;}int n = s.length;for (int left = 0, right = n - 1; left < right; left++, right--) {char temp = s[left];s[left] = s[right];s[right] = temp;}
}
1.2 K个一组反转
原题:力扣541.
public String reverseStr(String s, int k) {if (s == null || s.length() == 0) {return s;}int n = s.length();char[] arr = s.toCharArray();for (int i = 0; i < n; i += 2 * k) {reverse(arr, i, Math.min(i + k, n) - 1);}return new String(arr);
}
public void reverse(char[] arr, int left, int right) {while (left < right) {char temp = arr[left];arr[left] = arr[right];arr[right] = temp;left++;right--;}
}
1.3 仅仅反转字母
原题:力扣917.
1.转换为数组,使用双指针,两个都指向字母就互换位置,否则就移动不是指向字母的指针。
class Solution {public String reverseOnlyLetters(String s) {if (s == null || s.length() == 0) {return s;}int left = 0, right = s.length() - 1;char[] arr = s.toCharArray();while (left < right) {if (Character.isLetter(arr[left]) && Character.isLetter(arr[right])) {char temp = arr[left];arr[left] = arr[right];arr[right] = temp;left++;right--;} else if (Character.isLetter(arr[left])) {right--;} else if (Character.isLetter(arr[right])) {left++;} else {left++;right--;}}return new String(arr);}
}
2.使用栈。
class Solution {public String reverseOnlyLetters(String s) {Stack<Character> letters = new Stack();for (char c : s.toCharArray()) {if (Character.isLetter(c)) {letters.push(c);}}StringBuilder ans = new StringBuilder();for (char c : s.toCharArray()) {if (Character.isLetter(c)) {ans.append(letters.pop());} else {ans.append(c);}}return ans.toString();}
}
3.维护一个指针从后向前遍历数组,需要字母的时候使用。
class Solution {public String reverseOnlyLetters(String s) {if (s == null || s.length() == 0) {return s;}StringBuilder ans = new StringBuilder();int j = s.length() - 1;for (int i = 0; i < s.length(); i++) {if (Character.isLetter(s.charAt(i))) {while (!Character.isLetter(s.charAt(j))) {j--;}ans.append(s.charAt(j--));} else {ans.append(s.charAt(i));}}return ans.toString();}
}
1.4 反转字符串里的单词
原题:力扣151.
1.调用API。
public String reverseWords(String s) {if (s == null || s.length() == 0) {return s;}s = s.trim();List<String> wordList = Arrays.asList(s.split("\\s+"));Collections.reverse(wordList);return String.join(" ", wordList);
}
2.自己实现。
class Solution {public String reverseWords(String s) {StringBuilder sb = trimSpaces(s);reverse(sb, 0, sb.length() - 1);reverseEachWord(sb);return sb.toString();}public StringBuilder trimSpaces(String s) {int left = 0, right = s.length() - 1;while (left <= right && s.charAt(left) == ' ') {left++;}while (left <= right && s.charAt(right) == ' ') {right--;}StringBuilder sb = new StringBuilder();while (left <= right) {char c = s.charAt(left);if (c != ' ') {sb.append(c);} else if (sb.charAt(sb.length() - 1) != ' ') {sb.append(c);}left++;}return sb;}// 将整个字符串进行翻转public void reverse(StringBuilder sb ,int left, int right) {while (left < right) {char temp = sb.charAt(left);// String 没有这个方法,但是 StringBuilder 和 StringBuffer 可以使用// 将指定索引处修改为指定的 char 类型值sb.setCharAt(left++, sb.charAt(right));sb.setCharAt(right--, temp);}}// 针对每个单词进行翻转public void reverseEachWord(StringBuilder sb) {int n = sb.length();int start = 0, end = 0;while (start < n) {while (end < n && sb.charAt(end) != ' ') {end++;}reverse(sb, start, end - 1);start = end + 1;end++;}}
}
2.验证回文串
原题:力扣125.
public boolean isPalindrome(String s) {if (s == null || s.length() == 0) {return s;}StringBuffer sgood = new StringBuffer();int length = s.length();for (int i = 0; i < length; i++) {char ch = s.charAt(i);if (Character.isLetterOrDigit(ch) {sgood.append(Character.toLowerCase(ch));}}int n = sgood.length();int left = 0, right = n - 1;while (left < right) {if (sgood.charAt(left) != sgood.charAt(right)) {return false;}left++;right--;}return true;
}
3.字符串中的第一个唯一字符
原题:力扣387.
public int firstUniqChar(String s) {if (s == null || s.length() == 0) {return 0;}Map<Character, Integer> frequency = new HashMap<>();for (int i = 0; i < s.length(); i++) {char ch = s.charAt(i);frequency.put(ch, frequency.getOrDefault(ch, 0) + 1);}for (int i = 0; i < s.length(); i++) {if (frequency.get(s.charAt(i)) == 1) {return i;}}return -1;
}
4.判定是否互为字符重排
原题:力扣242.
只需要判断字符种类以及每个种类出现的次数是否相同,相同则可以重排。
public boolean checkPermutation(String s1, String s2) {if (s1.length() != s2.length()) {return false;}char[] s1Chars = s1.toCharArray();Map<Character, Integer> s1Map = getMap(s1);Map<Character, Integer> s2Map = getMap(s2);for (char s1Char : s1Chars) {if (!s2Map.containsKey(s1Char) || (int)s2Map.get(s1Char) != (int)s1Map.get(s1Char)) {return false;}}return true;
}public Map<Character, Integer> getMap(String str) {Map<Character, Integer> map = new HashMap<>();char[] chars = str.toCharArray();for (char aChar : chars) {map.put(aChar, map.getOrDefault(aChar, 0) + 1);}return map;
}
如果对您有帮助,请点赞关注支持我,谢谢!❤
如有错误或者不足之处,敬请指正!❤
个人主页:星不易 ❤
算法通关村专栏:不易|算法通关村 ❤