C# simple solution


  • 0
    D
    public class Solution {
    public int LengthOfLastWord(string s) {
       int ret = 0;
            bool word = false;
            for (int i = s.Length - 1; i >= 0; i--)
            {
                if (Char.IsLetter(Convert.ToChar(s.Substring(i, 1))))
                {
                    word = true; 
                    ret++;
                }
                if (s.Substring(i, 1) == " "&&word==false) continue;
                if (s.Substring(i, 1) == " " && word == true) break;
    
            }
                return ret;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.