Reverse String I:
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
Solution1(最开始自己的思路,效率较低):
public static String reverseString(String s)
{
StringBuilder res=new StringBuilder("");
for(int i=s.length()-1;i>=0;i--)
{
res=res.append(s.charAt(i));
}
return res.toString();
}
Solution2(愚蠢的我啊才知道StringBuilder人家有reverse()函数,当然效率还是较低):
public static String reverseString(String s)
{
StringBuilder sb = new StringBuilder(s);
return sb.reverse().toString();
}
Solution3(这才是正解!效率即正义!):
public static String reverseString(String s)
{
char[] word = s.toCharArray();
int i = 0;
int j = s.length() - 1;
while (i < j)
{
char temp = word[i];
word[i] = word[j];
word[j] = temp;
i++;
j--;
}
return new String(word);
}
Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.
Example:
Input: s = "abcdefg", k = 2 Output: "bacdfeg"
Restrictions:
- The string consists of lower English letters only.
- Length of the given string and k will in the range [1, 10000]
Solution(最开始自己的思路,效率较高,就是细节条件较多,注意考虑周全):
public String reverseStr(String s, int k)
{
char res[]=s.toCharArray();
if(k>s.length())
return new String(reverseAll(res,0,s.length()-1));
for(int p=0,q=k-1;p<s.length();p=p+2*k,q=q+2*k)
{
int i=p;
int j=q<s.length()?q:s.length()-1;
reverseAll(res,i,j);
}
return new String(res);
}
public static char[] reverseAll(char res[],int i,int j)
{
while(i<j)
{
char temp=res[i];
res[i]=res[j];
res[j]=temp;
i++;
j--;
}
return res;
}
Reverse String III:
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example 1:
Input: "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
Solution1(最开始自己的思路,效率一般,做复杂了= =):
public String reverseWords(String s)
{
String sArray[]=s.split("\\ ");
StringBuilder res=new StringBuilder("");
for(int i=0;i<sArray.length;i++)
{
res.append(String.valueOf(reverseAll(sArray[i].toCharArray(),0,sArray[i].length()-1)));
if(i!=sArray.length-1)
res.append(" ");
}
return new String(res);
}
public static char[] reverseAll(char res[],int i,int j)
{
while(i<j)
{
char temp=res[i];
res[i]=res[j];
res[j]=temp;
i++;
j--;
}
return res;
}
}
public String reverseWords(String s)
{
char[] s1 = s.toCharArray();
int i = 0;
for(int j = 0; j < s1.length; j++)
{
if(s1[j] == ' ')
{
reverseAll(s1, i, j - 1);
i = j + 1;
}
}
reverseAll(s1, i, s1.length - 1);
return new String(s1);
}
public char[] reverseAll(char res[],int i,int j)
{
while(i<j)
{
char temp=res[i];
res[i]=res[j];
res[j]=temp;
i++;
j--;
}
return res;
}
总结:掌握首尾交换的中心思想。