当前位置:网站首页>牛客 BM83 字符串变形(大小写转换,字符串反转,字符串替换)
牛客 BM83 字符串变形(大小写转换,字符串反转,字符串替换)
2022-07-03 14:37:00 【嘻嘻作者哈哈】
- 题目:BM83 字符串变形
思路
- 转换大小写。遍历字符串,遇到小写字母,转换成大写,遇到大写字母,转换成小写,遇到空格正常不变。
- 反转整个字符串。
- 以空格为界限,对每一个单词单独进行反转。
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);
// 第一步:转换大小写
for(int i = 0; i < len; ++i){
char cur = s.charAt(i);
if(cur >= 'a' && cur <= 'z'){
res.append((char)(cur - 'a' + 'A')); // 小写变大写
} else if(cur >= 'A' && cur <= 'Z'){
res.append((char)(cur - 'A' + 'a')); // 大写变小写
} else{
res.append(cur); // 空格
}
}
// 第二步:翻转整个字符串
res = res.reverse();
// 第三步:单个单词进行翻转
int i = 0;
int j = 0;
while(i < len){
j = i; // 指向空格
// 以空格为边界,翻转一个单词
while(j < len && res.charAt(j) != ' '){
++j;
}
// 截取[i, j)区间的子串
StringBuilder word = new StringBuilder(res.substring(i, j));
word = word.reverse(); // 翻转单词
res.replace(i, j, word.toString()); // 替换原单词
i = j + 1; // 从下一个单词开始
}
return res.toString();
}
}
总结
- 小写变大写:
ch = ch - 'a' + 'A' - 大写变小写:
ch = ch - 'A' + 'a' - 字符串反转:
str = str.reverse() - 截取子串:
sub = str.substring(start, end) - 子串替换:
str.replace(start, end, str1)
边栏推荐
- Stop asking yourself if you are suitable for software testing
- FPGA blocking assignment and non blocking assignment
- 7-1 positive integer a+b (15 points)
- 动态获取权限
- Thread.sleep和TimeUnit.SECONDS.sleep的区别
- Selective sorting
- 关于敏捷的一些概念
- Tailing rushes to the scientific and Technological Innovation Board: it plans to raise 1.3 billion, and Xiaomi Changjiang is the shareholder
- 添加Zabbix计算类型项目Calculated items
- 556. The next larger element III: simple construction simulation questions
猜你喜欢

Pyqt interface production (login + jump page)

Code writing and playing method of tonybot humanoid robot at fixed distance

adc128s022 ADC verilog设计实现

Analysis of gene family characteristics - chromosome location analysis
![洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解](/img/89/da1a3a38e02671628f385de0f30369.png)
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解

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

Tonybot humanoid robot infrared remote control play 0630

Jiuyi cloud black free encryption free version source code

Why is this error reported when modifying records in the database
![Luogu p4047 [jsoi2010] tribal division solution](/img/7f/3fab3e94abef3da1f5652db35361df.png)
Luogu p4047 [jsoi2010] tribal division solution
随机推荐
Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
Jiuyi cloud black free encryption free version source code
7-9 one way in, two ways out (25 points)
Zzuli:1046 product of odd numbers
Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules
洛谷P4047 [JSOI2010]部落划分 题解
puzzle(016.4)多米诺效应
Find the sum of the elements of each row of the matrix
洛谷P5536 【XR-3】核心城市 题解
US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars
Statistical capital consonants
一文了解微分段应用场景与实现机制
Thread.sleep和TimeUnit.SECONDS.sleep的区别
Luogu p5536 [xr-3] core city solution
Analysis of gene family characteristics - chromosome location analysis
Common commands for getting started with mongodb database
Pyqt interface production (login + jump page)
关于敏捷的一些概念
Too many files with unapproved license
Why is this error reported when modifying records in the database