leetcode:比较版本号

cpp 复制代码
class Solution {
public:
    stack<int> s1;
    
    
    stack<int> s2;
    
    long long getvalue(string s,int i,bool flag)
    {
        long long ret = 0;
        while (i < s.size() && s[i] == '0')
        {
            i++;
            if (flag)
                s1.push(i);
            else s2.push(i);
        }
        while (i<s.size()&& s[i] != '.')
        {
            ret = ret * 10 + s[i] - '0';
            i++;
            if (flag)
                s1.push(i);
            else s2.push(i);
        }
        return ret;
    }
    int compareVersion(string version1, string version2)
    {
        s1.push(-1);
        s2.push(-1);
        
        while (s1.top()+1 < version1.size() && s2.top()+1 < version2.size())
        {
            int ret = getvalue(version1, s1.top() + 1, 1) - getvalue(version2, s2.top() + 1, 0);
            if (ret > 0) return 1;
            else if (ret < 0) return -1;

        }
        int ret1 = 0;
        int ret2 = 0;
        while (s1.top() + 1 < version1.size())
        {
            ret1 += getvalue(version1, s1.top() + 1, 1);
        }
        while (s2.top() + 1 < version2.size())
        {
            ret2 += getvalue(version2, s2.top() + 1, 0);
        }
        if (ret1 > ret2)
            return 1;
        else if (ret1 < ret2)
            return -1;
        return 0;
        
    }
};

本人写的代码,不一定是最优解

相关推荐
小月球~5 小时前
天梯赛 · 并查集
数据结构·算法
仍然.5 小时前
算法题目---模拟
java·javascript·算法
6Hzlia6 小时前
【Hot 100 刷题计划】 LeetCode 118. 杨辉三角 | C++ 动态规划题解
c++·leetcode·动态规划
潇冉沐晴7 小时前
DP——背包DP
算法·背包dp
GIOTTO情8 小时前
2026 世界互联网大会亚太峰会|AI 时代媒介投放的技术实战与算法优化
人工智能·算法
逆境不可逃8 小时前
LeetCode 热题 100 之 543. 二叉树的直径 102. 二叉树的层序遍历 108. 将有序数组转换为二叉搜索树 98. 验证二叉搜索树
算法·leetcode·职场和发展
计算机安禾8 小时前
【数据结构与算法】第19篇:树与二叉树的基础概念
c语言·开发语言·数据结构·c++·算法·visual studio code·visual studio
副露のmagic9 小时前
哈希章节 leetcode 思路&实现
算法·leetcode·哈希算法
副露のmagic9 小时前
字符串章节 leetcode 思路&实现
windows·python·leetcode
csuzhucong9 小时前
puzzle(1037)黑白、黑白棋局
算法