Length of Last Word

来源:互联网 发布:java哪吒封神榜 编辑:程序博客网 时间:2024/05/17 17:57

设置一个变量显示是否读了新单词

若新单词,标识一下。

public class Solution {public int lengthOfLastWord(String s) {// Start typing your Java solution below// DO NOT write main() functionint len = s.length();if (len == 0)return 0;int i = 0;int count = 0;boolean is_new = true;while (i < len) {if (s.charAt(i) == ' ') {is_new = true;} else {if (is_new == true) {count = 1;is_new = false;} elsecount++;}i++;}return count;}}

原创粉丝点击