541. 反转字符串II
给定一个字符串 s 和一个整数 k,从字符串开头算起, 每计数至 2k 个字符,就反转这 2k 个字符中的前 k 个字符。
如果剩余字符少于 k 个,则将剩余字符全部反转。
如果剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符,其余字符保持原样。
示例:
输入: s = "abcdefg", k = 2
输出: "bacdfeg"
- 时间复杂度: O(n)
- 空间复杂度: O(1)
class Solution {
public String reverseStr(String s, int k) {
char[] ch = s.toCharArray();
for(int i = 0;i<ch.length;i+=2*k){
if(i+k<=ch.length){
swap(i, i+k-1, ch);
}else{
swap(i, ch.length-1, ch);
}
}
String result = new String(ch);
return result;
}
public char[] swap(int a, int b, char[] ch){
while(a<b){
char temp;
temp = ch[b];
ch[b] = ch[a];
ch[a] = temp;
a++;
b--;
}
return ch;
}
}
替换数字
给定一个字符串 s,它包含小写字母和数字字符,请编写一个函数,将字符串中的字母字符保持不变,而将每个数字字符替换为number。
例如,对于输入字符串 "a1b2c3",函数应该将其转换为 "anumberbnumbercnumber"。
对于输入字符串 "a5b",函数应该将其转换为 "anumberb"
输入:一个字符串 s,s 仅包含小写字母和数字字符。
输出:打印一个新的字符串,其中每个数字字符都被替换为了number
样例输入:a1b2c3
样例输出:anumberbnumbercnumber
数据范围:1 <= s.length < 10000。
import java.util.*;
public class Main {
public static void main(String[] args) {
//输入
Scanner sc = new Scanner(System.in);
String s = sc.next();
//确定新数组的长度
int len = s.length();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 0 && s.charAt(i) <= '9') {
len += 5;
}
}
//拷贝旧数组的字符到新数组
char[] ret = new char[len];
for (int i = 0; i < s.length(); i++) {
ret[i] = s.charAt(i);
}
//双指针法解题,从后往前填充
//左指针在已有字符的末尾,右指针在新数组末尾,从右向左遍历
for (int i = s.length() - 1, j = len - 1; i < j; i--) {
if ('0' <= ret[i] && ret[i] <= '9') {
ret[j--] = 'r';
ret[j--] = 'e';
ret[j--] = 'b';
ret[j--] = 'm';
ret[j--] = 'u';
ret[j--] = 'n';
} else {
ret[j--] = ret[i];
}
}
//输出
System.out.println(ret);
}
}
151.翻转字符串里的单词
给定一个字符串,逐个翻转字符串中的每个单词。
示例 1:
输入: "the sky is blue"
输出: "blue is sky the"
示例 2:
输入: " hello world! "
输出: "world! hello"
解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
示例 3:
输入: "a good example"
输出: "example good a"
解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
class Solution {
public String reverseWords(String s) {
//去除首尾以及中间多余空格
StringBuilder sb = removeSpace(s);
//反转整个字符串
reverseStrings(sb, 0, sb.length()-1);
//反转各个单词
reverseEachWord(sb);
return sb.toString();
}
//去除多余空格的函数
private StringBuilder removeSpace(String s){
int start = 0;
int end = s.length()-1;
//遍历,略过头尾的空格
while(s.charAt(start) == ' '){start++;}
while(s.charAt(end) == ' '){end--;}
StringBuilder sb = new StringBuilder();
//将去除多余空格后的字符加入容器
while(start <= end){
char ch = s.charAt(start);
if(ch!=' ' || sb.charAt(sb.length() - 1) != ' '){
sb.append(ch);
}
start++;
}
return sb;
}
//反转字符串的函数
public void reverseStrings(StringBuilder sb, int start, int end){
char temp;
while(start<end){
temp = sb.charAt(end);
sb.setCharAt(end, sb.charAt(start));
sb.setCharAt(start, temp);
start++;
end--;
}
}
//反转各个单词
private void reverseEachWord(StringBuilder sb){
int start = 0;
int end = 1;
int n = sb.length();
while(start < n){
while(end < n && sb.charAt(end) != ' '){
end++;
}
reverseStrings(sb, start, end-1);
start = end + 1;
end = start + 1;
}
}
}
class Solution {
public String reverseWords(String s) {
//将字符串放进字符数组
char [] initialArr = s.toCharArray();
//新字符数组
char[] newArr = new char[initialArr.length+1];
int newArrPos = 0;
//对源字符数组从后向前遍历
int i = initialArr.length-1;
while(i>=0){
//跳过末尾的空格
while(i>=0 && initialArr[i] == ' '){i--;}
//确定单词的右指针
int right = i;
while(i >= 0 && initialArr [i] != ' '){i--;}
//确定单词的左指针
//将指定区间内单词添加进新数组,并在结尾添加空格
for(int left = i+1;left <= right;left++) {
newArr[newArrPos++] = initialArr[left];
if(left == right){
newArr[newArrPos++] = ' ';
}
}
}
if(newArrPos == 0){
return "";
}else{
return new String(newArr,0,newArrPos-1);
}
}
}
右旋字符串
字符串的右旋转操作是把字符串尾部的若干个字符转移到字符串的前面。给定一个字符串 s 和一个正整数 k,请编写一个函数,将字符串中的后面 k 个字符移到字符串的前面,实现字符串的右旋转操作。
例如,对于输入字符串 "abcdefg" 和整数 2,函数应该将其转换为 "fgabcde"。
输入:输入共包含两行,第一行为一个正整数 k,代表右旋转的位数。第二行为字符串 s,代表需要旋转的字符串。
输出:输出共一行,为进行了右旋转操作后的字符串。
样例输入:
2
abcdefg
1
2
样例输出:
fgabcde
1
数据范围:1 <= k < 10000, 1 <= s.length < 10000;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
String s = sc.nextLine();
int len = s.length();
char[] chars = s.toCharArray();
reverseString(chars, 0, len-1);
reverseString(chars, 0, n-1);
reverseString(chars, n, len-1);
System.out.println(chars);
}
public static void reverseString(char[] ch, int start, int end){
while(start < end){
char temp = ch[start];
ch[start] = ch[end];
ch[end] = temp;
start++;
end--;
}
}
}
28. 实现 strStr()
实现 strStr() 函数。
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。
示例 1: 输入: haystack = "hello", needle = "ll" 输出: 2
示例 2: 输入: haystack = "aaaaa", needle = "bba" 输出: -1
说明: 当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。 对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。
class Solution {
public int strStr(String haystack, String needle) {
if(needle.length() == 0) return 0;
int[] next = new int[needle.length()];
getNext(next, needle);
//j为needle字符串中的下标
int j = 0;
//i为haystack 字符串中下标
for (int i=0; i<haystack.length(); i++) {
//从第二个字符开始,当字符比对不同时,将j移动到前缀表对应位置的下标然后继续比对字符,直到字符相同
while (j>0 && needle.charAt(j) != haystack.charAt(i)){
j = next[j-1];
}
//当字符相同时,移动一位needle字符串中的下标
if (needle.charAt(j) == haystack.charAt(i)){
j++;
}
if (j == needle.length()){
return i - needle.length() + 1;
}
}
return -1;
}
private void getNext(int[] next, String s) {
//j为前缀表中的值
int j = 0;
next[0] = 0;
//i为下标
for (int i = 1;i<s.length();i++) {
//前后缀不同
while (j > 0 && s.charAt(j) != s.charAt(i)){
j = next[j-1];//向前回退
}
if (s.charAt(j) == s.charAt(i))
j++;
next[i] = j;
}
}
}
459.重复的子字符串
给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。
示例 1:
- 输入: "abab"
- 输出: True
- 解释: 可由子字符串 "ab" 重复两次构成。
示例 2:
- 输入: "aba"
- 输出: False
示例 3:
- 输入: "abcabcabcabc"
- 输出: True
- 解释: 可由子字符串 "abc" 重复四次构成。 (或者子字符串 "abcabc" 重复两次构成。)
class Solution {
public boolean repeatedSubstringPattern(String s) {
if(s.length() == 0){
return false;
}
int[] next = new int[s.length()];
getNext(next, s);
int len = s.length();
if (next[len-1] != 0 && len % (len- (next[len-1])) == 0) {
return true;
}
return false;
}
private void getNext(int[] next, String s){
int j = 0;
next[0] = 0;
for(int i = 1; i<s.length();i++){
while(j>0 && s.charAt(j) != s.charAt(i)){
j = next[j-1];
}
if (s.charAt(j) == s.charAt(i))
j++;
next[i] = j;
}
}
}