zoukankan      html  css  js  c++  java
  • 【Leetcode】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 class Solution {
     2 public:
     3     int lengthOfLastWord(const char *s) {
     4         int total_len = strlen(s);
     5         if (total_len == 0) return 0;
     6         int ret = 0, i = total_len - 1;
     7         while (i >= 0 && s[i] == ' ') --i;
     8         while (i >= 0 && s[i] != ' ') {
     9             ++ret;
    10             --i;
    11         }
    12         return ret;
    13     }
    14 };
    View Code

    按题意数就可以了..

  • 相关阅读:
    遍历datatable的方法
    C# 存储过程使用方法
    C# 存储过程
    dev repositoryItem 手工定义
    无限极分类
    ThinkPHP
    ThinkPHP
    RBAC
    ThinkPHP
    正则表达式
  • 原文地址:https://www.cnblogs.com/dengeven/p/3738600.html
Copyright © 2011-2022 走看看