当前位置:网站首页>牛客 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)
边栏推荐
- 洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
- X86 assembly language - Notes from real mode to protected mode
- How Facebook moves instagram from AWS to its own server
- Use of constraintlayout
- 556. The next larger element III
- 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
- Zzuli:1045 numerical statistics
- China PETG market forecast and Strategic Research Report (2022 Edition)
- 亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?
- Zzuli:1049 square sum and cubic sum
猜你喜欢

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

NPM install is stuck with various strange errors of node NPY

retrofit

Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion

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

Tonybot Humanoïde Robot Infrared Remote play 0630

To improve efficiency or increase costs, how should developers understand pair programming?

Accelerating strategy learning using parallel differentiable simulation

Adc128s022 ADC Verilog design and Implementation

ShowMeBug入驻腾讯会议,开启专业级技术面试时代
随机推荐
全文检索引擎Solr系列—–全文检索基本原理
7-3 rental (20 points)
7-20 print 99 formula table (format output)
Mongodb index
Zzuli:1052 sum of sequence 4
牛客 BM83 字符串变形(大小写转换,字符串反转,字符串替换)
Find books ()
【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
Ultra simple mobile map development
【7.3】146. LRU caching mechanism
如何查询淘宝天猫的宝贝类目
7-14 sum integer segments (C language)
Code writing and playing method of tonybot humanoid robot at fixed distance
Puzzle (016.4) domino effect
Four data flows and cases of grpc
US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
Tailing rushes to the scientific and Technological Innovation Board: it plans to raise 1.3 billion, and Xiaomi Changjiang is the shareholder
Find the sum of the elements of each row of the matrix
puzzle(016.4)多米诺效应