• 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 }
  • 相关阅读:
    npm配置国内源方法
    数据库—事务—隔离级别
    Mybatis—日志
    Mybatis—动态 SQL
    Mybatis—mapper.xml配置文件
    declare命令
    shell杂项
    流程控制语句
    第一篇博客
    Linux 命令[2]:mkdir
  • 原文地址:https://www.cnblogs.com/RazerLu/p/3537040.html
Copyright © 2020-2023  润新知