试题
Given an input string, reverse the string word by word.
Example 1:
Input: “the sky is blue”
Output: “blue is sky the”
Example 2:
Input: " hello world! "
Output: “world! hello”
Explanation: Your reversed string should not contain leading or trailing spaces.
Example 3:
Input: “a good example”
Output: “example good a”
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
Note:
A word is defined as a sequence of non-space characters.
Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
You need to reduce multiple spaces between two words to a single space in the reversed string.
代码:
class Solution {
public String reverseWords(String s) {
if(s == null || s.length() == 0) return s;
String[] sArr = s.trim().split(" ");
StringBuilder temp = new StringBuilder();
for(int i = sArr.length-1; i >= 0; i--){
if(sArr[i] == null || sArr[i].equals("")) continue;
temp.append(sArr[i]);
temp.append(" ");
}
if(temp.length() > 0)
temp.deleteCharAt(temp.length()-1);
return temp.toString();
}
}
class Solution {
public String reverseWords(String s) {
StringBuilder out = new StringBuilder();
if(s==null || s.length()==0) return "";
s = s.trim();
String tmp = "";
for(int i=s.length()-1; i>=0; i--){
char t = s.charAt(i);
if(t==' '){
while(i-1>=0 && s.charAt(i-1)==' ') i--;
out.append(tmp+" ");
tmp = "";
}else{
tmp = t + tmp;
}
}
out.append(tmp);
return out.toString();
}
}
public String reverseWords(String s) {
StringBuilder sb = new StringBuilder();
int i = s.length() - 1;
while(i >= 0){
if(s.charAt(i) == ' '){
i--;
continue;
}
int start = s.lastIndexOf(' ', i);
sb.append(' ');
sb.append(s.substring(start + 1, i + 1));
i = start - 1;
}
if(sb.length() > 0){
sb.deleteCharAt(0);
}
return sb.toString();
}
翻转https://www.baidu.com成com.baidu.www://https:
public class Main{
public static void main(String[] args){
String in = "https://www.baidu.com";
Main main = new Main();
System.out.println(main.split(in));
System.out.println(main.split2(in));
System.out.println(main.split3(in));
}
private String split(String in){
if(in==null || in.length()==0) return "";
StringBuilder ss = new StringBuilder();
int end = in.length()-1;
while(end>=0){
int start = in.lastIndexOf(".",end);
if(start!=-1){
ss.append("."+in.substring(start+1,end+1));
}else{
start = in.lastIndexOf("://", end);
if(start!=-1){
ss.append("."+in.substring(start+3,end+1));
}else{
ss.append("://"+in.substring(start+1,end+1));
}
}
end = start-1;
}
ss.deleteCharAt(0);
return ss.toString();
}
private String split2(String in){
String[] strs = in.split("\\.");
StringBuilder out = new StringBuilder();
for(int i=strs.length-1; i>=0; i--){
if(i!=0){
out.append(strs[i]);
out.append(".");
}else{
String[] tmp = strs[i].split("://");
out.append(tmp[1]);
out.append("://");
out.append(tmp[0]);
}
}
return out.toString();
}
private String split3(String in){
String[] strs = in.split("\\.|(://)");
StringBuilder out = new StringBuilder();
for(int i=strs.length-1; i>=0; i--){
if(i!=0){
out.append(strs[i]);
out.append(".");
}else{
out.deleteCharAt(out.length()-1);
out.append("://");
out.append(strs[i]);
}
}
return out.toString();
}
}