## 题目关键字:字符串
### 344_题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台
### 代码实现##
package LeetCode;
public class YangSibo_344 {
public static void main (String args []) {
char arr[] = {'h','e','l','l','o'};
YangSibo_344_1 demo1 = new YangSibo_344_1();
demo1.reverseString(arr);
}
}
class YangSibo_344_1 {
public void reverseString(char[] s) {
if (s ==null ||s.length == 0) {
return;
}
int length = s.length;
for (int i = length-1; i >= length/2; i-- ) {
char temp = s[length-1 - i];
s[length-1 - i] = s[i];
s[i] = temp;
}
}
}
### 解题注意事项:
注意边界值
## 题目关键字:字符串分段处理
### 541_题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台
### 代码实现
package LeetCode;
public class YangSibo_541 {
public static void main (String args []) {
String s = "abcd";
int k = 4;
YangSibo_541_1 demo = new YangSibo_541_1();
String res = demo.reverseStr(s,k);
System.out.println(res);
}
}
class YangSibo_541_1 {
public String reverseStr(String s, int k) {
int length = s.length();
if(s == null || length == 0 || k == 0) {
return s;
}
StringBuilder trans = new StringBuilder(s);
StringBuilder res = new StringBuilder();
boolean flag = true;
for (int i = 0; i < length; i = i + k) {
if (flag) {
if((i + k) < length) {
StringBuilder temp = new StringBuilder(trans.substring(i,i+k));
temp.reverse();
res.append(temp.toString());
}else {
StringBuilder temp = new StringBuilder(trans.substring(i,length));
temp.reverse();
res.append(temp.toString());
}
if((2 * k + i ) < length) {
res.append(s.substring(i+k,2*k+i));
}else if((k + i ) < length){
res.append(s.substring(i+k,length));
}
flag = !flag;
}else {
flag = !flag;
continue;
}
}
return res.toString();
}
}
### 解题注意事项
注意题干,不同的分段需要不同处理,合理使用StringBuilder以及相关函数