当前位置:网站首页>[Li Kou] 1859. Sort sentences
[Li Kou] 1859. Sort sentences
2022-07-27 02:36:00 【aigo-2021】
One The sentence It refers to a sequence of words connected by a single space , And There are no spaces at the beginning and end . Every word contains only lowercase or uppercase letters .
We can add... To a sentence from 1 Start word position index , And put all the words in the sentence Out of order .
For example , The sentence "This is a sentence" Can be scrambled to get "sentence4 a3 is2 This1" perhaps "is2 sentence4 This1 a3" .
To give you one Out of order Sentences s , It contains no more than 9 individual , Please reconstruct and get the sentences in the original order .
Example 1:
Input :s = "is2 sentence4 This1 a3"
Output :"This is a sentence"
explain : take s The words in are sorted according to their initial positions , obtain "This1 is2 a3 sentence4" , Then delete the number .
Example 2:
Input :s = "Myself2 Me1 I4 and3"
Output :"Me Myself and I"
explain : take s The words in are sorted according to their initial positions , obtain "Me1 Myself2 and3 I4" , Then delete the number .
Tips :
2 <= s.length <= 200
s Only lowercase and uppercase letters 、 Spaces and from 1 To 9 The number of .
s The number of words in is 1 To 9 individual .
s Words in are separated by a single space .
s Does not contain any leading or suffix spaces .
Code :
Mode one :TreeMap
class Solution {
public String sortSentence(String s) {
TreeMap treemap = new TreeMap();//TreeMap Is ordered ( Orderly refers to key)
String[] strs=s.split(" ");// Truncate the string into the array by spaces
int key=-1;
for(int i=0;i<strs.length;i++){
key=strs[i].charAt(strs[i].length()-1)-'0';// Get the number after each word as key
String value=strs[i].substring(0, strs[i].length()-1);// Intercept words as value
treemap.put(key,value);
}
StringBuilder s1= new StringBuilder();
for(int i=1;i<= strs.length;i++){
s1.append(treemap.get(i)+" ");// Connect all words by spaces
}
return s1.substring(0,s1.length()-1);// Remove the space at the end
}
public static void main(String[] args) {
Solution s=new Solution();
System.out.println(s.sortSentence("is2 sentence4 This1 a3"));
}
}
Mode two : Sort the corresponding elements through the array subscript
class Solution {
public String sortSentence(String s) {
String[] strs = s.split(" ");
String[] res = new String[strs.length];
for (String str : strs) {
// Get the number after each word as an array subscript to store words , Sort in this way
int count = str.charAt(str.length() - 1) - '0' - 1;// reduce 1, Array subscripts start at zero
// Save words in an array
res[count] = str.substring(0, str.length() - 1);
}
return String.join(" ", res);// Splicing
}
}
边栏推荐
- Smooth data migration from single table to sub table
- 静态路由实验配置
- TCP三次握手、四次断开
- 最新多线程&高并发学习资料,面试心里有底气
- 关于序列化变量保存的坑,加了索引器的数据不能序列化
- C language - assignment operator, compound assignment operator, self increasing and self decreasing operator, comma operator, conditional operator, goto statement, comment
- RIP路由信息协议-拓扑实验
- N methods of SQL optimization
- 创业3年,现在鹅厂,年收入百万+,作为软件测试前辈的一些建议....
- OSPF路由信息协议-拓扑实验
猜你喜欢

从初级程序员到架构师学习路线+配套学习资源完整版

【洋哥带你玩转线性表(三)——双向链表】

I was fired at the age of 30. I want to understand a few things
![[use SQLite3 library to realize student information management system in Visual Studio 2019]](/img/49/5d6c47654e446ab96099a821a16369.png)
[use SQLite3 library to realize student information management system in Visual Studio 2019]
![[after this article, learning the linked list is a smooth way]](/img/e7/cbdfecd1d98f91ecfeeb620b265f89.png)
[after this article, learning the linked list is a smooth way]

Interesting C language

Sort the three integers from large to small (introduce various methods in detail)

Static routing experiment configuration

【用C语言绘制直角坐标系】

多线程的具体使用
随机推荐
What is the principle of synchronized lock escalation in multithreading?
Area optimization of digital chips: detailed explanation of question 1 in the digital direction of the third "Huawei Cup" graduate innovation core competition
I was fired at the age of 30. I want to understand a few things
C language - assignment operator, compound assignment operator, self increasing and self decreasing operator, comma operator, conditional operator, goto statement, comment
Today, let's talk about escape characters [cute new version]
OSPF总结(思维导图)
Solve prime numbers between 100 and 200
JUC concurrent programming
【用C语言绘制谢尔宾斯基三角形】
从单表到分表实现数据平滑迁移
hcip--ospf接口网络接口类型实验
Witness that the "decoding 2022 strong star of China's network security" is about to set sail
Array methods and loops in JS
Hcip the next day
After working in Tencent testing post for 5 years, I was ruthlessly dismissed in July, trying to wake up my brother who was still paddling
Is it useful to lie down with your eyes closed when you can't sleep?
聊聊自动化测试的度量指标
Summary of dataset operations in ppocrlabel format.
Prometheus 运维工具 Promtool (三) Debug 功能
HCIP-第六天-OSPF静态大实验