c++ simple loop


  • 0
    D

    class Solution {
    public:
    int lengthOfLastWord(string s) {
    int size=s.size();
    int sum=0;
    int flag=0;
    for(int i=size-1;i>=0;i--)
    {
    if(s[i]!=' ')
    flag=1;
    if(flag)
    {
    if(s[i]==' ')
    break;
    sum++;
    }
    }
    return sum;
    }
    };


Log in to reply
 

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