Cover image for leetcode每日一题 P165 比较版本号

leetcode每日一题 P165 比较版本号

字数 272
阅读
访客

时间轴

时间轴

2025-09-23

init

stringstream 字符串分割 std::stoi

题目:

这题比较简单,不过还是注意下 stringstream 的用法吧

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
#include <sstream>#include <stdio.h>#include <string>#include <vector>using std::string;using std::stringstream;using std::vector;class Solution {public:  int compareVersion(string version1, string version2) {    int n, i;    int v1, v2;    int revision_number;    string revision_number_str;    vector<int> vec1, vec2;    stringstream ss(version1);    while (std::getline(ss, revision_number_str, '.')) {      try {        revision_number = std::stoi(revision_number_str);      } catch (std::exception e) {        printf("illegal input: %s\n", e.what());        exit(-1);      }      vec1.push_back(revision_number);    }    // 覆盖缓冲区内容    ss.str(version2);    // 重置状态位    ss.clear();    while (std::getline(ss, revision_number_str, '.')) {      try {        revision_number = std::stoi(revision_number_str);      } catch (std::exception e) {        printf("Illegal input: %s\n", e.what());        exit(-1);      }      vec2.push_back(revision_number);    }    n = vec1.size() > vec2.size() ? vec1.size() : vec2.size();    for (i = 0; i < n; i++) {      // 赋值      if (i < vec1.size())        v1 = vec1[i];      else        v1 = 0;      if (i < vec2.size())        v2 = vec2[i];      else        v2 = 0;      if (v1 < v2) {        return -1;      } else if (v1 > v2) {        return 1;      } else {        continue;      }    }    return 0;  }};
评论加载中…