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

LeetCode75——Day6

文章目录

    • 一、题目
    • 二、题解

一、题目

151. Reverse Words in a String

Given an input string s, reverse the order of the words.

A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.

Return a string of the words in reverse order concatenated by a single space.

Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.

Example 1:

Input: s = “the sky is blue”
Output: “blue is sky the”
Example 2:

Input: s = " hello world "
Output: “world hello”
Explanation: Your reversed string should not contain leading or trailing spaces.
Example 3:

Input: s = “a good example”
Output: “example good a”
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.

Constraints:

1 <= s.length <= 104
s contains English letters (upper-case and lower-case), digits, and spaces ’ '.
There is at least one word in s.

Follow-up: If the string data type is mutable in your language, can you solve it in-place with O(1) extra space?

二、题解

class Solution {
public:string reverseWords(string s) {vector<string> tmp;istringstream ss(s);string token;while(getline(ss,token,' ')) tmp.push_back(token);string res = "";int n = tmp.size();for(int i = n - 1;i >= 0;i--){if(tmp[i] != ""){res += tmp[i];res += " ";}}//去除末尾空格while(res.back() == ' ') res.pop_back();return res;}
};
http://www.lryc.cn/news/196032.html

相关文章:

  • http代理有什么好处,怎么通过http代理服务安全上网呢?
  • vue3后台管理框架之axios二次封装
  • 你的Github账户可能被封禁!教你应对Github最新的2FA二次验证! 无地区限制, 无额外设备的全网最完美方案
  • 【C语言】#define宏与函数的优劣对比
  • flask基础开发知识学习
  • 内网和热点同时连接使用配置
  • C语言 形参、实参
  • linux入门到精通-第四章-gcc编译器
  • HCIP静态路由综合实验
  • nginx前端配置(新)
  • js,jquery,vue设置html标签隐藏不显示
  • 口袋参谋:如何实时监控对手数据?
  • Q-learning如何与ABC等一些元启发式算法能够结合在一起?
  • mysql 过滤多列重复的值(保留其中一条),对单列或者多列重复的值去重
  • 面向红队的自动化引擎工具
  • Python库学习(十):Matplotlib绘画库
  • coverity工具 代码审计
  • 女鹅冬天的第一件羽绒服,当然要时尚经典的
  • 智慧渔业方案:AI渔政视频智能监管平台助力水域禁渔执法
  • LXC、Docker、 Kubernetes 容器以及Hypervisor的区别
  • 电子杂志制作不求人:简单易用的工具推荐
  • Excel冻结窗格
  • Flink自定义sink并支持insert overwrite 功能
  • CSS Vue/RN 背景使用opacity,文字在背景上显示
  • vue3自定义指令批量注册
  • 山西电力市场日前价格预测【2023-10-18】
  • 2-k8s-控制器介绍
  • 【数据结构】二叉树--OJ练习题
  • 时间复杂度为 O(n^2) 的排序算法
  • ES6 Map数据结构