344反转字符串
没什么好写的
541. 反转字符串II
public class day8_541_反转字符串二 {
public static void main(String[] args) {
}
public String reverseStr(String s, int k) {
//总体就是两个转两个不转
//设定一个状态为就行,没必要四个一组,循环两个更改一次状态为就可以了
//string是不能更改的,创建一个buffer去更改
StringBuffer res=new StringBuffer();
int length=s.length();
int start=0;
int zt=1;
while (start+k<length) {
//这个循环的终止条件是不满足k个
StringBuffer temp=new StringBuffer();
//这个temp是存放临时的两个字符的
temp.append(s.substring(start, start+k));
if (zt==1) {
res.append(temp.reverse());
}else{
res.append(temp);
}
start=start+k;
zt*=-1;
}
if (zt==1) {
StringBuffer temp=new StringBuffer();
temp.append(s.substring(start, length));
res.append(temp.reverse());
}else{
res.append(s.substring(start,length));
}
return res.toString();
}
}
这里自己的思路是把他们两个分为一组,其实题目中的描述就是两个一组,然后交替反转,一个反一个不翻。然后我的思路是增加一个状态为,一开始自己默认的是-1 ,然后再循环开始时候先成-1,这样是不对的,比如直接k是2,字符串长度也是2,就没办法进入到循环里面,但是也要反转,所以正确的是,默认就是1,然后再最后在呈上-1,这样就符合逻辑了,其他的就是一些反转字符串的细节,这里使用sub去取的,让后再用一些反转函数。
54替换数字
import java.util.Scanner;
public class day8_kama替换数字 {
public static void main(String[] args) {
try (Scanner in = new Scanner(System.in)) {
String s = in.nextLine();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (Character.isDigit(s.charAt(i))) {
sb.append("number");
}else sb.append(s.charAt(i));
}
System.out.println(sb);
}
}
}
这一题思路也很简单,也是新建一个stringbuffer然后去一个一个去遍源数字,遇到数字就改成number,不是数字就不变。用了这个函数他是判断是否为数字字符的函数 是根据unicode和ascll判断的。
Character.isDigi
翻转字符串里面的单词
public class day8_151_反转字符串中的单词 {
public static void main(String[] args) {
System.out.println("kaishi");
String demo="the sky is blue";
System.out.println(reverseWords(demo));
System.out.println("jieshu");
}
public static String reverseWords(String s) {
//先把收尾的空格都去掉
int start=0;
int last=s.length()-1;
StringBuffer res=new StringBuffer();
while (s.charAt(start)==' ') {
start+=1;
}
while (s.charAt(last)==' ') {
last-=1;
}
StringBuffer temp=new StringBuffer();
while (last>=start) {
if (s.charAt(last)!=' ') {
temp.append(s.charAt(last));
}
if (s.charAt(last)==' ') {
if (temp.length()!=0) {
res.append(temp.reverse());
res.append(' ');
temp.delete(0, temp.length());
}
}
last-=1;
}
if (temp.length()!=0) {
res.append(temp.reverse());
}
return res.toString();
}
}
这是自己的思路,首先让收尾的空格都去掉
然后自己倒着遍历,遇到空格,然后再判断temp是否是空的,不是的话,把temp翻转存入res 然后再加入一个空格,这样遍历到最后当last《star》时候,temp里面其实装的还有一个单词,这个单词不不能忘记加进去。
卡码网:55.右旋转字符串
import java.util.Scanner;
public class day8_右旋字符串 {
public static void main(String[] args) {
try (Scanner in = new Scanner(System.in)) {
int n = Integer.parseInt(in.nextLine());
String s = in.nextLine();
int len = s.length(); //获取字符串长度
char[] chars = s.toCharArray();
reverseString(chars, 0, len - 1); //反转整个字符串
reverseString(chars, 0, n - 1); //反转前一段字符串,此时的字符串首尾尾是0,n - 1
reverseString(chars, n, len - 1); //反转后一段字符串,此时的字符串首尾尾是n,len - 1
System.out.println(chars);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void reverseString(char[] ch, int start, int end){
while (start<end) {
while (start < end) {
ch[start] ^= ch[end];
ch[end] ^= ch[start];
ch[start] ^= ch[end];
start++;
end--;
}
}
}
}
这题有最普通的思路,一步一步挪
题解的思路是,先把整体倒过来,然后前k个倒过来,后面的再倒过来,这样相当于负负得正。