import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @param n int整型 * @return string字符串 */ public String trans (String s, int n) { // write code here if(n==0){ return s; } char[] ch = new char[n]; ch = s.toCharArray(); for(int i=0;i<ch.length;i++){ if(ch[i]>=97&&ch[i]<=122){ ch[i] -=32; }else if(ch[i]>=65&&ch[i]<=90){ ch[i]+=32; } } if(!s.contains(" ")){ return new String(ch); } //先整体转 ch = transfor(ch,0,n-1); //转换单词,定义两个指针找到单词 int cur1 = 0; int cur2 = 0; while(cur2<n){ if(ch[cur2]!=' '){ cur2++; }else if(ch[cur2]==' '){ //遇到空格反转 transfor(ch,cur1,cur2-1); cur2++; cur1 = cur2; } } //遇到最后一个单词反转 transfor(ch,cur1,cur2-1); return new String(ch); } public char[] transfor(char[] ch,int l,int r){ int s =l; int e =r; while(s<=e){ char tmp=ch[s]; ch[s] = ch[e]; ch[e] = tmp; s++; e--; } return ch; } }