题解 | #字符串排序#

字符串排序

http://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584

用函数式编程还受不了,,感觉目标环境版本较低,有些不支持的api和没更新的api比如排序 es2019 已经原地排序了

function getEnglish(str) {
  const reg = /[a-zA-Z]/;
  const res = [];
  for (let i = 0; i < 26; i++) {
    for (let p of str) {
      if (reg.test(p) && p.charCodeAt() === i + 65 || p.charCodeAt() === i + 97) {
        res.push(p);
      }
    }
  }
  return res;
}
function sortString() {
  const str = readline().split('');
  const reg = /[a-zA-Z]/;

  const english = getEnglish(str);
  const res = [];
  for (let i = 0, j = 0; i < str.length; i++) {
    if (reg.test(str[i])) {
      res.push(english[j++]);
    } else {
      res.push(str[i]);
    }
  }
  console.log(res.join(''))
  return res.join('');
}

sortString()
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务