345. Reverse Vowels of a String
头尾指针开始扫描。
String.contains(char)
class Solution { public String reverseVowels(String s) { if(s == null || s.length() == 0) return s; String vowels = "aeiouAEIOU"; char[] chars = s.toCharArray(); int start = 0; int end = s.length() - 1; while(start < end){ while(start < end && !vowels.contains(chars[start]+"")) start++; while(start < end && !vowels.contains(chars[end]+"")) end--; char temp = chars[start]; chars[start] = chars[end]; chars[end] = temp; start++; end--; } return new String(chars); } }
205. Isomorphic Strings
数组下标可用字母
class Solution { public boolean isIsomorphic(String s, String t) { int[] m = new int[512]; for(int i = 0; i < s.length(); i++){ if(m[s.charAt(i)] != m[t.charAt(i) + 256]) return false; m[s.charAt(i)] = m[t.charAt(i) + 256] = i + 1; } return true; } }
原文:https://www.cnblogs.com/Afei-1123/p/11878286.html