zoukankan      html  css  js  c++  java
  • Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.

    If the last word does not exist, return 0.

    Note: A word is defined as a character sequence consists of non-space characters only.

    For example, 
    Given s = "Hello World",
    return 5.

     1 public class Solution {
     2     public int lengthOfLastWord(String s) {
     3         if(s == null || s == " ")
     4             return 0;
     5         
     6         char[] arr = s.trim().toLowerCase().toCharArray();
     7         int count = 0;
     8         for(int i = 0; i < arr.length; i++){
     9             char c = arr[i];
    10             if((c >= 'a' && c <= 'z')){
    11                 count++;
    12             }else{
    13                 count = 0;
    14             }
    15         }
    16         return count;
    17     }
    18 }
     1 public class Solution {
     2     public int lengthOfLastWord(String s) {
     3         if(s == null || s == " ")
     4             return 0;
     5         
     6         String[] arr = s.split(" ");
     7         
     8         if(arr.length != 0)
     9             return arr[arr.length -1].length();
    10         else 
    11             return 0;
    12     }
    13 }
  • 相关阅读:
    JSP实验四
    servlet应用
    SWPU电子邮件页面
    Java Web基础
    软件工程课程总结
    Beta-星期五
    个人作业-Alpha项目测试
    第三次作业
    学期总结-西北师范大学-18计师班助教牛百泉
    Java学习计划
  • 原文地址:https://www.cnblogs.com/RazerLu/p/3537040.html
Copyright © 2011-2022 走看看