Reverse String I、II、III(tag:String)

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);
}


Reverse String II

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:
  1. The string consists of lower English letters only.
  2. 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;
	}
}


Solution2(万变不离其宗,easier one):

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;
}


总结:掌握首尾交换的中心思想。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值