当前位置:网站首页>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();//从当前字符串中删除所有前导和尾随的空白字符。
}
边栏推荐
- Error[Pe147]: declaration is incompatible with '错误问题
- Dry goods likeshop takeout order system is open source, 100% open source, no encryption
- 关于硕博士开题报告编写的思考
- Sql Server 数据库之完整性约束
- [leetcode daily question 2021/2/18] [detailed explanation] minimum number of turns of 995. K continuous bits
- 反射机制简述
- px2rem-loader将px转化为rem,适配移动端vant-UI等框架
- Analysis of the transaction problem of chained method call
- 解决:无法加载文件 C:\Users\user\AppData\Roaming\npm\npx.ps1,因为在此系统上禁止运行脚本 。
- 【机器学习小记】【风格迁移】deeplearning.ai course4 4th week programming(tensorflow2)
猜你喜欢
![[dectectron2] follow the official demo](/img/aa/03e46897234c309415b336ac39b7d9.png)
[dectectron2] follow the official demo

sigmod 函数与softmax 函数对比

RT thread learning notes (I) -- configure RT thread development environment

Oracle cannot start tnslistener service cannot start

Add touch screen driver for stemwin 5.22 on Shenzhou IV development board

Issue 6: which mainstream programming language should college students choose

Redis docker instance and data structure
![[machine learning notes] [face recognition] deeplearning ai course4 4th week programming](/img/7e/9c0e88097b90c0c24ebf86f090805b.png)
[machine learning notes] [face recognition] deeplearning ai course4 4th week programming

20210807#1 C语言程序结构

Anaconda is used on vscode (the environment has been configured)
随机推荐
剑指Offer(五十二):正则化表达式
el-table实现可编辑表格
algorithm
Sql Server 之SQL语句对基本表及其中的数据的创建和修改
回到顶部的几种方案(js)
Dry goods likeshop takeout order system is open source, 100% open source, no encryption
剑指Offer(二十):包含min函数的栈
剑指Offer(二十):包含min函数的栈
[leetcode daily question 2021/5/8]1723. The shortest time to complete all work
剑指Offer(二十一):栈的压入、弹出序列
Successfully transplanted stemwin v5.22 on Shenzhou IV development board
RT-Thread 学习笔记(七)---开启基于SPI Flash的elmfat文件系统(中)
剑指Offer(七):斐波那契数列
Uninstall Meizu app store
超图 影像 如何去除黑边(两种方法)
.net operation redis hash object
toolstrip 去边框
在神州IV开发板上成功移植STemWin V5.22
在altium designer中禁用USBJATG
剑指Offer(九):变态跳台阶