当前位置:网站首页>牛客 BM83 字符串變形(大小寫轉換,字符串反轉,字符串替換)
牛客 BM83 字符串變形(大小寫轉換,字符串反轉,字符串替換)
2022-07-03 14:38: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)
边栏推荐
- String reverse order
- Time conversion ()
- Zzuli:1053 sine function
- Zzuli: sum of 1051 square roots
- Dllexport et dllimport
- Luogu p5536 [xr-3] core city solution
- 7-24 reduction of the simplest fraction (rolling Division)
- Thread. Sleep and timeunit SECONDS. The difference between sleep
- 7-28 monkeys choose King (Joseph problem)
- 2021-10-16 initial programming
猜你喜欢

dllexport和dllimport

Paper sharing: generating playful palettes from images
![洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解](/img/89/da1a3a38e02671628f385de0f30369.png)
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解

提高效率 Or 增加成本,开发人员应如何理解结对编程?

MySQL multi table query subquery

tonybot 人形机器人 红外遥控玩法 0630

Programming language: the essence of type system

pyQt界面制作(登录+跳转页面)

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 人形机器人 定距移动 代码编写玩法
随机推荐
tonybot 人形机器人 红外遥控玩法 0630
How Facebook moves instagram from AWS to its own server
Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
7-2 and then what time (15 minutes)
Pyqt interface production (login + jump page)
Luogu p4047 [jsoi2010] tribal division solution
7-15 calculation of PI
7-4 BCD decryption (10 points)
Time conversion ()
Selective sorting
使用并行可微模拟加速策略学习
Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules
Etcd cluster permission management and account password usage
Zzuli:1042 sum of sequence 3
Luogu p5536 [xr-3] core city solution
Zzuli:1049 square sum and cubic sum
pyQt界面制作(登录+跳转页面)
Write a 2-minute countdown.
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
分布式事务(Seata) 四大模式详解