代码随想录刷题记录 Day8
To Do List
- DAY 7 回顾
- LeetCode 344. 反转字符串
- LeetCode 541. 反转字符串II
- LeetCode 剑指Offer 05.替换空格
- LeetCode 151. 翻转字符串里的单词
- LeetCode 剑指Offer 58-II.左旋转字符串
DAY 7 回顾
()
LeetCode 344. 翻转字符串
1 | Difficulty: Easy |
Write a function that reverses a string. The input string is given as an array of characters s
.
You must do this by modifying the input array in-place with O(1)
extra memory.
Translated by zh-CN
编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 s 的形式给出。不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。
Example 1:
1 | Input: s = ["h","e","l","l","o"] |
Example 2:
1 | Input: s = ["H","a","n","n","a","h"] |
Constraints:
1 <= s.length <= 10^5
s[i]
is a printable ascii character.
Translated by zh-CN
s[i] 都是 ASCII 码表中的可打印字符解法:
(JavaScript)
1 | /** |
LeetCode 541. 翻转字符串 II / Reverse String II
1 | Difficulty: Easy |
Given a string s
and an integer k
, reverse the first k
characters for every 2k
characters counting from the start of the string.
If there are fewer than k
characters left, reverse all of them. If there are less than 2k
but greater than or equal to k
characters, then reverse the first k
characters and leave the other as original.
Translated by zh-CN
给定一个字符串 s 和一个整数 k,从字符串开头算起,每计数至 2k 个字符,就反转这 2k 字符中的前 k 个字符。如果剩余字符少于 k 个,则将剩余字符全部反转。
如果剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符,其余字符保持原样。
Example 1:
1 | Input: s = "abcdefg", k = 2 |
Example 2:
1 | Input: s = "abcd", k = 2 |
Constraints:
1 <= s.length <= 10^4
s
consists of only lowercase English letters.1 <= k <= 10^4
Translated by zh-CN
s 仅由小写英文组成解法:
(JavaScript)
1 | /** |
LeetCode 剑指 Offer 05. 替换空格
1 | Difficulty: Easy |
请实现一个函数,把字符串 s
中的每个空格替换成”%20”。
示例 1:
1 | 输入:s = "We are happy." |
限制:
1 | 0 <= s 的长度 <= 10000 |
解法:
(JavaScript 法一)
1 | /** |
PS: 用库函数解这道题很简单的 (x 一般情况下如果要练算法就不要过度依赖于库函数和 API.
(JavaScript 法二)
(其他方法带补充)
LeetCode 151. 翻转字符串里的单词 / Reverse Words in a String
1 | Difficulty: Medium |
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.
Translated by zh-CN
给你一个字符串 s ,请你反转字符串中 单词 的顺序。单词 是由非空格字符组成的字符串。s 中使用至少一个空格将字符串中的 单词 分隔开。
返回 单词 顺序颠倒且 单词 之间用单个空格连接的结果字符串.
注意:输入字符串 s中可能会存在前导空格、尾随空格或者单词间的多个空格。返回的结果字符串中,单词间应当仅用单个空格分隔,且不包含任何额外的空格。
Example 1:
1 | Input: s = "the sky is blue" |
Example 2:
1 | Input: s = " hello world " |
Example 3:
1 | Input: s = "a good example" |
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?
Translated by zh-CN
s 包含英文大小写字母、数字和空格 ' 's 中 至少存在一个 单词
如果字符串在你使用的编程语言中是一种可变数据类型,请尝试使用 O(1) 额外空间复杂度的 原地 解法。
解法:
(JavaScript 法一)
1 | /** |
PS: 用库函数解这道题很简单的 (x 一般情况下如果要练算法就不要过度依赖于库函数和 API.
(JavaScript 法二)
(其他方法带补充)
LeetCode 剑指 Offer 58 - II. 左旋转字符串
1 | Difficulty: Easy |
字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串”abcdefg”和数字2,该函数将返回左旋转两位得到的结果”cdefgab”。
示例 1:
1 | 输入: s = "abcdefg", k = 2 |
示例 2:
1 | 输入: s = "lrloseumgh", k = 6 |
限制:
1 <= k < s.length <= 10000
解法:
(JavaScript 法一)
1 | /** |
PS: 用库函数解这道题很简单的 (x 一般情况下如果要练算法就不要过度依赖于库函数和 API.
(JavaScript 法二)
(其他方法带补充)