(新卷,100分)- 数字字符串组合倒序(Java & JS & Python)
题目描述
对数字,字符,数字串,字符串,以及数字与字符串组合进行倒序排列。
- 字符范围:由 a 到 z, A 到 Z,
- 数字范围:由 0 到 9
符号的定义:
- “-”作为连接符使用时作为字符串的一部分,例如“20-years”作为一个整体字符串呈现;
- 连续出现 2 个 “-” 及以上时视为字符串间隔符,如“out--standing”中的”–“视为间隔符,是 2 个独立整体字符串”out”和”standing”;
- 除了 1,2 里面定义的字符以外其他的所有字符,都是非法字符,作为字符串的间隔符处理,倒序后间隔符作为空格处理;
- 要求倒排后的单词间隔符以一个空格表示;如果有多个间隔符时,倒排转换后也只允许出现一个字格间隔符;
输入描述
无
输出描述
无
用例
| 输入 | I am an 20-years out--standing @ * -stu- dent |
| 输出 | dent stu standing out 20-years an am I |
| 说明 | 无 |
题目解析
本题考察字符串处理。
题目描述中说
除了 1,2 里面定义的字符以外其他的所有字符,都是非法字符,作为字符串的间隔符处理,倒序后间隔符作为空格处理;
即非数字、字母、'-' 的字符都是作为单词分隔符,因此我们可以按此逻辑对输入的字符串进行分割,得到一个单词数组words。
接下来,倒序遍历words中每一个单词word,对word处理如下:
- 如果word首尾含有'-',则需要去除首尾'-'
- 如果word中间含有"--",则需要将word按照"--"分割为多个subWords单词数组
- 倒序遍历subWords单词数组,将不为空串的元素拼接到题解ans中
本题描述中:
连续出现 2 个 “-” 及以上时视为字符串间隔符
这里之前把“及以上”看漏了,因此上面对于word的处理中,第2步不能按照"--"分割,而是应该按照正则"-{2,}"分割
JS算法源码
/* JavaScript Node ACM模式 控制台输入获取 */ const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); rl.on("line", (line) => { console.log(getResult(line)); }); function getResult(s) { // 非字母、数字、-的字符都当成单词之间的分隔符 const words = s.split(/[^0-9a-zA-Z\-]/); // 记录题解 const ans = []; // 倒序遍历单词 for (let i = words.length - 1; i >= 0; i--) { let word = words[i]; if (word == "") continue; // 单词左边界不能是 - let l = 0; while (l < word.length && word[l] == "-") l++; // 单词右边界不能是 - let r = word.length - 1; while (r >= 0 && word[r] == "-") r--; // 如果单词就是“-”, 则经过上面边界处理后,会导致 l > r if (l > r) continue; // 处理完单词左、右边界后的新单词 word = word.slice(l, r + 1); // 单词中间的"--"视为分隔符 const subWords = word.split(/-{2,}/); // 按照"--"分割后,倒序记录入ans for (let j = subWords.length - 1; j >= 0; j--) { if (subWords[j] != "") ans.push(subWords[j]); } } return ans.join(" "); }Java算法源码
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println(getResult(sc.nextLine())); } public static String getResult(String s) { // 非字母、数字、-的字符都当成单词之间的分隔符 String[] words = s.split("[^0-9a-zA-Z\\-]"); // 记录题解 ArrayList<String> ans = new ArrayList<>(); // 倒序遍历单词 for (int i = words.length - 1; i >= 0; i--) { String word = words[i]; if ("".equals(word)) continue; // 单词左边界不能是 - int l = 0; while (l < word.length() && word.charAt(l) == '-') { l++; } // 单词右边界不能是 - int r = word.length() - 1; while (r >= 0 && word.charAt(r) == '-') { r--; } // 如果单词就是“-”, 则经过上面边界处理后,会导致 l > r if (l > r) continue; // 处理完单词左、右边界后的新单词 word = word.substring(l, r + 1); // 单词中间的"--"视为分隔符 String[] subWords = word.split("-{2,}"); // 按照"--"分割后,倒序记录入ans for (int j = subWords.length - 1; j >= 0; j--) { if (!"".equals(subWords[j])) ans.add(subWords[j]); } } return String.join(" ", ans); } }Python算法源码
# 输入获取 import re s = input() # 算法入口 def getResult(): # 非字母、数字、-的字符都当成单词之间的分隔符 words = re.split(r"[^0-9a-zA-Z\-]", s) # 记录题解 ans = [] # 倒序遍历单词 for i in range(len(words) - 1, -1, -1): word = words[i] if word == "": continue # 单词左、右边界不能是 - word = word.strip('-') # 单词中间的"--"视为分隔符 subWords = re.split(r"-{2,}", word) # 按照"--"分割后,倒序记录入ans for j in range(len(subWords) - 1, -1, -1): if subWords[j] != "": ans.append(subWords[j]) return " ".join(ans) # 算法调用 print(getResult())