标签:des style blog http io color os ar java
#include<iostream> #include<string.h> using namespace std; void reverse(char *str,int start,int end) { while(start<end) //有文章采用 start!=end作为判断条件,这样是不行的,因为当字符数位偶数时, //start指针与end 指针会交错。不会相遇 { char temp = str[start]; str[start] = str[end]; str[end] = temp; start++; end--; } } void rotatestr(char * str,int k) { if(str==NULL||strlen(str)==0||k<=0) return; int len = strlen(str); k = k%len; //若k==len,不旋转,但实际上将字符串旋转了两次 reverse(str,0,k-1); //先旋转前k个元素 reverse(str,k,len-1); reverse(str,0,len-1); }
1 public class reverseKStr 2 { 3 public String reversekelement(String str,k) 4 { 5 if(str==null || str.length()==0|| k<=0) return null; 6 int len = str.length(); 7 k= k%len; 8 StringBuffer sb1 = new StringBuffer(str.substring(0,k)); 9 StringBuffer sb2 = new StringBuffer(str.substring(k,len)); 10 sb1.reverse(); 11 sb2.reverse(); 12 sb1.append(sb2); 13 return sb1.reverse().toString(); 14 } 15 }
1 public class Solution { 2 public String reverseWords(String s) { 3 if(s==null) return s; 4 s.trim(); 5 String[] strarray = s.split(" "); 6 int len = strarray.length; 7 StringBuilder sb = new StringBuilder(); 8 for(int i=len-1;i>=0;i--) 9 { 10 if(strarray[i].length()>0) // 防止误加入空格 11 { 12 sb.append(strarray[i]); 13 sb.append(" "); 14 } 15 } 16 return sb.toString().trim(); 17 } 18 }
1 class Solution { 2 public: 3 void reverseWords(string &s) { 4 string str ; 5 int i =s.length()-1; 6 while(i>=0) 7 { 8 while(i>=0&&s[i]==‘ ‘) 9 i--; 10 11 if(i<0) break; //如果已经没有非空格元素,则跳出 12 if(str.length()!=0) str.append(" "); //如果,已经有了一个单词,则增加一个空格,前面一句话,保证了,后面还有单词添加进来; 13 14 string temp; 15 while(i>=0&&s[i]!=‘ ‘) 16 { temp.push_back(s[i]); 17 i--; 18 } 19 reverse(temp.begin(),temp.end()); 20 str.append(temp); 21 } 22 s=str; 23 } 24 };
1 public class Solution { 2 public int reverse(int x) { 3 boolean flag = true; 4 if(x<0) 5 { 6 flag=false; 7 x = -1*x; 8 } 9 int newint = x%10; 10 int temp=x/10; 11 while(temp!=0) 12 { 13 if(flag&&newint>=214748364&&temp%10>=7) return Integer.MAX_VALUE; 14 if(!flag&&newint>=214748364&&temp%10>=8) return Integer.MIN_VALUE; 15 newint= newint*10+temp%10; 16 temp=temp/10; 17 } 18 if(flag) 19 return newint; 20 else 21 return -1*newint; 22 } 23 }
标签:des style blog http io color os ar java
原文地址:http://www.cnblogs.com/lifreesh/p/4060600.html