当前位置:网站首页>Sword finger offer 05.58 Ⅱ string
Sword finger offer 05.58 Ⅱ string
2022-06-26 14:13:00 【hedgehog:】
Topic 1 :

Ideas : Traversal character array , Replace . Seem to use stringbuilder faster
Code :
// use string
class Solution {
public String replaceSpace(String s) {
String res = "";
for(Character c : s.toCharArray())
{
if(c == ' ')
res+="%20";
else
res+=c;
}
return res;
}
}
// use stringBulider
class Solution {
public String replaceSpace(String s) {
StringBuilder res = new StringBuilder();
for(Character c : s.toCharArray())
{
if(c == ' ')
res.append("%20");
else
res.append(c);
}
return res.toString();
}
}
result :


Topic two :

idea : Obtain substring parallel splicing in two times
Code :
class Solution {
public String reverseLeftWords(String s, int n) {
String res="";
res+=s.substring(n,s.length());
res+=s.substring(0,n);
return res;
}
}result :

Idea 2 : Out of commission substring when , It can be added to the new string twice .
Using the remainder method can simplify the code
Reference resources : author :jyd
link :https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/solution/mian-shi-ti-58-ii-zuo-xuan-zhuan-zi-fu-chuan-qie-p/
Code :
class Solution {
public String reverseLeftWords(String s, int n) {
StringBuilder res = new StringBuilder();
for(int i = n; i < n + s.length(); i++)
res.append(s.charAt(i % s.length()));
return res.toString();
}
}result :

边栏推荐
- Notes: the 11th and 12th generation mobile versions of Intel support the native thunderbolt4 interface, but the desktop version does not
- Educational Codeforces Round 117 (Rated for Div. 2)E. Messages
- Logical operation
- 证券开户安全吗,有没有什么危险啊
- Introduction to granular computing
- 虫子 内存管理 下 内存注意点
- Free machine learning dataset website (6300+ dataset)
- Calculate the distance between two points (2D, 3D)
- Formal parameters vs actual parameters
- Stream常用操作以及原理探索
猜你喜欢

ThreadLocal giant pit! Memory leaks are just Pediatrics

Detailed sorting of HW blue team traceability process

8. Ribbon load balancing service call

33. Use rgbd camera for target detection and depth information output
![[MySQL from introduction to mastery] [advanced part] (II) representation of MySQL directory structure and tables in the file system](/img/03/a1885e4740bbfdbdee2446e3dd81d0.png)
[MySQL from introduction to mastery] [advanced part] (II) representation of MySQL directory structure and tables in the file system

Zero basics of C language lesson 8: Functions

Stream常用操作以及原理探索

Cloudcompare - Poisson reconstruction

Embedded virlog code running process

Create your own cross domain proxy server
随机推荐
Why is there always a space (63 or 2048 sectors) in front of the first partition when partitioning a disk
MySQL | basic commands
Formal parameters vs actual parameters
Niuke challenge 53:c. strange magic array
mysql配置提高数据插入效率
9項規定6個嚴禁!教育部、應急管理部聯合印發《校外培訓機構消防安全管理九項規定》
Taishan Office Technology Lecture: four cases of using bold font
【HCSD应用开发实训营】一行代码秒上云评测文章—实验过程心得
Mongodb series window environment deployment configuration
Nexys A7开发板资源使用技巧
A must for programmers, an artifact utools that can improve your work efficiency n times
Notes: the 11th and 12th generation mobile versions of Intel support the native thunderbolt4 interface, but the desktop version does not
DOS command
Network remote access using raspberry pie
ThreadLocal giant pit! Memory leaks are just Pediatrics
GC is not used in D
[proteus simulation] Arduino uno key start / stop + PWM speed control DC motor speed
Bug STL string
Record: why is there no lightning 4 interface graphics card docking station and mobile hard disk?
Introduction to granular computing
https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/