当前位置:网站首页>List and dictionary instance applications (※)
List and dictionary instance applications (※)
2022-07-26 10:59:00 【Lovey_ Beihe】
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 .
Code :
public static string SortSentence(string s)
{
Dictionary<int, string> dic = new Dictionary<int, string>();
List<string> ls = s.Split(' ').ToList();// String rotation list
int ls_count = ls.Count;
string res = "";
for (int i = 0; i < ls_count; i++)
{
string[] vs = new string[2];
vs[0] = ls[i].Substring(ls[i].Length - 1, 1);// Starting position , Number of interceptions
vs[1] = ls[i].Substring(0, ls[i].Length - 1);
dic.Add(Convert.ToInt32(vs[0]), vs[1]);
}
int count = dic.Count + 1;
for (int i = 1; i < count; i++)
{
res += " ";
res += dic[i];// Dictionaries int,string adopt int Value string
}
return res.Trim();// Removes all leading and trailing white space characters from the current string .
}
边栏推荐
- Sword finger offer (53): a string representing a numeric value
- 1748. Sum of unique elements
- ThreadPoolExecutor是怎样执行任务的
- Bash shell learning notes (6)
- @Validated 和 @Valid 的真正区别和使用方式
- @JsonFormat和@DateTimeFormat的区别和使用
- Simple use of MFC multithreading
- Bash shell学习笔记(四)
- RT thread learning notes (III) -- building a compilation environment with scons
- Multipartfil to file
猜你喜欢

菜鸟看源码之ArrayDeque

2021-08-12 function recursion_ Learn C language with brother Peng

Bash shell学习笔记(七)

Pytest execution rules_ Basic usage_ Common plug-ins_ Common assertions_ Common parameters

Newbie sees the source code arraydeque

349. Intersection of two arrays

使用Selenium抓取zabbix性能监控图

WIRESHARK基础教程以太帧的分析。

Kali view IP address

20210807 1 c language program structure
随机推荐
Bigdecimal的加减乘除、比较大小、向上向下取整 和 Bigdecimal的集合累加、判断BigDecimal是否有小数
ISO 639:1988 : Code for the representation of names of languages
Bash shell learning notes (6)
Bash shell学习笔记(七)
During the interview, how did the interviewer evaluate the level of rust engineers?
SCADA and three industrial control systems PLC, DCS and FCS
pytest 用例执行顺序
很多人都不清楚自己找的是Kanban软件还是看板软件
1837.K进制表示下的各位数字总和
104.二叉树的最大深度
使用Selenium抓取zabbix性能监控图
Implementing queues with two stacks
Stringing of macro parameters and connection of macro parameters in C language
Sword finger offer (44): flip the word order sequence
Bash shell学习笔记(一)
面试知识点
由浅入深搭建神经网络
C language pengge 20210812c language function
Simple use of MFC multithreading
Solve the problem of the popularity of org.apache.commons.codec.binary.base64