当前位置:网站首页>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)
边栏推荐
猜你喜欢

7-15 calculation of PI

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

Use of form text box (I) select text

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

Puzzle (016.3) is inextricably linked

dllexport和dllimport

如何查询淘宝天猫的宝贝类目

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

论文分享:Generating Playful Palettes from Images

Tonybot humanoid robot infrared remote control play 0630
随机推荐
光猫超级账号密码、宽带账号密码 获取
Tailing rushes to the scientific and Technological Innovation Board: it plans to raise 1.3 billion, and Xiaomi Changjiang is the shareholder
Zzuli:1040 sum of sequence 1
7-23 currency conversion (using array conversion)
Solr series of full-text search engines - basic principles of full-text search
Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
2021-10-16 initial programming
7-28 monkeys choose King (Joseph problem)
7-14 sum integer segments (C language)
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
Statistical capital consonants
Pyqt interface production (login + jump page)
C language to implement a password manager (under update)
Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块
Sword finger offer 28 Symmetric binary tree
动态获取权限
Find the sum of the elements of each row of the matrix
Accelerating strategy learning using parallel differentiable simulation
Zzuli:1044 failure rate
分布式事务(Seata) 四大模式详解