登录
  • #刷题
  • #leetcode

新人求教leetcode第一题

hj867955629
1373
5
才开始新学java,打算同时用leetcode练手,结果第一题就有问题了。楼主代码如下,在本地eclipse上运行没问题,但是把solution类放到leetcode上就报错了,而且报的错莫名其妙的感觉,查了好久没查出来。。求帮助。。感谢!

class Solution {

public String reverseWords(String s) {

int i = s.length() - 1;

StringBuffer solu = new StringBuffer();

while(true){

while(s.charAt(i)!=' '){

i--;

if (i == -1) break;

}

int j = i + 1;

if (j == s.length())

j--;

while(j != s.length() && s.charAt(j)!=' ' ){

solu.append(s.charAt(j));

j++;

if(j == s.length() || s.charAt(j) == ' ')

solu.append(' ');

}



i--;

if(i == -1) break;



}

return solu.toString();



}

}

public class leetcode1 {

public static void main(String[] args){[br]
String s1 = new String(" the sky is blue ");

System.out.println("the initial string is:"+ s1);

Solution s = new Solution();

String s2 = s.reverseWords(s1);

System.out.println("reverse words is:"+ s2);



}

}
5条回复
热度排序

发表回复