151. Reverse Words in a String

[ 虚拟专用网 ] IPsce 虚拟局域网(安全的IP协议的虚拟专用网)详解(二) --- 安全联盟SA ???? 博主介绍 ????‍???? 博主介绍:大家好,我是_PowerShell,很高兴认识大家~ ✨主攻领域:【渗透领域】【数据通信】 【通讯安全】 【web安全】【面试分析】 ????点赞➕评论➕收藏 == 养成习惯(一键三连)???? ????欢迎关注????一起学习????一起讨论⭐️一起进步????文末有彩蛋 ????作者水平有限,欢迎各位大佬指点,相互学习进步! 目录 ???? 博主介绍 一、什么是安全联盟 二、协商参数两种方式:(手动、IKE) 1. 手动 2. 阅读详情

关键在于清除多余空格:

class Solution {
public:
    
    void reverseWords(string &s) {
        
       int begin = 0;
        for (; begin < s.size(); begin++)
            if (s[begin] != ' ')
                break;  //find the leading spaces
        s.erase(0, begin); //delete the leading spaces

        int end = s.size() - 1;
        for (; end > -1; end--)
            if (s[end] != ' ')
                break;
        s.erase(end + 1, s.size() - end);//delete the after spaces
        for (int i = 0; i < s.size(); i++)
        {
            if (s[i] == ' ')
            {
                int count = 1;
                int j = i + 1;
                while (s[j] == ' ')
                {
                    j++;
                    count++;
                }

                if (count > 1)
                    s.erase(i + 1, j - i - 1);
                i++;
            }
        } // delete tabbed spaces
        reverseSentence(s, 0, s.size()-1); //reverse the whole sentence

        reverseWord(s);
    }
    
    void reverseSentence(string &s,int begin,int end)   
    {
        while(begin<end)
        {
            char temp=s[begin];
            s[begin]=s[end];
            s[end]=temp;
            begin++;
            end--;
        }
    }
    
    void reverseWord(string &s)
    {
       int flag=0;
       for(int begin=0;begin<=s.length();begin++)
        {
            if(s[begin]==' '||s[begin]=='\0')  //find a word 
            {
                reverseSentence(s,flag,begin-1);
                flag=begin+1;
            } 
            
        }
    }
};

IDEA的使用指南 IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧. 立即下载

相关推荐

TCS34725颜色识别模块实战调校:从“一坨”例程到精准RGB

本文针对TCS34725颜色识别模块初始数据不准的问题,提供了从基础例程到精准调校的实战指南。核心在于通过白光校正与黑白光校正两种方法,补偿传感器偏差与环境光影响,将原始数据准确映射为标准RGB值。文章详细阐述了校准原理、操作步骤与代码实现,并分享了进阶调试技巧与避坑指南,帮助开发者显著提升颜色识别的准确性与项目可靠性。

weixin_29188043的博客 289

[亚麻高频题] Leetcode 151. Reverse Words in a String(翻转字符串)

