当前位置:网站首页>Sword finger offer Second Edition: string (simple)
Sword finger offer Second Edition: string (simple)
2022-07-23 21:18:00 【_ Soren】
List of articles
The finger of the sword Offer 05. Replace blank space
Please implement a function , Put the string s Replace each space in with "%20".
Original link : The finger of the sword Offer 05. Replace blank space
Example :
Input :s = “We are happy.”
Output :“We%20are%20happy.”
Answer key
class Solution {
public:
string replaceSpace(string s) {
if (s.empty()) {
return s;
}
string ans;
for (char c : s) {
if (c == ' ') {
ans.push_back('%');
ans.push_back('2');
ans.push_back('0');
}
else {
ans.push_back(c);
}
}
return ans;
}
};
The finger of the sword Offer 58 - II. Left rotation string
The left rotation operation of string is to transfer several characters in front of string to the end of string . Please define a function to implement the left rotation operation of string . such as , Input string "abcdefg" And number 2, This function will return the result of rotating two bits to the left "cdefgab".
Original link : The finger of the sword Offer 58 - II. Left rotation string
Answer key
Local inversion + Global inversion
Time complexity O(N)
Spatial complexity O(1)
class Solution {
public:
string reverseLeftWords(string s, int n) {
if (s.empty()) return s;
reverse(s.begin(), s.begin() + n);
reverse(s.begin() + n, s.end());
reverse(s.begin(), s.end());
return s;
}
};
String traversal
Time complexity O(N)
Spatial complexity O(N)
class Solution {
public:
string moveLeft(string s, int left) {
string ans = "";
for (int i = 0; i < s.length(); ++i) {
ans += s[(i + left) % s.length()];
}
return ans;
}
string reverseLeftWords(string s, int n) {
if (s.empty()) return s;
int left = n % s.length();
if (left == 0) return s;
else return moveLeft(s, left);
}
};
边栏推荐
- Failed to introspect class feignclientfactorybean exception troubleshooting
- 【攻防世界WEB】难度四星12分进阶题:FlatScience
- C——文件
- The common interfaces of Alipay are uniformly encapsulated and can be used directly for payment parameters (applicable to H5, PC, APP)
- [100 cases of scratch drawing] Figure 46-scratch drawing flowers children's programming scratch programming drawing case tutorial grade examination competition drawing training case
- Green-Tao 定理 (3): 反一致函数及其生成的 Sigma-代数
- 221. Largest square ● &1277. Square submatrix with statistics all 1 ● ●
- SQLite database
- 1309_STM32F103上增加GPIO的翻转并用FreeRTOS调度测试
- Unity - 3D mathematics -vector3
猜你喜欢

Synchronized同步锁的基本原理

基于速度、复杂性等因素比较KernelSHAP和TreeSHAP

OOM机制

High numbers | calculation of triple integral 1 | high numbers | handwritten notes

Stm32c8t6 driving lidar actual combat (II)

Network learning infrared module, 8-way emission independent control
![[continuous update] collection of raspberry pie startup and failure series](/img/f3/706a625cdc214960e2d9ca0c7ea41c.jpg)
[continuous update] collection of raspberry pie startup and failure series

Flink principle and development summary (detailed)

Protocol buffers 的问题和滥用

合宙ESP32C3硬件配置信息串口打印輸出
随机推荐
WinDbg practice -- Introduction
Network learning infrared module, 8-way emission independent control
Green Tao theorem (3): anti uniform functions and their generated sigma Algebras
做一个有职业操守的软件匠人
MySQL数据库索引
高数下|二重积分的计算4|高数叔|手写笔记
Is it safe to open a mobile stock account?
Synchronized同步锁的基本原理
High numbers | calculation of triple integral 2 | high numbers | handwritten notes
1061 Dating
【着色器实现RoundWave圆形波纹效果_Shader效果第六篇】
深入浅出边缘云 | 1. 概述
Chapter1 data cleaning
Scala programming (elementary)
221. 最大正方形 ●● & 1277. 统计全为 1 的正方形子矩阵 ●●
【攻防世界WEB】难度四星12分进阶题:Confusion1
STM32c8t6驱动激光雷达(一)
集群聊天服务器:如何解决跨服务器通信问题 | redis发布-订阅
flink原理及开发总结(详细)
基于速度、复杂性等因素比较KernelSHAP和TreeSHAP