[Swift]LeetCode186. Reverse Words in a String II $ 翻转字符串中的单词 II

Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.
The input string does not contain leading or trailing spaces and the words are always separated by a single space.
For example,
Given s = "the sky is blue",
return "blue is sky the".
Could you do it in-place without allocating extra space?



给定输入字符串,逐字反转字符串。单词被定义为非空格字符序列。

输入字符串不包含前导或后缀空格,并且单词总是由一个空格分隔。

例如,

给定  s = "the sky is blue",

返回 "blue is sky the".。

您能在不分配额外空间的情况下就地完成吗?


 1 class Solution {
 2     func reverseWords(_ s: inout String){
 3         var left:Int = 0
 4         for i in 0...s.count
 5         {
 6             if i == s.count || s[i] == " "
 7             {
 8                 reverse(&s, left, i - 1)
 9                 left = i + 1
10             }
11         }
12         reverse(&s, 0, s.count - 1)
13     }
14
15     func reverse(_ s: inout String,_ left:Int,_ right)
16     {
17         while (left < right)
18         {
19             var t:Character = s[left]
20             s[left] = s[right]
21             s[right] = t
22             left += 1
23             right -=1
24         }
25     }
26 }
27
28 extension String {
29     //subscript函数可以检索数组中的值
30     //直接按照索引方式截取指定索引的字符
31     subscript (_ i: Int) -> Character {
32         //读取字符
33         get {return self[index(startIndex, offsetBy: i)]}
34
35         //修改字符
36         set
37         {
38             var str:String = self
39             var index = str.index(startIndex, offsetBy: i)
40             str.remove(at: index)
41             str.insert(newValue, at: index)
42             self = str
43         }
44     }
45 }

原文地址:https://www.cnblogs.com/strengthen/p/10172134.html

时间: 2024-07-29 23:26:06

[Swift]LeetCode186. Reverse Words in a String II $ 翻转字符串中的单词 II的相关文章

[LeetCode] 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: "s'teL ekat edoCteeL tsetnoc"

[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". Update (2015-02-12):For C programmers: Try to solve it in-place in O(1) space. Clarification: What constitutes a

【LeetCode-面试算法经典-Java实现】【152-Reverse Words in a String(反转字符串中的单词)】

[152-Reverse Words in a String(反转字符串中的单词)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". 题目大意 给定一个字符串,将其反转,其的字词不转 解题思路

[LeetCode] 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". Update (2015-02-12): For C programmers: Try to solve it in-place in O(1) space. click to show clarification. Cl

[LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede". 这道题让我们翻转字符串中的元音字母,元音字母有五个a,e,i,o

151 Reverse Words in a String 翻转字符串里的单词

给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用O(1) 时间复杂度的原地解法.说明:    什么构成一个词?    一系列非空格字符组成一个词.    输入字符串是否可以包含前导或尾随空格?    是.但是,您的反转字符串不应包含前导或尾随空格.    两个单词之间多空格怎么样?    将它们缩小到反转字符串中的单个空格.详见:https://leetc

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

题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc" 注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格. 思路 分割字符串,再逆序,拼接到字符串 代码实现 package String; /** * 557. Reverse Words in a St

SAP ABAP编程 在string类型A字符串中查询匹配string类型B字符串

如题,在string类型A字符串中查询匹配string类型B字符串,方法如下: DATA: a TYPE string VALUE 'ABCDEFGHIJKLNM', b TYPE string VALUE 'EFGHIJ', c TYPE string . DATA: off  TYPE i VALUE 0,   "从自己个字符开始查找 moff TYPE i, mlen TYPE i. FIND b IN SECTION OFFSET off OF a MATCH OFFSET moff &

Javascript --扩展String实现替换字符串中index处字符

String.prototype.replaceCharAt = function(n,c){ return this.substr(0, n)+ c + this.substr(n+1,this.length-1-n); } Javascript --扩展String实现替换字符串中index处字符