题目描述 & 链接 Leetcode 151. Reverse Words in a String:将一个包含多个单词的字符串按单词顺序翻转 题目思路 题目比较直截了当,就是将所有单词逆序组合成最后结果,先通过空格进行分割,然后从后往前遍历合成即可,这里题目说单词之间原先可能存在多个空格,那么分割后的结果存在两种情况:1.空元素(开头,结尾存在空格);2.元素是空格(单词之间有过个空格时)。 代码如下: class Solution { public String reverse

ok1382038的博客 628

LabVIEW多设备采集实战:1个程序同时控制3个RS485 Modbus仪表

本文详细介绍了如何使用LabVIEW实现多设备RS485 Modbus同步采集,涵盖硬件架构设计、多线程通信框架搭建、数据帧解析技巧及性能优化策略。通过实际项目案例,展示如何解决端口冲突、数据解析错误等工业自动化常见问题,提升采集系统实时性与可靠性。

qsc901234的博客 308

算法:Reverse Words in a String(翻转字符串里的单词)

说明 算法:Reverse Words in a String LeetCode地址:https://leetcode.com/problems/reverse-words-in-a-string/ 题目: Given an input string, reverse the string word by word. Example 1: Input: &quot;the sky is blue&quot; Outp...

AI架构师易筋 1099

论文笔记:Group Equivariant Convolutional Networks

Group Equivariant Convolutional Networks 1、四个问题 要解决什么问题? 对卷积神经网络进行扩展,并提出一个在特定的变换(旋转、平移等,也可表示为一个特殊的群)下具有等变性的网络。 用了什么方法解决? 提出了一种新的卷积神经网络结构——群等变卷积神经网络(Group equivariant Convolutional Neural Network)...

hongbin_xu的博客 1万+

高通AI Engine SDK(QNN)使用教程(环境配置、模型转换、量化、推理、分析)

高通AI Engine使用教程

weixin_51031772的博客 1万+

【LeetCode 面试经典150题】151. Reverse Words in a String 反转字符串中的单词

Given an input string , reverse the order of the words.A word is defined as a sequence of non-space characters. The words in will be separated by at least one space.Return a string of the words in reverse order concatenated by a single space.Note that ma

qq_43513394的博客 1019

什么是IQ信号/为什么用IQ信号/IQ调制与解调/IQ包络与相位/IQ不平衡问题

什么是IQ信号/为什么用IQ信号/IQ调制与解调/IQ包络与相位/IQ不平衡问题

jane0819的博客 811

ledecode Reverse Words in a String III

557.Reverse Words in a String III Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. E...

abcdef00001111234的博客 218

代谢组数据分析(零):从质谱样本制备到MaxQuant搜库(蛋白质组和代谢组)

本文详细介绍了代谢组数据分析的过程,从LC-MS/MS技术的样本制备、分离、质谱鉴定到MaxQuant软件的使用。LC-MS/MS包括样本制备、液相色谱分离、质谱上机和鉴定等步骤。MaxQuant在蛋白质定性和定量中扮演关键角色,通过搜库和数据处理提供精准结果。此外,文章还涉及了数据采集模式、质谱图谱解析、数据库和软件选择以及结果评估等方面

专注生信领域 8594

电机驱动器MP6540H应用设计

/ ... 其他初始化函数(SystemClock_Config, MX_GPIO_Init, MX_TIM1_Init, MX_TIM2_Init)需要你根据STM32CubeMX生成或手动编写。// 假设霍尔传感器引脚是 GPIO_PIN_8, GPIO_PIN_9, GPIO_PIN_10。// 低侧U相GPIO引脚。

zhusl6688的博客 665

高频刷题-151. Reverse Words in a String

https://leetcode.com/problems/reverse-words-in-a-string/ 翻转字符串中的每一个单词,因为要翻转每一个单词,首先想到的是根据“ ”去分割字符串得到单词,然后入栈,再出栈。利用栈的LIFO的特性达到翻转的目的。这样就来了解法1: public String reverseWords(String s) { Stack<String> st = new Stack<String>(); ..

wangpu_baggio的博客 234

ESP32-C3入门教程 蓝牙篇①——基于Blufi 的 WiFi 智能配网

文章目录一、前言二、WiFi配网方式三、快速运行四、运行效果五、ESP32-C3程序流程5.1 主程序5.2 回调函数5.3 SmartConfig线程5.4 流程图六、关键函数6.1 设置SmartConfig的协议类型6.2 启动SmartConfig6.3 停止SmartConfig七、其他八、参考 一、前言 本文基于VS Code IDE进行编程、编译、下载、运行等操作 基础知识点请查阅:ESP32-C3入门教程 基础篇①——基于VS Code构建Hello World 二、WiFi配网方式 W

7766

Leetcode 151 Reverse Words in a String

lc151 Reverse Words in a String 利用split(),将String分成一个String[],可以用正则表达式"\\s+"(第一个\转义,\s表示空格,+表示多个空格),也可以直接用" ",第二种方法快一些 然后按题意翻转即可,从后面遍历String,一次将元素放入新String(这里可以用StringBuilder,会比直接用+连接字符串快,最后sb.toStr...

weixin_30609331的博客 103

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

【leetcode】【medium】【每日一题】151. Reverse Words in a String​​​​​​​

151. Reverse Words in a String Given an input string, reverse the string word by word. Example 1: Input: "the sky is blue" Output:"blue is sky the" Example 2: Input: " hello world! " Output...

lemonade13的博客 144

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Task4:LeetCode1:twoSum; LeetCode344:Reverse String;LeetCode151:Reverse Words in a String

1.Two Sum(两个数求和) class Solution: def twoSum(self, nums: 'List[int]', target: int) -> 'List[int]': dicti = {} for i, n in enumerate(nums): if n in dicti: ...

m0_37548423的博客 169

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

【LeetCode】151. Reverse Words in a String

LeetCode151 传送门 一种解法: 容易想到两次遍历的方法。第一次遍历分词,第二次遍历反向拼接单词成为句子。然后可以将两次遍历改进为一次遍历。首先初始化一个空字符串s_rev,初始化指针end为原字符串最后一个位置加一。倒序遍历原字符串s,若s[i]为空格,则赋值end为i。否则,如果i - 1位置为空格,或i为0位置,则令s_rev拼接s[i:j]。但该方法没有使用pytho...

weixin_42344158的博客 265

LeetCode(151)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". 分析如下:         很基础的题目,需要考虑各种corner cases,比如下列: 1 "

BUILD 4466

【LeetCode】 151. Reverse Words in a String 翻转字符串里的单词(Medium)(JAVA)每日一题

【LeetCode】 88. Merge Sorted Array 翻转字符串里的单词(Medium)(JAVA) 题目地址: https://leetcode.com/problems/merge-sorted-array/ 题目描述: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one s...

qq_16927853的博客 199

leetcode 151 Reverse Words in a String (python)

Python Reverse Words in a String

快递小可的博客 2013

小练习 - LeetCode151 Reverse Words in a String的C语言解法

LeetCode 151. Reverse Words in a String 很适合练习C语言基础,练习一个OJ题目除了找最佳解法,还可以多想想刻意去练习一些数据结构等其他知识点,让练习举一反三。 Given an input string, reverse the string word by word. Example: Input: “the sky is blue”, Output: ...

Life runs on code 673

151. Reverse Words in a String**

151. Reverse Words in a String** https://leetcode.com/problems/reverse-words-in-a-string/ 题目描述 Given an input string, reverse the string word by word. Example 1: Input: "the sky is blue" Output: "blue...

珍妮的选择的博客 317
上一篇: 【bit】see-260. Single Number III
下一篇: 【DFS】841. Keys and Rooms
云水谣CS
博客等级 码龄9年 8粉丝 268原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值