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

1 LeetCode介绍

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

  我也是之前因为要准备找实习,所以才开始了自己的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; }

 

posted @ 2015-06-20 00:39  Poll的笔记  阅读(3006)  评论(0编辑  收藏  举报