【LeetCode-面试算法经典-Java实现】【058-Length of Last Word (最后一个单词的长度)】

【058-Length of Last Word (最后一个单词的长度)】


【LeetCode-面试算法经典-Java实现】【所有题目目录索引】

原题

  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.

题目大意

  给定一个由大小写字母组和空格组成的字符串,返回字符串中的最后一个单词长度。

解题思路

  先从后找第一个字母的位置x,如果没有找到就返回0,如果找到,再找第一个空格的位记为y(y可能是-1,因为没有找到空格),返回结果x-y。

代码实现

算法实现类

public class Solution {
    public int lengthOfLastWord(String s) {

        int index = s.length() - 1;

        // 从后面向前找第一个不是‘ ‘的字符
        while (index >=0 && s.charAt(index) == ‘ ‘) {
            index--;
        }

        if (index < 0) {
            return 0;
        }

        int tmp = index;

        // 执行到下面说明存在最后一个单词

        // 从后面向前找第一个是‘ ‘的字符
        while (index >=0 && s.charAt(index) != ‘ ‘) {
            index--;
        }

        return tmp - index;
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47164433

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-24 22:36:12

【LeetCode-面试算法经典-Java实现】【058-Length of Last Word (最后一个单词的长度)】的相关文章

[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 char

58. Length of Last Word最后一个单词的长度

[抄题]: [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: "b a " 最后一位是空格,可能误判lastindexof().所以必须用.trim() [思维问题]: [一句话思路]: 用函数 再次强调是最后一位的索引是length() - 1 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: [二刷]: [三刷]: [四刷]: [

[LeetCode] 58. 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 cha

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 cha

88. Merge Sorted Array【leetcode】算法,java将两个有序数组合并到一个数组中

88. Merge Sorted Array Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The n

【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", di

【LeetCode-面试算法经典-Java实现】【064-Minimum Path Sum(最小路径和)】

[064-Minimum Path Sum(最小路径和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either

【LeetCode-面试算法经典-Java实现】【066-Plus One(加一)】

[066-Plus One(加一)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 题目大意 给定一个用数组表示的一个数,

【LeetCode-面试算法经典-Java实现】【067-Add Binary(二进制加法)】

[067-Add Binary(二进制加法)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100" 题目大意 给定两个二进制的字符串,返回它们的和,也是二进行制字符串. 解题思路 先将对应的两个二进制字符串

【LeetCode-面试算法经典-Java实现】【054-Spiral Matrix(螺旋矩阵)】

[054-Spiral Matrix(螺旋矩阵)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]