当前位置:网站首页>牛客 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)
边栏推荐
- 洛谷P5194 [USACO05DEC]Scales S 题解
- Zzuli:1057 prime number determination
- Thread.sleep和TimeUnit.SECONDS.sleep的区别
- Zzuli:1048 factorial table
- Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion
- Sword finger offer 28 Symmetric binary tree
- ZABBIX saves the page blank after adding calculated items
- Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
- Zzuli:1042 sum of sequence 3
- 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
猜你喜欢

天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库

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 starts for the first time 0630

7-18 finding the single root of polynomial by dichotomy

Puzzle (016.3) is inextricably linked

Detailed explanation of four modes of distributed transaction (Seata)

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

tonybot 人形机器人 首次开机 0630

Tonybot Humanoïde Robot Infrared Remote play 0630

Dllexport et dllimport
随机推荐
LNMP环境mail函数不能发送邮件解决
适用于XP的DDK
7-22 tortoise and rabbit race (result oriented)
7-23 currency conversion (using array conversion)
Use of constraintlayout
Tonybot humanoid robot infrared remote control play 0630
7-1 positive integer a+b (15 points)
Luogu p5536 [xr-3] core city solution
Zzuli:1041 sum of sequence 2
全文检索引擎Solr系列—–全文检索基本原理
Analysis of gene family characteristics - chromosome location analysis
7-16 find the set of integers that meet the given conditions
Ultra simple mobile map development
Understand the application scenario and implementation mechanism of differential segment
tonybot 人形機器人 紅外遙控玩法 0630
洛谷P4047 [JSOI2010]部落划分 题解
7-3 rental (20 points)
Zzuli:1047 logarithmic table
基因家族特征分析 - 染色体定位分析
Special research report on the market of lithium battery electrolyte industry in China (2022 Edition)