当前位置: 首页 > news >正文

面试经典150题——Day19

文章目录

    • 一、题目
    • 二、题解

一、题目

58. Length of Last Word

Given a string s consisting of words and spaces, return the length of the last word in the string.

A word is a maximal
substring
consisting of non-space characters only.

Example 1:

Input: s = “Hello World”
Output: 5
Explanation: The last word is “World” with length 5.
Example 2:

Input: s = " fly me to the moon "
Output: 4
Explanation: The last word is “moon” with length 4.
Example 3:

Input: s = “luffy is still joyboy”
Output: 6
Explanation: The last word is “joyboy” with length 6.

Constraints:

1 <= s.length <= 104
s consists of only English letters and spaces ’ '.
There will be at least one word in s.

题目来源: leetcode

二、题解

class Solution {
public:int lengthOfLastWord(string s) {vector<string> res;istringstream ss(s);string token;while(getline(ss,token,' ')) res.push_back(token);int n = res.size();for(int i = n - 1;i >= 0;i--){if(res[i] != "") return res[i].length();}return 0;}
};
http://www.lryc.cn/news/204058.html

相关文章:

  • TP6首页加载报错 Call to a member function run() on null
  • 洗车小程序源码:10个必备功能,提升洗车体验
  • macOS telnet替代方式
  • 【leetcode】独特的电子邮件地址
  • 解密Java中神奇的Synchronized关键字
  • 微信删除的好友还能找回来吗?盘点5种超实用的方法!
  • Nmap 常用命令汇总
  • 谷歌浏览器最新版和浏览器驱动下载地址
  • [游戏开发][Unity]Unity运行时加载不在BuildSetting里的场景
  • flutter开发实战-hero动画简单实现
  • Spring Boot + EasyUI 创建第一个项目(一)
  • 短视频矩阵系统源码/技术应用搭建
  • 硬核子牙:我准备写一本《带你手写64位多核操作系统》的书!
  • c++访问修饰符与继承关系
  • 【RTOS学习】事件组 | 任务通知
  • 【TES605】基于Virtex-7 FPGA的高性能实时信号处理平台
  • Java Azure开发 使用已有token字符串创建GraphServiceClient
  • 【Qt】消息机制和事件
  • 爬虫模拟用户登录
  • asp.net企业招聘管理系统VS开发sqlserver数据库web结构c#编程Microsoft Visual Studio计算机毕业设计
  • 艾泊宇产品战略:假如中国禁止直播带货,网红和店家该如何应对?
  • C#调用C/C++从零深入讲解
  • MyBatis篇---第五篇
  • 十三水中各种牌型判断LUA版
  • 2023.10.19 关于设计模式 —— 单例模式
  • 4个鲜为人知的Python迭代过滤函数
  • 使用logger.error(“自定义错误信息描述“,e)将错误信息输出到日志文件上
  • 音乐的数字未来:虚拟演唱会与TikTok的巅峰融合
  • 基于图像识别的跌倒检测算法 计算机竞赛
  • NSS [SWPUCTF 2021 新生赛]PseudoProtocols