LeetCode之“字符串”:Length of Last Word & Reverse Words in a String

  1. 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.

  Solution:

class Solution {
public:
    int lengthOfLastWord(string s) {
        if(s.size() == 0)
            return 0;
        else
        {
            while(s.back() == ‘ ‘)
                s.pop_back();

            if(s.find(‘ ‘) == -1)
                return s.size();
            else
            {
                int len = 0;
                while(s.back() != ‘ ‘)
                {
                    len++;
                    s.pop_back();
                }
                return len;
            }
        }
    }
};

  2. Reverse Words in a String

  Given an input string, reverse the string word by word.

  For example,
  Given s = "the sky is blue",
  return "blue is sky the".

  Solution:

class Solution {
public:
    void reverse(string &s, int start, int end)
    {
        int sz = (end - start) / 2 + 1;
        for (int i = 0; i < sz; i++)
        {
            if (start + i != end - i)
            {
                char tmp = s[start + i];
                s[start + i] = s[end - i];
                s[end - i] = tmp;
            }
        }
    }

    void clearExtralSpace(string &s)
    {
        while (s.front() == ‘ ‘)
        {
            s.erase(0, 1);
        }
        while (s.back() == ‘ ‘)
        {
            s.pop_back();
        }
        int spacePos = s.find(‘ ‘);
        while (spacePos != -1)
        {
            while (s[spacePos + 1] == ‘ ‘)
            {
                s.erase(spacePos + 1, 1);
            }
            spacePos = s.find(‘ ‘, spacePos + 1);
        }
    }

    void reverseWords(string &s) {
        clearExtralSpace(s);
        if (s.size() > 1)
        {
            int isSpaceExist = s.find(‘ ‘);
            if (isSpaceExist != -1)
            {
                int szS = s.size();
                reverse(s, 0, s.size() - 1);
                int charPos = 0;
                int nextSpacePos = s.find(‘ ‘, charPos + 1);
                while (nextSpacePos != -1)
                {
                    reverse(s, charPos, nextSpacePos - 1);
                    charPos = nextSpacePos + 1;
                    nextSpacePos = s.find(‘ ‘, charPos + 1);
                }
                reverse(s, charPos, s.size() - 1);
            }
        }
    }
};

时间: 2024-10-13 18:56:44

LeetCode之“字符串”:Length of Last Word & Reverse Words in a String的相关文章

leetcode || 58、Length of Last Word

problem: 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-

Leetcode(58)题解:Length of Last Word

https://leetcode.com/problems/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 de

Leetcode 细节实现 Length of Last Word

Length of Last Word Total Accepted: 17518 Total Submissions: 59526My Submissions 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, re

【一天一道LeetCode】#58. Length of Last Word

一天一道LeetCode系列 (一)题目 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 cons

【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

LeetCode OJ: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

【算法】LeetCode算法题-Length Of Last Word

这是悦乐书的第155次更新,第157篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第14题(顺位题号是58).给定一个字符串,包含戴尔字母.小写字母和空格,返回最后一个单词的长度,如果最后一个单词不存在则返回0.另外,单词不包含空格.例如: 输入: "Hello World" 输出: 5 说明:最后一个单词为world,其长度为5 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试. 0

leetcode—58 Length of Last Word Total(字符串中最后一个单词的长度)

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

leetCode 58. Length of Last Word 字符串

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 c