当前位置:网站首页>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)
边栏推荐
- 洛谷P5536 【XR-3】核心城市 题解
- Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules
- Zzuli:1058 solving inequalities
- String sort
- NPM install is stuck with various strange errors of node NPY
- Amazon, express, lazada, shopee, eBay, wish, Wal Mart, Alibaba international, meikeduo and other cross-border e-commerce platforms evaluate how Ziyang account can seize traffic by using products in th
- Tonybot humanoid robot infrared remote control play 0630
- DDK for XP
- 7-9 one way in, two ways out (25 points)
- Sendmail can't send mail and it's too slow to send. Solve it
猜你喜欢

MySQL multi table query subquery

亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?

【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?

Tonybot humanoid robot infrared remote control play 0630

7-15 calculation of PI

Puzzle (016.3) is inextricably linked

Dllexport and dllimport

Tonybot humanoid robot checks the port and corresponds to port 0701

tonybot 人形机器人 查看端口并对应端口 0701

tonybot 人形机器人 红外遥控玩法 0630
随机推荐
The mail function of LNMP environment cannot send mail
7-3 count the number of words in a line of text
FPGA blocking assignment and non blocking assignment
ShowMeBug入驻腾讯会议,开启专业级技术面试时代
Detailed explanation of four modes of distributed transaction (Seata)
J-luggage lock of ICPC Shenyang station in 2021 regional games (simple code)
Find the sum of the elements of each row of the matrix
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
数学常数表 by q779
Zhejiang University Edition "C language programming (4th Edition)" topic set reference ideas set
Doris学习笔记之数据表的创建
556. 下一个更大元素 III : 简单构造模拟题
dllexport和dllimport
Ultra simple mobile map development
SSH access control, blocking the IP when logging in repeatedly to prevent brute force cracking
【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
Tonybot humanoid robot starts for the first time 0630
556. The next larger element III: simple construction simulation questions
1017 a divided by B (20 points)
分布式事务(Seata) 四大模式详解