当前位置:网站首页>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();//从当前字符串中删除所有前导和尾随的空白字符。
}
边栏推荐
- Flutter jni混淆 引入.so文件release包闪退
- sigmod 函数与softmax 函数对比
- 剑指Offer(二十):包含min函数的栈
- 鹏哥C语言20210811程序结构作业
- 【机器学习小记】【人脸识别】deeplearning.ai course4 4th week programming
- flutter dart生成N个区间范围内不重复随机数List
- 工厂模式详解
- Centos8 (liunx) deploying WTM (asp.net 5) using PgSQL
- [leetcode daily question 2021/4/29]403. Frogs cross the river
- Build ARM embedded development environment
猜你喜欢

Issue 6: which mainstream programming language should college students choose
![[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

flutter 背景变灰效果,如何透明度,灰色蒙板遮罩
![[leetcode daily question 2021/8/30]528. Choose randomly by weight [medium]](/img/13/c6cb176d7065035f60d55ad20ed1bf.png)
[leetcode daily question 2021/8/30]528. Choose randomly by weight [medium]
![[leetcode每日一题2021/2/18]【详解】995. K 连续位的最小翻转次数](/img/de/62fca587cde95110c2a967ca93eea5.png)
[leetcode每日一题2021/2/18]【详解】995. K 连续位的最小翻转次数
![[dectectron2] follow the official demo](/img/aa/03e46897234c309415b336ac39b7d9.png)
[dectectron2] follow the official demo

Zongzi battle - guess who can win

2021-08-12函数递归_和鹏哥学习C语言

Uniapp uses the simple method signalr (only for web debugging, cannot package apps)

Oracle cannot start tnslistener service cannot start
随机推荐
Error[Pe147]: declaration is incompatible with '错误问题
[leetcode每日一题2021/4/29]403. 青蛙过河
[leetcode daily question 2021/5/8]1723. The shortest time to complete all work
[leetcode每日一题2021/2/14]765. 情侣牵手
Redis implementation of distributed lock solution
Flutter报错 Incorrect use of ParentDataWidget When the exception was thrown, this was the stack:
在神州IV开发板上为STemWin 5.22加入触屏驱动
[转]ArcGIS中判断两个Geometry之间的关系
第4期:大学生提前职业技能准备之一
Sql Server 数据库之初学体验
一文详解Nodejs中fs文件模块与path路径模块
$router和$route的区别
C language callback function
RT-Thread 学习笔记(六)--- 开启基于SPI Flash的elmfat文件系统(上)
【机器学习小记】【风格迁移】deeplearning.ai course4 4th week programming(tensorflow2)
[dectectron2] follow the official demo
display-inline+calc实现左中右布局,中间自适应
Analysis of the transaction problem of chained method call
20210807#1 C语言程序结构
MySQL速学-2021-09-01