标签:
1 class Reverse { 2 public: 3 string reverseString(string iniString) { 4 // write code here 5 int strLen = iniString.size(); 6 if(strLen < 0) 7 { 8 return NULL; 9 } 10 int left = 0; 11 int right = strLen-1; 12 char ch; 13 for (; left <right;) 14 { 15 ch = iniString[left]; 16 iniString[left] = iniString[right]; 17 iniString[right] = ch; 18 ++left; 19 --right; 20 } 21 return iniString; 22 } 23 };
1 import java.util.*; 2 public class Reverse 3 { 4 public String reverseString(String iniString) 5 { 6 StringBuilder sb = new StringBuilder(iniString); 7 int low = 0; 8 int high = iniString.length() - 1; 9 char temp = ‘\0‘; 10 11 while(low < high) 12 { 13 temp = sb.charAt(low); 14 sb.setCharAt(low, sb.charAt(high)); 15 sb.setCharAt(high, temp); 16 low++; 17 high--; 18 } 19 return sb.toString(); 20 } 21 }
1 class Reverse { 2 public: 3 string reverseString(string iniString) 4 { 5 string temp(iniString.crbegin(),iniString.crend()); 6 return temp; 7 } 8 };
标签:
原文地址:http://www.cnblogs.com/gxcdream/p/4919870.html