leetcode 557. Reverse Words in a String III(字符串中单词逆序III)

每天一道LeetCode-----以单词为单位逆序字符串,每个单词之间以一个空格分隔(原字符串中可能有多个空格) Reverse Words in a String 原题链接Reverse Words in a String 将字符串以单词为单位逆序,每个单词之间以一个空格间隔(原字符串中可能有多个空格) 逆序的方法就是先整体逆序,然后从头到尾每个单词逆序。不容易处理的是单词间空格,只能一个个字符移动 代码如下 class Solution { public: void revers 阅读详情

Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:

Input: s = “Let’s take LeetCode contest”
Output: “s’teL ekat edoCteeL tsetnoc”
Example 2:

Input: s = “God Ding”
Output: “doG gniD”

以空格分割单词,返回每个单词逆序之后的字符串。

思路:

方法1:
先用split分割单词,每个单词利用StringBuilder中的逆序函数进行逆序,
再拼接起来即可。
能通过,但是时间排名比较偏后。

public String reverseWords(String s) {
    String[] words = s.split(" ");
    
    for(int i = 0; i < words.length; i++)
        words[i] = (new StringBuilder(words[i])).reverse().toString();
    
    return String.join(" ", words);
}

方法2:
双指针

不需要用split分割单词,
用尾指针不断移动,指向空格 或者 字符串终点时 说明到达一个单词的结尾。
逆序就是首尾字母互换,用双指针一个指头一个指尾。
字符串直接是不能操作的,先转为char数组。

public String reverseWords(String s) {
    int n = s.length();
    char[] words = s.toCharArray();
    int start = 0;
    
    for(int end = 0; end <= n; end++) {
        if(end == n || words[end] == ' ') {
            reverse(start, end-1,  words);
            start = end + 1;
        }           
    }
    
    return new String(words);
}

void reverse(int start, int end, char[] arr) {
    while(start < end) {
        char tmp = arr[start];
        arr[start] = arr[end];
        arr[end] = tmp;
        start ++;
        end --;
    }
}
Unity新手必看:5分钟搞定TextMeshPro中文乱码,告别小方块(附7000+常用字库) 本文详细介绍了Unity中TextMeshPro中文乱码问题的解决方案,包括快速生成中文字体的步骤和优化技巧。通过使用支持中文的.ttf字体文件和自定义字符集,开发者可以在5分钟内解决TMP显示小方块的问题,提升游戏或应用的文本显示效果。 阅读详情

相关推荐

用STM32的SPI模式2搞定SSI绝对值编码器通信(附完整代码与避坑点)

本文详细解析了如何使用STM32的SPI模式2实现SSI绝对值编码器通信,包括硬件设计、SPI配置、数据处理算法及稳定性优化技巧。通过完整的代码示例和避坑指南,帮助工程师解决时序匹配、数据解析等常见问题,提升工业自动化系统中的编码器通信可靠性。

weixin_27252001的博客 264

leetcode58-单词反转

题目 输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。为简单起见,标点符号和普通字母一样处理。例如输入字符串"I am a student. “,则输出"student. a am I”。 示例 1: 输入: “the sky is blue” 输出: “blue is sky the” 示例 2: 输入: " hello world! " 输出: “world! hello”...

sinat_32165273的博客 543

Ubuntu22.04下RTX2080显卡驱动、CUDA、cuDNN保姆级避坑安装指南(实测有效)

本文提供Ubuntu22.04下RTX2080显卡驱动、CUDA和cuDNN的详细安装指南,涵盖前期准备、驱动安装、CUDA版本选择、cuDNN配置及常见问题解决。通过实测有效的步骤和优化建议,帮助用户快速搭建稳定的深度学习环境,避免常见陷阱。

weixin_42540248的博客 283

逆序输出字符串的单词——Leetcode系列(二)

Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". Clarification: What constitutes a word? A sequence of non-spac

Hooting的码农笔记本 1149

LTO编译器优化介绍以及开启方法

LTO优化的详细介绍

性感的小君君 4922

一文读懂无人机AI识别算法,原理、技术、应用

本文详细介绍了无人机识别算法的工作原理、技术优势以及在多个行业的应用前景,展现了其在提升安全监控、资源管理和应急响应中的关键作用。尽管无人机识别算法在多个领域展现出巨大潜力,但仍面临技术挑战,如算法的准确性、数据的实时处理能力以及对复杂环境的适应性。4. 消防与城市管理: 无人机识别算法在消防和城市管理中的应用,为相关部门提供了及时的信息支持,提升了应急处置能力和城市管理水平。1. 公安监控: 无人机识别算法在公安领域的应用,可以实时识别犯罪嫌疑人和违法行为,有效提升城市治安管理的智能化水平。

foiadrone的博客 1846

LeetCode -- 反转英文单词

问题:给定英文句子,反转里面的每个单词,例如"the sky is blue" 反转后为 "blue is the sky"实现思路:对英文句子每个字符做循环s存放反转的句子,w存放单词遇到空格(1个或多个)将w添加到s中非空格则将字符添加到w实现代码:var reverseWords = function(str) { str = str.trim(); var s = "";

我的英文站点:https://iorilan.medium.com/ 2927

TMS320F280049C 学习笔记21 交叉开关 Crossbar (X-BAR)

简单介绍了X-BAR。

whyorwhnt的专栏 2万+

剑指 Offer 58 - I. 翻转单词顺序-Python题解

输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。为简单起见,标点符号和普通字母一样处理。例如输入字符串"I am a student. “,则输出"student. a am I”。 示例 1: 输入: “the sky is blue” 输出: “blue is sky the” 示例 2: 输入: " hello world! " 输出: “world! hello” 解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。 示例 3: 输入: “a good

AiBigData的博客 465

10BASE-T1S协议中的PLCA机制:实现无碰撞网络传输的关键

本文深入解析了10BASE-T1S协议中的PLCA(物理层冲突避免)机制。作为IEEE 802.3cg标准的关键部分,PLCA通过循环调度与机会传递,从根源上避免了网络碰撞,为工业与汽车等场景提供了确定性的低延迟传输保障,是实现可靠实时通信的核心技术。

weixin_29250403的博客 209

人工智能技术基础系列之:迁移学习

作者:禅与计算机程序设计艺术 1.背景介绍 随着深度学习和神经网络的快速发展,越来越多的应用场景需要处理大量的数据和复杂的任务。然而,由于计算资源、存储空间等条件的限制,传统的人工智能方法往往不能满足实际需求。因此,为了提高效率和降低成本,研究人员提出了迁移学习的概念。本篇文章将详细介绍迁移学习的基

AI天才研究院 693

leetcode (Reverse Words in a String III)

Title:Reverse Words in a String III    557 Difficulty:Easy 原题leetcode地址:https://leetcode.com/problems/reverse-words-in-a-string-iii/   1.    注解见代码注释 时间复杂度:O(n^2),嵌套循环。 空间复杂度:O(n),申请额外空间List。 ...

hsx1612727380的专栏 211

基于深度学习的番茄成熟度检测系统——YOLOv5、YOLOv8、YOLOv10及UI界面的实现

YOLO是一种快速且准确的目标检测模型,近年来,YOLOv5、YOLOv8和YOLOv10作为YOLO系列的重要版本,分别在速度、精度和效率上进行了优化。下面我们简要介绍这些模型。本文介绍了如何基于YOLOv5、YOLOv8和YOLOv10等深度学习模型,结合PyQt5设计一个番茄成熟度检测系统。通过训练模型、优化算法和设计用户友好的界面,我们能够构建一个高效且准确的番茄成熟度检测系统,广泛应用于农业自动化和智能化管理中。

YOLO 1002

【leetcode】557. Reverse Words in a String III

Algorithm 【leetcode】557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-in-a-string-iii/ 1)problem Given a string, you need to reverse the order of characters in each w...

weixin_30319097的博客 117

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

leetcode 557. Reverse Words in a String III 反转字符串中的每个单词

https://leetcode.com/problems/reverse-words-in-a-string-iii/ 解法一:遇到字符就压栈,遇到空格或最后一个字符就弹出栈。 class Solution { public: string reverseWords(string s) { int n = s.length(); if(n<...

rock4you 301

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

LeetCode 557 Reverse Words in a String III(反转字符串中的单词3)

翻译原文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.Example 1: Input: “Let’s take LeetCode contest”

nomasp 7271

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

557. (Reverse Words in a String III)反转字符串中的单词 III

题目:解题思路:Python代码Java代码C++代码复杂度分析 题目: Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. 给定一个字符串s,将句子中每个单词的字符顺序颠倒,同时仍然保留空格和最初的单词顺序。 Example 1: Input: s = “Let’s take Le.

OIqng的博客 374

LeetCode-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.Example 1:Input: "Let's take LeetCode contest"

quliuwuyihmy的博客 413

Leetcode——557. Reverse Words in a String III

题目原址 https://leetcode.com/problems/reverse-words-in-a-string-iii/description/ 题目描述 Given a string, you need to reverse the order of characters in each word within a sentence while still preserving ...

想当厨子的程序媛 312

Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III) 题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。 示例 1: 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL ts...

