344. 反转字符串
class Solution {
public void reverseString(char[] s) {
int left = 0, right = s.length - 1;
while(left < right){
char tmp = s[left];
s[left] = s[right];
s[right] = tmp;
left++; right--;
}
}
}
541. 反转字符串 II
class Solution {
public String reverseStr(String s, int k) {
char[] str = s.toCharArray();
int iter = s.length() / (2 * k);
for(int i = 0; i <= str.length; i+=2*k){
int left = i, right = Math.min(s.length() - 1, left + k - 1);
while(left < right){
char tmp = str[left];
str[left] = str[right];
str[right] = tmp;
left++; right--;
}
}
return new String(str);
}
}
54. 替换数字(第八期模拟笔试)
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String s = sc.next();
int len = 0;
for(char c : s.toCharArray()){
if(c >= '0' && c <= '9') len += 5;
len++;
}
char[] ans = new char[len];
for(int i = 0; i < s.length(); i++){
ans[i] = s.charAt(i);
}
int right = ans.length - 1, left = s.length() - 1;
while(left >= 0){
if(ans[left] >= '0' && ans[left] <= '9'){
ans[right--] = 'r';
ans[right--] = 'e';
ans[right--] = 'b';
ans[right--] = 'm';
ans[right--] = 'u';
ans[right--] = 'n';
left--;
}else{
ans[right--] = ans[left--];
}
}
System.out.println(new String(ans));
}
}
标签:right,int,反转,随想录,char,字符串,left
From: https://www.cnblogs.com/12sleep/p/18295080