当前位置:网站首页>[jzof] 05 replace spaces
[jzof] 05 replace spaces
2022-07-24 21:05:00 【Sighed, angry】

import java.util.*;
public class Solution {
/** * The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly * * * @param s string character string * @return string character string */
public String replaceSpace(String s) {
if (s.length() == 0){
return s;
}
int blankNum = 0;
// Calculate the number of spaces
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 32){
blankNum++;
}
}
// The length of the array after replacement
int newStrLen = s.length() + blankNum*2;
// Expand the source string
char[] chars = Arrays.copyOf(s.toCharArray(), newStrLen);
int oldEndIndex = s.length() -1;
int newEndIndex = newStrLen - 1;
for (;oldEndIndex>=0;oldEndIndex--,newEndIndex--){
if (oldEndIndex == newEndIndex){
return new String(chars);
}
if (s.charAt(oldEndIndex) == 32){
chars[newEndIndex] = '0';
chars[--newEndIndex] = '2';
chars[--newEndIndex] = '%';
}else {
chars[newEndIndex] = s.charAt(oldEndIndex);
}
}
return new String(chars);
}
}
边栏推荐
- Docker builds redis and clusters
- Pressing Ctrl will cause several key press messages
- [learning notes] agc008
- Alibaba Sentinel 基操
- Pychart tutorial: 5 very useful tips
- what? Does the multi merchant system not adapt to app? This is coming!
- Hilditch refinement (implementation I)
- One bite of Stream(6)
- [training Day9] rotate [violence] [thinking]
- Bring new people's experience
猜你喜欢
![[feature selection] several methods of feature selection](/img/ee/2f5224f97ac3090a535c9c74bc898f.png)
[feature selection] several methods of feature selection

RESNET interpretation and 1 × 1 Introduction to convolution
![[record of question brushing] 16. The sum of the nearest three numbers](/img/b9/a78b72650e94c75ccbe22af1f43857.png)
[record of question brushing] 16. The sum of the nearest three numbers

Rce (no echo)

A new UI testing method: visual perception test
![[training Day9] maze [line segment tree]](/img/56/e8458245fe564821740ab94ece37a4.png)
[training Day9] maze [line segment tree]

What does software testing need to learn?
![[training Day10] silly [simulation] [greed]](/img/31/94c32e05b498f8ad192f8ec2c500ca.png)
[training Day10] silly [simulation] [greed]

The difference between map and flatmap in stream

2022 chemical automation control instrument test question simulation test platform operation
随机推荐
what? Does the multi merchant system not adapt to app? This is coming!
Smarter! Airiot accelerates the upgrading of energy conservation and emission reduction in the coal industry
Do you want to enroll in a training class or study by yourself?
Spark related FAQ summary
HSPF (hydraulic simulation program FORTRAN) model
API data interface of A-share transaction data
Put apples
Oracle creates table spaces and views table spaces and usage
MySQL docker installation master-slave deployment
Together again Net program hangs dead, a real case analysis of using WinDbg
Delete remote and local branches
Sword finger offer 15. number of 1 in binary
Intranet penetration learning (I) introduction to Intranet
High soft course summary
Alibaba Sentinel 基操
[training Day10] linear [mathematics] [thinking]
[install PG]
Software testing interview tips | if you don't receive the offer, I'll wash my hair upside down
Eight transformation qualities that it leaders should possess
Oracle primary key auto increment setting