【算法题】比较两个版本号的大小(js)



js 复制代码
const lines = ["5.2", "5.1a"];
const lines1 = ["5.6.1", "5.6.2a"];
const lines2 = ["5.6.8.a", "5.6.8.0a"];
const lines3 = ["5.06.08.a", "5.6.8.0a"];
const lines4 = ["5", "5.0.0.0"];
function solution(lines) {
  const [verson1, version2] = lines;
  if (!isValid(verson1) || !isValid(version2)) return 0;
  const str1 = verson1.replace(/[\.0]/g, "");
  const str2 = version2.replace(/[\.0]/g, "");
  return parseInt(str1) !== parseInt(str2)
    ? parseInt(str1) > parseInt(str2)
      ? 1
      : -1
    : 0;
  console.log(str1, str2);
  function isValid(ver) {
    if (/[^0-9a-zA-Z.]/.test(ver)) return false;
    const arr = ver.split(".");
    if (arr.length < 1) return false;
    return true;
  }
}

console.log(solution(lines));
/* 
5.2
5.1a
=> 1

5.6.1
5.6.2a
=>
-1

5.6.8.a
5.6.8.0a
=>
0

const lines3 = ["5.06.08.a", "5.6.8.0a"];  预计 0
const lines4 = ["5", "5.0.0.0"];    预计 0
 */
相关推荐
随意起个昵称6 小时前
区间dp-基础题目1(石子合并)
算法·动态规划
吞下星星的少年·-·6 小时前
线段树模板
算法
段一凡-华北理工大学6 小时前
2026 高炉炼铁智能化技术全景与演进路径~系列文章11:演进路径与行业未来
大数据·网络·人工智能·算法·工业智能体·高炉炼铁智能化
kyriewen6 小时前
微软用Go重写TypeScript编译器,速度提升10倍,网友:这是“背叛”还是“救赎”?
前端·typescript·ecmascript 6
Ceelog6 小时前
久坐党自救指南:屏幕前 8 小时,身体到底在经历什么
前端·后端
西陵6 小时前
Agent 为什么会陷入 Doom Loop?OpenClaw 的破解之道
前端·人工智能·ai编程
叶小鸡7 小时前
小鸡玩算法-力扣HOT100-多维动态规划
算法·leetcode·动态规划
星马梦缘7 小时前
aaaaa
数据结构·c++·算法
Hyyy7 小时前
普通前端续命周报——第2周
前端
swipe7 小时前
DeepAgents 实战:用多 Agent 架构搭一个深度调研助手
javascript·面试·llm