【算法题】比较两个版本号的大小(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
 */
相关推荐
恋猫de小郭9 分钟前
移动端开发稳了?AI 目前还无法取代客户端开发,小红书的论文告诉你数据
前端·flutter·ai编程
文心快码BaiduComate28 分钟前
百度云与光本位签署战略合作:用AI Agent 重构芯片研发流程
前端·人工智能·架构
闲云一鹤1 小时前
nginx 快速入门教程 - 写给前端的你
前端·nginx·前端工程化
QCY1 小时前
「完全理解」1 分钟实现自己的 Coding Agent
前端·agent·claude
一拳不是超人2 小时前
Electron主窗口弹框被WebContentView遮挡?独立WebContentView弹框方案详解!
前端·javascript·electron
anyup2 小时前
🔥2026最推荐的跨平台方案:H5/小程序/App/鸿蒙,一套代码搞定
前端·uni-app·harmonyos
雮尘2 小时前
如何在非 Claude IDE (TARE、 Cursor、Antigravity 等)下使用 Agent Skills
前端·agent·ai编程
icebreaker2 小时前
Weapp-vite:原生模式之外,多一种 Vue SFC 选择
前端·vue.js·微信小程序
icebreaker3 小时前
重走 Vue 长征路 Weapp-vite:编译链路与 Wevu 运行时原理拆解
前端·vue.js·微信小程序
wuhen_n3 小时前
代码生成:从AST到render函数
前端·javascript·vue.js