While loop 4ms 98.67% of java


  • 0
    S

    // 4ms 98.67% of java
    public class Solution {
    public int lengthOfLastWord(String s) {
    int len = s.length();
    int i = len -1;
    int empty = 0;
    if(s == null || len == 0)
    return 0;
    while(i>=0 && s.charAt(i)==' '){
    i--;
    empty++;
    }
    while(i>=0 && s.charAt(i)!=' ')
    i--;
    return len- empty - (i+1);
    }
    }


Log in to reply
 

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