武培轩 379

LeetCode 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. Example 1: Input: "Let's take LeetCode contes

nudt_oys的博客 792

【Leetcode】557. Reverse Words in a String III

思路: 用空格分割字符串,对每个子串,逆序,添加到结果数组。 public class Solution { public String reverseWords(String s) { String[] strs = s.split(" "); String result = ""; int len = strs.length;

筱葭的博客 2794

Leetcode日练笔记28 #151 #557 Reverse Words in a String & Reverse Words in a String III

Leetcode 151和557 reverse words in a string Python解法

Bittersweet_t的博客 206

LeetCode: 557Reverse Words in a String III(easy)

题目: 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. Example 1: Input: "Let's take LeetCode cont...

lxin_liu的博客 189

[LeetCode]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.Example 1:Input: "Let's take LeetCode contest" Output

云丫 287

leetcode 557. Reverse Words in a String III 反转字符串中的单词 III

题目要求反转字符串中的每个单词(由空格分隔),同时保持单词顺序不变。本文提供了一种C++解决方案:遍历字符串,当遇到空格时,提取当前单词并反转后加入结果字符串。最后处理末尾单词(如果没有以空格结尾)。该方法时间复杂度为O(n),空间复杂度为O(n),其中n是字符串长度。关键点在于正确处理单词边界和空格位置,确保每个单词都被独立反转。

邹九的个人博客 205
上一篇: leetcode 985. Sum of Even Numbers After Queries(query之后的偶数和)
下一篇: ubuntu20.04 安装cpp版onnxruntime
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值