【LeetCode笔记】Compare Version Numbers

本文介绍了一个用于比较两个版本号的C++函数实现。通过使用`std::stringstream`和`getline()`来逐段解析版本号字符串,并将其转换为整数进行逐位比较。此外,文章还解释了`getline()`的工作原理及其返回值的含义。
    int compareVersion(string version1, string version2) {
        stringstream s1(version1), s2(version2);
        while(1){
            int i1= 0, i2 = 0;
            if(getline(s1, version1, '.'))   //if读取成功,将字符串convert为整型  
                i1 = stoi(version1);         //else读取失败,i1 i2还为0
            if(getline(s2, version2, '.')) 
                i2 = stoi(version2);   
            
            //如果s1 s2同时被读完,说明两字符串相等
            if(!s1 && !s2) //getline()返回值为false:本次读取失败,即上次读取已经extract完了
                return 0;  //getline()返回值为true:本次读取成功,但本次可能已经extract完了
            //不相等的字符串,会在这里给出判断
            if(i1 != i2)
                return i1 > i2 ? 1 : -1;               
        }         
    }

本题解决了对于getline()的几个疑点:

1. getline()作用

(1)
istream& getline (istream& is, string& str, char delim);
(2)
istream& getline (istream& is, string& str);
Get line from stream into string
Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, '\n', for (2)).
The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.
If the delimiter is found, it is extracted and discarded, i.e. it is not stored and the next input operation will begin after it.
Each extracted character is appended to the string as if its member push_back was called.

遇见delim就停止,并discard,下次extract从delim后开始


2.返回值

返回一个istream的引用,可隐式转换为bool型,所以可在while(),if()中使用。当ss被extract完后,ss值还是为true,但如果再次调用getline()去取,ss将为false。



### 关于 LeetCode 学习笔记及相关资源 对于希望深入学习并通过 LeetCode 提升编程能力的人来说,找到合适的笔记和资源是非常重要的。以下是关于 LeetCode 笔记本或学习笔记 PDF 的相关内容: #### 1. **LeetCode 题目与 Python 编程** Python 是一种广泛应用于算法练习的语言,许多开发者通过它来解决 LeetCode 上的各种问题[^1]。这些笔记通常会记录解题思路、代码实现以及可能的优化方向。 #### 2. **高效记录与整理编程学习笔记的方法** 为了更好地掌握 LeetCode 中的知识点,可以采用一些高效的笔记记录方式。这不仅有助于巩固所学内容,还能够方便后续查阅。例如: - 使用 Markdown 文件创建结构化的文档。 - 将每道题目分为几个部分:问题描述、解决方案分析、代码实现及复杂度评估[^2]。 #### 3. **获取 LeetCode 笔记本或 PDF 资源的方式** 虽然网络上存在大量免费的学习资料,但需要注意版权问题。如果想下载高质量的 LeetCode 学习笔记 PDF 或其他形式的资源,可以通过以下途径查找: - 访问 GitHub 平台,在其中搜索关键词如 “LeetCode solutions” 或者特定语言版本(比如 Python)的相关仓库。 - 加入技术社区或者论坛讨论区,与其他成员分享彼此制作的优质笔记文件。 - 利用搜索引擎输入精确查询词组尝试定位到公开发布的电子书链接地址[^3]。 ```python # 示例代码片段展示如何保存本地Markdown格式化后的数据作为PDF导出功能之一 from markdown import markdown import pdfkit def save_as_pdf(text, output_filename='output.pdf'): html_text = markdown(text) config = pdfkit.configuration(wkhtmltopdf="C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe") # 替换为你实际安装路径 options = { 'page-size': 'Letter', 'margin-top': '0.75in', 'margin-right': '0.75in', 'margin-bottom': '0.75in', 'margin-left': '0.75in' } with open(output_filename,'wb') as f: pdf_content=pdfkit.from_string(html_text,False,options=options,configuration=config) f.write(pdf_content) example_note=""" # 动态规划入门经典案例 Fibonacci Sequence (斐波那契数列) 给定正整数 n ,返回第n项Fibonacci数值... """ save_as_pdf(example_note,"fibonacci_example.pdf") ``` 上述脚本演示了一个简单的例子,说明怎样把一段文字转换成HTML再进一步加工成为PDF档存盘操作过程[^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值