当前位置:网站首页>Niuke bm83 string deformation (case conversion, string inversion, string replacement)
Niuke bm83 string deformation (case conversion, string inversion, string replacement)
2022-07-03 14:38:00 【Hee hee author haha】
- subject :BM83 String deformation
Ideas
- Convert case . Traversal string , Encountered lowercase letters , Convert to uppercase , Encountered a capital letter , Convert to lowercase , When encountering spaces, it is normal and unchanged .
- Invert the entire string .
- Bounded by spaces , Invert each word individually .
import java.util.*;
public class Solution {
public String trans(String s, int n) {
// write code here
int len = s.length();
StringBuilder res = new StringBuilder(len);
// First step : Convert case
for(int i = 0; i < len; ++i){
char cur = s.charAt(i);
if(cur >= 'a' && cur <= 'z'){
res.append((char)(cur - 'a' + 'A')); // Lowercase to uppercase
} else if(cur >= 'A' && cur <= 'Z'){
res.append((char)(cur - 'A' + 'a')); // Capitalize to lowercase
} else{
res.append(cur); // Space
}
}
// The second step : Flip the entire string
res = res.reverse();
// The third step : Flip a single word
int i = 0;
int j = 0;
while(i < len){
j = i; // Point to space
// With space as the boundary , Flip a word
while(j < len && res.charAt(j) != ' '){
++j;
}
// Intercept [i, j) Substring of interval
StringBuilder word = new StringBuilder(res.substring(i, j));
word = word.reverse(); // Flip Words
res.replace(i, j, word.toString()); // Replace the original word
i = j + 1; // Start with the next word
}
return res.toString();
}
}
summary
- Lowercase to uppercase :
ch = ch - 'a' + 'A' - Capitalize to lowercase :
ch = ch - 'A' + 'a' - String inversion :
str = str.reverse() - Intercept substring :
sub = str.substring(start, end) - Substring substitution :
str.replace(start, end, str1)
边栏推荐
猜你喜欢

Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
![Luogu p4047 [jsoi2010] tribal division solution](/img/7f/3fab3e94abef3da1f5652db35361df.png)
Luogu p4047 [jsoi2010] tribal division solution

C language to realize mine sweeping

如何查询淘宝天猫的宝贝类目

Niuke: crossing the river

基因家族特征分析 - 染色体定位分析

使用并行可微模拟加速策略学习

Rasterization: a practical implementation (2)

retrofit

Four data flows and cases of grpc
随机推荐
ZABBIX saves the page blank after adding calculated items
Pyqt interface production (login + jump page)
retrofit
MySQL multi table query subquery
Luogu p3065 [usaco12dec]first! G problem solution
Zzuli:1059 highest score
【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
Recent learning summary
puzzle(016.3)千丝万缕
Special research report on the market of lithium battery electrolyte industry in China (2022 Edition)
Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution
SSH access control, blocking the IP when logging in repeatedly to prevent brute force cracking
Tiantu investment sprint Hong Kong stocks: asset management scale of 24.9 billion, invested in xiaohongshu and Naixue
Zzuli:1049 square sum and cubic sum
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
How Facebook moves instagram from AWS to its own server
Creation of data table of Doris' learning notes
Find specified characters
Accelerating strategy learning using parallel differentiable simulation
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon