[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; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM