当前位置:网站首页>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)
边栏推荐
- retrofit
- The mail function of LNMP environment cannot send mail
- 7-16 find the set of integers that meet the given conditions
- Tonybot humanoid robot infrared remote control play 0630
- Tailing rushes to the scientific and Technological Innovation Board: it plans to raise 1.3 billion, and Xiaomi Changjiang is the shareholder
- Mongodb index
- 7-3 count the number of words in a line of text
- ZABBIX saves the page blank after adding calculated items
- 基因家族特征分析 - 染色体定位分析
- Sendmail can't send mail and it's too slow to send. Solve it
猜你喜欢
retrofit
提高效率 Or 增加成本,开发人员应如何理解结对编程?
NPM install is stuck with various strange errors of node NPY
使用并行可微模拟加速策略学习
Creation of data table of Doris' learning notes
tonybot 人形机器人 红外遥控玩法 0630
Tailing rushes to the scientific and Technological Innovation Board: it plans to raise 1.3 billion, and Xiaomi Changjiang is the shareholder
Timecho of Tianmou technology completed an angel round financing of nearly 100 million yuan to create a native timing database of the industrial Internet of things
Four data flows and cases of grpc
Tonybot humanoid robot starts for the first time 0630
随机推荐
洛谷P5536 【XR-3】核心城市 题解
【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
dllexport和dllimport
Four data flows and cases of grpc
String reverse order
Understand the application scenario and implementation mechanism of differential segment
Thread. Sleep and timeunit SECONDS. The difference between sleep
7-10 stack of hats (25 points) (C language solution)
Write a 2-minute countdown.
DDK for XP
Programming language: the essence of type system
Puzzle (016.3) is inextricably linked
Common shortcut keys in PCB
7-22 tortoise and rabbit race (result oriented)
Code writing and playing method of tonybot humanoid robot at fixed distance
Ultra simple mobile map development
Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块
【7.3】146. LRU缓存机制
Paper sharing: generating playful palettes from images
Dllexport et dllimport