Leetcode: 344 反转字符串
注意List的toArray()方法的泛型参数不能是int或char,只能是其包装类Integer或Character,故直接用List的reverse函数应该如下
class Solution {
public void reverseString(char[] s) {
Character[] newArray = new Character[s.length];
List<Character> list1 = new ArrayList<>();
for(char c: s) list1.add(c);
Collections.reverse(list1);
list1.toArray(newArray);
for(int i = 0; i < s.length; i++) s[i] = newArray[i];
}
}
三次异或交换数据
class Solution {
public void reverseString(char[] s) {
int len = s.length;
int left = 0;
int right = len - 1;
for(; left < len / 2 ; left++){
s[left] ^= s[right];
s[right] ^= s[left];
s[left] ^= s[right];
right--;
}
}
}
Leetcode: 541 反转字符串Ⅱ
(看的答案)
class Solution {
public String reverseStr(String s, int k) {
char[] ch = s.toCharArray();
for(int i = 0; i < ch.length; i += 2 * k){
int start = i;
//这里是判断尾数够不够k个来取决end指针的位置
int end = Math.min(ch.length - 1, start + k - 1);
//用异或运算反转
while(start < end){
ch[start] ^= ch[end];
ch[end] ^= ch[start];
ch[start] ^= ch[end];
start++;
end--;
}
}
return new String(ch);
}
}
剑指Offer 05.替换空格
class Solution {
public String replaceSpace(String s) {
if(s == null || s == "") return s;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
//s.charAt(i) 为 char 类型,为了比较需要将其转为和 " " 相同的字符串类型
//if (" ".equals(String.valueOf(s.charAt(i)))){}
if (s.charAt(i) == ' ') {
sb.append("%20");
} else {
sb.append(s.charAt(i));
}
}
return sb.toString();
}
}
Leetcode: 151 翻转字符串里的单词
自己写的有点乱
class Solution {
public String reverseWords(String s) {
StringBuilder sb = new StringBuilder();
int head = 0;
int tail = s.length() - 1;
for(int i = 0; i < s.length(); i++){
if (s.charAt(i) == ' ') head++;
else break;
}
for(int i = s.length() - 1; i >= 0; i--){
if (s.charAt(i) == ' ') tail--;
else break;
}
int left = head, right = tail;
char[] cmiddle = new char[right - left + 1];
for (int i = right; i >= left; i--){
cmiddle[-i + right] = s.charAt(i);
}//先去除前后空格并倒转
int slow = 0;
int fast = 0;
for(; fast < cmiddle.length; fast++){
if(cmiddle[fast] != ' '){
cmiddle[slow] = cmiddle[fast];
slow++;
}
else if(cmiddle[fast] ==' '&&cmiddle[fast-1]!=' '){
cmiddle[slow] = cmiddle[fast];
slow++;
}
else continue;
}//去除中间多余空格
char[] cnext = new char[slow];
for (int i = 0; i < slow; i++) cnext[i] = cmiddle[i];
left = 0;
right = 0;
int tmp = 0;
int nextpos = 0;
while(tmp <= slow){
if(tmp == slow || cnext[tmp] == ' '){
left = nextpos;
right = tmp - 1;
nextpos = tmp + 1;
int sublen = right - left + 1;
for(int i = 0; i < sublen / 2; i++){
System.out.println(right);
System.out.println(left);
cnext[left] ^= cnext[right];
cnext[right] ^= cnext[left];
cnext[left] ^= cnext[right];
right --;
left ++;
}
}
tmp++;
}
return new String(cnext);
}
}
剑指Offer58-II.左旋转字符串
两份答案的代码
class Solution {
public String reverseLeftWords(String s, int n) {
int len=s.length();
StringBuilder sb=new StringBuilder(s);
reverseString(sb,0,n-1);
reverseString(sb,n,len-1);
return sb.reverse().toString();
}
public void reverseString(StringBuilder sb, int start, int end) {
while (start < end) {
char temp = sb.charAt(start);
sb.setCharAt(start, sb.charAt(end));
sb.setCharAt(end, temp);
start++;
end--;
}
}
}
//解法二:空间复杂度:O(1)。用原始数组来进行反转操作
//思路为:先整个字符串反转,再反转前面的,最后反转后面 n 个
class Solution {
public String reverseLeftWords(String s, int n) {
char[] chars = s.toCharArray();
reverse(chars, 0, chars.length - 1);
reverse(chars, 0, chars.length - 1 - n);
reverse(chars, chars.length - n, chars.length - 1);
return new String(chars);
}
public void reverse(char[] chars, int left, int right) {
while (left < right) {
chars[left] ^= chars[right];
chars[right] ^= chars[left];
chars[left] ^= chars[right];
left++;
right--;
}
}