为什么代码在eclipse上运行正确,但在LeeCode上提交却是错误的?

 

题目:

给你一个字符串 s,由若干单词组成,单词之间用空格隔开。返回字符串中最后一个单词的长度。如果不存在最后一个单词,请返回 0 。
单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。
示例 1:
输入:s = "Hello World"
输出:5
示例 2:
输入:s = " "
输出:0
来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/length-of-last-word

public class Last_word_length {
	public int lastLength(String s) {
		if(s =="") {
			return 0;
		}
		char[] arr = s.toCharArray();
		int count=0;
		for(int i =arr.length-1;i>=0;i--) {        //倒着遍历,直到遇到空格停止
			if(arr[i]==' ') {
				break;
			}
			count++;
		}
		return count;
	}
}
public class Test {
		public static void main(String[] args) {
			Last_word_length sum  = new Last_word_length();
			System.out.println(sum.lastLength("a"));
		}
}

eclipse上运行结果:

LeeCode上提交结果:

 

帮你去LeetCode执行了一下,点开“显示详情”,LeetCode使用的测试用例是"a "。在LeetCode遇到解答错误,就点开“显示详情”,看看具体错哪了,他们的用例是啥。