Length of Last Word

来源:互联网 发布:怎样举报淘宝店铺 编辑:程序博客网 时间:2024/06/07 01:09
public class Solution {    public int lengthOfLastWord(String s) {        int res = 0;         if(s.length() == 0){            return res;        }        char[] sArray = s.toCharArray();        boolean startWithSpace = false;        if(sArray[sArray.length - 1] == ' '){            startWithSpace = true;        }        for(int i = sArray.length - 1; i >= 0 ; i--){              if(!startWithSpace){                if(sArray[i] != ' '){                    res++;                }else{                    break;                }            }else{                if(sArray[i] == ' '){                    continue;                }else{                    res++;                    startWithSpace = false;                }            }                    }        return res;    }}

0 0
原创粉丝点击