[C/C++] LeetCode在线编程心得分享(不断更新中... ...)

1 LeetCode介绍

  LeetCode是一个很好的免费在线编程平台,对于程序员提升本身的编程技巧和编程思惟有着很大的帮助。LeetCode为用户提供了众多的主流编程语言,好比,C++、Java、Python、C、C#以及JavaScript等。此外,它还为每道题的难易程度和成功率进行了准确的统计,而且能够显示用户提交程序的运行时间使用户能够了解本身程序的运行效率,从而加以改进。尤为是对于面临找工做的苦逼同窗们,各家大公司的笔试和面试常常会有各类各样的编程题,有许多都出自LeetCode的原题,因此天天坚持练习才是王道啊。git

  我也是以前由于要准备找实习,因此才开始了本身的LeetCode之旅,我也在时刻提醒本身,要坚持完成LeetCode上的全部题目。我以为编程的这东西不能一律而过,相反它须要咱们不断地积累和总结,尤为是一些编程的技巧问题。借助这篇博文,我把本身当时遇到困难的问题总结到这里,但愿能与你们分享和讨论。程序员

2 Compare version numbers(2015.06.20)

  问题描述:Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.You may assume that the version strings are non-empty and contain only digits and the “.” character.The “.” character does not represent a decimal point and is used to separate number sequences.For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.面试

  Here is an example of version numbers ordering:
  0.1 < 1.1 < 1.2 < 13.37编程

  注意:2.1<2.10而不是“=”;01.1=1.1编程语言

  代码以下:函数

// 运行结果0ms
int
compareVersion(string version1, string version2) { istringstream iss1(version1); // 要学会灵活使用istringstream istringstream iss2(version2); string token1, token2; while(!iss1.eof() || !iss2.eof()) { getline(iss1, token1, '.'); getline(iss2, token2, '.'); const char *p1=token1.c_str(); // string.c_str():将字符串string转换为对应的(char *)类型,后边的atoi函数的输入只能为(char *)类型;
                        //这两行代码彻底没有必要加,可是我使用的Code::Blocks编译环境,没有stoi()这一函数,因此只能借助atoi()函数.
const char *p2=token2.c_str(); if(atoi(p1) > atoi(p2)) return 1; if(atoi(p1) < atoi(p2)) return -1; token1 = token2 = "0"; } return 0; }
相关文章
相关标签/搜索