LeetCode(Sorting)1859. Sorting the Sentence

女爷i 2023-09-23 23:52 180阅读 0赞

1.问题

A sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each word consists of lowercase and uppercase English letters.

A sentence can be shuffled by appending the 1-indexed word position to each word then rearranging the words in the sentence.

For example, the sentence “This is a sentence” can be shuffled as “sentence4 a3 is2 This1” or “is2 sentence4 This1 a3”.
Given a shuffled sentence s containing no more than 9 words, reconstruct and return the original sentence.

Example 1:

Input: s = “is2 sentence4 This1 a3”
Output: “This is a sentence”
Explanation: Sort the words in s to their original positions “This1 is2 a3 sentence4”, then remove the numbers.

Example 2:

Input: s = “Myself2 Me1 I4 and3”
Output: “Me Myself and I”
Explanation: Sort the words in s to their original positions “Me1 Myself2 and3 I4”, then remove the numbers.

Constraints:

  • 2 <= s.length <= 200
  • s consists of lowercase and uppercase English letters, spaces, and digits from 1 to 9.
  • The number of words in s is between 1 and 9.
  • The words in s are separated by a single space.
  • s contains no leading or trailing spaces.

2. 解题思路

方法1:

1.s去掉空白,转成数组s1;新建数组str,长度为s1;定义一个空的String为result
2.遍历数组s1,将元素中的最后索引的数值,放入到str数组中,取从0到数值之前的字符
3.数组的元素遍历,并添加到result
4.返回result

3. 代码

代码1:

  1. class Solution {
  2. public String sortSentence(String s) {
  3. String[] s1 = s.split(" ");//1.s去掉空白,转成数组s1;新建数组str,长度为s1;
  4. String[] str=new String[s1.length];
  5. for(int i=0;i<s1.length;i++){
  6. //2.遍历数组s1,将元素中的最后索引的数值,放入到str数组中,取从0到数值之前的字符
  7. int index= s1[i].length()-1;
  8. int nums= Character.getNumericValue( s1[i].charAt(index));
  9. // int index = Integer.parseInt (String.valueOf ( s1[i].charAt (index));
  10. //或者不需要转成int,char也是可以
  11. //char nums= s1[i].charAt(index);str[nums-'1']= s1[i].substring(0,index)
  12. str[nums-1]= s1[i].substring(0,index);
  13. }
  14. return String.join(" ", str);//4.使用join()插入空白,返回str
  15. }
  16. }

解题思路基本相同,不同的在于for循环遍历放入到String result中,把最后的的空格用trim()方法去除

  1. class Solution {
  2. public String sortSentence(String s) {
  3. String[] s1 = s.split(" ");//1.s去掉空白,转成数组s1;新建数组str,长度为s1,定义String的result为空;
  4. String[] str=new String[s1.length];
  5. String result = "";
  6. for(int i=0;i<s1.length;i++){
  7. //2.遍历数组s1,将元素中的最后索引的数值,放入到str数组中,取从0到数值之前的字符
  8. int index= s1[i].length()-1;
  9. int nums= Character.getNumericValue( s1[i].charAt(index));
  10. // int index = Integer.parseInt (String.valueOf ( s1[i].charAt (index));
  11. //或者不需要转成int,char也是可以
  12. //char nums= s1[i].charAt(index);str[nums-'1']= s1[i].substring(0,index)
  13. str[nums-1]= s1[i].substring(0,index);
  14. }
  15. for (int i = 0; i < str.length; i++) {
  16. result += str[i] + " ";
  17. }
  18. return result.trim();//4.返回result
  19. }
  20. }

代码2:

和代码1解题思路基本相同

  1. class Solution {
  2. public String sortSentence(String s) {
  3. String []ans = new String[s.split(" ").length];
  4. for(String st: s.split(" ")){
  5. ans[st.charAt(st.length()-1) - '1'] = st.substring(0,st.length()-1);
  6. }
  7. return String.join(" ", ans);
  8. }
  9. }

发表评论

表情:
评论列表 (有 0 条评论,180人围观)

还没有评论,来说两句吧...

相关阅读

    相关 1859. 将句子排序——Go语言

    一个 句子 指的是一个序列的单词用单个空格连接起来,且开头和结尾没有任何空格。每个单词都只包含小写或大写英文字母。 我们可以给一个句子添加 从 1 开始的单词位置索引 ,并且