LeetCode58. 最后一个单词的长度

题目:

给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度。

如果不存在最后一个单词,请返回 0 。

说明:一个单词是指由字母组成,但不包含任何空格的字符串。

答案:

 1  public static int LengthOfLastWord(string s)
 2         {
 3             int result = 0;
 4             //判断是否为空
 5             if (!string.IsNullOrWhiteSpace(s))
 6             {
 7                 string[] strArray=s.Split(' ');
 8                 //循环遍历,找到最后一个单词
 9                 for (int i = strArray.Length - 1; i >= 0; i--)
10                 {
11                     if (strArray[i].Length > 0)
12                     {
13                         return strArray[i].Length;
14                     }
15                 }
16                 
17             }
18             return result;
19         }
原文地址:https://www.cnblogs.com/shacoli/p/10164554.html