【算法题】比较两个版本号的大小(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
 */
相关推荐
深邃-2 分钟前
字符函数和字符串函数(2)
c语言·数据结构·c++·后端·算法·restful
yb305小白4 分钟前
echarts 排名Y轴数据过多出现滚动条,排名柱形条绑定事件
前端·echarts
bekote4 分钟前
PTA基础编程题目集-6-11 求自定类型元素序列的中位数(简单解法)
数据结构·c++·算法
skywalk81638 分钟前
Kotti Next:使用FastAPI+Vue 3构建的现代无头CMS-Kotti CMS的精神继承者(使用WorkBuddy AI自动编程)
前端·vue.js·人工智能·fastapi·kotti
小码哥_常10 分钟前
Android开发秘籍:给图片加上独特水印
前端
坊钰10 分钟前
SpringBean的生命周期
前端·html
happymaker062611 分钟前
vue的基本使用和指令
前端·javascript·vue.js
threerocks14 分钟前
【前端转 Agent】01 | 从 Claude Code 开源热议聊起,不急着转 Python
前端·agent·claude
凉生阿新16 分钟前
【React】从零配置 Git Hooks:提交前自动校验与格式化(Vite + React 19)
前端·git·react.js