【算法题】比较两个版本号的大小(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
 */
相关推荐
梯度下降中几秒前
求职面试中的线代知识总结
人工智能·线性代数·算法·机器学习
RichardLau_Cx7 分钟前
【保姆级实操】MediaPipe SDK/API 前端项目接入指南(Web版,可直接复制代码)
前端·vue·react·webassembly·mediapipe·手部追踪·前端计算机视觉
SmartBrain11 分钟前
OCR 模型在医疗场景的选型研究
人工智能·算法·语言模型·架构·aigc·ocr
梵刹古音12 分钟前
【C语言】 跳转语句
c语言·开发语言·算法
不爱写程序的东方不败16 分钟前
APP接口测试流程实战Posman+Fiddler
前端·测试工具·fiddler
liu****23 分钟前
29.路径类dp
c++·算法·acm
JMchen12324 分钟前
Android计算摄影实战:多帧合成、HDR+与夜景算法深度剖析
android·经验分享·数码相机·算法·移动开发·android-studio
阿猿收手吧!29 分钟前
【C++】C++模板特化:精准定制泛型逻辑
开发语言·c++·算法
智驱力人工智能43 分钟前
货车走快车道检测 高速公路安全治理的工程实践与价值闭环 高速公路货车占用小客车道抓拍系统 城市快速路货车违规占道AI识别
人工智能·opencv·算法·安全·yolo·目标检测·边缘计算
喵手1 小时前
Python爬虫实战:电商实体消歧完整实战 - 从混乱店铺名到标准化知识库的工程化实现,一文带你搞定!
爬虫·python·算法·爬虫实战·零基础python爬虫教学·同名实体消除·从混乱店铺名到标准化知识库