当前位置:网站首页>LIst和Dictionary实例应用(※)
LIst和Dictionary实例应用(※)
2022-07-26 10:41:00 【Lovey_北禾】
一个 句子 指的是一个序列的单词用单个空格连接起来,且开头和结尾没有任何空格。每个单词都只包含小写或大写英文字母。
我们可以给一个句子添加 从 1 开始的单词位置索引 ,并且将句子中所有单词 打乱顺序 。
比方说,句子 "This is a sentence" 可以被打乱顺序得到 "sentence4 a3 is2 This1" 或者 "is2 sentence4 This1 a3" 。
给你一个 打乱顺序 的句子 s ,它包含的单词不超过 9 个,请你重新构造并得到原本顺序的句子。
实例1:
输入:s = "is2 sentence4 This1 a3"
输出:"This is a sentence"
解释:将 s 中的单词按照初始位置排序,得到 "This1 is2 a3 sentence4" ,然后删除数字。
实例2:
输入:s = "Myself2 Me1 I4 and3"
输出:"Me Myself and I"
解释:将 s 中的单词按照初始位置排序,得到 "Me1 Myself2 and3 I4" ,然后删除数字。
代码:
public static string SortSentence(string s)
{
Dictionary<int, string> dic = new Dictionary<int, string>();
List<string> ls = s.Split(' ').ToList();//字符串转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);//开始位置,截取个数
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];//字典int,string通过int取值string
}
return res.Trim();//从当前字符串中删除所有前导和尾随的空白字符。
}
边栏推荐
- vscode上使用anaconda(已经配置好环境)
- 剑指Offer(五):用两个栈实现队列
- [leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array
- A semicolon is missing
- Problems encountered in QRcode QR code (C language)
- RT-Thread 学习笔记(一)---配置RT-Thread开发环境
- Flutter TextField设置高度并且自动换行,圆角边框去除下划线
- [leetcode daily question 2021/4/23]368. Maximum divisible subset
- SQL Server 之Sql语句创建数据库
- The problem of large fluctuation of hx711 data
猜你喜欢

Codepoint 58880 not found in font, aborting. flutter build apk时报错
![Error[Pe147]: declaration is incompatible with '错误问题](/img/4f/57145d78f4dc1fe84d2f271dd9d82f.png)
Error[Pe147]: declaration is incompatible with '错误问题

RT-Thread 学习笔记(一)---配置RT-Thread开发环境

在altium designer中禁用USBJATG

RT-Thread 学习笔记(七)---开启基于SPI Flash的elmfat文件系统(中)

Issue 5: the second essential skill for College Students

Problems encountered in QRcode QR code (C language)

英语基础句型结构------起源
![[leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array](/img/9d/5ce5d4144a9edc3891147290e360d8.png)
[leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array
![[leetcode daily question 2021/4/29]403. Frogs cross the river](/img/fb/612777c77df5a611506e72f4f4c3c8.png)
[leetcode daily question 2021/4/29]403. Frogs cross the river
随机推荐
在altium designer中禁用USBJATG
.net operation redis list list
【dectectron2】跟着官方demo一起做
RT-Thread 学习笔记(七)---开启基于SPI Flash的elmfat文件系统(中)
[leetcode每日一题2021/4/29]403. 青蛙过河
11 handle "self assignment" in operator=
1748.唯一元素的和
剑指Offer(四十九):把字符串转换成整数
10 let operator= return a reference to *this
MySQL速学-2021-09-01
扫雷pro版2021-08-19
点击el-dropdown-item/@click.native
.net5wtm (asp.net core) PgSQL unpacking operation
[paper after dinner] deep mining external perfect data for chestx ray disease screening
RT thread learning notes (I) -- configure RT thread development environment
Issue 8: cloud native -- how should college students learn in the workplace
putty的使用教程
Redis implementation of distributed lock solution
Redis special data type usage scenarios
剑指Offer(五十三):表示数值的字符串