From 5d3bd6f9d771fc6bf3b9bcf80c0b0529e87d391c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BF=97=E5=B3=B0?= <380238062@qq.com> Date: Sat, 24 Dec 2016 09:25:57 +0800 Subject: [PATCH 01/72] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/01/note.md | 48 +++++++++++++++++++++++++++++++++++++++++++++ leetcode/06/note.md | 48 +++++++++++++++++++++++++++++++++++++++++++++ leetcode/07/note.md | 48 +++++++++++++++++++++++++++++++++++++++++++++ leetcode/08/note.md | 48 +++++++++++++++++++++++++++++++++++++++++++++ leetcode/14/note.md | 48 +++++++++++++++++++++++++++++++++++++++++++++ leetcode/19/note.md | 48 +++++++++++++++++++++++++++++++++++++++++++++ leetcode/20/note.md | 48 +++++++++++++++++++++++++++++++++++++++++++++ leetcode/21/note.md | 48 +++++++++++++++++++++++++++++++++++++++++++++ leetcode/24/note.md | 48 +++++++++++++++++++++++++++++++++++++++++++++ leetcode/26/note.md | 48 +++++++++++++++++++++++++++++++++++++++++++++ leetcode/27/note.md | 48 +++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 528 insertions(+) create mode 100644 leetcode/01/note.md create mode 100644 leetcode/06/note.md create mode 100644 leetcode/07/note.md create mode 100644 leetcode/08/note.md create mode 100644 leetcode/14/note.md create mode 100644 leetcode/19/note.md create mode 100644 leetcode/20/note.md create mode 100644 leetcode/21/note.md create mode 100644 leetcode/24/note.md create mode 100644 leetcode/26/note.md create mode 100644 leetcode/27/note.md diff --git a/leetcode/01/note.md b/leetcode/01/note.md new file mode 100644 index 0000000..33fcd26 --- /dev/null +++ b/leetcode/01/note.md @@ -0,0 +1,48 @@ +## 链接 + + +https://leetcode.com/problems/two-sum/ + + +## 题目 + + + + + +## 释义 + + + + + + +## 补充描述 + + + + + + +## 代码 + + + + + + +```c++ + +//代码放在这个块里面,可以高亮关键字 + + + +``` + + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/06/note.md b/leetcode/06/note.md new file mode 100644 index 0000000..73540fe --- /dev/null +++ b/leetcode/06/note.md @@ -0,0 +1,48 @@ +## 链接 + + +https://leetcode.com/problems/zigzag-conversion/ + + +## 题目 + + + + + +## 释义 + + + + + + +## 补充描述 + + + + + + +## 代码 + + + + + + +```c++ + +//代码放在这个块里面,可以高亮关键字 + + + +``` + + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/07/note.md b/leetcode/07/note.md new file mode 100644 index 0000000..85c0664 --- /dev/null +++ b/leetcode/07/note.md @@ -0,0 +1,48 @@ +## 链接 + + +https://leetcode.com/problems/reverse-integer/ + + +## 题目 + + + + + +## 释义 + + + + + + +## 补充描述 + + + + + + +## 代码 + + + + + + +```c++ + +//代码放在这个块里面,可以高亮关键字 + + + +``` + + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/08/note.md b/leetcode/08/note.md new file mode 100644 index 0000000..806ad0d --- /dev/null +++ b/leetcode/08/note.md @@ -0,0 +1,48 @@ +## 链接 + + +https://leetcode.com/problems/string-to-integer-atoi/ + + +## 题目 + + + + + +## 释义 + + + + + + +## 补充描述 + + + + + + +## 代码 + + + + + + +```c++ + +//代码放在这个块里面,可以高亮关键字 + + + +``` + + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/14/note.md b/leetcode/14/note.md new file mode 100644 index 0000000..ae59167 --- /dev/null +++ b/leetcode/14/note.md @@ -0,0 +1,48 @@ +## 链接 + + +https://leetcode.com/problems/longest-common-prefix/ + + +## 题目 + + + + + +## 释义 + + + + + + +## 补充描述 + + + + + + +## 代码 + + + + + + +```c++ + +//代码放在这个块里面,可以高亮关键字 + + + +``` + + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/19/note.md b/leetcode/19/note.md new file mode 100644 index 0000000..7794d50 --- /dev/null +++ b/leetcode/19/note.md @@ -0,0 +1,48 @@ +## 链接 + + +https://leetcode.com/problems/remove-nth-node-from-end-of-list/ + + +## 题目 + + + + + +## 释义 + + + + + + +## 补充描述 + + + + + + +## 代码 + + + + + + +```c++ + +//代码放在这个块里面,可以高亮关键字 + + + +``` + + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/20/note.md b/leetcode/20/note.md new file mode 100644 index 0000000..f2ceedc --- /dev/null +++ b/leetcode/20/note.md @@ -0,0 +1,48 @@ +## 链接 + + +https://leetcode.com/problems/valid-parentheses/ + + +## 题目 + + + + + +## 释义 + + + + + + +## 补充描述 + + + + + + +## 代码 + + + + + + +```c++ + +//代码放在这个块里面,可以高亮关键字 + + + +``` + + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/21/note.md b/leetcode/21/note.md new file mode 100644 index 0000000..4f55fa9 --- /dev/null +++ b/leetcode/21/note.md @@ -0,0 +1,48 @@ +## 链接 + + +https://leetcode.com/problems/merge-two-sorted-lists/ + + +## 题目 + + + + + +## 释义 + + + + + + +## 补充描述 + + + + + + +## 代码 + + + + + + +```c++ + +//代码放在这个块里面,可以高亮关键字 + + + +``` + + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/24/note.md b/leetcode/24/note.md new file mode 100644 index 0000000..635c42a --- /dev/null +++ b/leetcode/24/note.md @@ -0,0 +1,48 @@ +## 链接 + + +https://leetcode.com/problems/swap-nodes-in-pairs/ + + +## 题目 + + + + + +## 释义 + + + + + + +## 补充描述 + + + + + + +## 代码 + + + + + + +```c++ + +//代码放在这个块里面,可以高亮关键字 + + + +``` + + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/26/note.md b/leetcode/26/note.md new file mode 100644 index 0000000..bb1c511 --- /dev/null +++ b/leetcode/26/note.md @@ -0,0 +1,48 @@ +## 链接 + + +https://leetcode.com/problems/remove-duplicates-from-sorted-array/ + + +## 题目 + + + + + +## 释义 + + + + + + +## 补充描述 + + + + + + +## 代码 + + + + + + +```c++ + +//代码放在这个块里面,可以高亮关键字 + + + +``` + + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/27/note.md b/leetcode/27/note.md new file mode 100644 index 0000000..b890ed2 --- /dev/null +++ b/leetcode/27/note.md @@ -0,0 +1,48 @@ +## 链接 + + +https://leetcode.com/problems/remove-element/ + + +## 题目 + + + + + +## 释义 + + + + + + +## 补充描述 + + + + + + +## 代码 + + + + + + +```c++ + +//代码放在这个块里面,可以高亮关键字 + + + +``` + + + +## 更多 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file From 278067ed1a53437bd883cc871fb87767e739588e Mon Sep 17 00:00:00 2001 From: Fei Chong Date: Sat, 24 Dec 2016 23:08:11 +0800 Subject: [PATCH 02/72] Create note_ZgblKylin.md --- leetcode/01/note_ZgblKylin.md | 93 +++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 leetcode/01/note_ZgblKylin.md diff --git a/leetcode/01/note_ZgblKylin.md b/leetcode/01/note_ZgblKylin.md new file mode 100644 index 0000000..f4f1d03 --- /dev/null +++ b/leetcode/01/note_ZgblKylin.md @@ -0,0 +1,93 @@ +## 閾炬帴 + +https://leetcode.com/problems/two-sum/ + +## 棰樼洰 + +Given an array of integers, return **indices** of the two numbers such that they add up to a specific target. + +You may assume that each input would have **exactly** one solution. + +**Example:** + +``` +Given nums = [2, 7, 11, 15], target = 9, + +Because nums[0] + nums[1] = 2 + 7 = 9, +return [0, 1]. +``` + +**UPDATE (2016/2/13):** + The return format had been changed to **zero-based** indices. Please read the above updated description carefully. + + + +## 閲婁箟 + +杈撳叆涓涓暣褰㈡暟缁刵ums锛屽拰鐩爣鏁板瓧c锛岃姹傚湪nums涓壘鍒版弧瓒砤+b=c鐨勪袱涓暟锛屽苟杩斿洖瀹冧滑鐨勪笅鏍囥 + + + +## 琛ュ厖鎻忚堪 +绗竴鍙嶅簲鏄洿瑙傜殑鍐欎釜鍙岄噸寰幆锛屽灞俰浠0鍒皊ize锛屽唴灞俲浠巌+1鍒皊ize锛屾墽琛屾鏁版槸n*(n+1)/2娆★紝涔熷氨鏄疧(n^2)鈥︹﹀お鍙曚簡锛岃偗瀹氳繃涓嶄簡銆 + +閭d箞灏卞緱鎯冲姙娉曚紭鍖栦簡锛屼紭鍖栨晥鐜囩殑绗竴鍙嶅簲灏辨槸涓囨伓鐨**鎵撹〃娉**鈥︹ + +鍋囪鏁扮粍涓渶灏忕殑鏁版槸1锛屾渶澶х殑鏁版槸n锛岄偅涔堬紝鎴戜滑鏄笉鏄彲浠ュ仛涓涓暱搴︿负n鐨勬暟缁勶紝鍒濆涓篺alse锛岀敤杈撳叆鏁版嵁浣滀负涓嬫爣锛屽搴斾綅缃负true銆 + +鐒跺悗锛屾垜浠彧闇瑕侀亶鍘嗕竴杞緭鍏ユ暟鎹氨琛屼簡锛屽浜庢瘡涓猲um锛屾煡璇笅target - num瀵瑰簲鐨勫竷灏斿兼槸涓嶆槸true锛屾槸鐨勮瘽灏辨壘鍒颁簡銆 + +杩欐牱鐨勮瘽锛宐鐨勫鎵句笉闇瑕侀亶鍘嗭紝鍙渶瑕佷竴娆$储寮曟煡璇紝鏄父閲忕骇鐨勶紝鏁翠綋绠楁硶鏃堕棿寮閿O(n)銆 + + + +鐒惰岃繖涔堝仛鐨勮瘽锛岃繖涓暟缁勫お澶т簡锛屾湁鍔炴硶浼樺寲涔堬紵 + +绛旀鏄敤hash锛屼篃灏辨槸unordered_map锛岃繖涓滀笢鍚屾牱鎻愪緵甯搁噺绾(涓鑸儏鍐)鐨勬煡璇€ + +鎵浠ヨВ鍐虫柟妗堝氨鍑烘潵浜嗭紝鍋氫竴涓猦ash锛宬ey鏄痭um锛寁alue鏄痠ndex銆傜劧鍚庨亶鍘嗕竴涓媙ums锛屽姣忎釜a锛屾眰瀹冨搴旂殑b锛屾煡璇笅b鍦ㄤ笉鍦╤ash閲岋紝鑻ュ瓨鍦ㄥ氨鎵惧埌浜嗐 + +鐒跺悗涓轰簡鎻愰珮鏁堢巼锛岃繕鍙互鎶奾ash鐨勫垵濮嬪寲鍚堝苟鍒版煡璇腑銆 + +鏈潵鏄繖鏍风殑锛 + +1. 閬嶅巻nums锛屽垵濮嬪寲hash +2. 閬嶅巻nums锛屾煡璇ash锛屽緱鍒扮瓟妗 + +鐜板湪鍙互鍐欐垚杩欐牱锛 + +- 閬嶅巻nums锛岄拡瀵规瘡涓涓猲um锛屾煡璇ash + - 鑻ュ瓨鍦ㄥ垯杩斿洖绛旀 + - 鑻ヤ笉瀛樺湪锛屽垯灏唍um鍜屽叾index鎻掑叆hash銆傚垯涓嬫閬嶅巻鍒板畠瀵瑰簲鐨刡鏃讹紝鑷劧灏辫兘鎵惧埌绛旀浜嗐 + +杩欐牱鐨勮瘽锛岄鍏堝彲浠ュ皯涓杞亶鍘嗭紝鍏舵hash鎴愬憳鏇村皯锛屾煡璇㈡晥鐜囨洿楂橈紝灏遍叡銆 + + + +## 浠g爜 + +```c++ +class Solution { +public: + vector twoSum(vector& nums, int target) { + unordered_map num_index; + vector ret; + for(int index=0;index Date: Sun, 25 Dec 2016 09:30:39 +0800 Subject: [PATCH 03/72] =?UTF-8?q?=E6=B7=BB=E5=8A=A0c++primer=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "primer/\351\224\231\350\257\257\350\241\250.md" | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 "primer/\351\224\231\350\257\257\350\241\250.md" diff --git "a/primer/\351\224\231\350\257\257\350\241\250.md" "b/primer/\351\224\231\350\257\257\350\241\250.md" new file mode 100644 index 0000000..164c0f0 --- /dev/null +++ "b/primer/\351\224\231\350\257\257\350\241\250.md" @@ -0,0 +1,13 @@ + + +## P330 + +表9.19 + +> q.pop() 返回queue的首元素或(),但不删除此元素 + +参考cppreference + +http://en.cppreference.com/w/cpp/container/queue/pop + +pop执行**删除首元素**的操作。 From c17292986eeaa6ddcecbecefeb0ec239d9e5c1ec Mon Sep 17 00:00:00 2001 From: leakey1905 Date: Sun, 25 Dec 2016 11:02:44 +0800 Subject: [PATCH 04/72] Update and rename note.md to note_leakey.md --- leetcode/01/note.md | 48 ------------------------------- leetcode/01/note_leakey.md | 58 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 48 deletions(-) delete mode 100644 leetcode/01/note.md create mode 100644 leetcode/01/note_leakey.md diff --git a/leetcode/01/note.md b/leetcode/01/note.md deleted file mode 100644 index 33fcd26..0000000 --- a/leetcode/01/note.md +++ /dev/null @@ -1,48 +0,0 @@ -## 链接 - - -https://leetcode.com/problems/two-sum/ - - -## 题目 - - - - - -## 释义 - - - - - - -## 补充描述 - - - - - - -## 代码 - - - - - - -```c++ - -//代码放在这个块里面,可以高亮关键字 - - - -``` - - - -## 更多 - -![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) - -PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/01/note_leakey.md b/leetcode/01/note_leakey.md new file mode 100644 index 0000000..77693fb --- /dev/null +++ b/leetcode/01/note_leakey.md @@ -0,0 +1,58 @@ +## 閾炬帴 + + +https://leetcode.com/problems/two-sum/ + + +## 棰樼洰 + + + + + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 +class Solution { +public: + vector twoSum(vector& nums, int target) { + int i = 0, n = nums.size(); + unordered_map idx; + while (i < n && idx.find(target - nums[i]) == idx.end()) { // find the other number and not same with the current number + idx[nums[i++]] = i; + } + return {idx[target - nums[i]] - 1, i}; + } +}; + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From 661d422a5d117450a896a0a1f9da089dbcaf1b60 Mon Sep 17 00:00:00 2001 From: acwa <1115424611@qq.com> Date: Mon, 26 Dec 2016 17:56:11 +0800 Subject: [PATCH 05/72] Create note_acwa --- leetcode/01/note_acwa | 88 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 leetcode/01/note_acwa diff --git a/leetcode/01/note_acwa b/leetcode/01/note_acwa new file mode 100644 index 0000000..a6c76ba --- /dev/null +++ b/leetcode/01/note_acwa @@ -0,0 +1,88 @@ +## 閾炬帴 + + +https://leetcode.com/problems/two-sum/ + + +## 棰樼洰 + + + + + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +class Solution { +public: + vector twoSum(vector& nums, int target) { + vector v; + map loc; + map c;//璁板綍鏁板瓧鍑虹幇娆℃暟 + for(int i=nums.size()-1; i>=0; i--) + { + loc[nums[i]]=i; + c[nums[i]]++; + } + for(int i=0; i1) + { + v.push_back(i); + if(nums[i]*2!=target) + { + v.push_back(loc[target-nums[i]]); + } + else + { + int f=0; + for(int j=i+1; j Date: Mon, 26 Dec 2016 17:57:45 +0800 Subject: [PATCH 06/72] Rename note_acwa to note_acwa.md --- leetcode/01/{note_acwa => note_acwa.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename leetcode/01/{note_acwa => note_acwa.md} (100%) diff --git a/leetcode/01/note_acwa b/leetcode/01/note_acwa.md similarity index 100% rename from leetcode/01/note_acwa rename to leetcode/01/note_acwa.md From 8f3bf4d273de5ef63b66dbb13e319475ae4c9f10 Mon Sep 17 00:00:00 2001 From: leakey1905 Date: Mon, 26 Dec 2016 20:25:02 +0800 Subject: [PATCH 07/72] Update and rename note.md to note_leakey.md --- leetcode/07/note.md | 48 ------------------------------- leetcode/07/note_leakey.md | 58 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 48 deletions(-) delete mode 100644 leetcode/07/note.md create mode 100644 leetcode/07/note_leakey.md diff --git a/leetcode/07/note.md b/leetcode/07/note.md deleted file mode 100644 index 85c0664..0000000 --- a/leetcode/07/note.md +++ /dev/null @@ -1,48 +0,0 @@ -## 链接 - - -https://leetcode.com/problems/reverse-integer/ - - -## 题目 - - - - - -## 释义 - - - - - - -## 补充描述 - - - - - - -## 代码 - - - - - - -```c++ - -//代码放在这个块里面,可以高亮关键字 - - - -``` - - - -## 更多 - -![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) - -PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/07/note_leakey.md b/leetcode/07/note_leakey.md new file mode 100644 index 0000000..cbae5a6 --- /dev/null +++ b/leetcode/07/note_leakey.md @@ -0,0 +1,58 @@ +## 閾炬帴 + + +https://leetcode.com/problems/reverse-integer/ + + +## 棰樼洰 + + + + + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 +class Solution { +public: + int reverse(int x) { + long long y = 0; + while (x) { + y = y * 10 + x % 10; + x /= 10; + } + return y < INT_MIN || y > INT_MAX ? 0 : y; + } +}; + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From 34beabc028f89c4555bdf6eaa743f8dfe5bf0c8a Mon Sep 17 00:00:00 2001 From: leakey1905 Date: Mon, 26 Dec 2016 20:26:37 +0800 Subject: [PATCH 08/72] Update note.md --- leetcode/08/note.md | 80 +++++++++++++++++++++++++++------------------ 1 file changed, 48 insertions(+), 32 deletions(-) diff --git a/leetcode/08/note.md b/leetcode/08/note.md index 806ad0d..7607bbd 100644 --- a/leetcode/08/note.md +++ b/leetcode/08/note.md @@ -1,48 +1,64 @@ -## 链接 +## 閾炬帴 - -https://leetcode.com/problems/string-to-integer-atoi/ - - -## 题目 - - - - -## 释义 +https://leetcode.com/problems/string-to-integer-atoi/ + + +## 棰樼洰 + + + + + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 - - - - -## 补充描述 - - - - -## 代码 - - - - ```c++ -//代码放在这个块里面,可以高亮关键字 +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 +class Solution { +public: + int myAtoi(string s) { + int i = 0, sign, MAX = 0; + long long val = 0; + for (i = 0; isspace(s[i]); i++); //remove spaces from left + sign = (s[i] == '-' ? -1 : 1); + if (s[i] == '+' || s[i] == '-') + i++; + for (val = 0; isdigit(s[i]); i++){ + val = 10 * val + (s[i] - '0'); + if (val > INT_MAX) + return (sign == 1 ? INT_MAX : INT_MIN); + } + return val * sign; + } +}; + - - ``` - - -## 更多 + + +## 鏇村 ![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) -PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From db4282e588679f85356087e665e041f97a038005 Mon Sep 17 00:00:00 2001 From: leakey1905 Date: Mon, 26 Dec 2016 20:27:17 +0800 Subject: [PATCH 09/72] Rename note.md to note_leakey.md --- leetcode/08/{note.md => note_leakey.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename leetcode/08/{note.md => note_leakey.md} (100%) diff --git a/leetcode/08/note.md b/leetcode/08/note_leakey.md similarity index 100% rename from leetcode/08/note.md rename to leetcode/08/note_leakey.md From 344f86f5de00e548122838d240c47afa767526cc Mon Sep 17 00:00:00 2001 From: leakey1905 Date: Mon, 26 Dec 2016 20:31:30 +0800 Subject: [PATCH 10/72] Update and rename note.md to note_leakey.md --- leetcode/14/note.md | 48 ------------------------------- leetcode/14/note_leakey.md | 59 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 48 deletions(-) delete mode 100644 leetcode/14/note.md create mode 100644 leetcode/14/note_leakey.md diff --git a/leetcode/14/note.md b/leetcode/14/note.md deleted file mode 100644 index ae59167..0000000 --- a/leetcode/14/note.md +++ /dev/null @@ -1,48 +0,0 @@ -## 链接 - - -https://leetcode.com/problems/longest-common-prefix/ - - -## 题目 - - - - - -## 释义 - - - - - - -## 补充描述 - - - - - - -## 代码 - - - - - - -```c++ - -//代码放在这个块里面,可以高亮关键字 - - - -``` - - - -## 更多 - -![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) - -PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/14/note_leakey.md b/leetcode/14/note_leakey.md new file mode 100644 index 0000000..03ed1ab --- /dev/null +++ b/leetcode/14/note_leakey.md @@ -0,0 +1,59 @@ +## 閾炬帴 + + +https://leetcode.com/problems/longest-common-prefix/ + + +## 棰樼洰 + + + + + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 +class Solution { +public: + string longestCommonPrefix(vector& strs) { + if (strs.size() == 0) return string(); + int i, j; + for (i = 0; i < strs[0].length(); i++) + for (j = 1; j < strs.size(); j++) + if (strs[j][i] != strs[0][i]) + return strs[0].substr(0, i); + return strs[0].substr(0, i); + } +}; + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From 1d4a2cce8a6c4776353d959287798fdbf85d4619 Mon Sep 17 00:00:00 2001 From: leakey1905 Date: Mon, 26 Dec 2016 20:32:33 +0800 Subject: [PATCH 11/72] Update note.md --- leetcode/19/note.md | 79 +++++++++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 32 deletions(-) diff --git a/leetcode/19/note.md b/leetcode/19/note.md index 7794d50..71d9264 100644 --- a/leetcode/19/note.md +++ b/leetcode/19/note.md @@ -1,48 +1,63 @@ -## 链接 +## 閾炬帴 - -https://leetcode.com/problems/remove-nth-node-from-end-of-list/ - - -## 题目 - - - - -## 释义 +https://leetcode.com/problems/remove-nth-node-from-end-of-list/ + + +## 棰樼洰 + + + + + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 - - - - -## 补充描述 - - - - -## 代码 - - - - ```c++ -//代码放在这个块里面,可以高亮关键字 +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 +class Solution { +public: + ListNode* removeNthFromEnd(ListNode* head, int n) { + ListNode *fp = head, *sp = head; //use two pointers + for (int i = 0; i < n; i++) + fp = fp -> next; + if (fp == NULL) + return head -> next; + while (fp -> next != NULL){ + fp = fp -> next; + sp = sp -> next; + } + sp -> next = sp -> next -> next; + return head; + } +}; + - - ``` - - -## 更多 + + +## 鏇村 ![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) -PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From f6b2a687bdd14e8e56ebe704650f459339037e73 Mon Sep 17 00:00:00 2001 From: leakey1905 Date: Mon, 26 Dec 2016 20:32:56 +0800 Subject: [PATCH 12/72] Rename note.md to note_leakey.md --- leetcode/19/{note.md => note_leakey.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename leetcode/19/{note.md => note_leakey.md} (100%) diff --git a/leetcode/19/note.md b/leetcode/19/note_leakey.md similarity index 100% rename from leetcode/19/note.md rename to leetcode/19/note_leakey.md From d47fcc76bd7a314ad8598213f42add359a364ea2 Mon Sep 17 00:00:00 2001 From: leakey1905 Date: Mon, 26 Dec 2016 20:36:12 +0800 Subject: [PATCH 13/72] Update note.md --- leetcode/20/note.md | 72 +++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/leetcode/20/note.md b/leetcode/20/note.md index f2ceedc..1be6bc0 100644 --- a/leetcode/20/note.md +++ b/leetcode/20/note.md @@ -1,48 +1,56 @@ -## 链接 +## 閾炬帴 - -https://leetcode.com/problems/valid-parentheses/ - - -## 题目 - - - - -## 释义 +https://leetcode.com/problems/valid-parentheses/ + + +## 棰樼洰 + + + + + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 - - - - -## 补充描述 - - - - -## 代码 - - - - ```c++ -//代码放在这个块里面,可以高亮关键字 +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 +class Solution { +public: + bool isValid(string s) { + stack p; + for (char& c : s) + p.empty() ? p.push(c) : (c == p.top() + 2 || c == p.top() + 1 ? p.pop() : p.push(c)); + return p.empty(); + } +}; + - - ``` - - -## 更多 + + +## 鏇村 ![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) -PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From 359ed0d1468650e5fd4f3e99e803309d669740a4 Mon Sep 17 00:00:00 2001 From: leakey1905 Date: Mon, 26 Dec 2016 20:36:29 +0800 Subject: [PATCH 14/72] Rename note.md to note_leakey.md --- leetcode/20/{note.md => note_leakey.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename leetcode/20/{note.md => note_leakey.md} (100%) diff --git a/leetcode/20/note.md b/leetcode/20/note_leakey.md similarity index 100% rename from leetcode/20/note.md rename to leetcode/20/note_leakey.md From 1c0dc122d3db22d3b74f2fa82a97dc5d22ceddab Mon Sep 17 00:00:00 2001 From: leakey1905 Date: Mon, 26 Dec 2016 20:37:34 +0800 Subject: [PATCH 15/72] Update and rename note.md to note_leakey.md --- leetcode/21/note.md | 48 --------------------- leetcode/21/note_leakey.md | 86 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 48 deletions(-) delete mode 100644 leetcode/21/note.md create mode 100644 leetcode/21/note_leakey.md diff --git a/leetcode/21/note.md b/leetcode/21/note.md deleted file mode 100644 index 4f55fa9..0000000 --- a/leetcode/21/note.md +++ /dev/null @@ -1,48 +0,0 @@ -## 链接 - - -https://leetcode.com/problems/merge-two-sorted-lists/ - - -## 题目 - - - - - -## 释义 - - - - - - -## 补充描述 - - - - - - -## 代码 - - - - - - -```c++ - -//代码放在这个块里面,可以高亮关键字 - - - -``` - - - -## 更多 - -![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) - -PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/21/note_leakey.md b/leetcode/21/note_leakey.md new file mode 100644 index 0000000..a3617b0 --- /dev/null +++ b/leetcode/21/note_leakey.md @@ -0,0 +1,86 @@ +## 閾炬帴 + + +https://leetcode.com/problems/merge-two-sorted-lists/ + + +## 棰樼洰 + + + + + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 +class Solution { +public: + ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { + ListNode *p1 = l1, *p2 = l2, *p, *head; + if (p1 == NULL) + return l2; + else if (p2 == NULL) + return l1; + if (p1 -> val <= p2 -> val){ + head = p1; + p1 = p1 -> next; + }// first node same with smaller one + else{ + head = p2; + p2 = p2 -> next; + } + p = head; + while (p1 != NULL || p2 != NULL){ + if (p1 == NULL){ + p -> next = p2; + p2 = p2 -> next; + } // if l1 ends + else if (p2 == NULL){ + p -> next = p1; + p1 = p1 -> next; + } + else if (p1 -> val <= p2 -> val){ + p -> next = p1; + p1 = p1 -> next; + } + else{ + p -> next = p2; + p2 = p2 -> next; + } + p = p -> next; // p move to the next node + } + return head; + } +}; + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From c0fdf69bb55ce799aa6f60b4fb0fd117910bfbd7 Mon Sep 17 00:00:00 2001 From: leakey1905 Date: Mon, 26 Dec 2016 20:38:44 +0800 Subject: [PATCH 16/72] Update and rename note.md to note_leakey.md --- leetcode/24/note.md | 48 ------------------------ leetcode/24/note_leakey.md | 75 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 48 deletions(-) delete mode 100644 leetcode/24/note.md create mode 100644 leetcode/24/note_leakey.md diff --git a/leetcode/24/note.md b/leetcode/24/note.md deleted file mode 100644 index 635c42a..0000000 --- a/leetcode/24/note.md +++ /dev/null @@ -1,48 +0,0 @@ -## 链接 - - -https://leetcode.com/problems/swap-nodes-in-pairs/ - - -## 题目 - - - - - -## 释义 - - - - - - -## 补充描述 - - - - - - -## 代码 - - - - - - -```c++ - -//代码放在这个块里面,可以高亮关键字 - - - -``` - - - -## 更多 - -![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) - -PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/24/note_leakey.md b/leetcode/24/note_leakey.md new file mode 100644 index 0000000..cf96fa9 --- /dev/null +++ b/leetcode/24/note_leakey.md @@ -0,0 +1,75 @@ +## 閾炬帴 + + +https://leetcode.com/problems/swap-nodes-in-pairs/ + + +## 棰樼洰 + + + + + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 +class Solution { +public: + ListNode* swapPairs(ListNode* head) { + if (head == NULL || head -> next == NULL) + return head; + ListNode *p1 = head, *p2 = head, *p3 = head -> next, *p4 = head -> next -> next; + p2 -> next = p4; + p3 -> next = p2; + p1 = p3; + head = p1; //change first two nodes + while (p2 != NULL && p3 != NULL && p4 != NULL){ + if (p3 -> next -> next -> next != NULL){ + p1 = p1 -> next; + p2 = p2 -> next; + p3 = p3 -> next -> next -> next; + p4 = p4 -> next -> next; + p1 -> next = NULL; + p2 -> next = p4; + p3 -> next = p2; + p1 -> next = p3; + p1 = p1 -> next; + } + else + break; + } + return head; + } +}; + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From e025b881178d05e521df7d5e1d6261af2246b4ba Mon Sep 17 00:00:00 2001 From: leakey1905 Date: Mon, 26 Dec 2016 20:39:45 +0800 Subject: [PATCH 17/72] Update and rename note.md to note_leakey.md --- leetcode/26/note.md | 48 -------------------------------- leetcode/26/note_leakey.md | 57 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 48 deletions(-) delete mode 100644 leetcode/26/note.md create mode 100644 leetcode/26/note_leakey.md diff --git a/leetcode/26/note.md b/leetcode/26/note.md deleted file mode 100644 index bb1c511..0000000 --- a/leetcode/26/note.md +++ /dev/null @@ -1,48 +0,0 @@ -## 链接 - - -https://leetcode.com/problems/remove-duplicates-from-sorted-array/ - - -## 题目 - - - - - -## 释义 - - - - - - -## 补充描述 - - - - - - -## 代码 - - - - - - -```c++ - -//代码放在这个块里面,可以高亮关键字 - - - -``` - - - -## 更多 - -![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) - -PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/26/note_leakey.md b/leetcode/26/note_leakey.md new file mode 100644 index 0000000..349dda5 --- /dev/null +++ b/leetcode/26/note_leakey.md @@ -0,0 +1,57 @@ +## 閾炬帴 + + +https://leetcode.com/problems/remove-duplicates-from-sorted-array/ + + +## 棰樼洰 + + + + + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 +class Solution { +public: + int removeDuplicates(vector& nums) { + int i = 0, n = nums.size(); + for (int j = 1; j < n; j++) + if (nums[i] != nums[j]) + nums[++i] = nums[j]; + return n <= 1 ? n : i + 1; + } +}; + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From 85edfcd0be49068b74fb1318ec7debe5fbf00882 Mon Sep 17 00:00:00 2001 From: leakey1905 Date: Mon, 26 Dec 2016 20:40:46 +0800 Subject: [PATCH 18/72] Update and rename note.md to note_leakey.md --- leetcode/27/note.md | 48 ------------------------------- leetcode/27/note_leakey.md | 59 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 48 deletions(-) delete mode 100644 leetcode/27/note.md create mode 100644 leetcode/27/note_leakey.md diff --git a/leetcode/27/note.md b/leetcode/27/note.md deleted file mode 100644 index b890ed2..0000000 --- a/leetcode/27/note.md +++ /dev/null @@ -1,48 +0,0 @@ -## 链接 - - -https://leetcode.com/problems/remove-element/ - - -## 题目 - - - - - -## 释义 - - - - - - -## 补充描述 - - - - - - -## 代码 - - - - - - -```c++ - -//代码放在这个块里面,可以高亮关键字 - - - -``` - - - -## 更多 - -![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) - -PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/27/note_leakey.md b/leetcode/27/note_leakey.md new file mode 100644 index 0000000..bac5e7b --- /dev/null +++ b/leetcode/27/note_leakey.md @@ -0,0 +1,59 @@ +## 閾炬帴 + + +https://leetcode.com/problems/remove-element/ + + +## 棰樼洰 + + + + + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 +class Solution { +public: + int removeElement(vector& nums, int val) { + vector::iterator n; + for (n = nums.begin(); n != nums.end();) + if (*n == val) + n = nums.erase(n); // del the element and return to the next position of deleted element + else + n++; + return nums.size(); + } +}; + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From ea859f13de9303519c10c59c2c10aef2722bc308 Mon Sep 17 00:00:00 2001 From: bajdcc Date: Mon, 26 Dec 2016 22:41:34 +0800 Subject: [PATCH 19/72] Add note: two sum --- leetcode/01/note_bajdcc.md | 237 +++++++++++++++++++++++++++++++++++++ 1 file changed, 237 insertions(+) create mode 100644 leetcode/01/note_bajdcc.md diff --git a/leetcode/01/note_bajdcc.md b/leetcode/01/note_bajdcc.md new file mode 100644 index 0000000..183868d --- /dev/null +++ b/leetcode/01/note_bajdcc.md @@ -0,0 +1,237 @@ +## 閾炬帴 + + +https://leetcode.com/problems/two-sum/ + + +## 棰樼洰 + +缁欏畾涓涓暟缁刟rray鍜屼竴涓暟n锛屾眰array涓煇涓や釜鍏冪礌锛堝畠浠殑鍜屼负n锛夌殑涓嬫爣銆 + +鍋囧畾涓嬫爣鎬绘槸瀛樺湪銆 + +## 閲婁箟 + +### 鏂规硶涓锛氱┓涓 + +鏈绠鍗曠殑鏂规硶锛岀┓涓炬硶鎴栨毚鍔涚牬瑙f硶锛圔F锛夛紝涓ゅ眰for寰幆銆 + +鐢ㄤ吉浠g爜琛ㄧず锛 + +```c +for (i,index_i in array) { + for (j,index_j in array[index_i+1..]) { + if(i+j==n) return [i,j] + } +} +//not found! +``` + +绌蜂妇娉曠殑**缂虹偣鏄惊鐜鏁板お澶氾紝澶嶆潅搴︿负O(n^2)**锛屽お楂橈紝鎵浠ラ渶瑕佷紭鍖栫瓥鐣ャ + +**鎬濊冿細** + +绌蜂妇娉曠殑缂虹偣锛氭暟缁勬瘡澧炲姞涓涓厓绱狅紝灏遍渶瑕佸閬嶅巻n娆★紙n涓哄師鏉ユ暟缁勭殑闀垮害锛夈 + +鏁扮粍array涓篴rray=[a1,a2,a3...an]锛屽亣璁捐姹傚拰涓簁銆 + +绌蜂妇娉曠殑鎬濊矾鏄細 + +```c +for (i,index_i in array) { + if (k-i 鍦 array 涓笖涓嶅拰i閲嶅) return 涓嬫爣 +} +//not found! +``` + +杩欏氨杞彉涓**銆屽浣曞湪鏁扮粍涓煡鎵句竴涓壒瀹氬笺**鐨勯棶棰樸 + +瀵逛簬杩欎釜闂鐨勮В鍐虫柟妗堝氨鏄**鏁e垪琛**锛堟垨绉板搱甯岃〃锛夈 + +鏈绠鍗曠殑鎬濊矾锛**鎵撹〃娉**銆傝鏌ユ壘鐨勬暟瀛楄寖鍥存槸0-n锛岄偅涔堝彧瑕佹瀯寤簄涓哄ぇ灏忕殑鏁扮粍arr锛屽垽鏂璦rr[n]==1鍗冲彲銆傜己鐐癸細浠ョ┖闂存崲鏃堕棿锛岀┖闂村鏉傚害澶珮锛堝崰鐢ㄥ緢澶х殑绌洪棿锛夛紝闇瑕佹潈琛° + +鏉冭 缁撴灉锛堝嵆鎴戜娇鐢ㄧ殑鏂规硶锛=鎵撹〃娉+鏁e垪鏌ユ壘銆 + +### 鏂规硶浜岋細鏁e垪 + +鐢变簬绌洪棿鏈夐檺锛屾垜浠彧鑳藉皢int32鑼冨洿鍐呯殑鏁板瓧鏄犲皠鍒0-255涔嬮棿銆 + +鏈绠鍗曠殑鏂规硶锛氬彇妯¤繍绠椼俬ash=n mod 256銆 + +鎴戦噰鐢ㄧ殑鑷繁鐬庢幇鐨勪竴濂楁暎鍒楀嚱鏁帮紙浠呬緵濞变箰锛屽叾瀹炵敤鏈鐢ㄥ彇妯″氨瓒冲锛夛細 + +```c++ + inline int hash(int key) const { + int value = 0xabcdabcd; + key += 0x12345678; + int k = key; + while (key) { + value += key & 0x3; + value = (value >> 1) ^ 0xbadbadcc; + key >>= 2; + } + value ^= k; + value ^= 0xccbadbad; + value = (value & 0xff); + value ^= (value & 0xff00) >> 8; + value ^= (value & 0xff0000) >> 16; + value ^= (value & 0xff000000) >> 24; + for (int i = 0; i < k % 0xf; i++) { + value = (k % 0xff) - value; + value = (value & 0xff); + } + return value; + } +``` + +闂鏉ヤ簡锛氫袱涓暟鐨勬暎鍒楀间竴鏍锋庝箞鍔烇紵绠鍗曪紝鐢ㄩ摼琛ㄣ + +浣嗘槸鏈変汉璇翠簡锛氶摼琛ㄥお闀夸簡锛侀偅鍚屾牱绠鍗曪紝涓涓粨鐐逛繚瀛5涓煎憲锛岀敤瀹屽啀寤恒 + +鏈缁堝氨褰㈡垚浜嗕笅闈㈢殑绠楁硶銆 + +## 琛ュ厖鎻忚堪 + +鎻愪氦锛歨ttps://leetcode.com/submissions/detail/86803868/ + +鐢ㄦ椂锛9ms锛宐eat锛96%锛堜笉鐢╯td灏辨槸蹇級 + + +## 浠g爜 + + + + +```c++ +#include +#include + +using namespace std; + +static const int INVALID_NUM = INT32_MIN; + +struct Pair { + int key, value; +}; + +class Hash { + static const int SEED = 0xbadbadcc; + static const int CACHE_SIZE = 0x100; + static const int CACHE_MIN = -CACHE_SIZE/2; + static const int CACHE_MAX = CACHE_SIZE/2; + int cache[CACHE_SIZE]; + static const int HASH_BITS = 8; + static const int NODE_SIZE = 1 << HASH_BITS; + static const int NODE_CHUNK_SIZE = 5; + class Node { + Pair data[NODE_CHUNK_SIZE]; + int index; + struct Node *next; + public: + Node() { + index = 0; + next = nullptr; + } + + ~Node() { + if (next) + delete next; + } + + void add(int key, int value) { + if (index >= NODE_CHUNK_SIZE) { + if (!next) { + next = new Node(); + } + next->add(key, value); + } else { + data[index].key = key; + data[index].value = value; + index++; + } + } + + int get(int key) const { + for (int i = 0; i < index; i++) { + if (data[i].key != INVALID_NUM && data[i].key == key) { + return data[i].value; + } + } + if (next) + return next->get(key); + return INVALID_NUM; + } + }; + Node data[NODE_SIZE]; + +public: + Hash() { + for (int i = 0; i < CACHE_SIZE; i++) { + cache[i] = INVALID_NUM; + } + } + + inline int hash(int key) const { + int value = 0xabcdabcd; + key += 0x12345678; + int k = key; + while (key) { + value += key & 0x3; + value = (value >> 1) ^ 0xbadbadcc; + key >>= 2; + } + value ^= k; + value ^= 0xccbadbad; + value = (value & 0xff); + value ^= (value & 0xff00) >> 8; + value ^= (value & 0xff0000) >> 16; + value ^= (value & 0xff000000) >> 24; + for (int i = 0; i < k % 0xf; i++) { + value = (k % 0xff) - value; + value = (value & 0xff); + } + return value; + } + + int get(int key) const { + if (key >= -CACHE_MIN && key < CACHE_MAX) { + return cache[key + CACHE_MIN]; + } + return data[hash(key)].get(key); + } + + void add(int key, int value) { + if (key >= -CACHE_MIN && key < CACHE_MAX) { + cache[key + CACHE_MIN] = value; + } else { + data[hash(key)].add(key, value); + } + } +}; + +class Solution { +public: + vector twoSum(vector &nums, int target) { + Hash hash; + int i, index = 0, value; + for (i = 0; i < nums.size(); i++) { + if ((index = hash.get(target - (value = nums[i]))) != INVALID_NUM) { + return vector{index, i}; + } + hash.add(value, i); + } + if (hash.get(target - nums[i]) != INVALID_NUM) { + return vector{index, i}; + } + return vector{-1, -1}; + } +}; +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file From 2608c5a1aa76212334449153f5d8e6141efb6e53 Mon Sep 17 00:00:00 2001 From: bajdcc Date: Mon, 26 Dec 2016 23:38:12 +0800 Subject: [PATCH 20/72] Add medium-difficulty problems (After you completed easy problems) List: 3. Longest Substring Without Repeating Characters 5. Longest Palindromic Substring 12. Integer to Roman 15. 3Sum 22. Generate Parentheses --- leetcode/03/note.md | 74 +++++++++++++++++++++++++++++++++++++++++++++ leetcode/05/note.md | 68 +++++++++++++++++++++++++++++++++++++++++ leetcode/12/note.md | 51 +++++++++++++++++++++++++++++++ leetcode/15/note.md | 59 ++++++++++++++++++++++++++++++++++++ leetcode/22/note.md | 61 +++++++++++++++++++++++++++++++++++++ 5 files changed, 313 insertions(+) create mode 100644 leetcode/03/note.md create mode 100644 leetcode/05/note.md create mode 100644 leetcode/12/note.md create mode 100644 leetcode/15/note.md create mode 100644 leetcode/22/note.md diff --git a/leetcode/03/note.md b/leetcode/03/note.md new file mode 100644 index 0000000..24aa51e --- /dev/null +++ b/leetcode/03/note.md @@ -0,0 +1,74 @@ +## 閾炬帴 + +銆愪腑绛夐毦搴︺3. Longest Substring Without Repeating Characters 锛堟渶闀夸笉閲嶅瀛愪覆锛 + +https://leetcode.com/problems/longest-substring-without-repeating-characters/ + + +## 棰樼洰 + +Given a string聽**s**, find the longest palindromic substring in聽**s**. You may assume that the maximum length of聽**s**聽is 1000. + +**Example:** + +``` +Input: "babad" + +Output: "bab" + +Note: "aba" is also a valid answer. + +``` + +**Example:** + +``` +Given a string, find the length of the longest substring without repeating characters. + +Examples: + +Given "abcabcbb", the answer is "abc", which the length is 3. + +Given "bbbbb", the answer is "b", with the length of 1. + +Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring. +``` + +*Tags: Hash Table, Two Pointers* + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 + + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/05/note.md b/leetcode/05/note.md new file mode 100644 index 0000000..ef4d48a --- /dev/null +++ b/leetcode/05/note.md @@ -0,0 +1,68 @@ +## 閾炬帴 + +銆愪腑绛夐毦搴︺5. Longest Palindromic Substring 锛堟渶闀垮洖鏂囧瓙涓诧級 + +https://leetcode.com/problems/longest-palindromic-substring/ + + +## 棰樼洰 + +Given a string聽**s**, find the longest palindromic substring in聽**s**. You may assume that the maximum length of聽**s**聽is 1000. + +**Example:** + +``` +Input: "babad" + +Output: "bab" + +Note: "aba" is also a valid answer. + +``` + +**Example:** + +``` +Input: "cbbd" + +Output: "bb" +``` + + + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 + + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/12/note.md b/leetcode/12/note.md new file mode 100644 index 0000000..51fb2d2 --- /dev/null +++ b/leetcode/12/note.md @@ -0,0 +1,51 @@ +## 閾炬帴 + +銆愪腑绛夐毦搴︺12. Integer to Roman锛堟暟瀛楄浆缃楅┈鏂囷級 + +https://leetcode.com/problems/integer-to-roman/ + + +## 棰樼洰 + +Given an integer, convert it to a roman numeral. + +Input is guaranteed to be within the range from 1 to 3999. + +*Tags: Hash Table or Syntax-directed Translation(Advanced)* + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 + + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/15/note.md b/leetcode/15/note.md new file mode 100644 index 0000000..3dfc32e --- /dev/null +++ b/leetcode/15/note.md @@ -0,0 +1,59 @@ +## 閾炬帴 + +銆愪腑绛夐毦搴︺15. 3Sum + +https://leetcode.com/problems/3sum/ + + +## 棰樼洰 + +Given an array聽*S*聽of聽*n*聽integers, are there elements聽*a*,聽*b*,聽*c*聽in聽*S*聽such that聽*a*聽+聽*b*聽+聽*c*聽= 0? Find all unique triplets in the array which gives the sum of zero. + +**Note:**聽The solution set must not contain duplicate triplets. + +``` +For example, given array S = [-1, 0, 1, 2, -1, -4], + +A solution set is: +[ + [-1, 0, 1], + [-1, -1, 2] +] +``` + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 + + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/22/note.md b/leetcode/22/note.md new file mode 100644 index 0000000..d743f43 --- /dev/null +++ b/leetcode/22/note.md @@ -0,0 +1,61 @@ +## 閾炬帴 + +銆愪腑绛夐毦搴︺22. Generate Parentheses锛堢敓鎴愭嫭鍙凤級 + +https://leetcode.com/problems/generate-parentheses/ + + +## 棰樼洰 + +Given聽*n*聽pairs of parentheses, write a function to generate all combinations of well-formed parentheses. + +For example, given聽*n*聽= 3, a solution set is: + +``` +[ + "((()))", + "(()())", + "(())()", + "()(())", + "()()()" +] +``` + +*Tags: Backtracking* + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 + + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file From c86c3d7abdd75c63ac39e73b0bfaec614c87ee25 Mon Sep 17 00:00:00 2001 From: JosanSun <1030460698@qq.com> Date: Wed, 28 Dec 2016 09:38:09 +0800 Subject: [PATCH 21/72] upload my code, please check Unfamiliar with GitHub, still learning it! ^_^ --- leetcode/12/note.md | 51 ------------------------ leetcode/12/note_Josan.md | 84 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 51 deletions(-) delete mode 100644 leetcode/12/note.md create mode 100644 leetcode/12/note_Josan.md diff --git a/leetcode/12/note.md b/leetcode/12/note.md deleted file mode 100644 index 51fb2d2..0000000 --- a/leetcode/12/note.md +++ /dev/null @@ -1,51 +0,0 @@ -## 閾炬帴 - -銆愪腑绛夐毦搴︺12. Integer to Roman锛堟暟瀛楄浆缃楅┈鏂囷級 - -https://leetcode.com/problems/integer-to-roman/ - - -## 棰樼洰 - -Given an integer, convert it to a roman numeral. - -Input is guaranteed to be within the range from 1 to 3999. - -*Tags: Hash Table or Syntax-directed Translation(Advanced)* - -## 閲婁箟 - - - - - - -## 琛ュ厖鎻忚堪 - - - - - - -## 浠g爜 - - - - - - -```c++ - -//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 - - - -``` - - - -## 鏇村 - -![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) - -PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/12/note_Josan.md b/leetcode/12/note_Josan.md new file mode 100644 index 0000000..7b03dab --- /dev/null +++ b/leetcode/12/note_Josan.md @@ -0,0 +1,84 @@ +## 閾炬帴 + +銆愪腑绛夐毦搴︺12. Integer to Roman锛堟暟瀛楄浆缃楅┈鏂囷級 + +https://leetcode.com/problems/integer-to-roman/ + + +## 棰樼洰 + +Given an integer, convert it to a roman numeral. + +Input is guaranteed to be within the range from 1 to 3999. + +*Tags: Hash Table or Syntax-directed Translation(Advanced)* + +## 閲婁箟 + +棰樼洰寰堢洿鐧斤紝灏辨槸鏁板瓧杞綏椹暟瀛 +## 琛ュ厖鎻忚堪 + + +浣跨敤浜嗚緟鍔╂暟缁勮繘琛屾眰瑙c傚綋鐒惰繖涓緟鍔╂暟缁勫叿鏈変竴瀹氱殑鐗瑰緛锛屽彲浠ヤ娇鐢ㄥ嚱鏁拌繘琛屾眰瑙c傜劧鑰岋紝鎴戜滑涓鑸繕鏄敤绌洪棿鏉ユ崲鏃堕棿銆 +杩欓噷浣跨敤浜嗕竴涓壒寰侊紝灏辨槸789鈥斺斿搴旂綏椹暟瀛楃殑锛7銆愮櫨浣嶃+8銆愬崄浣嶃+9銆愪釜浣嶃戯級銆 + + + + +## 浠g爜 + + + + + + +```c++ + + +//杈呭姪鏁扮粍 +string str[4][10] = { + { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" }, + { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" }, + { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" }, + { "", "M", "MM", "MMM" } +}; + +class Solution +{ + public: + string intToRoman(int num) + { + if(num <= 0 || num > 3999) return string(); + + string res; + 聽 聽//杩欐牱鎻愬墠鍒嗛厤绌洪棿鍙互閫傚綋浼樺寲鏃堕棿 + res.reserve(200); + + stack st; + + int digits = 0;//digits琛ㄧずnum鐨勪綅鏁 + while(num) + { + st.push(num % 10); + num /= 10; + ++digits; + } + while(!st.empty()) + { + int temp = st.top(); + res += str[--digits][temp]; + st.pop(); + } + return res; + } +}; + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From 4b50b615a51c3a9dfa5f24748cdbd0a078c04f86 Mon Sep 17 00:00:00 2001 From: JosanSun <1030460698@qq.com> Date: Wed, 28 Dec 2016 15:28:51 +0800 Subject: [PATCH 22/72] Update my code for 001 9-12ms, CPP, Using Two-Pointers Tech --- leetcode/01/note_Josan.md | 106 ++++++++++++++++++++++++++++++++++ leetcode/01/note_ZgblKylin.md | 93 ----------------------------- 2 files changed, 106 insertions(+), 93 deletions(-) create mode 100644 leetcode/01/note_Josan.md delete mode 100644 leetcode/01/note_ZgblKylin.md diff --git a/leetcode/01/note_Josan.md b/leetcode/01/note_Josan.md new file mode 100644 index 0000000..01268bb --- /dev/null +++ b/leetcode/01/note_Josan.md @@ -0,0 +1,106 @@ +## 閾炬帴 + +https://leetcode.com/problems/two-sum/ + +## 棰樼洰 + +Given an array of integers, return **indices** of the two numbers such that they add up to a specific target. + +You may assume that each input would have **exactly** one solution. + +**Example:** + +``` +Given nums = [2, 7, 11, 15], target = 9, + +Because nums[0] + nums[1] = 2 + 7 = 9, +return [0, 1]. +``` + +**UPDATE (2016/2/13):** + The return format had been changed to **zero-based** indices. Please read the above updated description carefully. + + + +## 閲婁箟 + +杈撳叆涓涓暣褰㈡暟缁刵ums锛屽拰鐩爣鏁板瓧c锛岃姹傚湪nums涓壘鍒版弧瓒砤+b=c鐨勪袱涓暟锛屽苟杩斿洖瀹冧滑鐨勪笅鏍囥 + + + +## 琛ュ厖鎻忚堪 +鐪嬪埌濂藉鍐欐垚Hash鏍煎紡锛屼富瑕佺洰鐨勫氨鏄揩閫熸煡鎵俱傛垜杩欒竟鏉ヤ竴涓娇鐢ㄢ淭wo-pointers Technology鈥濇妧鏈殑瑙f硶銆 + +棣栧厛锛屽ぇ鑷寸殑鎬濇兂灏辨槸瀵规暟缁勮繘琛屾帓搴忥紝鐒跺悗璁剧疆涓や釜鎸囬拡锛屼竴涓寚鍚戝ご涓涓寚鍚戝熬锛屽墠鍚庨艰繎锛屽氨鑳藉垽鏂槸鍚︽壘鍒板搴旂殑鍊笺 + +浣嗘槸杩欎釜棰樼洰杩斿洖鍘熸潵鐨勫簭鍙凤紝鎵浠ヨ繘琛屾帓搴忕殑鏃跺欙紝鍘熸潵鐨勫簭鍙峰氨鍙戠敓鏀瑰彉锛屽洜姝ゆ垜寮曞叆浜嗕竴涓緟鍔╂暟缁剉ector锛岃繘琛屽瓨鏀惧搴旂殑搴忓彿銆 + + +## 浠g爜 + +```c++ +class Solution +{ + public: + vector twoSum(vector& nums, int target) + { + struct Node + { + int index; //鍘熸潵鐨勫簭鍙 + int num; //瀵瑰簲搴忓彿鐨勬暟鍊 + }; + vector n_vec; + n_vec.reserve(nums.size()); + + //鍒濆鍖杗_vec + for(int i = 0; i != nums.size(); ++i) + { + n_vec.push_back(Node{ i, *(nums.begin() + i) }); + } + + //灏唍_vec鎸塶um杩涜鎺掑簭 + sort(n_vec.begin(), n_vec.end(), [](Node lh, Node rh) { return lh.num < rh.num; }); + + + int i = 0, j = static_cast (nums.size() - 1); + while(i < j) + { + int temp = (n_vec.begin() + i)->num + (n_vec.begin() + j)->num; + if(temptarget) + { + --j; + } + else + { + break; + } + } + + + if(i < j) + { + int tem1 = (n_vec.begin() + i)->index, tem2 = (n_vec.begin() + j)->index; + if(tem1 < tem2) + { + return { tem1, tem2 }; + } + else + { + return { tem2, tem1 }; + } + + } + return vector(); + } +}; +``` +璺戜簡涓ゆ锛屼竴娆9ms;涓娆12ms 聽 聽閫熷害杩樺彲浠 +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ diff --git a/leetcode/01/note_ZgblKylin.md b/leetcode/01/note_ZgblKylin.md deleted file mode 100644 index f4f1d03..0000000 --- a/leetcode/01/note_ZgblKylin.md +++ /dev/null @@ -1,93 +0,0 @@ -## 閾炬帴 - -https://leetcode.com/problems/two-sum/ - -## 棰樼洰 - -Given an array of integers, return **indices** of the two numbers such that they add up to a specific target. - -You may assume that each input would have **exactly** one solution. - -**Example:** - -``` -Given nums = [2, 7, 11, 15], target = 9, - -Because nums[0] + nums[1] = 2 + 7 = 9, -return [0, 1]. -``` - -**UPDATE (2016/2/13):** - The return format had been changed to **zero-based** indices. Please read the above updated description carefully. - - - -## 閲婁箟 - -杈撳叆涓涓暣褰㈡暟缁刵ums锛屽拰鐩爣鏁板瓧c锛岃姹傚湪nums涓壘鍒版弧瓒砤+b=c鐨勪袱涓暟锛屽苟杩斿洖瀹冧滑鐨勪笅鏍囥 - - - -## 琛ュ厖鎻忚堪 -绗竴鍙嶅簲鏄洿瑙傜殑鍐欎釜鍙岄噸寰幆锛屽灞俰浠0鍒皊ize锛屽唴灞俲浠巌+1鍒皊ize锛屾墽琛屾鏁版槸n*(n+1)/2娆★紝涔熷氨鏄疧(n^2)鈥︹﹀お鍙曚簡锛岃偗瀹氳繃涓嶄簡銆 - -閭d箞灏卞緱鎯冲姙娉曚紭鍖栦簡锛屼紭鍖栨晥鐜囩殑绗竴鍙嶅簲灏辨槸涓囨伓鐨**鎵撹〃娉**鈥︹ - -鍋囪鏁扮粍涓渶灏忕殑鏁版槸1锛屾渶澶х殑鏁版槸n锛岄偅涔堬紝鎴戜滑鏄笉鏄彲浠ュ仛涓涓暱搴︿负n鐨勬暟缁勶紝鍒濆涓篺alse锛岀敤杈撳叆鏁版嵁浣滀负涓嬫爣锛屽搴斾綅缃负true銆 - -鐒跺悗锛屾垜浠彧闇瑕侀亶鍘嗕竴杞緭鍏ユ暟鎹氨琛屼簡锛屽浜庢瘡涓猲um锛屾煡璇笅target - num瀵瑰簲鐨勫竷灏斿兼槸涓嶆槸true锛屾槸鐨勮瘽灏辨壘鍒颁簡銆 - -杩欐牱鐨勮瘽锛宐鐨勫鎵句笉闇瑕侀亶鍘嗭紝鍙渶瑕佷竴娆$储寮曟煡璇紝鏄父閲忕骇鐨勶紝鏁翠綋绠楁硶鏃堕棿寮閿O(n)銆 - - - -鐒惰岃繖涔堝仛鐨勮瘽锛岃繖涓暟缁勫お澶т簡锛屾湁鍔炴硶浼樺寲涔堬紵 - -绛旀鏄敤hash锛屼篃灏辨槸unordered_map锛岃繖涓滀笢鍚屾牱鎻愪緵甯搁噺绾(涓鑸儏鍐)鐨勬煡璇€ - -鎵浠ヨВ鍐虫柟妗堝氨鍑烘潵浜嗭紝鍋氫竴涓猦ash锛宬ey鏄痭um锛寁alue鏄痠ndex銆傜劧鍚庨亶鍘嗕竴涓媙ums锛屽姣忎釜a锛屾眰瀹冨搴旂殑b锛屾煡璇笅b鍦ㄤ笉鍦╤ash閲岋紝鑻ュ瓨鍦ㄥ氨鎵惧埌浜嗐 - -鐒跺悗涓轰簡鎻愰珮鏁堢巼锛岃繕鍙互鎶奾ash鐨勫垵濮嬪寲鍚堝苟鍒版煡璇腑銆 - -鏈潵鏄繖鏍风殑锛 - -1. 閬嶅巻nums锛屽垵濮嬪寲hash -2. 閬嶅巻nums锛屾煡璇ash锛屽緱鍒扮瓟妗 - -鐜板湪鍙互鍐欐垚杩欐牱锛 - -- 閬嶅巻nums锛岄拡瀵规瘡涓涓猲um锛屾煡璇ash - - 鑻ュ瓨鍦ㄥ垯杩斿洖绛旀 - - 鑻ヤ笉瀛樺湪锛屽垯灏唍um鍜屽叾index鎻掑叆hash銆傚垯涓嬫閬嶅巻鍒板畠瀵瑰簲鐨刡鏃讹紝鑷劧灏辫兘鎵惧埌绛旀浜嗐 - -杩欐牱鐨勮瘽锛岄鍏堝彲浠ュ皯涓杞亶鍘嗭紝鍏舵hash鎴愬憳鏇村皯锛屾煡璇㈡晥鐜囨洿楂橈紝灏遍叡銆 - - - -## 浠g爜 - -```c++ -class Solution { -public: - vector twoSum(vector& nums, int target) { - unordered_map num_index; - vector ret; - for(int index=0;index Date: Wed, 28 Dec 2016 15:39:11 +0800 Subject: [PATCH 23/72] Upload my code A clean code. --- leetcode/07/note_Josan.md | 59 ++++++++++++++++++++++++++++++++++++++ leetcode/07/note_leakey.md | 58 ------------------------------------- 2 files changed, 59 insertions(+), 58 deletions(-) create mode 100644 leetcode/07/note_Josan.md delete mode 100644 leetcode/07/note_leakey.md diff --git a/leetcode/07/note_Josan.md b/leetcode/07/note_Josan.md new file mode 100644 index 0000000..a74b18b --- /dev/null +++ b/leetcode/07/note_Josan.md @@ -0,0 +1,59 @@ +## 閾炬帴 + + +https://leetcode.com/problems/reverse-integer/ + + +## 棰樼洰 + +Reverse digits of an integer. + +Example1: x = 123, return 321 +Example2: x = -123, return -321 + +```c++ +class Solution { +public: + int reverse(int x) { + + } +}; +``` + +## 閲婁箟 + +缈昏浆涓涓暣鏁帮紝濡傛灉鏄123锛岀粨鏋滃氨鏄321锛涘鏋滆緭鍏ユ槸-123锛岀粨鏋滃氨鏄-321銆 + +## 琛ュ厖鎻忚堪 + +涓昏鐨勬敞鎰忛」灏辨槸12345 67899锛岀炕杞箣鍚庡氨鏄99876 54321锛涗細鍙戠敓婧㈠嚭鐨勬儏鍐点 + + +## 浠g爜 + + + + + + +```c++ + +class Solution { +public: + int reverse(int x) { + long long r = 0; + while (x) r = r*10 + x%10, x /= 10; + return (int(r) == r) * r; +} +}; + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ diff --git a/leetcode/07/note_leakey.md b/leetcode/07/note_leakey.md deleted file mode 100644 index cbae5a6..0000000 --- a/leetcode/07/note_leakey.md +++ /dev/null @@ -1,58 +0,0 @@ -## 閾炬帴 - - -https://leetcode.com/problems/reverse-integer/ - - -## 棰樼洰 - - - - - -## 閲婁箟 - - - - - - -## 琛ュ厖鎻忚堪 - - - - - - -## 浠g爜 - - - - - - -```c++ - -//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 -class Solution { -public: - int reverse(int x) { - long long y = 0; - while (x) { - y = y * 10 + x % 10; - x /= 10; - } - return y < INT_MIN || y > INT_MAX ? 0 : y; - } -}; - - -``` - - - -## 鏇村 - -![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) - -PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From 9a8842b20b812be51f5dd558bc87d4a0aad7f016 Mon Sep 17 00:00:00 2001 From: JosanSun <1030460698@qq.com> Date: Wed, 28 Dec 2016 15:50:59 +0800 Subject: [PATCH 24/72] Upload my code for 006 This is my former code and my learning tips --- leetcode/06/note.md | 48 ---------- leetcode/06/note_Josan.md | 180 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+), 48 deletions(-) delete mode 100644 leetcode/06/note.md create mode 100644 leetcode/06/note_Josan.md diff --git a/leetcode/06/note.md b/leetcode/06/note.md deleted file mode 100644 index 73540fe..0000000 --- a/leetcode/06/note.md +++ /dev/null @@ -1,48 +0,0 @@ -## 链接 - - -https://leetcode.com/problems/zigzag-conversion/ - - -## 题目 - - - - - -## 释义 - - - - - - -## 补充描述 - - - - - - -## 代码 - - - - - - -```c++ - -//代码放在这个块里面,可以高亮关键字 - - - -``` - - - -## 更多 - -![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) - -PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/06/note_Josan.md b/leetcode/06/note_Josan.md new file mode 100644 index 0000000..37de60c --- /dev/null +++ b/leetcode/06/note_Josan.md @@ -0,0 +1,180 @@ +## 閾炬帴 + + +https://leetcode.com/problems/zigzag-conversion/ + + +## 棰樼洰 + +The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) + +P A H N +A P L S I I G +Y I R +And then read line by line: "PAHNAPLSIIGYIR" +Write the code that will take a string and make this conversion given a number of rows: + +string convert(string text, int nRows); +convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR". + +```c++ +class Solution { +public: + string convert(string s, int numRows) { + + } +}; +``` + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + +涓寮濮嬭涓洪鐩緢绠鍗曪紝浣嗘槸瀹為檯鎿嶄綔璧锋潵锛屽彂鐜拌嚜宸辩殑鎬濊矾瀹炵幇璧锋潵骞舵病鏈夐偅涔堢畝鍗曘備技涔庤繖绉嶆劅瑙夌粡甯镐細鍑虹幇銆傛墍浠ワ紝涓嶈兘鐪奸珮鎵嬩綆锛屼竴瀹氳瀹炵幇鍑烘潵锛岀粷涓嶈兘鎯冲綋鐒躲 +鎬濊矾锛 +棣栧厛锛屽皢zigzag鍥炬鐨勬瘡琛岀殑鍒楁暟绠楀嚭鏉ワ紱濡備笅锛 + +```c++ +P 聽 A 聽 H 聽 N 鈥︹︹︹︹︹︹︹︹︹︹︹︹︹︹︹ col[0]=4 +A P L S I I G 鈥︹︹︹︹︹︹︹︹︹︹︹︹︹︹︹ col[1]=7 +Y I R 鈥︹︹︹︹︹︹︹︹︹︹︹︹︹︹︹ col[2]=3 +``` + +鐒跺悗璁$畻缁撴灉res锛涗緷娆′粠姣忚寮濮嬮亶鍘嗭紝鐒跺悗瀵逛簬姣忚鐨勬瘡涓鍒楄繘琛岄亶鍘嗭紝杩愮敤鐩稿簲鐨勮鍒楀搴斿叕寮忓氨鍙互寰楀嚭缁撴灉銆 +鎬昏瘎锛氬搴斿叕寮忔瘮杈冨鏉傦紝鏈熼棿璋冭瘯寰堜箙銆傝繖鐪熸槸涓涓碂绯曠殑绠楁硶銆 + + +## 鑷繁鐨勬敹鑾 + +瀵硅竟鐣屾潯浠讹紙Boundary Conditions锛夊姞娣辩悊瑙 + + +鑷繁鍐欑殑绋嬪簭锛屼竴寮濮嬫病鏈夎冭檻杈圭晫鏉′欢銆傛绘槸锛岀洿鎺ユ彁浜わ紝鐒跺悗鍐嶇湅閫氫笉杩囩殑娴嬭瘯妗堜緥锛岀劧鍚庡啀淇敼銆傝櫧鐒舵渶鍚庝篃鑳介氳繃锛屼絾鏄湪CCF璁よ瘉鑰冭瘯锛屾禉澶х殑PAT鑰冭瘯锛屾壘宸ヤ綔鐨勬満璇曢兘鏄彧鑳芥彁浜や唬鐮佺殑銆傚伓灏旂殑绯荤粺杩樹細鍛婅瘔浣狅紝杩欎釜绋嬪簭鏈夋病鏈夊叏閮ㄩ氳繃娴嬭瘯妗堜緥锛屼絾鏄嚑涔庡叏涓嶄細鍍廘eetCode杩欎箞浜烘у寲杩樼粰浣犻敊璇殑妗堜緥銆傚洜姝わ紝蹇呴』瀵规鍔犱互閲嶈锛屽惁鍒欒鎵句笉鍒板伐浣滅殑鑺傚銆 +鎬荤粨涓涓嬶細杩欐鐘殑閿欒銆 +杈圭晫鏉′欢锛屼富瑕佸氨鏄杈撳叆鐨勬妸鎺с + + +濡傛灉绋嬪簭涓紝鏈夐櫎娉曡繍绠楋紝涓瀹氳璁板緱鑰冭檻鏄惁闄ゆ暟涓0鐨勬儏鍐点 + + +瀵逛簬string锛岃冭檻size()涓0鐨勬儏鍐点傚浜巌nt锛岃冭檻涓0 浠ュ強姝h礋鏁扮殑鎯呭喌銆 + + +鏈锛屾病鏈夎冭檻鍒拌礋鏁扮殑鎯呭喌锛岃鏄庢垜鐨勭▼搴忚繕鏄湁婕忔礊鐨勶紝闇瑕侀噸瑙嗐傚埆浜虹殑绋嬪簭灏辨湁鑰冭檻銆 + + +瀵逛簬string閮ㄥ垎鐨勫垵濮嬪寲銆乻ize浠ュ強capacity鐞嗚В鏇存繁銆 + +```C++ +string a(10); //no exist such initialization but vector a(10) is okay +string a(10,鈥橽0鈥) //but you can do like this +瀵逛簬涓涓猻tring a;闇瑕佹椂鍒昏冭檻浠栫殑size浠ュ強capacity銆 +eg: string a; + a.reserve(10); //PS: reserve()鐨勬晥鏋滄槸鍙涓嶅噺 + a[6]=鈥檟鈥; //灏辨槸浼氭姤閿欑殑锛宺eserve鍙槸鏀瑰彉浜哻apacity,鑰屼笉鏄痵ize +``` + + +## 浠g爜 + +鍒嗕韩涓涓繛鎴戣嚜宸遍兘瑙夊緱鍛曞績鐨勪唬鐮併 + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 + +class Solution +{ + public: + string convert(string s, int numRows) + { + if (s.size() == 0) + { + return string(); + } + else if (numRows == 1) + { + return string(s.begin(), s.end()); + } + + int sz = s.size(); + string res(sz,'\0'); + + vector col(numRows, 0); + + + + col[ 0 ] = ( sz - 1 ) / ( (numRows << 1) - 2 ) + 1; + + for (int i = 1; i < numRows - 1; ++i) + { + if (sz - ( ( numRows - 1 )*(( col[ 0 ] - 1 ) << 1) + 1 + i ) < 0) + { + col[ i ] = (col[ 0 ]-1) << 1; + } + else if (sz - ( ( numRows - 1 )*(( col[ 0 ] - 1 ) << 1) + (numRows << 1) - i - 1 ) < 0) + { + col[ i ] = (col[ 0 ] << 1) - 1; + } + else + { + col[ i ] = col[ 0 ]<< 1; + } + } + + //compute col[numRows-1] + if (sz - ( ( numRows - 1 )*(( col[ 0 ] - 1 ) << 1) + numRows ) < 0) + { + col[ numRows - 1 ] = col[ 0 ] - 1; + } + else + { + col[ numRows - 1 ] = col[ 0 ]; + } + + + for (int index = 0, i = 0; i < numRows&&index != sz; ++i) + { + if (i == 0 || i == numRows - 1) + { + for (int j = 0; j != col[ i ]; ++j) + { + + res[ index++ ] = s[ ( ( ( numRows - 1 )*j ) << 1 ) + i ]; + } + } + else + { + for (int j = 0; j != col[ i ]; ++j) + { + if (j % 2 == 0) + { + res[ index++ ] = s[ ( j >> 1 )*( ( numRows - 1 ) << 1 ) + i ]; + } + else + { + res[ index++ ] = s[ ( j >> 1 )*( ( numRows - 1 ) << 1 ) + ( numRows << 1 ) - i - 2 ]; + } + } + } + } + return res; + } +}; +``` + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From f2799d26768cdffa1bc84cf3eebecef46bbb9cdc Mon Sep 17 00:00:00 2001 From: JosanSun <1030460698@qq.com> Date: Wed, 28 Dec 2016 16:00:18 +0800 Subject: [PATCH 25/72] Upload my code for 008 This is my code and other's code. A pre-check trick is nice! --- leetcode/08/note_Josan.md | 131 +++++++++++++++++++++++++++++++++++++ leetcode/08/note_leakey.md | 64 ------------------ 2 files changed, 131 insertions(+), 64 deletions(-) create mode 100644 leetcode/08/note_Josan.md delete mode 100644 leetcode/08/note_leakey.md diff --git a/leetcode/08/note_Josan.md b/leetcode/08/note_Josan.md new file mode 100644 index 0000000..b8f60e8 --- /dev/null +++ b/leetcode/08/note_Josan.md @@ -0,0 +1,131 @@ +## 閾炬帴 + + +https://leetcode.com/problems/string-to-integer-atoi/ + + +## 棰樼洰 + +Implement atoi to convert a string to an integer. + +Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. + +Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front. + +Update (2015-02-10): +The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition. + +spoilers alert... click to show requirements for atoi. + +```C++ +class Solution { +public: + int myAtoi(string str) { + + } +}; +``` + +## 閲婁箟 + +灏嗗搴旂殑String杞崲鎴愮浉搴旂殑鏁存暟褰㈠紡 + +## 琛ュ厖鎻忚堪 + +string format: [whitespace] [sign] [0] [x] [digits/letters] +鍑℃槸涓嶇鍚堣繖涓牸寮忕殑string閮芥槸闈炴硶鐨 + +## 浠g爜 + +寰堢畝鍗曪紝灏辨槸渚濇杞瓨銆備絾鏄紝鎴戞祴璇曚笉閫氳繃浜4娆★紝鍙冭檻string涓虹┖鐨勬儏鍐碉紝鍏朵粬鎯呭喌閮芥病鏈夎冭檻銆傚彲瑙侊紝瀵逛簬绋嬪簭鐨勫仴澹ц繖鐐癸紝浠ュ強绋嬪簭娴嬭瘯杩欏潡锛屾垜鐨勬按骞冲緢宸紝闇瑕佺户缁笉鏂敞鎰忚缁冦 + + +```c++ + + +class Solution +{ + public: + int myAtoi(string str) + { + long long res = 0; + if(0 == str.size()) + { + return res; + } + auto i = str.begin(); + //remove the whitespace + while(i != str.end() && isspace(*i)) + { + ++i; + } + if(i == str.end()) + { + return res; + } + bool flag = true; + int sign = 1; + for(; i != str.end(); ++i) + { + if(flag&&*i == '-') + { + sign = -1; + flag = false; + continue; + } + else if(flag&&*i == '+') + { + flag = false; + continue; + } + if(*i >= '0'&&*i <= '9') + { + res = res * 10 + (*i - '0'); + } + else + { + break; + } + if(res > INT_MAX) + { + break; + } + } + res = sign*res; + if(res > INT_MAX) + { + res = INT_MAX; + } + else if(res < INT_MIN) + { + res = INT_MIN; + } + return static_cast(res); + } +}; + + + +``` + + +鍐嶅垎浜竴涓瘮杈冩帴杩戝畼鏂规枃浠剁殑浠g爜锛屽畼鏂规枃浠堕噰鐢ㄤ竴涓猼rick,灏辨槸pre-check鎶鏈紝涓嬮潰杩欎釜浠g爜灏辨槸鐢ㄤ簡杩欎釜鎶鏈 +```C++ +int myAtoi(string str) +{ + int ret = 0, sign = 1, i = str.find_first_not_of(' '), base = INT_MAX / 10; + if(str[i] == '+' || str[i] == '-') sign = str[i++] == '+' ?: -1; + while(isdigit(str[i])) + { + if(ret > base || (ret == base && str[i] - '0' > INT_MAX % 10)) return sign > 0 ? INT_MAX : INT_MIN; + ret = 10 * ret + (str[i++] - '0'); + } + return sign * ret; +} + +``` +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ diff --git a/leetcode/08/note_leakey.md b/leetcode/08/note_leakey.md deleted file mode 100644 index 7607bbd..0000000 --- a/leetcode/08/note_leakey.md +++ /dev/null @@ -1,64 +0,0 @@ -## 閾炬帴 - - -https://leetcode.com/problems/string-to-integer-atoi/ - - -## 棰樼洰 - - - - - -## 閲婁箟 - - - - - - -## 琛ュ厖鎻忚堪 - - - - - - -## 浠g爜 - - - - - - -```c++ - -//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 -class Solution { -public: - int myAtoi(string s) { - int i = 0, sign, MAX = 0; - long long val = 0; - for (i = 0; isspace(s[i]); i++); //remove spaces from left - sign = (s[i] == '-' ? -1 : 1); - if (s[i] == '+' || s[i] == '-') - i++; - for (val = 0; isdigit(s[i]); i++){ - val = 10 * val + (s[i] - '0'); - if (val > INT_MAX) - return (sign == 1 ? INT_MAX : INT_MIN); - } - return val * sign; - } -}; - - -``` - - - -## 鏇村 - -![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) - -PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From e649fb5965aa8ad34918864016bfcc247010327d Mon Sep 17 00:00:00 2001 From: JosanSun <1030460698@qq.com> Date: Wed, 28 Dec 2016 16:05:26 +0800 Subject: [PATCH 26/72] Upload my code for 013 This is my later code. --- leetcode/13/note_Josan.md | 78 +++++++++ leetcode/13/note_ZgblKylin.md | 289 ---------------------------------- 2 files changed, 78 insertions(+), 289 deletions(-) create mode 100644 leetcode/13/note_Josan.md delete mode 100644 leetcode/13/note_ZgblKylin.md diff --git a/leetcode/13/note_Josan.md b/leetcode/13/note_Josan.md new file mode 100644 index 0000000..fd6ca88 --- /dev/null +++ b/leetcode/13/note_Josan.md @@ -0,0 +1,78 @@ +## 閾炬帴 + +https://leetcode.com/problems/roman-to-integer/ + +## 棰樼洰 + +灏嗙綏椹暟瀛楄浆鎹负鏁村舰锛岃緭鍏ヨ寖鍥存槸1-3999 + +## 閲婁箟 + +1-3999鐨勭綏椹暟锛岀敤鍒扮殑绗﹀彿鍜屽搴旂殑鍗佽繘鍒舵暟濡備笅锛 + +I 鈫 1 + +V 鈫 5 + +X 鈫 10 + +L 鈫 50 + +C 鈫 100 + +D 鈫 500 + +M 鈫 1000 + + + +## 琛ュ厖鎻忚堪 + + + + +## 浠g爜 + +鍒╃敤hash杩涜蹇熻鍙栵紝鐒跺悗浠庡ご鍒板熬鍥犳閬嶅巻锛屽鏋滅涓娆¢亶鍘嗙殑鏁帮紝姣旀帴涓嬫潵閬嶅巻鐨勬暟灏忥紝鍒欏噺鎺夌涓娆¢亶鍘嗙殑鏁板硷紝鍚﹀垯鍔犱笂銆傚浜庢渶鍚庝竴浣嶏紝鍒欐槸鐩存帴鍔犱笂鐩稿簲鐨勬暟鍊笺 + +```c++ +class Solution +{ + public: + int romanToInt(string s) + { + map rmaMap = { + { 'I', 1 }, + { 'V', 5 }, + { 'X', 10 }, + { 'L', 50 }, + { 'C', 100 }, + { 'D', 500 }, + { 'M', 1000 } + }; + int res = 0; + for(string::size_type i = 0; i != s.size(); ++i) + { + auto j = i + 1; + int val = rmaMap[s[i]]; + if(j != s.size() && val < rmaMap[s[j]]) + { + res -= val; + } + else + { + res += val; + } + } + return res; + } +}; + +``` + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ diff --git a/leetcode/13/note_ZgblKylin.md b/leetcode/13/note_ZgblKylin.md deleted file mode 100644 index 7d90fcf..0000000 --- a/leetcode/13/note_ZgblKylin.md +++ /dev/null @@ -1,289 +0,0 @@ -## 閾炬帴 - -https://leetcode.com/problems/roman-to-integer/ - -## 棰樼洰 - -灏嗙綏椹暟瀛楄浆鎹负鏁村舰锛岃緭鍏ヨ寖鍥存槸1-3999 - -## 閲婁箟 - -1-3999鐨勭綏椹暟锛岀敤鍒扮殑绗﹀彿鍜屽搴旂殑鍗佽繘鍒舵暟濡備笅锛 - -I 鈫 1 - -V 鈫 5 - -X 鈫 10 - -L 鈫 50 - -C 鈫 100 - -D 鈫 500 - -M 鈫 1000 - -涔﹀啓瑙勫垯澶ц嚧濡備笅锛屾寜鐓у崄杩涘埗鐨勬瘡涓浣嶆暟鏉ユ弿杩 - -- 鑻ヨ浣嶄负鍩哄噯鏁帮紙濡1銆10銆100锛夌殑1-3鍊嶏紝鍒欓氳繃杩炵画涔﹀啓琛ㄧず鐩稿姞锛屽III琛ㄧず3 -- 鑻ヤ负鍥涘嶏紝鍒欐敼涓轰功鍐欎竴娆★紝鍐嶅姞涔﹀啓5鍊嶇殑鏁帮紝琛ㄧず5-1锛屽4=5-1=IV -- 鑻ヤ负6-9鍊嶏紝鍒欏厛涔﹀啓5鐨勫嶆暟锛屽墿浣欐暟瀛楁寜鐓1-4鍊嶅湪鍚庢柟杩藉姞涔﹀啓銆傚8=5+3=VIII - -## 琛ュ厖鎻忚堪 - -杩欓鐨勮В娉曪紝鏈変袱绉嶏細 - -1. 鏌ヨ〃娉曪紝灏嗕笂杩颁竷涓綏椹暟绗﹀彿鎵撹〃锛岀劧鍚庝緷娆¢亶鍘嗭紝鏌ヨ〃杞崲涓烘暣褰紱 -2. switch-case娉曪紝鍏跺疄灏辨槸鎶婃煡琛ㄦ硶灞曞紑鎴愪唬鐮併 - -鎬ц兘涓婏紝涓よ呮湰璐ㄧ浉鍚岋紝鏌ヨ〃娉曟槸閫氳繃涓嬫爣杩涜鏁版嵁瀵诲潃銆俿witch-case鏄氳繃switch鍐呭杩涜浠g爜瀵诲潃銆 - - - -#### 鎬ц兘鍒嗘瀽 - -**姝ゆ鍙互鍏堜笉鐪嬶紝鍚庣画鐗堟湰浼樺寲涓湅涓嶆噦浜嗗啀鍊掑洖鏉ョ湅** - -浣嗛渶娉ㄦ剰鈥斺旂幇浠PU锛屽叿鏈夋祦姘寸嚎銆佸垎鏀娴嬨佷贡搴忔墽琛岀瓑楂樻ц兘娴佺▼锛屽叿浣撳師鐞嗗彲浠ュ垎鍒煡璇㈢浉鍏宠瘝姹囩殑缁村熀鐧剧锛屾垜浠湪甯歌浠g爜浼樺寲涓紝鍙渶瑕佹敞鎰忚繖涓ょ偣锛 - -1. 閬垮厤鏉′欢鍒ゆ柇锛岃В閲婅鍚庛 -2. 閬垮厤浠g爜璺宠浆锛岃В閲婅鍚庛 -3. 閬垮厤鍫嗘爤鍑哄叆锛屽璋冪敤鍑芥暟锛岃繘鍑簕}璇彞鍧楋紝杩欎釜涓轰粈涔堜細鏈夋ц兘鎹熻楀簲璇ヤ笉鐢ㄨВ閲婁簡銆 - -鐜颁唬CPU鍦ㄦ墽琛屾寚浠よ繃绋嬩腑锛屼細閫氳繃娴佹按绾胯繘琛屻 - -鍦ㄦ墽琛屽綋鏉℃寚浠ゅ悓鏃讹紝灏变細鎶婂悗闈㈢殑鍑犳潯鎸囦护椤哄簭璇诲彇杩涘瘎瀛樺櫒锛屼互渚垮悗缁伐浣溿傛洿涓х梾鐨勬儏鍐典笅锛屼細鎶婂悗缁寚浠ょ殑鎿嶄綔鏁颁篃鎻愬墠璇诲彇杩涙潵鍑嗗鎵ц銆 - -濡傛灉鍓嶅悗涓ゆ潯鎸囦护鐨勬暟鎹病鏈夌浉浜掍緷璧栵紝閭d箞灏变細鍦ㄦ祦姘寸嚎閲屽苟琛屾墽琛岋紝涔熷氨鏄贡搴忔墽琛屻傝屾潯浠跺垽鏂垯浼氭墦鏂繖涓彁鍓嶈鍙栵紝鍥犱负鍦ㄥ垽鏂垚鍔熶箣鍓嶏紝鏃犳硶纭畾鍚庣画宸ヤ綔鍒嗘敮銆 - -涓嶈繃鐜颁唬CPU澶у鍏锋湁鍒嗘敮棰勬祴鎶鏈紝涔熷氨鏄鏋滃悓涓涓垎鏀娆℃墽琛岋紙濡俧or閲岄潰鐨刬f锛夛紝涔嬪墠鍑犳璧扮殑閮芥槸true锛岄偅涔堜笅涓娆″氨浼氭寜鐓rue鏉ユ彁鍓嶅鐞嗭紝濡傛灉瀹為檯涓篺alse鍒欏洖婊氾紝瀹為檯涓簍rue鍒欎細缁х画锛岃繖鏍峰氨閬垮厤浜嗘寚浠ら澶勭悊銆佹祦姘寸嚎绛夋搷浣滆鏉′欢璇彞鎵撴柇銆 - -鑰屽湪瀹為檯鍦哄悎涓紝鍒嗘敮棰勬祴鎴愬姛鐜囬珮杈惧叓鎴愨斺斾篃灏辨槸璇达紝濡傛灉鎴戜滑鍦╢or閲岄潰鏈塱f锛岄偅涔堝彧瑕佽繖涓猧f璇彞缁濆ぇ閮ㄥ垎鎯呭喌涓嬬粨鏋滈兘鐩稿悓锛堝悓涓簍rue鎴栧悓涓篺alse锛夛紝CPU鍒嗘敮棰勬祴鎶鏈氨鍙互寰堝ぇ绋嬪害鐨勯伩鍏嶄簡鏉′欢璺宠浆甯︽潵鐨勬ц兘鎹熷け銆傝屽鏋滄垜浠殑if璇彞锛宼rue鍜宖alse鐨勬垚鍔熺巼宸紓骞朵笉鏄庢樉锛岄偅涔堣繖涓娆℃墽琛岀殑if锛屼細鏄捐憲鎷栨參鎵ц鏁堢巼锛屾湁鏃舵參涓婁竴涓暟閲忕骇閮戒笉濂囨 - -鑰屼唬鐮佽烦杞紝濡俫oto銆乻witch-case锛屼篃浼氬甫鏉ヤ竴瀹氱殑鎬ц兘鎹熷け锛屽師鍥犲氨鍜宨f鍒ゆ柇涔嬪悗鐨勮烦杞浉鍚屻 - -鎵╁睍闃呰锛歔CPU 鐨勫垎鏀爯娓櫒鏄庢ǎ宸ヤ綔鐨勶紵 - 鐭ヤ箮](https://www.zhihu.com/question/23973128) - - - -鎵浠ワ紝鍦ㄤ竴寮濮嬫彁鍒扮殑鏌ヨ〃娉曞拰switch-case娉曚腑锛屼竴鑸潵璇存煡琛ㄦ硶鎬ц兘浼氭洿楂橈紝鍥犱负涓嶉渶瑕佽繘琛屼唬鐮佽烦杞 - -閭d箞锛屽氨寮濮嬬紪绋嬪惂锛 - - - -#### 鐗堟湰涓 - -杩欎竴閬撻锛岃В棰樻濊矾涓婏紝鍙互鎵撹崏绋挎潵妯℃嫙璁$畻锛屾垜鐢↖II(3)銆両V(4)鍜孷I(6)鏉ヨ繘琛屾帹鐞嗐 - -- 浠庡彸寰宸︽暟锛屾渶鍙宠竟涓涓偗瀹氭槸鍒濆鍊笺 -- 寰鍓嶉愪竴鏁拌繃鍘伙紝鏈绠鍗曠殑灏辨槸渚濇鍔犱笂鍘伙紝濡侷II锛屽氨鏄1+1+1寰楀埌3锛屾病姣涚梾銆 -- 閬囧埌涓嶅悓鐨勬暟锛屽氨瑕佸垽鏂笅浜嗐備粠V鍒癐(IV)锛屼箣鍓嶄竴姝(V)鏄5锛屽悗涓姝(I)搴旇鍑忔帀銆備粠I鍒癡(VI)鍒欑浉鍙嶃 - -浜庢槸灏辨湁浜嗕笅闈㈢殑浠g爜锛 - -```c++ -class Solution { -public: - int romanToInt(string s) { - unordered_map romanMap = { { 'I', 1 }, - { 'V', 5 }, - { 'X', 10 }, - { 'L', 50 }, - { 'C', 100 }, - { 'D', 500 }, - { 'M', 1000 } }; - int num = romanMap.at(s.back()); - for(int i=s.length()-2;i>=0;--i) { - if(romanMap.at(s.at(i)) < romanMap.at(s.at(i+1))) - num -= romanMap.at(s.at(i)); - else - num += romanMap.at(s.at(i)); - } - return num; - } -}; -``` - -鍦ㄨ繖閲岋紝鎴戦氳繃unordered_map鍒涘缓浜嗕竴寮爃ash琛紝hash琛ㄧ殑濂藉鍦ㄤ簬鏌ヨ鎺ヨ繎浜庡父閲忕骇锛屾晥鐜囨瀬涓烘帴杩戞暟缁勪笅鏍囧鍧銆 - -leetcode閲岋紝浠g爜楠岃瘉鏃剁殑杈撳叆鏁版嵁閲忓ソ鍍忎笉澶э紝鎵浠ュ娆℃墽琛屼唬鐮佽楁椂娉㈠姩寰堝ぇ銆傛彁浜ゅ悗鐐光渕ore details鈥濆彲浠ョ湅鑰楁椂鍦ㄦ墍鏈夌瓟妗堥噷鐨勬帓琛屻傚鏁版儏鍐典細绋冲畾鍦ㄤ竴涓緝蹇殑鍊硷紝浣嗕篃鏈夎澶氭椂鍊欎細鎺変笅鍘汇 - -杩欎釜鐗堟湰锛屽娆℃墽琛屽悗锛屾瘮杈冪ǔ瀹氱殑蹇熸暟鍊硷紝鏄秴杩囦簡53.92%鐨勪汉銆 - - - -#### 鐗堟湰浜 - -鎴戜滑娉ㄦ剰鍒帮紝鍦ㄤ笂涓増鏈噷锛宍romanMap.at(s.at(i))`杩欐浠g爜鍑虹幇浜嗕笁娆°備篃灏辨槸锛屽嚭鐜颁簡鍏at瀵诲潃锛屼絾杩欏叚娆$殑鍊煎叾瀹炴槸瀹屽叏涓鏍风殑銆 - -浜庢槸锛屽彲浠ョ敤涓涓彉閲忔潵浠f浛杩欐浠g爜鈥斺擿int cur = romanMap.at(s.at(i));` - -鑰宍romanMap.at(s.at(i+1))`杩欐锛岀湅浼煎彧鍑虹幇浜嗕竴娆★紝浣嗚繖涓娆″叾瀹炴槸鏉ヨ嚜浜庝笂涓娆℃墽琛岀殑cur锛屾墍浠ヤ篃鍙互鎶婂畠浼樺寲鎺夛紝鍦ㄥ惊鐜粨灏句娇鐢ㄥ彉閲廯prev = cur`鏉ュ皢鍏剁紦瀛樸 - -浜庢槸鏈変簡涓嬮潰鐨勭増鏈細 - -```c++ -class Solution { -public: - int romanToInt(string s) { - unordered_map romanMap = { { 'I', 1 }, - { 'V', 5 }, - { 'X', 10 }, - { 'L', 50 }, - { 'C', 100 }, - { 'D', 500 }, - { 'M', 1000 } }; - int num = romanMap.at(s.back()); - int prev = num; - for(int i=s.length()-2;i>=0;--i) { - int cur = romanMap.at(s.at(i)); - if(cur < prev) - num -= cur; - else - num += cur; - prev = cur; - } - return num; - } -}; -``` - -杩愯閫氳繃浜嗭紝鏁堢巼鎵撹触浜55.88%瀵规墜銆 - -杩欓噷鎶奰romanMap.at(s.at(i+1))`鏇挎崲涓簆rev锛岀┒绔熸槸璧氭槸浜忔垜娌$粏缁嗘濊冭繃锛屼絾cur鍙橀噺鏄剧劧鏄禋鐨勶紝鐒惰屾晥鐜囨彁鍗囧苟涓嶆槑鏄撅紝涔熻prev鍙橀噺鐣ヤ簭浜嗕竴鐐癸紵 - - - -#### 鐗堟湰涓夛紙濂藉瀛愬彲浠ュ - -鐗堟湰浜屽凡缁忓彲浠ヤ綔涓烘瘮杈冨畬缇庣殑绛旀鎻愪氦浜嗭紝鍦ㄦ墽琛屾晥鐜囧拰浠g爜鏄撹鎬т笂鍋氬埌浜嗗钩琛° - -濡傛灉鏄**鍦ㄥ伐浣滅幆澧冮噷锛岀増鏈簩鏄渶瀹岀編鐨勶紒** - -鎺ヤ笅鏉ワ紝涓轰簡鍘嬫Θ鎬ц兘鐨勬瀬闄愶紙姣曠珶杩欏氨鏄痮j鐨勭洰鐨勶級锛屾垜浠彲浠ラ夋嫨鏀惧純浠g爜鍙鎬ф潵鎻愬崌鎬ц兘銆傝繖涓鐐瑰湪宸ヤ綔鐜涓瀬搴︿笉鎺ㄨ崘锛岄櫎闈炴槸搴曞眰鐨勬墽琛岃秴绾ч绻侊紝瀵规ц兘褰卞搷寰堟槑鏄剧殑鏍稿績浠g爜銆傞渶鐗㈣鈥斺**涓嶈杩囧害浼樺寲**銆 - -```c++ -class Solution { -public: - int romanToInt(string s) { - unordered_map romanMap = { { 'I', 1 }, - { 'V', 5 }, - { 'X', 10 }, - { 'L', 50 }, - { 'C', 100 }, - { 'D', 500 }, - { 'M', 1000 } }; - int num = romanMap.at(s.back()); - int prev = num; - for(int i=s.length()-2;i>=0;--i) { - int cur = romanMap.at(s.at(i)); - int curLargerThanPrev = cur >= prev; - num += (2 * curLargerThanPrev - 1) * cur; - prev = cur; - } - return num; - } -}; -``` - -鍦ㄨ繖閲岋紝鎴戞妸if璇彞寮勬病浜嗏︹ - -寰幆浣撳唴锛屽娆℃墽琛岀殑if璇彞鏄竾鎭朵箣婧愶紝瀵规ц兘鐨勫奖鍝嶉潪甯镐弗閲嶃傝繖棰樿繕濂斤紝if鍐呯殑浠g爜鍧楅噷锛屽氨涓琛屼唬鐮侊紝杩愮畻閲忓緢灏忋傚鏋滀唬鐮佸潡閲岀殑杩愮畻姣旇緝澶嶆潅鐨勮瘽锛宨f甯︽潵鐨勬ц兘鎹熷け灏卞緢涓ラ噸浜嗐 - -鎴戣繖閲岀敤浜嗕釜trick锛屾病鍘荤粏鏌ユ爣鍑嗏斺攂ool鍏跺疄鏄釜鍗曞瓧鑺傛暣褰紝瀹冪殑true涓鑸槸1锛宖alse涓鑸槸0銆傚叿浣撶殑寰楀幓鏌++瑙勮寖浜嗭紝鎴戞病鍏蜂綋楠岃瘉杩囷紝浣嗗湪甯歌缂栬瘧鍣ㄤ笂鏄繖鏍风殑銆 - -浜庢槸锛屾垜浠氨鑳芥妸杩欎釜if鍒ゆ柇鐨勫垽鏂潯浠讹紝淇濆瓨涓轰竴涓暣褰㈠笺傜劧鍚庢妸杩欎釜1/0鐨勬暣褰㈠硷紝鍜宨f-else鍐呯殑璁$畻鎻夊湪涓璧凤紝鍚堝苟鎴愪竴涓笢瑗裤 - -鍏蜂綋鎬濊矾鏄繖鏍风殑: - -```c++ -result = isTrue * ...true expression... - + (1 - isTrue) * ...false expression... -``` - -鍙傜収杩欎釜鍐欐硶锛屽悎骞朵竴涓嬪椤瑰紡锛屾彁鍙栦竴涓嬬郴鏁帮紝灏卞緱鍒颁簡涓婇潰鐨勪唬鐮併 - -杩欑増浠g爜锛屾晥鐜囨彁鍗囨病鎯宠薄涓槑鏄撅紝鎵ц浜嗗娆″悗锛岀ǔ瀹氶熷害鏄墦璐ヤ簡59.85%瀵规墜锛屽伓灏斿彲浠ヨ功鍒60%鐢氳嚦70%銆 - -涓嶈繃鍦ㄦ垜鐩墠宸ヤ綔鐨勯」鐩腑锛屾牳蹇冪畻娉曢噷鏈夎澶氬湴鏂圭敤鍒颁簡杩欐牱鐨則rick銆備负閬垮厤bool鈫抜nt鐨勬搷浣滀竾涓鏄疷ndefined behavior锛屾垜杩樼壓鐗蹭簡涓瀹氱殑鎬ц兘鎶婂畠鍐欐垚`int isTrue = (boolean expression)?1:0`锛屼絾鏁堢巼鎻愬崌渚濇棫寰堟槑鏄俱 - - - -#### 鏈缁堢増锛堝ソ瀛╁瓙涓嶈瀛 - -鍏跺疄锛屾垜澶у鏄按杩囧幓鐨勶紝澶у洓鍋氭瘯璁炬墠鍏堝鐨凲t鍋欸UI锛岀粨鏋滆繕鏄病瀛︿細锛岀瓟杈╂椂鐢≦t Designer鐢讳簡涓猽i鎴浘鍐欒繘璁烘枃鈥︹ - -杩樺ソ鎴戜滑澶у鐨勮蒋浠舵槸涓湡绛旇京鏃舵紨绀猴紝涔嬪悗灏卞彧闇瑕佽鏂囦簡銆備腑鏈熼偅浼氬効鎴戣繕鍙互鍊熷彛璇寸晫闈㈣繕娌″仛濂芥潵鎺ㄨ劚锛岀劧鍚庢垜鎶婄晫闈㈠師鍨嬪拰绾懡浠よ鐨勬牳蹇冨姛鑳戒唬鐮佸垎鍒紨绀轰簡涓嬶紝鑰佸笀寰堝ぇ搴︾殑璁╂垜杩囦簡鈥︹ - -姣曚笟宸ヤ綔鍚庯紝鎵嶅紑濮嬪ソ濂界殑瀛++鍜孮t锛屽苟涓斿洜涓轰竴寮濮嬪氨鍏ヤ簡Qt鍧戯紝瀵艰嚧鎴戝埌鐜板湪宸ヤ綔涓夊勾澶氫簡锛屽boost閮藉彧鏄竴鐭ュ崐瑙o紝STL涔熸槸浠庡崐骞村墠鎵嶅紑濮嬪ソ濂藉鐨勨︹ - -鑷充簬ACM銆丱J杩欎簺涓滆タ锛岄偅鏇存槸绂绘垜寰堣繙锛屾垜鐜板湪鏄敤鍙互鍋歍L鐨勫伐浣滅粡楠岋紝楂樺眿寤虹摯鐨勯嗘帹杩欎簺鐭ヨ瘑鈥︹ - -鑰岃繖涓鍩熼噷锛岀潃閲嶄簬绠楁硶锛屾墍浠++鍜岄潰鍚戝璞″叾瀹炵敤澶勪笉澶э紝鏍稿績杩樻槸C鐨勯偅涓濂椼 - -鑰屽綋鎴戠煡閬撲簡鎵撹〃娉曠殑瀛樺湪鏃讹紝绠鐩存槸姣佷笁瑙備簡锛岃寰楄繖瀹屽叏鏄綔寮娾︹︿絾鍙嶈繃鏉ヤ竴鎯筹紝宸ョ▼棰嗗煙涓嶅氨鏄弧婊$殑灏嗗氨鍜屽噾鍚堜箞锛屽湪鍙互鎺ュ彈鐨勭簿搴︽垨鍏朵粬鎹熷け鑼冨洿鍐咃紝鑳藉鎻愰珮N涓暟閲忕骇瀹屾垚宸ヤ綔鐨勬柟娉曪紝灏辨槸鏈瀹岀編鐨勬柟娉曪紝姣斿鎵撹〃娉曘 - - -鍍忚繖棰橈紝鏈鐩磋鐨勫叾瀹炲簲璇ユ槸switch-case锛屼絾鏈蹇殑鍗存槸鎵撹〃娉曘 - -浣嗘墦琛ㄦ硶鐨勬渶楂樺鐣岋紝鏄暟缁勶紝鑰屼笖鏄啓鍦ㄦ爤涓婄殑鏁扮粍锛岃屼笉鏄痭ew鍑烘潵鐨勫爢鏁扮粍锛堟垜閮戒笉鎻怉CM閲岋紝閫氳繃棰勭紪璇戞寚浠ゆ妸鏍堟墿瀹瑰埌鍑犲崄鍑犵櫨鍏嗙劧鍚庡叏绋嬪湪鏍堜笂鍐欑畻娉曠殑濂囨妧娣阀浜嗏︹︼級銆傝岃繖閲岀敤鐨勫彧涓嶈繃鏄痷nordered_map锛岃櫧鐒秇ash鐞嗚鏌ヨ鎬ц兘鏄疧(1)锛屼絾姣旇捣鐪熸O(1)鐨勬暟缁勪笅鏍囪闂紝杩樻槸鏈夐偅涔堢偣甯告暟涓婄殑宸窛鐨勩 - -浜庢槸锛屾湁浜嗘渶缁堢増鐨勪唬鐮侊紝涓у績鐥呯媯鐨勬墦琛ㄣ傚鏋滅湅涓嶆噦鐨勮瘽锛屽彲浠ョ湅涓涓婭銆乂銆乆銆丩銆丆銆丏銆丮鐨凙SCII鐮侊紝浣犲氨鎳備簡銆 - - - -## 浠g爜 - -```c++ -class Solution { -public: - int romanToInt(string s) { - int romanMap['X'+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 100, 500, 0, - 0, 0, 0, 1, 0, 0, 50, 1000, 0, 0, - 0, 0, 0, 0, 0, 0, 5, 0, 10 }; - int num = romanMap[s.back()]; - int prev = num; - for(int i=s.length()-2;i>=0;--i) { - int cur = romanMap[s.at(i)]; - int curLargerThanPrev = cur >= prev; - num += (2 * curLargerThanPrev - 1) * cur; - prev = cur; - } - return num; - } -}; -``` - -杩欎釜鐗堟湰锛屽钩鍧囨ц兘鍦60-80%锛屾渶楂樻墦璐ヤ簡97.73%鐨勫鎵嬨 - -娌℃瘺鐥咃紒 - - - -#### 鎵╁睍闃呰 - -鍙儨leetcode涓嶆敮鎸乺ust锛屼笉鐒舵垜瑙夊緱锛宺ust鐨刾attern match锛屼篃璁告槸鏈蹇殑銆 - -github涓婃湁涓璇█鎬ц兘鐨刡enchmark椤圭洰锛屽叾涓湁涓猼est case鏄疄鐜颁竴涓畝鏄撶殑js瑙i噴鍣ㄣ - -杩欎釜棰樼洰閲岋紝ruby绛夊浜庡瓧绗︿覆澶勭悊鏈変抚蹇冪梾鐙傜殑浼樺寲鐨勫姩鎬佽瑷锛屾ц兘鐢氳嚦瓒呰繃浜嗙敤switch-case鐨凜/C++鈥斺旇鐭ラ亾锛屽湪鍏朵粬棰樼洰閲岋紝C/C++鐩告瘮鍔ㄦ佽瑷閮芥湁鐫鏈灏戜竴涓暟閲忕骇鐨勭⒕鍘嬩紭鍔裤 - -鐒惰宺ust鐢ㄦ帴杩戜竴涓暟閲忕骇鐨勫樊璺濈⒕鍘嬩簡杩欎簺瀵规墜鈥︹︾敤鐨勫氨鏄痯attern match锛屼唬鐮佹瀬搴︾畝娲佷紭闆呫 - -鍙儨鎴戜竴鐩存病绌哄rust鈥︹ - - - -## 鏇村 - -![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) - -PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From 364f234652bd5160183bdc55531e98bcbe11431d Mon Sep 17 00:00:00 2001 From: JosanSun <1030460698@qq.com> Date: Wed, 28 Dec 2016 16:18:43 +0800 Subject: [PATCH 27/72] Upload my code for 015 This is my code --- leetcode/15/note.md | 59 ------------------ leetcode/15/note_Josan.md | 128 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 59 deletions(-) delete mode 100644 leetcode/15/note.md create mode 100644 leetcode/15/note_Josan.md diff --git a/leetcode/15/note.md b/leetcode/15/note.md deleted file mode 100644 index 3dfc32e..0000000 --- a/leetcode/15/note.md +++ /dev/null @@ -1,59 +0,0 @@ -## 閾炬帴 - -銆愪腑绛夐毦搴︺15. 3Sum - -https://leetcode.com/problems/3sum/ - - -## 棰樼洰 - -Given an array聽*S*聽of聽*n*聽integers, are there elements聽*a*,聽*b*,聽*c*聽in聽*S*聽such that聽*a*聽+聽*b*聽+聽*c*聽= 0? Find all unique triplets in the array which gives the sum of zero. - -**Note:**聽The solution set must not contain duplicate triplets. - -``` -For example, given array S = [-1, 0, 1, 2, -1, -4], - -A solution set is: -[ - [-1, 0, 1], - [-1, -1, 2] -] -``` - -## 閲婁箟 - - - - - - -## 琛ュ厖鎻忚堪 - - - - - - -## 浠g爜 - - - - - - -```c++ - -//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 - - - -``` - - - -## 鏇村 - -![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) - -PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/15/note_Josan.md b/leetcode/15/note_Josan.md new file mode 100644 index 0000000..2f70cb0 --- /dev/null +++ b/leetcode/15/note_Josan.md @@ -0,0 +1,128 @@ +## 閾炬帴 + +銆愪腑绛夐毦搴︺15. 3Sum + +https://leetcode.com/problems/3sum/ + + +## 棰樼洰 + +Given an array聽*S*聽of聽*n*聽integers, are there elements聽*a*,聽*b*,聽*c*聽in聽*S*聽such that聽*a*聽+聽*b*聽+聽*c*聽= 0? Find all unique triplets in the array which gives the sum of zero. + +**Note:**聽The solution set must not contain duplicate triplets. + +``` +For example, given array S = [-1, 0, 1, 2, -1, -4], + +A solution set is: +[ + [-1, 0, 1], + [-1, -1, 2] +] +``` + +## 閲婁箟 + +缁欏畾涓涓暟缁勶紝鎵惧嚭鎵鏈夌殑3鍏冪粍锛岃繖浜3鍏冪粍涔嬪拰涓0锛屽苟涓旀病鏈夐噸澶嶇殑鍏冪粍銆傝屼笖鎸夐『搴忚緭鍑 + +## 琛ュ厖鎻忚堪 + +涓寮濮嬫墦绠楃敤涓や釜鎸囬拡鎶鏈紝杩涜姹傝В銆備絾鏄亣鍒扳滀笅涓姝ユ庝箞璧扳滈棶棰樸 +鏈鍚庣敤浜嗕竴涓噸澶嶆煡璇㈢殑鏂规硶锛岃繘琛屾悳绱€ + + + + + +## 浠g爜 + + + + + + +```c++ + + +vector> threeSum(vector& nums) +{ + bool myComp(vector & a, vector & b); + sort(nums.begin(), nums.end()); + vector temp(3); + vector> res; + res.reserve(100); + auto size = nums.size(); + if(size >= 3) + { + decltype(size) st = 0; + decltype(size) ed = size - 1; + while((ed - st) >= 1 && nums[st] <= 0 && nums[ed] >= 0) + { + while((ed - st) >= 1 && nums[ed] >= 0) + { + int wt = -(nums[st] + nums[ed]); + auto mid = find(nums.begin() + st + 1, nums.begin() + ed, wt); + if(mid != nums.begin() + ed) + { + temp[0] = nums[st]; + temp[1] = *mid; + temp[2] = nums[ed]; + res.push_back(temp); + } + 聽 聽 聽 聽 聽 聽 聽 聽//瀵绘壘涓嬩竴涓笉閲嶅鐨勬暟鍊糿um[ed] + 聽 聽 聽 聽 聽 聽 聽 聽while((ed - st) >= 1 && nums[ed] == nums[ed - 1]) + { + --ed; + } + --ed; + } + 聽 聽 聽 聽 聽 聽//瀵绘壘涓嬩竴涓笉閲嶅鐨勬暟鍊糿um[st] + 聽 聽 聽 聽 聽 聽while((ed - st) >= 1 && nums[st] == nums[st + 1]) + { + ++st; + } + ++st; + 聽 聽 聽 聽 聽 聽//鏇存柊ed + 聽 聽 聽 聽 聽 聽ed = size - 1; + } + } + return res; +} + + + +``` + +鍒汉姣旇緝濂界殑绋嬪簭锛屽緢绠鐭備娇鐢ㄧ殑鏄疶wo-pointer Tech + +```c++ +vector> threeSum(vector& nums) +{ + vector> triples; + triples.reserve(200); + sort(nums.begin(), nums.end()); + int i = 0, last = nums.size() - 1; + while(i < last) + { + int a = nums[i], j = i + 1, k = last; + while(j < k) + { + int b = nums[j], c = nums[k], sum = a + b + c; + if(sum == 0) triples.push_back({ a, b, c }); + if(sum <= 0) while(nums[j] == b && j < k) j++; + if(sum >= 0) while(nums[k] == c && j < k) k--; + } + while(nums[i] == a && i < last) i++; + } + return triples; +} + + +``` + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From acb93b2eac7cce28d68213de17488776576466e5 Mon Sep 17 00:00:00 2001 From: cormen Date: Wed, 28 Dec 2016 16:50:01 +0800 Subject: [PATCH 28/72] add 01's note_cormen.md --- leetcode/01/note_cormen.md | 84 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 leetcode/01/note_cormen.md diff --git a/leetcode/01/note_cormen.md b/leetcode/01/note_cormen.md new file mode 100644 index 0000000..4da9a2c --- /dev/null +++ b/leetcode/01/note_cormen.md @@ -0,0 +1,84 @@ +## 閾炬帴 + + +https://leetcode.com/problems/two-sum/ + + +## 棰樼洰 + + +Given an array of integers, return **indices** of the two numbers such that they add up to a specific target. + +You may assume that each input would have **exactly** one solution. + +**Example:** + +``` +Given nums = [2, 7, 11, 15], target = 9, + +Because nums[0] + nums[1] = 2 + 7 = 9, +return [0, 1]. +``` + +**UPDATE (2016/2/13):** + The return format had been changed to **zero-based** indices. Please read the above updated description carefully. + + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 +class Solution { +public: + vector twoSum(vector& nums, int target) { + + vector result(2); + mapdic; + int len = nums.size(); + + for(int i = 0; i < len; i++) + { + if(dic.find(nums[i]) == dic.end()) + { + dic[target - nums[i]] = i; + } + else + { + result[0] = dic[nums[i]]; + result[1] = i; + } + } + return result; + } +}; + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From de1b150bbd5422f2e014b9a3d2b975ec9af819b8 Mon Sep 17 00:00:00 2001 From: bajdcc Date: Wed, 28 Dec 2016 20:38:17 +0800 Subject: [PATCH 29/72] Restore files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 鎭㈠琚噸鍛藉悕鍜屽垹闄ょ殑鏂囦欢 --- leetcode/01/note_ZgblKylin.md | 93 +++++++++++ leetcode/06/note.md | 48 ++++++ leetcode/07/note_leakey.md | 58 +++++++ leetcode/08/note_leakey.md | 64 ++++++++ leetcode/12/note.md | 51 ++++++ leetcode/13/note_ZgblKylin.md | 289 ++++++++++++++++++++++++++++++++++ leetcode/15/note.md | 59 +++++++ 7 files changed, 662 insertions(+) create mode 100644 leetcode/01/note_ZgblKylin.md create mode 100644 leetcode/06/note.md create mode 100644 leetcode/07/note_leakey.md create mode 100644 leetcode/08/note_leakey.md create mode 100644 leetcode/12/note.md create mode 100644 leetcode/13/note_ZgblKylin.md create mode 100644 leetcode/15/note.md diff --git a/leetcode/01/note_ZgblKylin.md b/leetcode/01/note_ZgblKylin.md new file mode 100644 index 0000000..f4f1d03 --- /dev/null +++ b/leetcode/01/note_ZgblKylin.md @@ -0,0 +1,93 @@ +## 閾炬帴 + +https://leetcode.com/problems/two-sum/ + +## 棰樼洰 + +Given an array of integers, return **indices** of the two numbers such that they add up to a specific target. + +You may assume that each input would have **exactly** one solution. + +**Example:** + +``` +Given nums = [2, 7, 11, 15], target = 9, + +Because nums[0] + nums[1] = 2 + 7 = 9, +return [0, 1]. +``` + +**UPDATE (2016/2/13):** + The return format had been changed to **zero-based** indices. Please read the above updated description carefully. + + + +## 閲婁箟 + +杈撳叆涓涓暣褰㈡暟缁刵ums锛屽拰鐩爣鏁板瓧c锛岃姹傚湪nums涓壘鍒版弧瓒砤+b=c鐨勪袱涓暟锛屽苟杩斿洖瀹冧滑鐨勪笅鏍囥 + + + +## 琛ュ厖鎻忚堪 +绗竴鍙嶅簲鏄洿瑙傜殑鍐欎釜鍙岄噸寰幆锛屽灞俰浠0鍒皊ize锛屽唴灞俲浠巌+1鍒皊ize锛屾墽琛屾鏁版槸n*(n+1)/2娆★紝涔熷氨鏄疧(n^2)鈥︹﹀お鍙曚簡锛岃偗瀹氳繃涓嶄簡銆 + +閭d箞灏卞緱鎯冲姙娉曚紭鍖栦簡锛屼紭鍖栨晥鐜囩殑绗竴鍙嶅簲灏辨槸涓囨伓鐨**鎵撹〃娉**鈥︹ + +鍋囪鏁扮粍涓渶灏忕殑鏁版槸1锛屾渶澶х殑鏁版槸n锛岄偅涔堬紝鎴戜滑鏄笉鏄彲浠ュ仛涓涓暱搴︿负n鐨勬暟缁勶紝鍒濆涓篺alse锛岀敤杈撳叆鏁版嵁浣滀负涓嬫爣锛屽搴斾綅缃负true銆 + +鐒跺悗锛屾垜浠彧闇瑕侀亶鍘嗕竴杞緭鍏ユ暟鎹氨琛屼簡锛屽浜庢瘡涓猲um锛屾煡璇笅target - num瀵瑰簲鐨勫竷灏斿兼槸涓嶆槸true锛屾槸鐨勮瘽灏辨壘鍒颁簡銆 + +杩欐牱鐨勮瘽锛宐鐨勫鎵句笉闇瑕侀亶鍘嗭紝鍙渶瑕佷竴娆$储寮曟煡璇紝鏄父閲忕骇鐨勶紝鏁翠綋绠楁硶鏃堕棿寮閿O(n)銆 + + + +鐒惰岃繖涔堝仛鐨勮瘽锛岃繖涓暟缁勫お澶т簡锛屾湁鍔炴硶浼樺寲涔堬紵 + +绛旀鏄敤hash锛屼篃灏辨槸unordered_map锛岃繖涓滀笢鍚屾牱鎻愪緵甯搁噺绾(涓鑸儏鍐)鐨勬煡璇€ + +鎵浠ヨВ鍐虫柟妗堝氨鍑烘潵浜嗭紝鍋氫竴涓猦ash锛宬ey鏄痭um锛寁alue鏄痠ndex銆傜劧鍚庨亶鍘嗕竴涓媙ums锛屽姣忎釜a锛屾眰瀹冨搴旂殑b锛屾煡璇笅b鍦ㄤ笉鍦╤ash閲岋紝鑻ュ瓨鍦ㄥ氨鎵惧埌浜嗐 + +鐒跺悗涓轰簡鎻愰珮鏁堢巼锛岃繕鍙互鎶奾ash鐨勫垵濮嬪寲鍚堝苟鍒版煡璇腑銆 + +鏈潵鏄繖鏍风殑锛 + +1. 閬嶅巻nums锛屽垵濮嬪寲hash +2. 閬嶅巻nums锛屾煡璇ash锛屽緱鍒扮瓟妗 + +鐜板湪鍙互鍐欐垚杩欐牱锛 + +- 閬嶅巻nums锛岄拡瀵规瘡涓涓猲um锛屾煡璇ash + - 鑻ュ瓨鍦ㄥ垯杩斿洖绛旀 + - 鑻ヤ笉瀛樺湪锛屽垯灏唍um鍜屽叾index鎻掑叆hash銆傚垯涓嬫閬嶅巻鍒板畠瀵瑰簲鐨刡鏃讹紝鑷劧灏辫兘鎵惧埌绛旀浜嗐 + +杩欐牱鐨勮瘽锛岄鍏堝彲浠ュ皯涓杞亶鍘嗭紝鍏舵hash鎴愬憳鏇村皯锛屾煡璇㈡晥鐜囨洿楂橈紝灏遍叡銆 + + + +## 浠g爜 + +```c++ +class Solution { +public: + vector twoSum(vector& nums, int target) { + unordered_map num_index; + vector ret; + for(int index=0;index INT_MAX ? 0 : y; + } +}; + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ diff --git a/leetcode/08/note_leakey.md b/leetcode/08/note_leakey.md new file mode 100644 index 0000000..4554f05 --- /dev/null +++ b/leetcode/08/note_leakey.md @@ -0,0 +1,64 @@ +## 閾炬帴 + + +https://leetcode.com/problems/string-to-integer-atoi/ + + +## 棰樼洰 + + + + + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 +class Solution { +public: + int myAtoi(string s) { + int i = 0, sign, MAX = 0; + long long val = 0; + for (i = 0; isspace(s[i]); i++); //remove spaces from left + sign = (s[i] == '-' ? -1 : 1); + if (s[i] == '+' || s[i] == '-') + i++; + for (val = 0; isdigit(s[i]); i++){ + val = 10 * val + (s[i] - '0'); + if (val > INT_MAX) + return (sign == 1 ? INT_MAX : INT_MIN); + } + return val * sign; + } +}; + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ diff --git a/leetcode/12/note.md b/leetcode/12/note.md new file mode 100644 index 0000000..51fb2d2 --- /dev/null +++ b/leetcode/12/note.md @@ -0,0 +1,51 @@ +## 閾炬帴 + +銆愪腑绛夐毦搴︺12. Integer to Roman锛堟暟瀛楄浆缃楅┈鏂囷級 + +https://leetcode.com/problems/integer-to-roman/ + + +## 棰樼洰 + +Given an integer, convert it to a roman numeral. + +Input is guaranteed to be within the range from 1 to 3999. + +*Tags: Hash Table or Syntax-directed Translation(Advanced)* + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 + + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/13/note_ZgblKylin.md b/leetcode/13/note_ZgblKylin.md new file mode 100644 index 0000000..7d90fcf --- /dev/null +++ b/leetcode/13/note_ZgblKylin.md @@ -0,0 +1,289 @@ +## 閾炬帴 + +https://leetcode.com/problems/roman-to-integer/ + +## 棰樼洰 + +灏嗙綏椹暟瀛楄浆鎹负鏁村舰锛岃緭鍏ヨ寖鍥存槸1-3999 + +## 閲婁箟 + +1-3999鐨勭綏椹暟锛岀敤鍒扮殑绗﹀彿鍜屽搴旂殑鍗佽繘鍒舵暟濡備笅锛 + +I 鈫 1 + +V 鈫 5 + +X 鈫 10 + +L 鈫 50 + +C 鈫 100 + +D 鈫 500 + +M 鈫 1000 + +涔﹀啓瑙勫垯澶ц嚧濡備笅锛屾寜鐓у崄杩涘埗鐨勬瘡涓浣嶆暟鏉ユ弿杩 + +- 鑻ヨ浣嶄负鍩哄噯鏁帮紙濡1銆10銆100锛夌殑1-3鍊嶏紝鍒欓氳繃杩炵画涔﹀啓琛ㄧず鐩稿姞锛屽III琛ㄧず3 +- 鑻ヤ负鍥涘嶏紝鍒欐敼涓轰功鍐欎竴娆★紝鍐嶅姞涔﹀啓5鍊嶇殑鏁帮紝琛ㄧず5-1锛屽4=5-1=IV +- 鑻ヤ负6-9鍊嶏紝鍒欏厛涔﹀啓5鐨勫嶆暟锛屽墿浣欐暟瀛楁寜鐓1-4鍊嶅湪鍚庢柟杩藉姞涔﹀啓銆傚8=5+3=VIII + +## 琛ュ厖鎻忚堪 + +杩欓鐨勮В娉曪紝鏈変袱绉嶏細 + +1. 鏌ヨ〃娉曪紝灏嗕笂杩颁竷涓綏椹暟绗﹀彿鎵撹〃锛岀劧鍚庝緷娆¢亶鍘嗭紝鏌ヨ〃杞崲涓烘暣褰紱 +2. switch-case娉曪紝鍏跺疄灏辨槸鎶婃煡琛ㄦ硶灞曞紑鎴愪唬鐮併 + +鎬ц兘涓婏紝涓よ呮湰璐ㄧ浉鍚岋紝鏌ヨ〃娉曟槸閫氳繃涓嬫爣杩涜鏁版嵁瀵诲潃銆俿witch-case鏄氳繃switch鍐呭杩涜浠g爜瀵诲潃銆 + + + +#### 鎬ц兘鍒嗘瀽 + +**姝ゆ鍙互鍏堜笉鐪嬶紝鍚庣画鐗堟湰浼樺寲涓湅涓嶆噦浜嗗啀鍊掑洖鏉ョ湅** + +浣嗛渶娉ㄦ剰鈥斺旂幇浠PU锛屽叿鏈夋祦姘寸嚎銆佸垎鏀娴嬨佷贡搴忔墽琛岀瓑楂樻ц兘娴佺▼锛屽叿浣撳師鐞嗗彲浠ュ垎鍒煡璇㈢浉鍏宠瘝姹囩殑缁村熀鐧剧锛屾垜浠湪甯歌浠g爜浼樺寲涓紝鍙渶瑕佹敞鎰忚繖涓ょ偣锛 + +1. 閬垮厤鏉′欢鍒ゆ柇锛岃В閲婅鍚庛 +2. 閬垮厤浠g爜璺宠浆锛岃В閲婅鍚庛 +3. 閬垮厤鍫嗘爤鍑哄叆锛屽璋冪敤鍑芥暟锛岃繘鍑簕}璇彞鍧楋紝杩欎釜涓轰粈涔堜細鏈夋ц兘鎹熻楀簲璇ヤ笉鐢ㄨВ閲婁簡銆 + +鐜颁唬CPU鍦ㄦ墽琛屾寚浠よ繃绋嬩腑锛屼細閫氳繃娴佹按绾胯繘琛屻 + +鍦ㄦ墽琛屽綋鏉℃寚浠ゅ悓鏃讹紝灏变細鎶婂悗闈㈢殑鍑犳潯鎸囦护椤哄簭璇诲彇杩涘瘎瀛樺櫒锛屼互渚垮悗缁伐浣溿傛洿涓х梾鐨勬儏鍐典笅锛屼細鎶婂悗缁寚浠ょ殑鎿嶄綔鏁颁篃鎻愬墠璇诲彇杩涙潵鍑嗗鎵ц銆 + +濡傛灉鍓嶅悗涓ゆ潯鎸囦护鐨勬暟鎹病鏈夌浉浜掍緷璧栵紝閭d箞灏变細鍦ㄦ祦姘寸嚎閲屽苟琛屾墽琛岋紝涔熷氨鏄贡搴忔墽琛屻傝屾潯浠跺垽鏂垯浼氭墦鏂繖涓彁鍓嶈鍙栵紝鍥犱负鍦ㄥ垽鏂垚鍔熶箣鍓嶏紝鏃犳硶纭畾鍚庣画宸ヤ綔鍒嗘敮銆 + +涓嶈繃鐜颁唬CPU澶у鍏锋湁鍒嗘敮棰勬祴鎶鏈紝涔熷氨鏄鏋滃悓涓涓垎鏀娆℃墽琛岋紙濡俧or閲岄潰鐨刬f锛夛紝涔嬪墠鍑犳璧扮殑閮芥槸true锛岄偅涔堜笅涓娆″氨浼氭寜鐓rue鏉ユ彁鍓嶅鐞嗭紝濡傛灉瀹為檯涓篺alse鍒欏洖婊氾紝瀹為檯涓簍rue鍒欎細缁х画锛岃繖鏍峰氨閬垮厤浜嗘寚浠ら澶勭悊銆佹祦姘寸嚎绛夋搷浣滆鏉′欢璇彞鎵撴柇銆 + +鑰屽湪瀹為檯鍦哄悎涓紝鍒嗘敮棰勬祴鎴愬姛鐜囬珮杈惧叓鎴愨斺斾篃灏辨槸璇达紝濡傛灉鎴戜滑鍦╢or閲岄潰鏈塱f锛岄偅涔堝彧瑕佽繖涓猧f璇彞缁濆ぇ閮ㄥ垎鎯呭喌涓嬬粨鏋滈兘鐩稿悓锛堝悓涓簍rue鎴栧悓涓篺alse锛夛紝CPU鍒嗘敮棰勬祴鎶鏈氨鍙互寰堝ぇ绋嬪害鐨勯伩鍏嶄簡鏉′欢璺宠浆甯︽潵鐨勬ц兘鎹熷け銆傝屽鏋滄垜浠殑if璇彞锛宼rue鍜宖alse鐨勬垚鍔熺巼宸紓骞朵笉鏄庢樉锛岄偅涔堣繖涓娆℃墽琛岀殑if锛屼細鏄捐憲鎷栨參鎵ц鏁堢巼锛屾湁鏃舵參涓婁竴涓暟閲忕骇閮戒笉濂囨 + +鑰屼唬鐮佽烦杞紝濡俫oto銆乻witch-case锛屼篃浼氬甫鏉ヤ竴瀹氱殑鎬ц兘鎹熷け锛屽師鍥犲氨鍜宨f鍒ゆ柇涔嬪悗鐨勮烦杞浉鍚屻 + +鎵╁睍闃呰锛歔CPU 鐨勫垎鏀爯娓櫒鏄庢ǎ宸ヤ綔鐨勶紵 - 鐭ヤ箮](https://www.zhihu.com/question/23973128) + + + +鎵浠ワ紝鍦ㄤ竴寮濮嬫彁鍒扮殑鏌ヨ〃娉曞拰switch-case娉曚腑锛屼竴鑸潵璇存煡琛ㄦ硶鎬ц兘浼氭洿楂橈紝鍥犱负涓嶉渶瑕佽繘琛屼唬鐮佽烦杞 + +閭d箞锛屽氨寮濮嬬紪绋嬪惂锛 + + + +#### 鐗堟湰涓 + +杩欎竴閬撻锛岃В棰樻濊矾涓婏紝鍙互鎵撹崏绋挎潵妯℃嫙璁$畻锛屾垜鐢↖II(3)銆両V(4)鍜孷I(6)鏉ヨ繘琛屾帹鐞嗐 + +- 浠庡彸寰宸︽暟锛屾渶鍙宠竟涓涓偗瀹氭槸鍒濆鍊笺 +- 寰鍓嶉愪竴鏁拌繃鍘伙紝鏈绠鍗曠殑灏辨槸渚濇鍔犱笂鍘伙紝濡侷II锛屽氨鏄1+1+1寰楀埌3锛屾病姣涚梾銆 +- 閬囧埌涓嶅悓鐨勬暟锛屽氨瑕佸垽鏂笅浜嗐備粠V鍒癐(IV)锛屼箣鍓嶄竴姝(V)鏄5锛屽悗涓姝(I)搴旇鍑忔帀銆備粠I鍒癡(VI)鍒欑浉鍙嶃 + +浜庢槸灏辨湁浜嗕笅闈㈢殑浠g爜锛 + +```c++ +class Solution { +public: + int romanToInt(string s) { + unordered_map romanMap = { { 'I', 1 }, + { 'V', 5 }, + { 'X', 10 }, + { 'L', 50 }, + { 'C', 100 }, + { 'D', 500 }, + { 'M', 1000 } }; + int num = romanMap.at(s.back()); + for(int i=s.length()-2;i>=0;--i) { + if(romanMap.at(s.at(i)) < romanMap.at(s.at(i+1))) + num -= romanMap.at(s.at(i)); + else + num += romanMap.at(s.at(i)); + } + return num; + } +}; +``` + +鍦ㄨ繖閲岋紝鎴戦氳繃unordered_map鍒涘缓浜嗕竴寮爃ash琛紝hash琛ㄧ殑濂藉鍦ㄤ簬鏌ヨ鎺ヨ繎浜庡父閲忕骇锛屾晥鐜囨瀬涓烘帴杩戞暟缁勪笅鏍囧鍧銆 + +leetcode閲岋紝浠g爜楠岃瘉鏃剁殑杈撳叆鏁版嵁閲忓ソ鍍忎笉澶э紝鎵浠ュ娆℃墽琛屼唬鐮佽楁椂娉㈠姩寰堝ぇ銆傛彁浜ゅ悗鐐光渕ore details鈥濆彲浠ョ湅鑰楁椂鍦ㄦ墍鏈夌瓟妗堥噷鐨勬帓琛屻傚鏁版儏鍐典細绋冲畾鍦ㄤ竴涓緝蹇殑鍊硷紝浣嗕篃鏈夎澶氭椂鍊欎細鎺変笅鍘汇 + +杩欎釜鐗堟湰锛屽娆℃墽琛屽悗锛屾瘮杈冪ǔ瀹氱殑蹇熸暟鍊硷紝鏄秴杩囦簡53.92%鐨勪汉銆 + + + +#### 鐗堟湰浜 + +鎴戜滑娉ㄦ剰鍒帮紝鍦ㄤ笂涓増鏈噷锛宍romanMap.at(s.at(i))`杩欐浠g爜鍑虹幇浜嗕笁娆°備篃灏辨槸锛屽嚭鐜颁簡鍏at瀵诲潃锛屼絾杩欏叚娆$殑鍊煎叾瀹炴槸瀹屽叏涓鏍风殑銆 + +浜庢槸锛屽彲浠ョ敤涓涓彉閲忔潵浠f浛杩欐浠g爜鈥斺擿int cur = romanMap.at(s.at(i));` + +鑰宍romanMap.at(s.at(i+1))`杩欐锛岀湅浼煎彧鍑虹幇浜嗕竴娆★紝浣嗚繖涓娆″叾瀹炴槸鏉ヨ嚜浜庝笂涓娆℃墽琛岀殑cur锛屾墍浠ヤ篃鍙互鎶婂畠浼樺寲鎺夛紝鍦ㄥ惊鐜粨灏句娇鐢ㄥ彉閲廯prev = cur`鏉ュ皢鍏剁紦瀛樸 + +浜庢槸鏈変簡涓嬮潰鐨勭増鏈細 + +```c++ +class Solution { +public: + int romanToInt(string s) { + unordered_map romanMap = { { 'I', 1 }, + { 'V', 5 }, + { 'X', 10 }, + { 'L', 50 }, + { 'C', 100 }, + { 'D', 500 }, + { 'M', 1000 } }; + int num = romanMap.at(s.back()); + int prev = num; + for(int i=s.length()-2;i>=0;--i) { + int cur = romanMap.at(s.at(i)); + if(cur < prev) + num -= cur; + else + num += cur; + prev = cur; + } + return num; + } +}; +``` + +杩愯閫氳繃浜嗭紝鏁堢巼鎵撹触浜55.88%瀵规墜銆 + +杩欓噷鎶奰romanMap.at(s.at(i+1))`鏇挎崲涓簆rev锛岀┒绔熸槸璧氭槸浜忔垜娌$粏缁嗘濊冭繃锛屼絾cur鍙橀噺鏄剧劧鏄禋鐨勶紝鐒惰屾晥鐜囨彁鍗囧苟涓嶆槑鏄撅紝涔熻prev鍙橀噺鐣ヤ簭浜嗕竴鐐癸紵 + + + +#### 鐗堟湰涓夛紙濂藉瀛愬彲浠ュ + +鐗堟湰浜屽凡缁忓彲浠ヤ綔涓烘瘮杈冨畬缇庣殑绛旀鎻愪氦浜嗭紝鍦ㄦ墽琛屾晥鐜囧拰浠g爜鏄撹鎬т笂鍋氬埌浜嗗钩琛° + +濡傛灉鏄**鍦ㄥ伐浣滅幆澧冮噷锛岀増鏈簩鏄渶瀹岀編鐨勶紒** + +鎺ヤ笅鏉ワ紝涓轰簡鍘嬫Θ鎬ц兘鐨勬瀬闄愶紙姣曠珶杩欏氨鏄痮j鐨勭洰鐨勶級锛屾垜浠彲浠ラ夋嫨鏀惧純浠g爜鍙鎬ф潵鎻愬崌鎬ц兘銆傝繖涓鐐瑰湪宸ヤ綔鐜涓瀬搴︿笉鎺ㄨ崘锛岄櫎闈炴槸搴曞眰鐨勬墽琛岃秴绾ч绻侊紝瀵规ц兘褰卞搷寰堟槑鏄剧殑鏍稿績浠g爜銆傞渶鐗㈣鈥斺**涓嶈杩囧害浼樺寲**銆 + +```c++ +class Solution { +public: + int romanToInt(string s) { + unordered_map romanMap = { { 'I', 1 }, + { 'V', 5 }, + { 'X', 10 }, + { 'L', 50 }, + { 'C', 100 }, + { 'D', 500 }, + { 'M', 1000 } }; + int num = romanMap.at(s.back()); + int prev = num; + for(int i=s.length()-2;i>=0;--i) { + int cur = romanMap.at(s.at(i)); + int curLargerThanPrev = cur >= prev; + num += (2 * curLargerThanPrev - 1) * cur; + prev = cur; + } + return num; + } +}; +``` + +鍦ㄨ繖閲岋紝鎴戞妸if璇彞寮勬病浜嗏︹ + +寰幆浣撳唴锛屽娆℃墽琛岀殑if璇彞鏄竾鎭朵箣婧愶紝瀵规ц兘鐨勫奖鍝嶉潪甯镐弗閲嶃傝繖棰樿繕濂斤紝if鍐呯殑浠g爜鍧楅噷锛屽氨涓琛屼唬鐮侊紝杩愮畻閲忓緢灏忋傚鏋滀唬鐮佸潡閲岀殑杩愮畻姣旇緝澶嶆潅鐨勮瘽锛宨f甯︽潵鐨勬ц兘鎹熷け灏卞緢涓ラ噸浜嗐 + +鎴戣繖閲岀敤浜嗕釜trick锛屾病鍘荤粏鏌ユ爣鍑嗏斺攂ool鍏跺疄鏄釜鍗曞瓧鑺傛暣褰紝瀹冪殑true涓鑸槸1锛宖alse涓鑸槸0銆傚叿浣撶殑寰楀幓鏌++瑙勮寖浜嗭紝鎴戞病鍏蜂綋楠岃瘉杩囷紝浣嗗湪甯歌缂栬瘧鍣ㄤ笂鏄繖鏍风殑銆 + +浜庢槸锛屾垜浠氨鑳芥妸杩欎釜if鍒ゆ柇鐨勫垽鏂潯浠讹紝淇濆瓨涓轰竴涓暣褰㈠笺傜劧鍚庢妸杩欎釜1/0鐨勬暣褰㈠硷紝鍜宨f-else鍐呯殑璁$畻鎻夊湪涓璧凤紝鍚堝苟鎴愪竴涓笢瑗裤 + +鍏蜂綋鎬濊矾鏄繖鏍风殑: + +```c++ +result = isTrue * ...true expression... + + (1 - isTrue) * ...false expression... +``` + +鍙傜収杩欎釜鍐欐硶锛屽悎骞朵竴涓嬪椤瑰紡锛屾彁鍙栦竴涓嬬郴鏁帮紝灏卞緱鍒颁簡涓婇潰鐨勪唬鐮併 + +杩欑増浠g爜锛屾晥鐜囨彁鍗囨病鎯宠薄涓槑鏄撅紝鎵ц浜嗗娆″悗锛岀ǔ瀹氶熷害鏄墦璐ヤ簡59.85%瀵规墜锛屽伓灏斿彲浠ヨ功鍒60%鐢氳嚦70%銆 + +涓嶈繃鍦ㄦ垜鐩墠宸ヤ綔鐨勯」鐩腑锛屾牳蹇冪畻娉曢噷鏈夎澶氬湴鏂圭敤鍒颁簡杩欐牱鐨則rick銆備负閬垮厤bool鈫抜nt鐨勬搷浣滀竾涓鏄疷ndefined behavior锛屾垜杩樼壓鐗蹭簡涓瀹氱殑鎬ц兘鎶婂畠鍐欐垚`int isTrue = (boolean expression)?1:0`锛屼絾鏁堢巼鎻愬崌渚濇棫寰堟槑鏄俱 + + + +#### 鏈缁堢増锛堝ソ瀛╁瓙涓嶈瀛 + +鍏跺疄锛屾垜澶у鏄按杩囧幓鐨勶紝澶у洓鍋氭瘯璁炬墠鍏堝鐨凲t鍋欸UI锛岀粨鏋滆繕鏄病瀛︿細锛岀瓟杈╂椂鐢≦t Designer鐢讳簡涓猽i鎴浘鍐欒繘璁烘枃鈥︹ + +杩樺ソ鎴戜滑澶у鐨勮蒋浠舵槸涓湡绛旇京鏃舵紨绀猴紝涔嬪悗灏卞彧闇瑕佽鏂囦簡銆備腑鏈熼偅浼氬効鎴戣繕鍙互鍊熷彛璇寸晫闈㈣繕娌″仛濂芥潵鎺ㄨ劚锛岀劧鍚庢垜鎶婄晫闈㈠師鍨嬪拰绾懡浠よ鐨勬牳蹇冨姛鑳戒唬鐮佸垎鍒紨绀轰簡涓嬶紝鑰佸笀寰堝ぇ搴︾殑璁╂垜杩囦簡鈥︹ + +姣曚笟宸ヤ綔鍚庯紝鎵嶅紑濮嬪ソ濂界殑瀛++鍜孮t锛屽苟涓斿洜涓轰竴寮濮嬪氨鍏ヤ簡Qt鍧戯紝瀵艰嚧鎴戝埌鐜板湪宸ヤ綔涓夊勾澶氫簡锛屽boost閮藉彧鏄竴鐭ュ崐瑙o紝STL涔熸槸浠庡崐骞村墠鎵嶅紑濮嬪ソ濂藉鐨勨︹ + +鑷充簬ACM銆丱J杩欎簺涓滆タ锛岄偅鏇存槸绂绘垜寰堣繙锛屾垜鐜板湪鏄敤鍙互鍋歍L鐨勫伐浣滅粡楠岋紝楂樺眿寤虹摯鐨勯嗘帹杩欎簺鐭ヨ瘑鈥︹ + +鑰岃繖涓鍩熼噷锛岀潃閲嶄簬绠楁硶锛屾墍浠++鍜岄潰鍚戝璞″叾瀹炵敤澶勪笉澶э紝鏍稿績杩樻槸C鐨勯偅涓濂椼 + +鑰屽綋鎴戠煡閬撲簡鎵撹〃娉曠殑瀛樺湪鏃讹紝绠鐩存槸姣佷笁瑙備簡锛岃寰楄繖瀹屽叏鏄綔寮娾︹︿絾鍙嶈繃鏉ヤ竴鎯筹紝宸ョ▼棰嗗煙涓嶅氨鏄弧婊$殑灏嗗氨鍜屽噾鍚堜箞锛屽湪鍙互鎺ュ彈鐨勭簿搴︽垨鍏朵粬鎹熷け鑼冨洿鍐咃紝鑳藉鎻愰珮N涓暟閲忕骇瀹屾垚宸ヤ綔鐨勬柟娉曪紝灏辨槸鏈瀹岀編鐨勬柟娉曪紝姣斿鎵撹〃娉曘 + + +鍍忚繖棰橈紝鏈鐩磋鐨勫叾瀹炲簲璇ユ槸switch-case锛屼絾鏈蹇殑鍗存槸鎵撹〃娉曘 + +浣嗘墦琛ㄦ硶鐨勬渶楂樺鐣岋紝鏄暟缁勶紝鑰屼笖鏄啓鍦ㄦ爤涓婄殑鏁扮粍锛岃屼笉鏄痭ew鍑烘潵鐨勫爢鏁扮粍锛堟垜閮戒笉鎻怉CM閲岋紝閫氳繃棰勭紪璇戞寚浠ゆ妸鏍堟墿瀹瑰埌鍑犲崄鍑犵櫨鍏嗙劧鍚庡叏绋嬪湪鏍堜笂鍐欑畻娉曠殑濂囨妧娣阀浜嗏︹︼級銆傝岃繖閲岀敤鐨勫彧涓嶈繃鏄痷nordered_map锛岃櫧鐒秇ash鐞嗚鏌ヨ鎬ц兘鏄疧(1)锛屼絾姣旇捣鐪熸O(1)鐨勬暟缁勪笅鏍囪闂紝杩樻槸鏈夐偅涔堢偣甯告暟涓婄殑宸窛鐨勩 + +浜庢槸锛屾湁浜嗘渶缁堢増鐨勪唬鐮侊紝涓у績鐥呯媯鐨勬墦琛ㄣ傚鏋滅湅涓嶆噦鐨勮瘽锛屽彲浠ョ湅涓涓婭銆乂銆乆銆丩銆丆銆丏銆丮鐨凙SCII鐮侊紝浣犲氨鎳備簡銆 + + + +## 浠g爜 + +```c++ +class Solution { +public: + int romanToInt(string s) { + int romanMap['X'+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 100, 500, 0, + 0, 0, 0, 1, 0, 0, 50, 1000, 0, 0, + 0, 0, 0, 0, 0, 0, 5, 0, 10 }; + int num = romanMap[s.back()]; + int prev = num; + for(int i=s.length()-2;i>=0;--i) { + int cur = romanMap[s.at(i)]; + int curLargerThanPrev = cur >= prev; + num += (2 * curLargerThanPrev - 1) * cur; + prev = cur; + } + return num; + } +}; +``` + +杩欎釜鐗堟湰锛屽钩鍧囨ц兘鍦60-80%锛屾渶楂樻墦璐ヤ簡97.73%鐨勫鎵嬨 + +娌℃瘺鐥咃紒 + + + +#### 鎵╁睍闃呰 + +鍙儨leetcode涓嶆敮鎸乺ust锛屼笉鐒舵垜瑙夊緱锛宺ust鐨刾attern match锛屼篃璁告槸鏈蹇殑銆 + +github涓婃湁涓璇█鎬ц兘鐨刡enchmark椤圭洰锛屽叾涓湁涓猼est case鏄疄鐜颁竴涓畝鏄撶殑js瑙i噴鍣ㄣ + +杩欎釜棰樼洰閲岋紝ruby绛夊浜庡瓧绗︿覆澶勭悊鏈変抚蹇冪梾鐙傜殑浼樺寲鐨勫姩鎬佽瑷锛屾ц兘鐢氳嚦瓒呰繃浜嗙敤switch-case鐨凜/C++鈥斺旇鐭ラ亾锛屽湪鍏朵粬棰樼洰閲岋紝C/C++鐩告瘮鍔ㄦ佽瑷閮芥湁鐫鏈灏戜竴涓暟閲忕骇鐨勭⒕鍘嬩紭鍔裤 + +鐒惰宺ust鐢ㄦ帴杩戜竴涓暟閲忕骇鐨勫樊璺濈⒕鍘嬩簡杩欎簺瀵规墜鈥︹︾敤鐨勫氨鏄痯attern match锛屼唬鐮佹瀬搴︾畝娲佷紭闆呫 + +鍙儨鎴戜竴鐩存病绌哄rust鈥︹ + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ diff --git a/leetcode/15/note.md b/leetcode/15/note.md new file mode 100644 index 0000000..3dfc32e --- /dev/null +++ b/leetcode/15/note.md @@ -0,0 +1,59 @@ +## 閾炬帴 + +銆愪腑绛夐毦搴︺15. 3Sum + +https://leetcode.com/problems/3sum/ + + +## 棰樼洰 + +Given an array聽*S*聽of聽*n*聽integers, are there elements聽*a*,聽*b*,聽*c*聽in聽*S*聽such that聽*a*聽+聽*b*聽+聽*c*聽= 0? Find all unique triplets in the array which gives the sum of zero. + +**Note:**聽The solution set must not contain duplicate triplets. + +``` +For example, given array S = [-1, 0, 1, 2, -1, -4], + +A solution set is: +[ + [-1, 0, 1], + [-1, -1, 2] +] +``` + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 + + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file From 1cd0177fa91f527ec2bc07c6a798d0db614ca32c Mon Sep 17 00:00:00 2001 From: BigBrother007 <717354154@qq.com> Date: Thu, 29 Dec 2016 11:11:56 +0800 Subject: [PATCH 30/72] Update and rename note.md to note_BigBrother007.md --- leetcode/01/note.md | 48 ----------------------- leetcode/01/note_BigBrother007.md | 65 +++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 48 deletions(-) delete mode 100644 leetcode/01/note.md create mode 100644 leetcode/01/note_BigBrother007.md diff --git a/leetcode/01/note.md b/leetcode/01/note.md deleted file mode 100644 index 33fcd26..0000000 --- a/leetcode/01/note.md +++ /dev/null @@ -1,48 +0,0 @@ -## 链接 - - -https://leetcode.com/problems/two-sum/ - - -## 题目 - - - - - -## 释义 - - - - - - -## 补充描述 - - - - - - -## 代码 - - - - - - -```c++ - -//代码放在这个块里面,可以高亮关键字 - - - -``` - - - -## 更多 - -![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) - -PS: 请保留二维码链接,以便更多人参与进来。谢谢。 \ No newline at end of file diff --git a/leetcode/01/note_BigBrother007.md b/leetcode/01/note_BigBrother007.md new file mode 100644 index 0000000..e49502d --- /dev/null +++ b/leetcode/01/note_BigBrother007.md @@ -0,0 +1,65 @@ +## 閾炬帴 + + +https://leetcode.com/problems/two-sum/ + + +## 棰樼洰 + + + + + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 +class Solution { +public: + vector twoSum(vector& nums, int target) { + int n = nums.size(); + int first = 0, second = 0; + for (int i = 0; i < n - 1; ++i) + { + for (int j = i + 1; j < n; ++j) + if (nums[i] + nums[j] == target) + { + first = i; + second = j; + break; + } + } + return vector {first, second}; + } +}; + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From f4ab730b275c7946193d7aaa47da163109635930 Mon Sep 17 00:00:00 2001 From: BigBrother007 <717354154@qq.com> Date: Thu, 29 Dec 2016 23:37:24 +0800 Subject: [PATCH 31/72] Create note_BigBrother007 --- leetcode/06/note_BigBrother007 | 63 ++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 leetcode/06/note_BigBrother007 diff --git a/leetcode/06/note_BigBrother007 b/leetcode/06/note_BigBrother007 new file mode 100644 index 0000000..e30fb1d --- /dev/null +++ b/leetcode/06/note_BigBrother007 @@ -0,0 +1,63 @@ +## 閾炬帴 + + +https://leetcode.com/problems/zigzag-conversion/ + + +## 棰樼洰 + + + + + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 +string result; + vector svec(numRows); + int length = s.length(); + if(numRows == 1) + return s; + for (int i = 0; i < length ; ++i) + { + //杩欎釜鍏紡鏈夌偣浜岋紝鎶奿 涓娆″彉鎹负 0, 1, ... numRows-1, numRows-2, ... 0, ... + int j = abs(abs(i % (2 * numRows - 2)- numRows + 1) - numRows + 1); + svec[j] += s[i]; + } + for (int i = 0; i < numRows; ++i) + { + result += svec[i]; + } + return result; + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From 481d771b09a9544d4c4e7c88b5a1ad3534b87b00 Mon Sep 17 00:00:00 2001 From: BigBrother007 <717354154@qq.com> Date: Thu, 29 Dec 2016 23:38:56 +0800 Subject: [PATCH 32/72] Rename note_BigBrother007 to note_BigBrother007.md --- leetcode/06/{note_BigBrother007 => note_BigBrother007.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename leetcode/06/{note_BigBrother007 => note_BigBrother007.md} (100%) diff --git a/leetcode/06/note_BigBrother007 b/leetcode/06/note_BigBrother007.md similarity index 100% rename from leetcode/06/note_BigBrother007 rename to leetcode/06/note_BigBrother007.md From 704f68618c0c7e06261f07c7f2b5e772cf909e54 Mon Sep 17 00:00:00 2001 From: bajdcc Date: Fri, 30 Dec 2016 16:57:15 +0800 Subject: [PATCH 33/72] Add note: zigzag conv MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 鎺㈣骞跺疄鐜颁粠杈撳叆鍒拌緭鍑虹殑鏄犲皠鍏崇郴銆 --- leetcode/06/note_bajdcc.md | 103 +++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 leetcode/06/note_bajdcc.md diff --git a/leetcode/06/note_bajdcc.md b/leetcode/06/note_bajdcc.md new file mode 100644 index 0000000..b4c9d67 --- /dev/null +++ b/leetcode/06/note_bajdcc.md @@ -0,0 +1,103 @@ +## 閾炬帴 + +https://leetcode.com/problems/zigzag-conversion/ + +## 棰樼洰 + +The string `"PAYPALISHIRING"` is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) + + P A H N + A P L S I I G + Y I R +And then read line by line: `"PAHNAPLSIIGYIR"` +Write the code that will take a string and make this conversion given a number of rows: + + string convert(string text, int nRows); +`convert("PAYPALISHIRING", 3)` should return `"PAHNAPLSIIGYIR"`. + +## 閲婁箟 + +宸茬煡杈撳叆涓哄瓧涓睞鍜屾暟瀛梟锛屾眰杈撳嚭涓哄瓧涓睟銆傚嵆B=fun(A,n)銆 + +A鍜孊涔嬮棿瀛樺湪鏄犲皠鍏崇郴f锛屽嵆B[i]=A[f(i)]锛岄棶棰樺湪浜庢眰鏄犲皠f銆 + + 0 4 8 + 1 3 5 7 9 + 2 6 + +浠ヤ笂杩颁负渚嬶紝杈撳叆A=0123456789锛宯=3銆 + +鍙互鐪嬪嚭锛氫竴鍏眓琛屻 + +- 绗竴琛岋細璧峰琛岋紝浠0寮濮嬶紝鍏樊涓4銆 +- 绗簩琛岋細浠1寮濮嬶紝鍏樊涓4锛屽叕宸箣鍐呭寘鎷袱涓暟瀛椼 +- 绗笁琛岋細缁撴潫琛岋紝浠2寮濮嬶紝鍏樊涓4銆 + +缁忚繃涓嶆柇鎺ㄥ锛屾渶缁堝緱鍑烘槧灏勫叧绯汇 + +鑻ヤ覆涓篈锛岄暱搴︿负len锛岃鏁颁负numRows锛屽垯鍏崇郴涓猴細 + +- 绗竴琛岋紝浠0寮濮嬶紝鍏樊d=(numRows - 1)*2锛屼笅鏍囪秴杩噇en-1鏃犳晥銆 +- 绗簩琛屽埌鍊掓暟绗簩琛岋紝璁捐琛屾槸k琛岋紝鍒欒琛屼粠k寮濮嬶紝鍏樊涓篸锛屽叕宸唴鍖呮嫭涓や釜鏁板瓧锛岀浜屼釜鏁板瓧=绗竴涓暟瀛+d- (k*2)锛屼笅鏍囪秺鐣屼綔搴熴 +- 鏈鍚庝竴琛岋紝鍚岀涓琛屻 + +娉細璇ユ硶涓嶇敤鎶妟igzag鐭╅樀缃楀垪鍑烘潵锛岀洿鎺ュ啓浜庣粨鏋滃綋涓 + +## 琛ュ厖鎻忚堪 + +鎻愪氦锛歨ttps://leetcode.com/submissions/detail/87277753/ + +鐢ㄦ椂锛12ms锛宐eat锛96%锛堣繍姘斿ソ锛 + + +## 浠g爜 + + + + +```c++ +#include +#include + +using namespace std; + +class Solution { +public: + string convert(string s, int numRows) { + int n = (int) s.length(); + auto arr = new char[n]; + if (numRows <= 1) { + return s; + } else { + int index, add, start; + index = start = 0; + add = (numRows - 1) << 1; + for (int i = start++; i < n; i += add) { + arr[index++] = s[i]; + } + for (int k = 1, j; k < numRows - 1; ++k) { + for (int i = start++; i < n; i += add) { + arr[index++] = s[i]; + if (((j = i + add - (k << 1)) < n)) { + arr[index++] = s[j]; + } + } + } + for (int i = start; i < n; i += add) { + arr[index++] = s[i]; + } + } + decltype(s) result(arr, s.length()); + delete[]arr; + return result; + } +}; +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From 0103490bdd35a93714fb6093e0d83e170829ad58 Mon Sep 17 00:00:00 2001 From: Yac836 <374012581@qq.com> Date: Fri, 30 Dec 2016 18:45:25 +0800 Subject: [PATCH 34/72] add Yac836 note --- leetcode/01/note_Yac836.md | 46 +++++++++++++++++++++++ leetcode/06/note_Yac836.md | 53 ++++++++++++++++++++++++++ leetcode/07/note_Yac836.md | 59 +++++++++++++++++++++++++++++ leetcode/08/note_Yac836.md | 77 ++++++++++++++++++++++++++++++++++++++ leetcode/14/note_Yac836.md | 57 ++++++++++++++++++++++++++++ leetcode/19/note_Yac836.md | 57 ++++++++++++++++++++++++++++ leetcode/20/note_Yac836.md | 73 ++++++++++++++++++++++++++++++++++++ leetcode/21/note_Yac836.md | 68 +++++++++++++++++++++++++++++++++ leetcode/24/note_Yac836.md | 49 ++++++++++++++++++++++++ leetcode/26/note_Yac836.md | 49 ++++++++++++++++++++++++ leetcode/27/note_Yac836.md | 47 +++++++++++++++++++++++ 11 files changed, 635 insertions(+) create mode 100644 leetcode/01/note_Yac836.md create mode 100644 leetcode/06/note_Yac836.md create mode 100644 leetcode/07/note_Yac836.md create mode 100644 leetcode/08/note_Yac836.md create mode 100644 leetcode/14/note_Yac836.md create mode 100644 leetcode/19/note_Yac836.md create mode 100644 leetcode/20/note_Yac836.md create mode 100644 leetcode/21/note_Yac836.md create mode 100644 leetcode/24/note_Yac836.md create mode 100644 leetcode/26/note_Yac836.md create mode 100644 leetcode/27/note_Yac836.md diff --git a/leetcode/01/note_Yac836.md b/leetcode/01/note_Yac836.md new file mode 100644 index 0000000..d8412ff --- /dev/null +++ b/leetcode/01/note_Yac836.md @@ -0,0 +1,46 @@ +## 閾炬帴 + + +https://leetcode.com/problems/two-sum/ + + +## 棰樼洰 + +Given an array of integers, return聽**indices**聽of the two numbers such that they add up to a specific target. + +You may assume that each input would have聽**exactly**聽one solution. + + + +## 閲婁箟 + +浠庝竴涓暟缁勪腑缁欏嚭鎵惧嚭涓や釜鏁存暟锛屽拰绛変簬鎵缁欑殑鐩爣鍊笺 + + +## 琛ュ厖鎻忚堪 +涓嬫爣浠庨浂寮濮 + +## 浠g爜 +```c++ +vector twoSum(vector& nums, int target) { + decltype(nums.size()) len = nums.size(); + vector res; + for (decltype(nums.size()) i = 0; i < len; i++) { + for (decltype(nums.size()) j = i + 1; j < len; j++) { + if (nums[i] + nums[j] == target) { + res.push_back(i); + res.push_back(j); + } + } + } + return res; +} +``` + + + +## 鏇村 + +![d](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/06/note_Yac836.md b/leetcode/06/note_Yac836.md new file mode 100644 index 0000000..02bb284 --- /dev/null +++ b/leetcode/06/note_Yac836.md @@ -0,0 +1,53 @@ +## 閾炬帴 + + +https://leetcode.com/problems/zigzag-conversion/ + +## 瑙i鎬濊矾 + +杩欓亾棰樼洰璁╂寜`z`鍨嬭緭鍑恒傛牴鎹牱渚嬶紝鎴戜滑鍙互寰楀嚭瀹炵幇鏂规硶銆 + +1. 鏈夊嚑琛岋紝鎴戜滑灏遍渶瑕佸嚑涓瓧绗︿覆璁板綍姣忎竴琛岄兘鏄粈涔堝瓧绗︺傛垜浠彲浠ラ噰鐢╜vector`鏉ヨ褰曟瘡涓琛岄兘鏈変粈涔堝瓧绗 +2. 閬嶅巻鍘熷瓧绗︿覆锛屽皢姣忎釜瀛楃锛岃褰曞埌浠栧簲璇ュ睘浜庣殑瀛楃涓蹭腑銆 +3. 灏嗘瘡涓瓧绗︿覆杩炴帴璧锋潵 + + + + +## 浠g爜 + + + + + + +```c++ +string convert(string s, int numRows) { + if (s.empty()) + return ""; + vector tres(numRows); + vector::size_type index; + string res; + auto len = s.size(); + for (index = 0; index < len; ) { + for (int i = 0; i < numRows && index < len; i++) + tres[i] = tres[i] + s[index++]; + for (int j = numRows - 2; j > 0 && index < len; j--) + tres[j] = tres[j] + s[index++]; + } + for (const string e : tres) + res = res + e; + + return res; +} + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/07/note_Yac836.md b/leetcode/07/note_Yac836.md new file mode 100644 index 0000000..fd1c322 --- /dev/null +++ b/leetcode/07/note_Yac836.md @@ -0,0 +1,59 @@ +## 閾炬帴 + + +https://leetcode.com/problems/reverse-integer/ + + + + +## 鎬濊矾 + +1. 灏嗘瘡浣嶄笂鐨勬暟瀛楁嬁鍑烘潵锛屽瓨鍒癭vector`涓紝浠庝綆浣嶅紑濮嬫嬁 +2. 鍐嶅皢姣忎竴浣嶅姞璧锋潵 + + + + +## 浠g爜 + + + + + + +```c++ +int reverse(int x) { + vector ivct; + //2147483647 + vector maxInt{ 2,1,4,7,4,8,3,6,4,7 }; + int flag = 1; + int res = 0; + if (x == 0 || x == -2147483647 - 1) + return 0; + if (x < 0) { + x = -x; + flag = -1; + } + while (x != 0) { + ivct.push_back(x % 10); + x = x / 10; + } + decltype(ivct.size()) len = ivct.size(); + if (len == 10) { + if (ivct > maxInt) + return 0; + } + for (decltype(ivct.size()) index = 0; index < ivct.size(); index++) + res = res * 10 + ivct[index]; + res = res * flag; + return res; +} +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/08/note_Yac836.md b/leetcode/08/note_Yac836.md new file mode 100644 index 0000000..4304645 --- /dev/null +++ b/leetcode/08/note_Yac836.md @@ -0,0 +1,77 @@ +## 閾炬帴 + + +https://leetcode.com/problems/string-to-integer-atoi/ + + +## 瑙i鎬濊矾 +1. 鎵惧嚭寮濮嬫暟瀛楀拰缁撴潫鏁板瓧鐒跺悗瑁呮崲 + +***娉ㄦ剰*** +1. int 鏈灏忓 +2. 姝h礋鏁 + + + +## 浠g爜 + + + + + + +```c++ +int myAtoi(string str) { + int i = 0; + int flag = 0; + double res = 0; + int sign = 1; + for (auto c : str) { + if (!isdigit(c)) { + i++; + } + else + break; + } + int temp = i - 1; + for (int index = 0; index < temp; index++) { + if (isspace(str[index])) + continue; + else { + flag = 1; + break; + } + } + if (flag) + return 0; + if (i > 0) { + if (str[i - 1] == '+' || isspace(str[i - 1])) + sign = 1; + else if (str[i - 1] == '-') + sign = -1; + else + return 0; + } + for (decltype(str.size()) index = i; index < str.size(); index++) + if (isdigit(str[index])) + res = res * 10 + (str[index] - '0'); + else + break; + res = res * sign; + if (res > 2147483647) + return 2147483647; + // -2147483648 鐩存帴鍐 vs2015 浼氭姤閿 codeblocks 涓嶄細 + if (res < -2147483647 - 1) + return -2147483647 - 1; + return res; +} + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/14/note_Yac836.md b/leetcode/14/note_Yac836.md new file mode 100644 index 0000000..216a71e --- /dev/null +++ b/leetcode/14/note_Yac836.md @@ -0,0 +1,57 @@ +## 閾炬帴 + + +https://leetcode.com/problems/longest-common-prefix/ + + + +## 琛ュ厖鎻忚堪 + +1. 浠ョ涓涓瓧绗︿覆涓哄熀鍑嗗紑濮嬫瘮杈冿紝鍘荤涓涓瓧绗︿覆涓瘡涓瓧绗﹀紑濮嬩笌鍚庨潰鐨勫瓧绗︿覆涓殑瀛楃寮濮嬫瘮杈 +2. 濡傛灉鐩稿悓锛屽垯缁х画姣旇緝绗簩涓紝绗笁涓紝... 骞朵笖灏嗘瘡娆$浉鍚岀粨鏋滆繘琛岀疮鍔 +3. 濡傛灉涓嶅悓锛屽垯缁撴潫 +4. 杩斿洖绱姞缁撴灉 + + +## 浠g爜 + + +```c++ +string longestCommonPrefix(vector& strs) { + if (strs.empty()) + return ""; + if (strs.size() == 1) + return strs[0]; + vector::size_type begin; + int index; + int len = strs[0].size(); + vector::size_type strsLen = strs.size(); + string res; + bool flag = true; + for (index = 0; index < len; index++) { + for (begin = 1;begin < strsLen; begin++) { + if (strs[0][index] != strs[begin][index]) { + flag = false; + break; + } + } + if (!flag) { + break; + } + else { + res = res + strs[0][index]; + } + } + return res; +} + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/19/note_Yac836.md b/leetcode/19/note_Yac836.md new file mode 100644 index 0000000..8f20898 --- /dev/null +++ b/leetcode/19/note_Yac836.md @@ -0,0 +1,57 @@ +## 閾炬帴 + + +https://leetcode.com/problems/remove-nth-node-from-end-of-list/ + + +## 瑙i鎬濊矾 + +1. 閬嶅巻姹傚嚭閾捐〃闀垮害 +2. 鎵惧嚭瑕佸垹闄ょ殑鍏冪礌鐨勫墠涓涓綅缃 +3. 鍒犻櫎鍏冪礌 + +***娉ㄦ剰*** + +1. 瑕佸垹闄ゅ厓绱犱负绗竴涓綅缃椂锛岃繑鍥炲奸棶棰樸 + + + + + + +## 浠g爜 + + + + + + +```c++ +ListNode* removeNthFromEnd(ListNode* head, int n) { + ListNode *begin = head; + int len = 1; + while (begin->next != nullptr) { + len++; + begin = begin->next; + } + if (len == n) + return head->next; + int remove = len - n; + begin = head; + len = 1; + while (len < remove) { + begin = begin->next; + len++; + } + begin->next = begin->next->next; + return head; +} +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/20/note_Yac836.md b/leetcode/20/note_Yac836.md new file mode 100644 index 0000000..182c4eb --- /dev/null +++ b/leetcode/20/note_Yac836.md @@ -0,0 +1,73 @@ +## 閾炬帴 + + +https://leetcode.com/problems/valid-parentheses/ + +## 瑙i鎬濊矾 + +1. 鍒╃敤stack +2. 濡傛灉鏍堥《鍏冪礌鍜屽瓧绗︿覆涓笅涓涓厓绱犵浉绛夛紝鍒欐爤椤跺厓绱犲嚭鏍堬紝濡傛灉涓嶈兘锛屽垯涓嬩竴涓厓绱犲叆鏍堛 + + + + +## 浠g爜 + + + + + + +```c++ +bool isValid(string s) { + if (s.empty()) + return true; + auto len = s.size(); + string::size_type index; + stack cs; + cs.push(s[0]); + for (index = 1; index < len; index++) { + if (cs.empty()) + if (s[index] != ')'&&s[index] != '}'&&s[index] != ']') { + cs.push(s[index]); + continue; + } + else { + return false; + } + char e = cs.top(); + if (e == '(') { + if (s[index] == ')') + cs.pop(); + else + cs.push(s[index]); + } + else if (e == '{') { + if (s[index] == '}') + cs.pop(); + else + cs.push(s[index]); + } + else if (e == '[') { + if (s[index] == ']') + cs.pop(); + else + cs.push(s[index]); + } + } + if (cs.empty()) + return true; + else + return false; +} + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/21/note_Yac836.md b/leetcode/21/note_Yac836.md new file mode 100644 index 0000000..82bf960 --- /dev/null +++ b/leetcode/21/note_Yac836.md @@ -0,0 +1,68 @@ +## 閾炬帴 + + +https://leetcode.com/problems/merge-two-sorted-lists/ + + +## 瑙i鎬濊矾 + +1. 鍏堟柊寤轰竴涓妭鐐箃res锛屾寚鍚戜袱涓摼琛ㄤ腑鏈灏忕殑銆 +2. 鍐嶆柊寤轰竴涓妭鐐箁es锛屼娇瀹冩寚鍚憈res,鐢ㄤ簬淇濆瓨缁撴灉銆 +3. 鐒跺悗涓娆℃瘮杈冧袱涓摼琛ㄤ腑鐨勫厓绱狅紝浣縯res鎸囧悜鍏朵腑杈冨皬鍏冪礌锛岀劧鍚庡悗绉籺res锛屼娇瀹冩寚鍚戝畠鐨勪笅涓涓 + + + + +## 浠g爜 + + + + + + +```c++ +ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { + if (!l1 && !l2) + return nullptr; + if (!l1) + return l2; + if (!l2) + return l1; + ListNode* tres; + if (l1->val < l2->val) { + tres = l1; + l1 = l1->next; + } + else { + tres = l2; + l2 = l2->next; + } + ListNode* res = tres; + while (l1&&l2) { + if (l1->val < l2->val) { + tres->next = l1; + l1 = l1->next; + } + else { + tres->next = l2; + l2 = l2->next; + } + tres = tres->next; + } + if (l1) + tres->next = l1; + if (l2) + tres->next = l2; + return res; +} + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/24/note_Yac836.md b/leetcode/24/note_Yac836.md new file mode 100644 index 0000000..64794b4 --- /dev/null +++ b/leetcode/24/note_Yac836.md @@ -0,0 +1,49 @@ +## 閾炬帴 + + +https://leetcode.com/problems/swap-nodes-in-pairs/ + + +## 瑙i鎬濊矾 + +1. 鎵惧埌瑕佷氦鎹㈢殑鍓嶉┍灏辫銆 + + + + +## 浠g爜 + + + + + + +```c++ +//鍊熼壌鍒汉鐨勪唬鐮 +ListNode* swapPairs(ListNode* head) { + if (!head || !head->next) return head; + ListNode tempNode(0); + ListNode *helper = &tempNode; + ListNode *ret = head; + ListNode *cur = helper; + while (ret && ret->next) { + ListNode *next = ret->next->next; + cur->next = ret->next; + cur = cur->next; + cur->next = ret; + cur = cur->next; + cur->next = nullptr; + ret = next; + } + if (ret) cur->next = ret; + return helper->next; +} +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/26/note_Yac836.md b/leetcode/26/note_Yac836.md new file mode 100644 index 0000000..bf00bb4 --- /dev/null +++ b/leetcode/26/note_Yac836.md @@ -0,0 +1,49 @@ +## 閾炬帴 + + +https://leetcode.com/problems/remove-duplicates-from-sorted-array/ + +##瑙i鎬濊矾 + +1. 瀹氫箟涓や釜杩唬鍣╜s1`,`s2`,` s1`鐢ㄦ潵璁板綍涓嶉噸澶嶅厓绱犵殑浣嶇疆锛宻2鐢ㄥ鎵句笉閲嶅鐨勫厓绱犮 +2. 褰揱s2` 鍙戠幇浜嗕笉鏄噸澶嶇殑鍏冪礌鐨勬椂鍊欙紝灏卞皢`s2`鎵鎸囩殑鍏冪礌鎻掑叆鍒癭s1`涓嬩竴涓墍鎸囩殑浣嶇疆銆 +3. 鏈鍚庡垹闄s1`鍒版暟缁勭粨鏉熺殑鍏冪礌 + + +## 浠g爜 + + + + + + +```c++ +int removeDuplicates(vector& nums) { + if (nums.size() == 0) + return 0; + vector::size_type len = nums.size(); + vector::iterator s1 = nums.begin(); + vector::iterator s2 = nums.begin() + 1; + vector::iterator e = nums.end(); + while (s2 != e) { + if (*s1 == *s2) + s2++; + else { + s1++; + *s1 = *s2; + s2++; + } + } + nums.erase(s1 + 1, e); + return nums.size(); +} + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/27/note_Yac836.md b/leetcode/27/note_Yac836.md new file mode 100644 index 0000000..3614fdb --- /dev/null +++ b/leetcode/27/note_Yac836.md @@ -0,0 +1,47 @@ +## 閾炬帴 + + +https://leetcode.com/problems/remove-element/ + + +## 瑙i鎬濊矾 + +1. 瀹氫箟涓涓鏁板櫒锛屼綔鐢ㄦ湁浜 + ++ 璁板綍鏈夊嚑涓洰鏍囧 ++ 灏嗕笉鍚屼笌鐩爣鍊肩殑鏁板瓧锛岀Щ鍔ㄥ埌鏁扮粍鍓嶉潰 + + + + + + + + +## 浠g爜 + + + + + + +```c++ +int removeElement(vector& nums, int val) { + vector::size_type i = 0, j = 0; + for (;i != nums.size();i++) { + if (nums[i] == val) + continue; + nums[j] = nums[i]; + j++; + } + return j; +} +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file From 625f06760aae9d0b71ccffd0f81fba23869030ed Mon Sep 17 00:00:00 2001 From: leakey1905 Date: Sat, 31 Dec 2016 20:37:27 +0800 Subject: [PATCH 35/72] Create note_leakey.md Use two pointers --- leetcode/03/note_leakey.md | 109 +++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 leetcode/03/note_leakey.md diff --git a/leetcode/03/note_leakey.md b/leetcode/03/note_leakey.md new file mode 100644 index 0000000..49c77a4 --- /dev/null +++ b/leetcode/03/note_leakey.md @@ -0,0 +1,109 @@ +## 閾炬帴 + +銆愪腑绛夐毦搴︺3. Longest Substring Without Repeating Characters 锛堟渶闀夸笉閲嶅瀛愪覆锛 + +https://leetcode.com/problems/longest-substring-without-repeating-characters/ + + +## 棰樼洰 + +Given a string聽**s**, find the longest palindromic substring in聽**s**. You may assume that the maximum length of聽**s**聽is 1000. + +**Example:** + +``` +Input: "babad" + +Output: "bab" + +Note: "aba" is also a valid answer. + +``` + +**Example:** + +``` +Given a string, find the length of the longest substring without repeating characters. + +Examples: + +Given "abcabcbb", the answer is "abc", which the length is 3. + +Given "bbbbb", the answer is "b", with the length of 1. + +Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring. +``` + +*Tags: Hash Table, Two Pointers* + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 +A similar python code: +```python +class Solution(object): + def lengthOfLongestSubstring(self, s): + """ + :type s: str + :rtype: int + """ + sLen = len(s) + if sLen <= 1: return sLen # s is shorter than 2 letters + subStrLen, p1, p2 = 0, 0, 1 # use 2 pointers + while p2 < sLen and subStrLen < 95: + for p2 in xrange(p1, sLen): + if s[p2] in s[p1:p2]: + break + else: + p2 += 1 + subStrLen = max(p2 - p1, subStrLen) + p1 += 1 + return subStrLen +``` + + + + + +## 浠g爜 + + + + + + +```c++ +class Solution { +public: + int lengthOfLongestSubstring(string s) { + int sLen = s.size(), subStrLen = 0, p1 = 0, p2 = 1; + if (sLen <= 1) + return sLen; + string subStr; + while (p2 < sLen && subStrLen < 95){ + for (p2 = p1; p2 < sLen; p2++){ + subStr.assign(s, p1, p2 - p1); //substr is s[p1:p2] + if (subStr.find(s[p2]) != -1) // s[p2] in substr + break; + } + subStrLen = max(p2 - p1, subStrLen); + p1++; + } + return subStrLen; + }; +}; + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From 797c183b6d0c8c0d5ae282cce3de948652e69872 Mon Sep 17 00:00:00 2001 From: leakey1905 Date: Sat, 31 Dec 2016 20:43:12 +0800 Subject: [PATCH 36/72] Create note_leakey.md --- leetcode/05/note_leakey.md | 88 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 leetcode/05/note_leakey.md diff --git a/leetcode/05/note_leakey.md b/leetcode/05/note_leakey.md new file mode 100644 index 0000000..2b4b0ea --- /dev/null +++ b/leetcode/05/note_leakey.md @@ -0,0 +1,88 @@ +## 閾炬帴 + +銆愪腑绛夐毦搴︺5. Longest Palindromic Substring 锛堟渶闀垮洖鏂囧瓙涓诧級 + +https://leetcode.com/problems/longest-palindromic-substring/ + + +## 棰樼洰 + +Given a string聽**s**, find the longest palindromic substring in聽**s**. You may assume that the maximum length of聽**s**聽is 1000. + +**Example:** + +``` +Input: "babad" + +Output: "bab" + +Note: "aba" is also a valid answer. + +``` + +**Example:** + +``` +Input: "cbbd" + +Output: "bb" +``` + + + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ +class Solution { +public: + string longestPalindrome(string s) { + int sLen = s.length(), maxLen = 0, maxStart = 0; + int pi = 0, pl = 0, pr = 0, subLen = 0; + while (pi <= sLen - maxLen / 2){ + pl = pr = pi; + while (pr < sLen - 1 && s[pr + 1] == s[pr]) + pr++; + pi = pr + 1; + while (pl > 0 && pr < sLen - 1 && s[pr + 1] == s[pl - 1]){ + pl--; + pr++; + } + subLen = pr - pl + 1; + if (maxLen < subLen){ + maxLen = subLen; + maxStart = pl; + } + } + return s.substr(maxStart, maxLen); + } +}; + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From d75286c64f8599f32f45f2bbfb643dee9b7ade0c Mon Sep 17 00:00:00 2001 From: leakey1905 Date: Sat, 31 Dec 2016 20:46:44 +0800 Subject: [PATCH 37/72] Create note_leakey.md --- leetcode/06/note_leakey.md | 108 +++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 leetcode/06/note_leakey.md diff --git a/leetcode/06/note_leakey.md b/leetcode/06/note_leakey.md new file mode 100644 index 0000000..7c22fdc --- /dev/null +++ b/leetcode/06/note_leakey.md @@ -0,0 +1,108 @@ +## 閾炬帴 + + +https://leetcode.com/problems/zigzag-conversion/ + + +## 棰樼洰 + + + + + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 +Another python solution: +```python +class Solution(object): + def convert(self, s, numRows): + """ + :type s: str + :type numRows: int + :rtype: str + """ + lens = len(s) + if lens <= numRows or numRows <= 1: + return s + else: + strre = "" + lenstr = (numRows - 1) * 2 + i = j = k = m = 0 + lenstrre = 0 + while lenstrre < lens: + strre = strre + s[j] + lenstrre += 1 + if not j % (lenstr / 2): + j += lenstr + else: + m += 1 + k = lenstr - j if m == 1 else lenstr + k + if k >= lens: + i += 1 + j = i + m = 0 + else: + strre = strre + s[k] + lenstrre += 1 + j += lenstr + if j >= lens: + i += 1 + j = i + m = 0 + return strre +``` + + + + + +## 浠g爜 + + + + + + +```c++ + +class Solution { +public: + string convert(string s, int numRows) { + if (numRows <= 1 || numRows >= s.length()) + return s; + string result = ""; + int maxStep = numRows * 2 - 2; + for (int i = 1; i <= numRows; i++){ + int j = i - 1; + int step = (i < numRows) ? (numRows - i) * 2 : maxStep; + while (j < s.length()) { + result += s[j]; + if (result.length() == s.length()) + return result; + j += step; + if (step < maxStep) { + step = maxStep - step; + } + } + } + return result; + } +}; + + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From 929ad38a2bd29550dd65130b47a36ad1960cbf88 Mon Sep 17 00:00:00 2001 From: leakey1905 Date: Sat, 31 Dec 2016 20:49:31 +0800 Subject: [PATCH 38/72] Create note_leakey.md --- leetcode/12/note_leakey.md | 93 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 leetcode/12/note_leakey.md diff --git a/leetcode/12/note_leakey.md b/leetcode/12/note_leakey.md new file mode 100644 index 0000000..f4998da --- /dev/null +++ b/leetcode/12/note_leakey.md @@ -0,0 +1,93 @@ +## 閾炬帴 + +銆愪腑绛夐毦搴︺12. Integer to Roman锛堟暟瀛楄浆缃楅┈鏂囷級 + +https://leetcode.com/problems/integer-to-roman/ + + +## 棰樼洰 + +Given an integer, convert it to a roman numeral. + +Input is guaranteed to be within the range from 1 to 3999. + +*Tags: Hash Table or Syntax-directed Translation(Advanced)* + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 +Similar python solution: +```python +class Solution(object): + def intToRoman(self, num): + """ + :type num: int + :rtype: str + :ref: http://www.cnblogs.com/dosxp/archive/2008/08/13/1266781.html + """ + hrom = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"] + trom = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"] + rom = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"] + intdex = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + hdic = dict(zip(intdex, hrom)) + tdic = dict(zip(intdex, trom)) + dic = dict(zip(intdex, rom)) + + rostr = "" + k = num // 1000 + num = num % 1000 + + h = num // 100 + num = num % 100 + + t = num // 10 + num = num % 10 + + rostr = "M"*k + hdic[h] + tdic[t] + dic[num] + return rostr + +``` + + + + + +## 浠g爜 + + + + + + +```c++ + +class Solution { +public: // Use dict to convert 0 - 3999 to Roman + string intToRoman(int num) { + char* rom3[4] = {"", "M", "MM", "MMM"}; //0XXX - 3XXX + char* rom2[10] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; //0XX - 9XX + char* rom1[10] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}; //0X - 9X + char* rom0[10] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}; //0 - 9 + string s; + s += rom3[num / 1000]; //X... + s += rom2[num % 1000 / 100]; //.X.. + s += rom1[num % 100 / 10]; //..X. + s += rom0[num % 10]; //...X + return s; + } +}; + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From dca5d5c748c5505923365d04ec44a8607f8d2cd7 Mon Sep 17 00:00:00 2001 From: leakey1905 Date: Sat, 31 Dec 2016 20:53:02 +0800 Subject: [PATCH 39/72] Create note_leakey.md --- leetcode/15/note_leakey.md | 97 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 leetcode/15/note_leakey.md diff --git a/leetcode/15/note_leakey.md b/leetcode/15/note_leakey.md new file mode 100644 index 0000000..309cdf2 --- /dev/null +++ b/leetcode/15/note_leakey.md @@ -0,0 +1,97 @@ +## 閾炬帴 + +銆愪腑绛夐毦搴︺15. 3Sum + +https://leetcode.com/problems/3sum/ + + +## 棰樼洰 + +Given an array聽*S*聽of聽*n*聽integers, are there elements聽*a*,聽*b*,聽*c*聽in聽*S*聽such that聽*a*聽+聽*b*聽+聽*c*聽= 0? Find all unique triplets in the array which gives the sum of zero. + +**Note:**聽The solution set must not contain duplicate triplets. + +``` +For example, given array S = [-1, 0, 1, 2, -1, -4], + +A solution set is: +[ + [-1, 0, 1], + [-1, -1, 2] +] +``` + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 +Simple python solution: +```python +class Solution(object): + def threeSum(self, nums): + """ + :type nums: List[int] + :rtype: List[List[int]] + """ + nums.sort() + C, maxi = set(), len(nums) - 2 # maxi: max index of pointer i backward + for i in xrange(maxi): + for j in xrange(i + 1, maxi + 1): + if -nums[i] - nums[j] in nums[j + 1: ]: + C.add((nums[i], nums[j], -nums[i] - nums[j])) + return list(C) +``` + + + + + +## 浠g爜 + + + + + + +```c++ +class Solution { +public: + vector> threeSum(vector& nums) { + sort(nums.begin(), nums.end()); + vector> idx; + int numsLen = nums.size(); + for (int pl = 0; pl < numsLen; pl++){ + if (pl > 0 && nums[pl] == nums[pl - 1]) + continue; // jump duplicate number from left + int pm = pl + 1, pr = numsLen - 1; + while (pm < pr){ + int sum = nums[pl] + nums[pm] + nums[pr]; + if (sum > 0) + pr--; // get a smaller number + else if (sum < 0) + pm++; // get a bigger number + else{ + idx.push_back(vector {nums[pl], nums[pm], nums[pr]}); + while (pm + 1 < pr && nums[pm] == nums[pm + 1]) pm++; // pass dup + while (pr - 1 < pm && nums[pr] == nums[pr - 1]) pr--; + pm++; + pr--; + } + } + } + return idx; + } +}; +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From 46980a458436c56bfe57d60e42a934fa5d9d64fa Mon Sep 17 00:00:00 2001 From: leakey1905 Date: Sat, 31 Dec 2016 20:57:18 +0800 Subject: [PATCH 40/72] Create note_leakey.md --- leetcode/22/note_leakey.md | 125 +++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 leetcode/22/note_leakey.md diff --git a/leetcode/22/note_leakey.md b/leetcode/22/note_leakey.md new file mode 100644 index 0000000..2f4863d --- /dev/null +++ b/leetcode/22/note_leakey.md @@ -0,0 +1,125 @@ +## 閾炬帴 + +銆愪腑绛夐毦搴︺22. Generate Parentheses锛堢敓鎴愭嫭鍙凤級 + +https://leetcode.com/problems/generate-parentheses/ + + +## 棰樼洰 + +Given *n* pairs of parentheses, write a function to generate all combinations of well-formed parentheses. + +For example, given *n* = 3, a solution set is: + +``` +[ + "((()))", + "(()())", + "(())()", + "()(())", + "()()()" +] +``` + +*Tags: Backtracking* + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 +A python solution: +```python +class Solution(object): + def generateParenthesis(self, n): + """ + :type n: int + :rtype: List[str] + """ + B = [i for i in xrange(n)] + p = "" + for k in xrange(0, 2 * n): + p += "(" if k in B else ")" + A = [p] + j = i = n - 1 + while True: + if B[j] != 2 * j: # adjustment b + B[j] += 1 + else: + while j != 0: + j -= 1 + if B[j] != 2 * j: + B[j] += 1 + for m in xrange(j + 1, n): + B[m] = B[m - 1] + 1 + j = n - 1 + break + if j <= 0: + return A + p = "" + for k in xrange(0, 2 * n): + p += "(" if k in B else ")" + A.append(p) + +``` + + + + + +## 浠g爜 + + + + + + +```c++ + +class Solution { +public: + vector generateParenthesis(int n) { + vector parenTable; + if (n == 0) + return parenTable; + string s; + int pl = n, pr = n; + for (int i = 0; i < n; i++) s += "("; + for (int i = 0; i < n; i++) s += ")"; //s = "(((...()...)))" + while (s != ""){ + parenTable.push_back(s); + while (s != "" && (s.back() == ')' || pl - 1 < pr + 1)){ + s.back() == ')' ? pr-- : pl --; + s.pop_back(); + } + if (s != ""){ + s.back() = ')'; + pl--, pr++; + while (pl < n){ + s += '('; + pl++; + } + while (pr < n){ + s += ')'; + pr++; + } + } + } + return parenTable; + } +}; + + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From c215c1ad21b0cc58860832dd768e336ca64a987b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BF=97=E5=B3=B0?= <380238062@qq.com> Date: Mon, 2 Jan 2017 08:13:25 +0800 Subject: [PATCH 41/72] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=AF=84=E5=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/14/note_leakey.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/leetcode/14/note_leakey.md b/leetcode/14/note_leakey.md index 03ed1ab..bdfb712 100644 --- a/leetcode/14/note_leakey.md +++ b/leetcode/14/note_leakey.md @@ -50,7 +50,12 @@ public: ``` +## mzf璇勫 +1. 鐩存帴涓婁唬鐮侊紵宸瘎銆備笅娆″啀鍐欐垚杩欐牱灏辨病鏈夊鍝佹嬁浜嗭紝鍝堝搱銆 +2. `if (strs.empty()) return "";` +3. 鎴戣繕鏄缓璁妸鑺辨嫭鍙烽兘鍔犱笂 +4. `i++' 鏀规垚`++i` ## 鏇村 From 3650c701c8e9f2b07ff989c2fea0667aab6b7215 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BF=97=E5=B3=B0?= <380238062@qq.com> Date: Mon, 2 Jan 2017 08:15:33 +0800 Subject: [PATCH 42/72] =?UTF-8?q?=E8=AF=84=E5=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/14/note_Yac836.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/leetcode/14/note_Yac836.md b/leetcode/14/note_Yac836.md index 216a71e..762e7db 100644 --- a/leetcode/14/note_Yac836.md +++ b/leetcode/14/note_Yac836.md @@ -48,10 +48,13 @@ string longestCommonPrefix(vector& strs) { ``` +## mzf璇勫 +1. 缂撳瓨鐨勪笢瑗挎湁鐐瑰浜嗭紝璺戜笅鏉ユ槸澶氬皯ms锛 +2. flag娌″繀瑕侊紝鐪嬩笅鎴戠殑鎻愪氦锛屾垨鑰卍iscuss ## 鏇村 ![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) -PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From 0bd35f474f31f695273ae2474dc8ca3368fd361c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BF=97=E5=B3=B0?= <380238062@qq.com> Date: Mon, 2 Jan 2017 08:26:13 +0800 Subject: [PATCH 43/72] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=A7=A3=E9=A2=98?= =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/14/note_mzf.md | 179 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 leetcode/14/note_mzf.md diff --git a/leetcode/14/note_mzf.md b/leetcode/14/note_mzf.md new file mode 100644 index 0000000..229eec2 --- /dev/null +++ b/leetcode/14/note_mzf.md @@ -0,0 +1,179 @@ +## 链接 + +[https://leetcode.com/problems/longest-common-prefix/](https://leetcode.com/problems/longest-common-prefix/) + +## 题目 + +题目是要求出一个string序列中,所有字符串共有的前缀。 + +比如 ["aa", "ab"], 那么结果就是"a" + +## 释义 + +N.A. + +## 补充描述 + +开始写代码之前可以先设计一些测试用例 + +### 集合长度 + +``` +["aa"]-->"" +[]-->"" +``` + +### 集合内容 + +``` +["aa", "ab"]-->"a" +["aaa", "aab"]-->"aa" +["aa", "bb"]-->"" +``` + +## 代码 + +比较容易想到的是遍历第一个string中的字符,确认后序string中是否也包含这些字符 + +```c++ +string longestCommonPrefix(vector& strs) +{ + if( !strs.size() ) + { + return ""; + } + + if( strs.size() ==1 ) + { + return strs.at(0); + } + + string sFirst = strs.at(0); + + for( int i = 0; i < sFirst.size(); ++i) + { + for( int j = 1; j < strs.size(); ++j ) + { + string sTemp = strs.at(j); + if( sTemp.size() <= i || sTemp[i] != sFirst[i] ) + { + return i == 0 ? "" : sFirst.substr(0, i); + } + } + + } + + return sFirst; +} +``` + +运行之后是13ms,查看细节会发现大部分人的解题时间都控制在了10ms以内。 + +我们需要再看一下哪里能省出些时间来 + +`size()`操作进行了多次,缓存起来试一下? **大神帮忙解答下这里有必要吗?** + +循环内部取到`strs.at(j)`是没必要缓存的,顺序容器的下标操作都是常量级的。 + +同样的,sFirst我们也不缓存了 + +```c++ +string longestCommonPrefix(vector& strs) +{ + auto vecSize = strs.size(); + + if( !vecSize ) + { + return ""; + } + + if( vecSize ==1 ) + { + return strs.at(0); + } + + + for( int i = 0; i < strs.at(0).size(); ++i) + { + for( int j = 1; j < vecSize; ++j ) + { + if( strs.at(j).size() <= i || strs.at(j)[i] != strs.at(0)[i] ) + { + return i == 0 ? "" : strs.at(0).substr(0, i); + } + } + + } + + return strs.at(0); +} + +``` + +6ms,只能击败17.75%。因为大多数人都在6ms + +自己想不出哪里可以优化了,来和高票答案对比一下 + +[https://discuss.leetcode.com/topic/20991/accepted-c-6-lines-4ms/2](https://discuss.leetcode.com/topic/20991/accepted-c-6-lines-4ms/2) + +主要的一个区别应该是在substr上,我们也用他的方式来试下 + +``` +string longestCommonPrefix(vector& strs) { + auto vecSize = strs.size(); + + if( !vecSize ) + { + return ""; + } + + string sPrefix = ""; + for( int i = 0; i < strs.at(0).size(); ++i) + { + for( int j = 1; j < vecSize; ++j ) + { + if( strs.at(j).size() <= i || strs.at(j)[i] != strs.at(0)[i] ) + { + return sPrefix; + } + } + sPrefix += strs.at(0)[i]; + } + + return strs.at(0); +} +``` + +还是6ms,再改的话代码可读性就变差了。 + +果然大神是无法靠近的。。。就这样吧。。。 + +## 更多 + +我们用新学的`string.find`练练手 + +```c++ +string longestCommonPrefix(vector& strs) { + auto vecSize = strs.size(); + + if( !vecSize ) + { + return ""; + } + + string sPrefix = ""; + for( int i = 0; i < strs.at(0).size(); ++i) + { + sPrefix += strs.at(0)[i]; + for( int j = 1; j < vecSize; ++j ) + { + if( strs.at(j).find(sPrefix) != 0 ) + { + return sPrefix.erase(i,1); + } + } + } + + return strs.at(0); +} +``` From 4c745bb87840ea864ca338dad4a4f851143055cb Mon Sep 17 00:00:00 2001 From: Superllb Date: Mon, 2 Jan 2017 16:32:07 +0800 Subject: [PATCH 44/72] Create note_Superllb.md --- leetcode/01/note_Superllb.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 leetcode/01/note_Superllb.md diff --git a/leetcode/01/note_Superllb.md b/leetcode/01/note_Superllb.md new file mode 100644 index 0000000..e30f815 --- /dev/null +++ b/leetcode/01/note_Superllb.md @@ -0,0 +1,32 @@ +#鎻忚堪 +##瑙i鎬濊矾 +###涓鑸潵璇达紝浣跨敤unordered_map锛屽苟灏嗘暟缁勫厓绱犲強鍏冪礌瀵瑰簲鐨勭储寮曞垎鍒綔涓簁ey锛寁alue锛屽彲浠ユ湁鏁堝噺灏忚繍琛屾椂闂达紝浣嗘槸瀛樺湪涓や釜鐤戦棶銆 +* 鍒濆unordered_map涓虹┖锛屼竴杈规煡璇竴杈瑰皢涓嶇鍚堣姹傜殑鏁扮粍鍏冪礌鍜岀储寮曟彃鍏nordered_map涓紝鍥犳锛宎=nums[i]鏃讹紝b鍙兘鍦╪um[0]~nums[i-1]涓煡鎵撅紝杩欎篃瀵艰嚧 +绋嬪簭鍦ㄧ涓娆″惊鐜(i=1)鏃讹紝姘歌繙涓嶄細杈撳嚭缁撴灉锛屼緥濡傦細 + + ``` + input: nums=[1,3,5,7],target=4 + ``` +* 鍋囧鏁扮粍涓紝瀛樺湪鐩稿悓鐨勫厓绱狅紝鎬庝箞鍔烇紵 + +#浠g爜 +```cpp +class Solution { +public: + vector twoSum(vector& nums, int target) { + unordered_map unmap; + vector result; + for (int i = 0; i < nums.size(); ++i) { + int x = target - nums[i]; + auto search = unmap.find(x); + if (search != unmap.end()) { + result.push_back(search->second); + result.push_back(i); + return result; + } + unmap.insert({nums[i],i}); + } + return result; + } +}; +``` From 4aa6df296ce6acd3810e245a04f2c97a6f574878 Mon Sep 17 00:00:00 2001 From: Superllb Date: Tue, 3 Jan 2017 21:57:55 +0800 Subject: [PATCH 45/72] Update note_Superllb.md add a judgement to guarantee these two returned values are not equal --- leetcode/01/note_Superllb.md | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/leetcode/01/note_Superllb.md b/leetcode/01/note_Superllb.md index e30f815..798287a 100644 --- a/leetcode/01/note_Superllb.md +++ b/leetcode/01/note_Superllb.md @@ -1,14 +1,7 @@ #鎻忚堪 ##瑙i鎬濊矾 -###涓鑸潵璇达紝浣跨敤unordered_map锛屽苟灏嗘暟缁勫厓绱犲強鍏冪礌瀵瑰簲鐨勭储寮曞垎鍒綔涓簁ey锛寁alue锛屽彲浠ユ湁鏁堝噺灏忚繍琛屾椂闂达紝浣嗘槸瀛樺湪涓や釜鐤戦棶銆 -* 鍒濆unordered_map涓虹┖锛屼竴杈规煡璇竴杈瑰皢涓嶇鍚堣姹傜殑鏁扮粍鍏冪礌鍜岀储寮曟彃鍏nordered_map涓紝鍥犳锛宎=nums[i]鏃讹紝b鍙兘鍦╪um[0]~nums[i-1]涓煡鎵撅紝杩欎篃瀵艰嚧 -绋嬪簭鍦ㄧ涓娆″惊鐜(i=1)鏃讹紝姘歌繙涓嶄細杈撳嚭缁撴灉锛屼緥濡傦細 - - ``` - input: nums=[1,3,5,7],target=4 - ``` -* 鍋囧鏁扮粍涓紝瀛樺湪鐩稿悓鐨勫厓绱狅紝鎬庝箞鍔烇紵 - +###涓轰簡淇濊瘉绗竴娆℃煡鎵炬湁鏁堬紝閲囩敤鍏堟彃鍐嶆煡銆備箣鍓嶆彁鍒版暟缁勯噷鍙兘浼氭湁閲嶅鍏冪礌锛宐ajdcc鎻愮ず浣跨敤multimap锛岃繍琛岀粨鏋滄樉绀猴紝涓よ呯粨鏋滅浉鍚屻傚彧瑕佽繖涓噸澶嶅厓绱犵鍚堟潯浠讹紝杩斿洖鐨勫缁堟槸绗竴娆″嚭鐜版椂瀵瑰簲鐨勭储寮曘 +###姝ゅ锛屼緥濡俙``nums=[1,3,5,7,9],target=14```锛屽鏋滀笉鍔犲垽鏂紝杩斿洖缁撴灉灏变細鏄痐``[3,3]```,鑰屼笉鏄痐``[3,4]```銆傚鏋滈噰鐢ㄥ厛鏌ュ啀鎻掞紝杩欑鎯呭舰涓嶇敤鑰冭檻銆 #浠g爜 ```cpp class Solution { @@ -17,14 +10,17 @@ public: unordered_map unmap; vector result; for (int i = 0; i < nums.size(); ++i) { - int x = target - nums[i]; - auto search = unmap.find(x); - if (search != unmap.end()) { - result.push_back(search->second); - result.push_back(i); - return result; - } - unmap.insert({nums[i],i}); + //int x = target - nums[i]; + unmap.insert({nums[i],i}); + auto search = unmap.find(target - nums[i]); + if (search != unmap.end()) { + if (i != search->second){ + result.push_back(i); + result.push_back(search->second); + return result; + } + } + //unmap.insert({nums[i],i}); } return result; } From a54aa73db1223ec12a87d2ac079e9cdca82cb7de Mon Sep 17 00:00:00 2001 From: bajdcc Date: Wed, 4 Jan 2017 22:25:54 +0800 Subject: [PATCH 46/72] Add note: longest common prefix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 浜屽垎瑙f硶 --- leetcode/14/note_bajdcc.md | 156 +++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 leetcode/14/note_bajdcc.md diff --git a/leetcode/14/note_bajdcc.md b/leetcode/14/note_bajdcc.md new file mode 100644 index 0000000..c57994e --- /dev/null +++ b/leetcode/14/note_bajdcc.md @@ -0,0 +1,156 @@ +## 閾炬帴 + + +https://leetcode.com/problems/longest-common-prefix/ + + +## 棰樼洰 + +姹傜粰瀹氬瓧绗︿覆鏁扮粍鐨勬渶澶у叕鍏卞墠缂銆 + + + +## 閲婁箟 + +### 甯歌瑙f硶 + +鍙岄噸for寰幆锛屽皢strs鐪嬩綔浜岀淮鏁扮粍锛屾寜鍒楁帹杩涖 + +### 鍙︾被瑙f硶 + +鑴戞礊鍑烘病锛氬彧鑳戒粠宸﹀線鍙抽亶鍘嗭紵閿欎簡锛佽繕鍙互浜屽垎閬嶅巻~ + +璇ユ柟娉曠函灞炲ū涔愶紝鍏跺疄璨屼技澶х害涔熻鍙兘骞朵笉鑳芥彁楂樻晥鐜囷紝鎽旓紒涓嶈繃灏濊瘯涓绉嶆柊鏂规硶鏈皾涓嶅彲锛 + +### 浜屽垎閬嶅巻娉 + +鏉ョ啛鎮変竴涓嬩簩鍒嗘煡鎵緙 + +1. 璁惧畾涓嬬晫lower鍜屼笂鐣寀pper锛岃瀹氫腑鐐筸id=(lower+upper)/2 +2. 鑻ヤ腑鐐瑰艰緝澶э紝鍒欒寖鍥寸缉灏忎负lower~mid-1 +3. 鑻ヤ腑鐐瑰艰緝灏忥紝鍒欒寖鍥寸缉灏忎负mid+1~upper +4. 鑻ヤ腑鐐瑰肩浉绛夛紝鍒欐煡鎵炬垚鍔 +5. 鑻ower>upper锛屽垯鏌ユ壘澶辫触 + +閭d箞杩欓噷鐨勪簩鍒嗛亶鍘嗘槸鎬庢牱鍛紵 + +1. 璁惧畾涓嬬晫lower鍜屼笂鐣寀pper锛岃瀹氫腑鐐筸id=(lower+upper)/2 +2. 鑻ヤ腑鐐瑰煎尮閰嶏紝杩欐椂瑕佺湅lower~mid-1閮ㄥ垎锛涘鏋渓ower~mid-1鍏ㄩ儴鍖归厤锛屽垯缁撴灉鍙栧喅浜巑id+1~upper鐨勫硷紝鍚﹀垯缁撴灉鍙栧喅浜巐ower~mid-1鐨勫 +3. 鑻ヤ腑鐐瑰间笉鍖归厤锛岃繖鏃跺彧瑕佺湅lower~mid-1閮ㄥ垎 + + +### 澶氱嚎绋嬩紭鍖 + +鐪熷疄鎯呭喌涓紝鎴戠寽锛宻trs搴旇鏄竴涓狦绾у埆鐨勫簽鐒跺ぇ鐗┿傘 + +涓轰簡瑙e喅鏁堢巼鐨勯棶棰橈紝闇瑕佽繘琛屽绾跨▼浼樺寲銆 + +甯歌瑙f硶锛氭寜鏈鐭暱搴en锛屽亣濡傛垜鏈4涓嚎绋嬶紝閭d箞姣忎釜绾跨▼鍒嗛厤len/4闀垮害鐨勪换鍔° + +闂鏉ヤ簡鈥斺斿緢涓嶅垢锛3涓嚎绋嬮兘鍋氫簡鏃犵敤鍔熴傘 + +- 绾跨▼1锛歩'm the first!!! oh, 2500000 strings are true! 铔よ洡铔! +- 绾跨▼2锛歠alse on 1st, excuse me??? +- 绾跨▼3锛歠alse on 1st, r u crazy??? +- 绾跨▼4锛歠alse on 1st, what the %^&$??? +- CPU锛歳 u kidding me??? remind me of lian-fa-ke... + +灏村艾锛氬綋绾跨▼2-4闆嗕綋缃㈠伐鐨勬椂鍊欙紝绾跨▼1杩樺湪鍢垮捇鍢垮捇鍦板伐浣滐紝鏉浜虹殑蹇冮兘鏈変簡銆傝繖閲岀嚎绋婣涓嶈兘鎵撴壈绾跨▼B宸ヤ綔銆 + +杩欎釜灏村艾鐨勯棶棰樻槸鏃犳硶瑙e喅鐨勶紝瑕佷箞鎮ㄨ佸垎閰嶄换鍔$殑鏃跺欏ソ濂芥兂鎯筹紵锛 + +濂藉惂锛**鍑忓皬浠诲姟绮掑害**锛屽彲浠ヨВ鍐抽棶棰樸 + +鈥斺斺斺斺斺斺斺斺斺 + +锛堜簩鍒嗚В娉曪級 + +鎬濊矾锛氬厛鐢讳竴涓猯ower-upper鐨勯ゼ锛岃鏌愪釜绾跨▼鍘诲垎楗笺傚畠浠庝腑闂村紑濮嬪垎锛屽皢楗煎垎鎴愪袱鍧楋紙宸﹁竟涓鍧楀ぇ鐨勶紝鍙宠竟涓鍧楀皬鐨勶級锛屽鏋滆繍姘斾笉濂斤紝涓ゅ潡杩樺緱缁х画鍒嗭紱杩愭皵濂斤紝鍙渶瑕佸垎鍓嶉潰涓鍧楋紝鍚庨潰涓鍧楀彲浠ヤ笉瑕佷簡銆傝繍姘斿ソ鐨勬儏鍐典笉璁ㄨ銆傝繍姘斾笉濂界殑鏃跺欙紝闂鏉ヤ簡锛屽彸杈逛竴鍧楀尮閰嶏紒鐒惰屽乏杈逛竴鍧楀け璐ワ紒澶勭悊鍙宠竟涓鍧楃殑绾跨▼锛歟xcuse me???涓嶈繃銆併併佸彸杈逛竴鍧楅ゼ骞朵笉鏄敱鍗曠嫭涓涓嚎绋嬪幓鍒嗙殑锛堥掑綊鍢涳級锛屼篃灏辨槸璇达紝濡傛灉浠诲姟澶辫触锛岄偅涔堢浉褰撲簬鎵鏈夌嚎绋嬮兘鐧界櫧骞蹭簡涓浠惰牏浜嬶紱褰撶劧浜嗭紝濡傛灉浠诲姟椤哄埄锛岀嚎绋嬩滑閮戒細娆㈡榧撹垶銆 + +鎵浠ヤ簩鍒嗘硶鏈変竴涓汉鎯呭懗鐨勫ソ澶勶細**鍚岀敇鍏辫嫤**锛屼豢浣涚嚎绋嬩滑鏈変簡鐢熷懡涓鏍枫傛瘡涓嚎绋嬮兘鐭ラ亾鑷繁鐨勪换鍔¤寖鍥村湪鍙樺緱瓒婃潵瓒婂皬锛岃繖鏍峰氨鑳界湅鍒板笇鏈涳紒 + +浠庡揩閫熷畾鐣屾柟闈㈢湅锛屽父瑙勮В娉曟槸涓娆℃у嚭涓婄晫锛岃屼簩鍒嗚В娉曞彲浠ユ笎娓愮缉灏忎笂鐣岃寖鍥淬 + +浠庡绾跨▼鏂归潰鐪嬶紝浜屽垎娉曠矑搴︽槸1锛屽父瑙勬硶鍙互>=1銆 + +鎬荤粨锛屼簩鍒嗘硶姣斿父瑙勬硶澶氬仛浜嗕竴浠朵簨浠讹細**鎺㈡祴涓婄晫鑼冨洿**锛**閫傜敤浜庡揩閫熶及绠**銆備负浠涔堝啓浜屽垎娉曪紝鍥犱负闂层傘 + +鈥斺斺斺斺斺斺斺斺斺 + +鍋囧瑕佸仛涓繘搴︽潯锛岀敤甯歌瑙f硶锛屼粠宸﹀埌鍙宠鏉★紝涓嶇煡閬撲粈涔堟椂鍊欑獊鐒跺氨婊′簡锛岀瓑寰呮椂闂村幓闂枦瀹氳皵鐨勭尗鍚э紱鐢ㄤ簩鍒嗚В娉曪紝鍙傝儀p鐨勭鐩樼鐗囨暣鐞嗭紝鍙互鍦ㄦ病鏈夐亶鍘嗙粨鏉熷墠缂╁皬鑼冨洿銆 + +## 琛ュ厖鎻忚堪 + +杩愯鏃堕棿鐪嬩汉鍝佺郴鍒楋細https://leetcode.com/submissions/detail/87863222/ + + + + +## 浠g爜 + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 +#include +#include + +using namespace std; + +class Solution { +public: + string longestCommonPrefix(vector &strs) { + if (strs.empty()) + return ""; + if (strs.size() == 1) + return strs[0]; + long min_len = -1; + int min_idx = -1; + int i = 0; + for (auto &s : strs) { + if (s.length() < min_len) { + min_len = s.length(); + min_idx = i; + } + i++; + } + if (min_len == 0) + return ""; + auto min_str = strs[min_idx]; + return min_str.substr(0, 1 + getMax(strs, min_str, 0, min_len - 1)); + } + + long getMax(const vector &vec, const string &str, long lower, long upper) { + if (lower == upper) + return can(vec, str[lower], lower) ? lower : -1; + auto mid = (lower + upper) / 2; + if (!can(vec, str[mid], mid)) { + return lower == mid ? -1 : getMax(vec, str, lower, mid - 1); + } + if (lower == mid) { + auto up = getMax(vec, str, mid + 1, upper); + return mid == upper ? upper : (up == -1 ? mid : up); + } + auto lo = getMax(vec, str, lower, mid - 1); + if (lo == mid - 1) { + auto up = getMax(vec, str, mid + 1, upper); + return mid == upper ? upper : (up == -1 ? mid : up); + } + return lo; + } + + bool can(const vector &vec, const char &ch, long idx) { + for (auto &s : vec) { + if (s[idx] != ch) + return false; + } + return true; + } +}; +``` + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From b19f58a04b3a2d528f986e30e4e771e31f57ef50 Mon Sep 17 00:00:00 2001 From: JosanSun <1030460698@qq.com> Date: Wed, 4 Jan 2017 23:04:38 +0800 Subject: [PATCH 47/72] Upload my Code 003 this is my code, please check --- leetcode/03/note_Josan.md | 97 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 leetcode/03/note_Josan.md diff --git a/leetcode/03/note_Josan.md b/leetcode/03/note_Josan.md new file mode 100644 index 0000000..9b886e5 --- /dev/null +++ b/leetcode/03/note_Josan.md @@ -0,0 +1,97 @@ +## 閾炬帴 + +銆愪腑绛夐毦搴︺3. Longest Substring Without Repeating Characters 锛堟渶闀夸笉閲嶅瀛愪覆锛 + +https://leetcode.com/problems/longest-substring-without-repeating-characters/ + + +## 棰樼洰 + +Given a string聽**s**, find the longest palindromic substring in聽**s**. You may assume that the maximum length of聽**s**聽is 1000. + +**Example:** + +``` +Input: "babad" + +Output: "bab" + +Note: "aba" is also a valid answer. + +``` + +**Example:** + +``` +Given a string, find the length of the longest substring without repeating characters. + +Examples: + +Given "abcabcbb", the answer is "abc", which the length is 3. + +Given "bbbbb", the answer is "b", with the length of 1. + +Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring. +``` + +*Tags: Hash Table, Two Pointers* + +## 閲婁箟 + +鏈闀夸笉閲嶅瀛愪覆 + + +## 琛ュ厖鎻忚堪 + +杩愮敤鍔ㄦ佽鍒掕繘琛屾眰瑙c +dp[i]鏄寚瀛楃涓蹭粠澶村埌s[i]鎵鎵惧埌鐨勬渶闀挎棤閲嶅瀛愬瓧绗︿覆闀垮害銆傚洜姝わ紝绛旀dp[s.size()-1]灏辨槸鎵瑕佺殑缁撴灉銆傛湡闂寸敤鍒颁簡杈呭姪鏁扮粍鏍囪璁块棶浣嶃 +鏃堕棿澶嶆潅搴︿负O(n*n)銆 + + + +## 浠g爜 + +```C++ +int flag[128] = { 0 }; +int getMax(int x, int y) +{ + return x > y?x:y; +} +void init(int* flag, int num) +{ + for(int i = 0; i != num; ++i) + { + flag[i] = 0; + } +} +class Solution +{ + public: + int lengthOfLongestSubstring(string s) + { + if(s.size() == 0) { return 0; } + vector dp(s.size(), 1); + for(int i = 1; i != s.size(); ++i) + { + int j = i; + int len = 0; + //鍚戝墠寮濮嬫煡鎵撅紝鎵惧埌浠[i]缁撳熬鐨勬渶闀挎棤閲嶅瀛愬簭鍒 + for(; (j >= 0) && (flag[s[j]] == 0); --j) + { + flag[s[j]] = 1; + ++len; + } + dp[i] = getMax(dp[i - 1], len); + init(flag, 128); + } + return dp[s.size() - 1]; + } +}; +``` + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From eb0716ed98eb0c5720770d83d44fe7951b14a5b7 Mon Sep 17 00:00:00 2001 From: JosanSun <1030460698@qq.com> Date: Wed, 4 Jan 2017 23:14:01 +0800 Subject: [PATCH 48/72] Upload my code 005 This is my code , please check --- leetcode/05/note_Josan.md | 150 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 leetcode/05/note_Josan.md diff --git a/leetcode/05/note_Josan.md b/leetcode/05/note_Josan.md new file mode 100644 index 0000000..c8973ab --- /dev/null +++ b/leetcode/05/note_Josan.md @@ -0,0 +1,150 @@ +## 閾炬帴 + +銆愪腑绛夐毦搴︺5. Longest Palindromic Substring 锛堟渶闀垮洖鏂囧瓙涓诧級 + +https://leetcode.com/problems/longest-palindromic-substring/ + + +## 棰樼洰 + +Given a string聽**s**, find the longest palindromic substring in聽**s**. You may assume that the maximum length of聽**s**聽is 1000. + +**Example:** + +``` +Input: "babad" + +Output: "bab" + +Note: "aba" is also a valid answer. + +``` + +**Example:** + +``` +Input: "cbbd" + +Output: "bb" +``` + +## 閲婁箟 + +鏈闀垮洖鏂囧瓙涓 + + +## 琛ュ厖鎻忚堪 + +鑷繁閲囩敤鐨勬槸Brute Force锛岀劧鍚庨氳繃涓嶆柇浼樺寲绠楁硶锛屽己琛孉C銆備竴娆℃浼樺寲鐨勮繃绋嬶紝鐪熺殑寰堟湁鎴愬氨鎰熴 +鎬濊矾寰堢畝鍗曪紝灏辨槸閲囩敤涓ゆ寚閽堬紝浠庡熬寮濮嬪垽鍒紝濡傛灉鏄洖鏂囨暟锛屽摢鑲畾鏄偅娆″惊鐜綋浣滄渶闀跨殑鍥炴枃瀛愬瓧绗︿覆锛屽洜姝ょ洿鎺reak;杩涘叆涓嬩釜寰幆銆 + + + + +## 浠g爜 + +涓寮濮嬬▼搴忓涓嬶細杩欎簺閮戒細鍑虹幇LTE閿欒锛屼富瑕佹槸娴嬭瘯鐢ㄤ緥缁欑殑string寰堝ぇ銆 + +```c++ +class Solution +{ + public: + string longestPalindrome(string s) + { + if(s.size() == 0) { return{}; } + int max = 0; + string res; + for(int i = 0; i != s.size(); ++i) + { +//鐢变簬j姣忔閮芥槸鏌ョ湅鏈鍚庝竴涓綅缃紝鍏跺疄鍙渶瑕佷粠绛変簬s[i]鐨勬渶鍚庝竴涓綅缃紑濮 + for(auto j = s.size(); j != i; --j) + { + if(isPalindrome(s, i, j)) + { + if(j - i > max) + { + max = j - i; +//鍙互涓嶉渶瑕佹瘡娆″彇鍑簊ubstr,鍙互鐩存帴璁板綍璧峰浣嶇疆灏辫锛涘洜涓烘湁涓猰ax锛屾墍浠ュ彲浠ュ氨璁板紑濮嬩綅缃氨濂 + res = s.substr(i, j - i); + } + break; + } + } + } + return res; + } + //杩欎釜鍙互浼樺寲锛屼笉闇瑕佸叏閮ㄥ掑簭灏辫兘鍒ゅ埆鏄惁鏄洖鏂囧瓧绗︿覆 + bool isPalindrome(string s, int beg, int end) + { + string tem = s.substr(beg, end - beg); + reverse(tem.begin(), tem.end()); + return s.substr(beg, end - beg) == tem?true:false; + } +}; + +涔嬪悗锛屾敼涓 +//NOTE锛氬浜庡瓧绗︿覆鐨勫彇鍊硷紝鍙互鐩存帴s[beg],杩欐牱鏄惧緱寰堟暣娲侊紝铏界劧涔熸槸绗﹀彿閲嶈浇涓鸿凯浠e櫒瀹炵幇鐨 +bool isPalindrome(string s, int beg, int end) + { + while((end > beg) && *(s.begin() + beg) == *(s.begin() + end - 1)) + { + ++beg; + --end; + } + if(end > beg) return false; + else + { + return true; + } + } +``` +閫氳繃鍑犳浼樺寲锛屽彲浠ュ媺寮洪氳繃绋嬪簭锛屼笉杩囨椂闂磋姳璐规瘮杈冨ぇ锛509ms. + +```c++ +class Solution +{ + public: + string longestPalindrome(string s) + { + if(s.size() == 0) { return{}; } + int max = 0; + int beg = 0; + for(int i = 0; i != s.size(); ++i) + { + for(auto j = s.size(); j != i; --j) + { + j = s.find_last_of(s[i], j); + //鑲畾浼氭壘鍒癹鐨勶紝鏈鍧忕殑鎯呭喌j==i;瀵逛簬鎵惧埌鐨勫瓙瀛楃涓插鏋滈暱搴<=max,閭d箞鐩存帴璺冲嚭寰幆 + if(j + 1 - i <= max) { break; } + if(isPalindrome(s, i, j + 1)) + { + max = j + 1 - i; + beg = i; + break; + } + } + } + return s.substr(beg, max); + } + bool isPalindrome(string s, int beg, int end) + { + while((end > beg) && s[beg] == s[end - 1]) + { + ++beg; + --end; + } + if(end > beg) return false; + else + { + return true; + } + } +}; +``` + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From bc71d0ad591141528b711a7878c48e37cb3680f5 Mon Sep 17 00:00:00 2001 From: JosanSun <1030460698@qq.com> Date: Thu, 5 Jan 2017 08:29:36 +0800 Subject: [PATCH 49/72] Upload my code 009 This is my code. Plz check; --- leetcode/09/note_Josan.md | 97 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 leetcode/09/note_Josan.md diff --git a/leetcode/09/note_Josan.md b/leetcode/09/note_Josan.md new file mode 100644 index 0000000..85ddcb7 --- /dev/null +++ b/leetcode/09/note_Josan.md @@ -0,0 +1,97 @@ +## 閾炬帴 + + +https://leetcode.com/problems/palindrome-number/ + + +## 棰樼洰 + + +鍒ゆ柇涓涓32浣嶆暣鍨嬫槸鍚︽槸鍥炴枃鏁般 + + +## 琛ュ厖鎻忚堪 + +棣栧厛鍙栧嚭鍓嶅悗涓や釜鏁板瓧锛岀劧鍚庢瘮杈冭繖涓や釜鏁板瓧锛涗箣鍚庯紝鍘绘帀杩欎袱涓暟瀛楀悗锛岀户缁瘮杈冦傝繖鏍峰彲浠ラ槻姝㈠嚭鐜版孩鍑虹幇璞° + + + + +## 浠g爜 + +1銆佽嚜宸辩殑绋嬪簭锛 +```C++ +class Solution +{ + public: + bool isPalindrome(int x) + { + if(x < 0) return false; + if(x == 0) return true; + int digits = 0; + int tem = x; + digits = int(log10(x)); + tem = x; + while((digits > 0) && + (tem % 10) == (tem / int(pow(10, digits)))) + { + tem -= tem % 10 + (tem % 10)*int(pow(10, digits)); + tem /= 10; + digits -= 2; + } + return (digits <= 0)?true:false; + } +}; +``` + +2銆佸埆浜虹殑绋嬪簭 +瀵硅嚜宸辩▼搴忕殑鏀硅繘鐗 + +```C++ +class Solution +{ + public: + bool isPalindrome(int x) + { + if(x < 0) return false; + int d = 1; // divisor + while(x / d >= 10) d *= 10; + while(x > 0) + { + int q = x / d; // quotient + int r = x % 10; // remainder + if(q != r) return false; + x = x % d / 10; + d /= 100; + } + return true; + } +}; +``` + +杩欓鏈濂界殑瑙i鏂规硶锛屼竴涓彧闇瑕侀亶鍘嗕竴鍗婏紝灏卞彲浠 +```C++ +class Solution +{ + public: + bool isPalindrome(int x) + { +//涓涓竟鐣屾潯浠堕兘涓嶈兘灏 + if(x < 0 || (x != 0 && x % 10 == 0)) return false; + int sum = 0; + while(x > sum) + { + sum = sum * 10 + x % 10; + x = x / 10; + } + return (x == sum) || (x == sum / 10); + } +}; +``` + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From 00392cc7dcf8bd5ebce666d254609b2e2fd8a41e Mon Sep 17 00:00:00 2001 From: JosanSun <1030460698@qq.com> Date: Thu, 5 Jan 2017 08:42:59 +0800 Subject: [PATCH 50/72] Upload my code 014 This my code, please check --- leetcode/14/note_Josan.md | 128 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 leetcode/14/note_Josan.md diff --git a/leetcode/14/note_Josan.md b/leetcode/14/note_Josan.md new file mode 100644 index 0000000..ba5127f --- /dev/null +++ b/leetcode/14/note_Josan.md @@ -0,0 +1,128 @@ +## 閾炬帴 + +[https://leetcode.com/problems/longest-common-prefix/](https://leetcode.com/problems/longest-common-prefix/) + +## 棰樼洰 + +棰樼洰鏄姹傚嚭涓涓猻tring搴忓垪涓紝鎵鏈夊瓧绗︿覆鍏辨湁鐨勫墠缂銆 + +姣斿 ["aa", "ab"], 閭d箞缁撴灉灏辨槸"a" + +## 閲婁箟 + +N.A. + +## 琛ュ厖鎻忚堪 + + + +#鑷繁鐨勮В棰樻濊矾锛堢旱鍚戯級 + +渚濇姣旇緝姣忎釜瀛楃涓茬殑鐩稿悓搴忓彿鐨勫瓧绗︼紝濡傛灉閮界浉鍚岋紝姣旇緝涓嬩竴涓紱濡傛灉涓嶅悓锛岃烦鍑哄惊鐜 + + +#鍒汉鐨勮В棰樻濊矾锛堟í鍚戯級 + +姣忎袱涓瓧绗︿覆鎵惧嚭鏈闀跨殑鍏叡瀛愪覆锛岀劧鍚庡緱鍑烘渶闀跨殑鍏叡瀛愪覆銆 + + +## 浠g爜 + +1銆佽嚜宸辩殑绋嬪簭锛 +```C++ +class Solution +{ + public: + string longestCommonPrefix(vector& strs) + { + int n = strs.size(); + if(n == 0) return{}; + string res; + int min = INT_MAX; + for(int i = 0; i != n; ++i) + { + min = int((strs.begin() + i)->size()) < min + ?int((strs.begin() + i)->size()):min; + } + for(int i = 0; i < min; ++i) + { + int j = 1; + for(; j < n; ++j) + { + if(strs[j - 1][i] != strs[j][i]) + { + break; + } + } + if(j < n) { break; } + else + { + res.push_back(strs[0][i]); + } + } + return res; + } +}; +``` + +鑷繁鏀硅繘鐗堬紝閫熷害鏈夋墍鎻愬崌銆傚幓鎺変复鏃跺瓨鍌ㄥ瓧绗︿覆鐨剅es锛屽洜涓哄彧闇瑕佹渶鍚庝竴娆″彇鍑哄氨琛屻 +```C++ +class Solution +{ + public: + string longestCommonPrefix(vector& strs) + { + int n = strs.size(); + if(n == 0) return{}; + int min = INT_MAX; + for(int i = 0; i != n; ++i) + { + min = int((strs.begin() + i)->size()) < min + ?int((strs.begin() + i)->size()):min; + } + int i = 0; + for(; i < min; ++i) + { + int j = 1; + for(; j < n; ++j) + { + if(strs[j - 1][i] != strs[j][i]) + { + break; + } + } + if(j < n) { break; } + } + return strs[0].substr(0, i); + } +}; +``` + +2銆佸埆浜虹殑绋嬪簭 + +```C++ +class Solution +{ + public: + string longestCommonPrefix(vector &strs) + { + if(strs.empty()) return ""; + int right_most = strs[0].size() - 1; + for(size_t i = 1; i < strs.size(); i++) + for(int j = 0; j <= right_most; j++) + if(strs[i][j] != strs[0][j]) + right_most = j - 1; + return strs[0].substr(0, right_most + 1); + } +}; +``` + + + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From b616348c24c06ec262012534d83ddb5171ffaf8f Mon Sep 17 00:00:00 2001 From: bajdcc Date: Thu, 5 Jan 2017 11:25:38 +0800 Subject: [PATCH 51/72] Add note: longest pali.. substring MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 鍋氫簡涓ゅ浼樺寲銆 --- leetcode/05/note_bajdcc.md | 211 +++++++++++++++++++++++++++++++++++++ 1 file changed, 211 insertions(+) create mode 100644 leetcode/05/note_bajdcc.md diff --git a/leetcode/05/note_bajdcc.md b/leetcode/05/note_bajdcc.md new file mode 100644 index 0000000..7d0cc36 --- /dev/null +++ b/leetcode/05/note_bajdcc.md @@ -0,0 +1,211 @@ +## 閾炬帴 + +銆愪腑绛夐毦搴︺5. Longest Palindromic Substring 锛堟渶闀垮洖鏂囧瓙涓诧級 + +https://leetcode.com/problems/longest-palindromic-substring/ + + +## 棰樼洰 + +Given a string聽**s**, find the longest palindromic substring in聽**s**. You may assume that the maximum length of聽**s**聽is 1000. + +**Example:** + +``` +Input: "babad" + +Output: "bab" + +Note: "aba" is also a valid answer. +``` + +**Example:** + +``` +Input: "cbbd" + +Output: "bb" +``` + + + +## 閲婁箟 + +姹傜粰瀹氫覆鐨勬渶闀垮洖鏂囧瓙涓 + +### 绠鍗曟濊矾 + +浠庡乏寰鍙虫壘鍥炴枃涓诧紝鏈鍚庣粰涓渶闀跨殑銆 + +绠鍗曟柟娉曚笉绠鍗曘傘傛庝箞鎵惧洖鏂囦覆鏄釜闂锛屼粠杈逛笂寰涓棿鎵捐偗瀹氫笉琛岋紝鍥炴枃涓茬殑鐗圭偣鏄袱杈瑰绉帮紝鎵浠ュ簲璇ヤ粠瀵圭О鏂归潰鐫鎵嬨 + +### 浼樺寲鎬濊矾 + +棣栧厛锛屼竴涓洖鏂囦覆鐨勫瓧绗﹂搴﹀簲璇ユ槸锛氫腑鐐归搴︽渶浣庝负1锛屽叾浠栧瓧绗﹂搴︽渶浣庝负2銆 + +閭d箞锛**濡傛灉涓蹭腑鏈夐搴︽槸1鐨勫瓧绗︼紝瀹冭偗瀹氫綅浜庡洖鏂囦覆鐨勪腑蹇冿紝涓嶇劧灏变笉灞炰簬浠讳綍鍥炴枃涓**銆 + +鍥犳锛屾寜棰戝害鍙互绛涢夋帀涓瀹氶噺鐨勫浣欏瓧绗︼紝灏嗘瘝涓茶繘琛**鍒嗗壊**銆傚垎鍓茬殑濂藉鏄**瀛愪覆鏈夌晫**銆 + +鏈鎳掓柟娉曪細閬嶅巻鏁翠覆锛屼粠涓績鍚戜袱渚ф墿寮犲苟鍋氭瘮杈冿紝鍙栧緱闀垮害锛屾渶鍚庤繑鍥炴渶澶ч暱搴︽墍鍦ㄧ殑涓层 + +浼樺寲锛 + +- 鍦ㄩ亶鍘嗘暣涓茶繃绋嬩腑锛屾渶澶ч暱搴axlen浼**鏃跺埢澧炲姞**锛岄偅涔堬紝褰撳垎鍓插悗鐨**鏈夌晫瀛愪覆**闀垮害灏忎簬鏈澶ч暱搴axlen鏃讹紝灏变笉闇瑕佸啀鍘诲垽鏂簡銆 +- 濡傛灉涓茬殑鏌愪釜杩炵画瀛愪覆(len>=2)瀹冧滑鐨勯搴﹂兘鏄1锛岄偅涔堝畠浠氨**涓嶅睘浜庝换浣曞洖鏂囦覆**锛屽彲浠ュ揩閫熷墧闄わ紝鑺傜渷鏃堕棿銆傝繖鏄叧閿 +- 鍏朵粬鏂规硶杩樻病鎯冲埌锛屾杩庤ˉ鍏呫 + +## 琛ュ厖鎻忚堪 + +debug涓ゅぉ锛宼ime=9ms https://leetcode.com/submissions/detail/87484290/ + +浠g爜鏄负浜嗛獙璇佹濊矾鏄纭殑銆備笉寰椾笉鍚愭Ыleetcode鐨則est case浜嗭紝瀹冨浠g爜涓殑浼樺寲鏂规硶瑙嗚屼笉瑙 :( + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 + +#include + +using namespace std; + +class Solution { + int count[256]; + +public: + string longestPalindrome(string str) { + int start = 0, end = str.length() - 1; + if (longestPalindromePartition(str.c_str(), start, end, 1) > 1) { + return str.substr(start, end - start + 1); + } else { + return str.substr(0, end < start ? 0 : 1); + } + } + + int longestPalindromePartition(const char *str, int &start, int &end, int maxLen) { + if (end - start + 1 <= maxLen) + return 1; + int maxStart, maxEnd, len = maxLen; + int reserve = (maxLen + 1) / 2; + int reserveEnd = end - reserve + 1; + for (int i = start + reserve - 1, j, k; i <= reserveEnd; ++i) { + if (str[i] == str[i + 1]) { + for (j = i + 1, k = 1; i - k >= start && j + k <= end; ++k) { + if (str[i - k] != str[j + k]) + break; + } + k--; + if (j - i + 1 + k * 2 > len) { + len = j - i + 1 + k * 2; + reserve = (len + 1) / 2; + reserveEnd = end - reserve + 1; + maxStart = i - k; + maxEnd = j + k; + } + } + for (j = reserve; j > 0; --j) { + if (str[i - j] != str[i + j]) + break; + } + if (j == 0) { + for (j = reserve + 1; i - j >= start && i + j <= end; ++j) { + if (str[i - j] != str[i + j]) + break; + } + reserve = --j; + reserveEnd = end - reserve; + if (j * 2 + 1 > len) { + len = j * 2 + 1; + maxStart = i - j; + maxEnd = i + j; + } + } + } + if (maxLen < len) { + start = maxStart; + end = maxEnd; + return len; + } + return 1; + } + + int longestPalindrome(const char *str, int &start, int &end) { + if (start == end) + return 1; + for (int i = 0; i <= 26; ++i) { + count['a' + i] = 0; + } + int len = end - start + 1; + for (int i = start; i <= end; ++i) { + count[str[i]]++; + } + auto dups = new int[len]; + int dupCount = 0; + for (int i = start; i <= end; ++i) { + dups[i] = count[str[i]] > 1 ? (dupCount++, 1) : 0; + if (dups[i] == len) + return len; + } + int tmpStart = -1, tmpEnd = -1, maxStart = -1, maxEnd = -1, tmpLen, maxLen = 1; + for (int i = start; i <= end; ++i) { + if (dups[i]) { + tmpStart = i++; + for (; i <= end; ++i) { + if (!dups[i]) { + tmpEnd = i - 1; + break; + } + } + if (i-- > end) { + tmpEnd = end; + } + if ((tmpLen = longestPalindromePartition(str, tmpStart, tmpEnd, maxLen)) > maxLen) { + maxLen = tmpLen; + maxStart = tmpStart; + maxEnd = tmpEnd; + } + } else { + tmpStart = i; + for (; i <= end; ++i) { + if (dups[i]) { + tmpEnd = i - 1; + break; + } + } + if (i-- > end) { + tmpEnd = end; + } + if (tmpStart == tmpEnd) { + while (tmpStart >= start && tmpEnd <= end && str[--tmpStart] == str[++tmpEnd]); + tmpLen = (--tmpEnd) - (++tmpStart) + 1; + if (tmpLen > maxLen) { + maxLen = tmpLen; + maxStart = tmpStart; + maxEnd = tmpEnd; + } + } + } + } + start = maxStart; + end = maxEnd; + delete[]dups; + return maxLen; + } +}; +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file From f92751ba4817bc462ed7a095f1b70ea18f14a451 Mon Sep 17 00:00:00 2001 From: BigBrother007 <717354154@qq.com> Date: Thu, 5 Jan 2017 16:23:09 +0800 Subject: [PATCH 52/72] =?UTF-8?q?Update=20=E9=94=99=E8=AF=AF=E8=A1=A8.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 鏄笉鏄炕璇戦敊浜嗭紵杩樻槸鍓嶇疆閫掑杩愮畻绗︽槸 i++? --- "primer/\351\224\231\350\257\257\350\241\250.md" | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git "a/primer/\351\224\231\350\257\257\350\241\250.md" "b/primer/\351\224\231\350\257\257\350\241\250.md" index 164c0f0..6777f16 100644 --- "a/primer/\351\224\231\350\257\257\350\241\250.md" +++ "b/primer/\351\224\231\350\257\257\350\241\250.md" @@ -2,12 +2,16 @@ ## P330 -表9.19 +琛9.19 -> q.pop() 返回queue的首元素或(),但不删除此元素 +> q.pop() 杩斿洖queue鐨勯鍏冪礌鎴栵紙锛夛紝浣嗕笉鍒犻櫎姝ゅ厓绱 -参考cppreference +鍙傝僣ppreference http://en.cppreference.com/w/cpp/container/queue/pop -pop执行**删除首元素**的操作。 +pop鎵ц**鍒犻櫎棣栧厓绱**鐨勬搷浣溿 +##P470 +缁冧範 13.41 + +>涓轰粈涔堝湪construct璋冪敤涓娇鐢**鍓嶇疆**閫掑杩愮畻绗︼紵 From 1f1e5680040ec6c38155224a820bcef25d3c7419 Mon Sep 17 00:00:00 2001 From: JosanSun <1030460698@qq.com> Date: Wed, 11 Jan 2017 16:35:51 +0800 Subject: [PATCH 53/72] Upload my code 019 This is my code. please check! --- leetcode/19/note_Josan019.md | 77 ++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 leetcode/19/note_Josan019.md diff --git a/leetcode/19/note_Josan019.md b/leetcode/19/note_Josan019.md new file mode 100644 index 0000000..736199b --- /dev/null +++ b/leetcode/19/note_Josan019.md @@ -0,0 +1,77 @@ +## 閾炬帴 + + +https://leetcode.com/problems/remove-nth-node-from-end-of-list/ + + +## 瑙i鎬濊矾 + +1. 鍏堥亶鍘嗕竴涓猵re锛屼綔涓轰睛鎺㈠叺銆 +2. 涓娆¢亶鍘嗭紝鍙pre涓篘ULL锛屽氨鍙互鎵惧嚭瑕佸垹闄ょ殑鍏冪礌鐨勫墠涓涓綅缃紝鍗充负res銆 +3. 鍒犻櫎鍏冪礌 + +***娉ㄦ剰*** + +瑕佸垹闄ゅ厓绱犱负绗竴涓綅缃椂锛岃繑鍥炲奸棶棰樸 + + + +## 浠g爜 + + + +```c++ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution +{ + public: + ListNode* removeNthFromEnd(ListNode* head, int n) + { + if (n < 0||head==NULL||head->next==NULL) return NULL; + + //pre琛ㄧず浠巋ead寮濮嬮亶鍘嗙n涓粨鐐癸紝鎰忔濆氨鏄痯re鍦╮es涔嬪墠n+1涓粨鐐 + //閭d箞褰損re鎸囧悜NULL鏃讹紝res姝eソ鏄掓暟绗琻涓粨鐐圭殑鍓嶇疆缁撶偣銆 + ListNode* pre = head; + while (n&&pre) + { + pre = pre->next; + --n; + } + + //n!=0琛ㄧずn鐨勫ぇ浜庨摼琛ㄧ殑闀垮害锛屾姤閿 + if(n!=0) return NULL; + + //flag鐢ㄦ潵鏍囪瘑鏄惁鍒犻櫎鐨勬槸澶存寚閽堬紝濡傛灉鏄紝鍒欎负true + bool flag = (n == 0 && pre == NULL); + ListNode* dumpHead = new ListNode(0); + dumpHead->next = head; + ListNode* res = dumpHead; + while (pre) + { + pre = pre->next; + res = res->next; + } + //鍒犻櫎 + res->next = res->next->next; + if (flag) head = res->next; + delete dumpHead; + return head; + } +}; + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From d363f7efe4d47ebc10f1a4c9cac861c9f6704ebb Mon Sep 17 00:00:00 2001 From: JosanSun <1030460698@qq.com> Date: Wed, 11 Jan 2017 16:46:56 +0800 Subject: [PATCH 54/72] Upload my code 020 please check --- leetcode/20/note_Josan020.md | 118 +++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 leetcode/20/note_Josan020.md diff --git a/leetcode/20/note_Josan020.md b/leetcode/20/note_Josan020.md new file mode 100644 index 0000000..b3445c7 --- /dev/null +++ b/leetcode/20/note_Josan020.md @@ -0,0 +1,118 @@ +## 閾炬帴 + + +https://leetcode.com/problems/valid-parentheses/ + +## 瑙i鎬濊矾 + +鍏稿瀷鐨剆tack鐨勭鍙烽厤瀵归棶棰樸 + +娉ㄦ剰涓浜涚壒娈婄殑娴嬭瘯鐢ㄤ緥灏卞ソ +()( ) ()() [ + + +## 浠g爜 + +```c++ +class Solution { +public: + bool isValid(string s) { + if(s.size( ) == 0) + { + return false; + } + stack st; + int i = 0; + //鍔犲叆@涓昏鏄负浜嗗綋stack涓虹┖鏃讹紝浠ラ槻鍑虹幇鍙杝t.top()鐨勬姤閿 + //鐩稿簲娴嬭瘯鐢ㄤ緥锛宔g: ) + st.push('@'); + while(!st.empty( )&&i!=s.size()){ + switch(s[i]){ + case '(': + case '[': + case '{': + st.push(s[i++]); + break; + case ')': + if(st.top( ) != '('){ + return false; + } + else{ + st.pop( ); + ++i; + } + break; + case ']': + if(st.top( ) != '['){ + return false; + } + else{ + st.pop( ); + ++i; + } + break; + case '}': + if(st.top( ) != '{'){ + return false; + } + else{ + st.pop( ); + ++i; + } + break; + //鍑虹幇鍏朵粬鏃犳晥瀛楃 + default: return false; + } + } + return (st.size( ) == 1) ? true : false; + } +}; +``` + +涓婇潰鐨勭▼搴忔瘮杈冪箒鐞愶紝鑰屼笖瀵筩ase ]})鐨勬渚嬫棤娉曡繘琛岀粺涓 + +鍙互鍙傝冭繖涓敼杩涚増 +```c++ +class Solution { +public: + bool isValid(string s){ + //suppose null string is illegal + if(s.size( ) == 0){ + return false; + } + stack st; + map cm{ {')','(' },{']','[' },{'}','{' } }; + for(int i = 0; i != s.size( ); ++i){ + switch(s[i]){ + case '(': + case '[': + case '{': + st.push(s[i]); + break; + case ')': + case ']': + case '}': + if(st.empty( ) || st.top( ) != cm[s[i]]){ + return false; + } + else{ + st.pop( ); + break; + } + //if illegal character appears, like @ # $ % a ... , return false + default: + return false; + } + } + return st.empty( ); + } +}; +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From da19d34398132ba1188133f8264e7d16c25f806a Mon Sep 17 00:00:00 2001 From: JosanSun <1030460698@qq.com> Date: Wed, 11 Jan 2017 16:55:34 +0800 Subject: [PATCH 55/72] Upload my code 021 Please check --- leetcode/21/note_Josan021.md | 85 ++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 leetcode/21/note_Josan021.md diff --git a/leetcode/21/note_Josan021.md b/leetcode/21/note_Josan021.md new file mode 100644 index 0000000..32f037e --- /dev/null +++ b/leetcode/21/note_Josan021.md @@ -0,0 +1,85 @@ +## 閾炬帴 + + +https://leetcode.com/problems/merge-two-sorted-lists/ + + +## 瑙i鎬濊矾 + +鍏堝缓绔嬩竴涓ご鎸囬拡锛岀劧鍚庝緷娆℃寜椤哄簭鎻掑叆杩欎釜澶存寚閽堢殑鍚庨潰銆 + + + +## 浠g爜 + +```c++ +/** +* Definition for singly-linked list. +* struct ListNode { +* int val; +* ListNode *next; +* ListNode(int x) : val(x), next(NULL) {} +* }; +*/ +class Solution +{ + public: + ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) + { + if (l1 == NULL) + { + return l2; + } + if (l2 == NULL) + { + return l1; + } + ListNode head(0); + ListNode* res = &head; + ListNode* p = res; + + while (l1&&l2) + { + if (l1->val < l2->val) + { + p->next = l1; + l1 = l1->next; + } + else + { + p->next = l2; + l2 = l2->next; + } + p = p->next; + } + p->next = l1 ? l1 : l2; + return res->next; + } +}; +``` +缃戜笂鐪嬪埌涓涓瘮杈冩湁鎰忔濈殑瑙f硶锛 +閫掑綊鐗 +```c++ +class Solution { +public: + ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { + if(l1 == NULL) return l2; + if(l2 == NULL) return l1; + + if(l1->val < l2->val) { + l1->next = mergeTwoLists(l1->next, l2); + return l1; + } else { + l2->next = mergeTwoLists(l2->next, l1); + return l2; + } + } +}; +``` + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From a9754938b1782fe68d79d595db84a6eaebba387d Mon Sep 17 00:00:00 2001 From: JosanSun <1030460698@qq.com> Date: Wed, 11 Jan 2017 17:05:09 +0800 Subject: [PATCH 56/72] Upload my code 022 Please check. --- leetcode/22/note_Josan022.md | 80 ++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 leetcode/22/note_Josan022.md diff --git a/leetcode/22/note_Josan022.md b/leetcode/22/note_Josan022.md new file mode 100644 index 0000000..d3cd97c --- /dev/null +++ b/leetcode/22/note_Josan022.md @@ -0,0 +1,80 @@ +## 閾炬帴 + +銆愪腑绛夐毦搴︺22. Generate Parentheses锛堢敓鎴愭嫭鍙凤級 + +https://leetcode.com/problems/generate-parentheses/ + + +## 棰樼洰 + +Given *n* pairs of parentheses, write a function to generate all combinations of well-formed parentheses. + +For example, given *n* = 3, a solution set is: + +``` +[ + "((()))", + "(()())", + "(())()", + "()(())", + "()()()" +] +``` + +*Tags: Backtracking* + +## 閲婁箟 + +閬囧埌涓瓑闅惧害鐨勯鐩紝灏辫鍍忓緢涔呫傝繖棰樿繕鏄湅鐫绛旀锛岀爜浜嗕竴閬嶃 + +搴旇鏄洖婧殑鏂规硶銆侭acktraking + +## 浠g爜 + +```c++ +class Solution +{ + private: + //lhs琛ㄧずpath閲岄潰宸︽嫭鍙蜂釜鏁帮紝rhs琛ㄧずpath閲岄潰宸︽嫭鍙蜂釜鏁 + //res鐢ㄦ潵璁板綍杩斿洖缁撴灉锛宲ath璁板綍褰撳墠瀛楃涓诧紱 + void generate(int n, int lhs, int rhs, vector& res, string& path) + { + //lhs杈惧埌鏈澶у硷紝閭d箞鍙兘缁х画鍔犲叆鍙虫嫭鍙 + if(n == lhs) + { + string s(path); + res.push_back(s.append(n - rhs, ')')); + return; + } + path.push_back('('); + generate(n, lhs + 1, rhs, res, path); + path.pop_back(); + + //宸︽嫭鍙蜂釜鏁板ぇ浜庡彸鎷彿涓暟锛屾墠鍙互鎻掑叆锛 + if(lhs > rhs) + { + path.push_back(')'); + generate(n, lhs, rhs + 1, res, path); + path.pop_back(); + } + } + + public: + vector generateParenthesis(int n) + { + vector res; + string path; + if(n > 0) generate(n, 0, 0, res, path); + return res; + } +}; + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From bd32fa7ff9a8fe4352c258d762032eb7d33e758c Mon Sep 17 00:00:00 2001 From: JosanSun <1030460698@qq.com> Date: Wed, 11 Jan 2017 17:15:59 +0800 Subject: [PATCH 57/72] Upload my code 024 Please check --- leetcode/24/note_Josan024.md | 70 ++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 leetcode/24/note_Josan024.md diff --git a/leetcode/24/note_Josan024.md b/leetcode/24/note_Josan024.md new file mode 100644 index 0000000..f0c6f41 --- /dev/null +++ b/leetcode/24/note_Josan024.md @@ -0,0 +1,70 @@ +## 閾炬帴 + + +https://leetcode.com/problems/swap-nodes-in-pairs/ + + +## 瑙i鎬濊矾 + +棣栧厛锛屽缓浜嗕竴涓櫄鍋囧ご锛岃繖鏍峰彲浠ョ粺涓鎿嶄綔銆 +寤虹珛涓や釜鎸囬拡pre,p;pre鎸囧悜p鐨勫墠闈竴涓粨鐐广 +鐒跺悗姣忎袱涓寚閽堬紝杩涜涓娆℃搷浣溿 + +娉ㄦ剰锛氱粨鐐逛釜鏁颁负濂囨暟鐨勭壒渚嬨 + + + + +## 浠g爜 + + +```c++ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution +{ + public: + ListNode* swapPairs(ListNode* head) + { + ListNode* dumpHead = new ListNode(100); + dumpHead->next = head; + + ListNode* p = head; + ListNode* pre = dumpHead; + + while(p) + { + //澶勭悊濂囨暟缁撶偣鐨勬儏鍐 + if(p->next == NULL) + { + ListNode* tem = dumpHead->next; + delete dumpHead; + return tem; + } + pre->next = p->next; + p->next = p->next->next; + pre->next->next = p; + pre = p; + p = p->next; + } + + ListNode* tem = dumpHead->next; + delete dumpHead; + return tem; + } +}; +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From c751c0363070430bb1c8202ba75d0da094c29dfe Mon Sep 17 00:00:00 2001 From: JosanSun <1030460698@qq.com> Date: Wed, 11 Jan 2017 17:25:44 +0800 Subject: [PATCH 58/72] Upload my code 026 Please check --- leetcode/26/note_Josan026.md | 86 ++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 leetcode/26/note_Josan026.md diff --git a/leetcode/26/note_Josan026.md b/leetcode/26/note_Josan026.md new file mode 100644 index 0000000..835023b --- /dev/null +++ b/leetcode/26/note_Josan026.md @@ -0,0 +1,86 @@ +## 閾炬帴 + +https://leetcode.com/problems/remove-duplicates-from-sorted-array/ + +##瑙i鎬濊矾 + +鍘绘帀閲嶅鐨勬暟瀛楋紝鍏跺疄灏辨槸erase()鍑芥暟鐨勫唴閮ㄥ疄鐜般 + +鎴戦噰鐢ㄧ殑pre-check鎶宸с + +涓嶈繃锛岃繖棰樿繕鏈夋洿绮剧畝鐨勫仛娉曘 + + + +## 浠g爜 + + + + + + +```c++ +class Solution +{ + public: + int removeDuplicates(vector& nums) + { + int res = 0; + for(int i = 0; i != nums.size();++i) + { + while((i+1)!=nums.size()&&nums[i+1] == nums[i]) + { + ++i; + } + nums[res++] = nums[i]; + } + return res; + } +}; + +``` + +涓庢垜鐨勪唬鐮佽繘琛屾瘮杈冿紝鎰熻涓嬮潰鐨勪唬鐮佹洿绠娲佷竴鐐 +```c++ +class Solution +{ + public: + int removeDuplicates(vector& nums) + { + //蹇呴』鎻愬墠鍒ゆ柇nums.empty() + if(nums.empty()) { return 0; } + + int index = 0; + for(int i = 1; i < nums.size(); ++i) + { + if(nums[index] != nums[i]) + { + nums[++index] = nums[i]; + } + } + return index + 1; + } +}; +``` + + +浣滃紛娉 +```c++ +class Solution +{ + public: + int removeDuplicates(vector& nums) + { + return distance(nums.begin(),unique(nums.begin(), nums.end())); + } +}; +``` + + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From 21f1b36948d404fce65b8233a4b90e8a1b392824 Mon Sep 17 00:00:00 2001 From: JosanSun <1030460698@qq.com> Date: Wed, 11 Jan 2017 17:30:09 +0800 Subject: [PATCH 59/72] Upload my code 027 Please check --- leetcode/27/note_Josan027.md | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 leetcode/27/note_Josan027.md diff --git a/leetcode/27/note_Josan027.md b/leetcode/27/note_Josan027.md new file mode 100644 index 0000000..567e566 --- /dev/null +++ b/leetcode/27/note_Josan027.md @@ -0,0 +1,40 @@ +## 閾炬帴 + + +https://leetcode.com/problems/remove-element/ + + +## 瑙i鎬濊矾 + +闈炲父绠鍗曠殑涓涓鐩傚垹闄ゆ暟缁勪腑鎸囧畾鐨勫笺 + + +## 浠g爜 + + +```c++ +class Solution +{ + public: + int removeElement(vector& nums, int val) + { + int index = 0; + for(int i = 0; i != nums.size(); ++i) + { + if(nums[i] != val) + { + nums[index++] = nums[i]; + } + } + return index; + } +}; +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From d09df35169705a05b758fb245c0611421182226b Mon Sep 17 00:00:00 2001 From: bajdcc Date: Mon, 16 Jan 2017 12:51:40 +0800 Subject: [PATCH 60/72] Add hard problems --- leetcode/04/note.md | 65 +++++++++++++++++++++++++++++++++++++++++++ leetcode/10/note.md | 65 +++++++++++++++++++++++++++++++++++++++++++ leetcode/37/note.md | 56 +++++++++++++++++++++++++++++++++++++ leetcode/41/note.md | 50 +++++++++++++++++++++++++++++++++ leetcode/51/note.md | 67 +++++++++++++++++++++++++++++++++++++++++++++ leetcode/65/note.md | 53 +++++++++++++++++++++++++++++++++++ 6 files changed, 356 insertions(+) create mode 100644 leetcode/04/note.md create mode 100644 leetcode/10/note.md create mode 100644 leetcode/37/note.md create mode 100644 leetcode/41/note.md create mode 100644 leetcode/51/note.md create mode 100644 leetcode/65/note.md diff --git a/leetcode/04/note.md b/leetcode/04/note.md new file mode 100644 index 0000000..42b09db --- /dev/null +++ b/leetcode/04/note.md @@ -0,0 +1,65 @@ +## 閾炬帴 + +4.Median of Two Sorted Arrays(Hard) + +## 棰樼洰 + +There are two sorted arrays聽**nums1**聽and聽**nums2**聽of size m and n respectively. + +Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). + +**Example 1:** + +``` +nums1 = [1, 3] +nums2 = [2] + +The median is 2.0 + +``` + +**Example 2:** + +``` +nums1 = [1, 2] +nums2 = [3, 4] + +The median is (2 + 3)/2 = 2.5 +``` + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 + + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/10/note.md b/leetcode/10/note.md new file mode 100644 index 0000000..220e116 --- /dev/null +++ b/leetcode/10/note.md @@ -0,0 +1,65 @@ +## 閾炬帴 + +10.Regular Expression Matching(Hard) + +## 棰樼洰 + +Implement regular expression matching with support for聽`'.'`聽and聽`'*'`. + +``` +'.' Matches any single character. +'*' Matches zero or more of the preceding element. + +The matching should cover the entire input string (not partial). + +The function prototype should be: +bool isMatch(const char *s, const char *p) + +Some examples: +isMatch("aa","a") 鈫 false +isMatch("aa","aa") 鈫 true +isMatch("aaa","aa") 鈫 false +isMatch("aa", "a*") 鈫 true +isMatch("aa", ".*") 鈫 true +isMatch("ab", ".*") 鈫 true +isMatch("aab", "c*a*b") 鈫 true +``` + + + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 + + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/37/note.md b/leetcode/37/note.md new file mode 100644 index 0000000..d6e0bb2 --- /dev/null +++ b/leetcode/37/note.md @@ -0,0 +1,56 @@ +## 閾炬帴 + +37.Sudoku Solver(Hard) + +## 棰樼洰 + +Write a program to solve a Sudoku puzzle by filling the empty cells. + +Empty cells are indicated by the character聽`'.'`. + +You may assume that there will be only one unique solution. + +![img](http://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png) + +A sudoku puzzle... + +![img](http://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Sudoku-by-L2G-20050714_solution.svg/250px-Sudoku-by-L2G-20050714_solution.svg.png) + +...and its solution numbers marked in red. + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 + + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/41/note.md b/leetcode/41/note.md new file mode 100644 index 0000000..20029b6 --- /dev/null +++ b/leetcode/41/note.md @@ -0,0 +1,50 @@ +## 閾炬帴 + +41.First Missing Positive(Hard) + +## 棰樼洰 + +Given an unsorted integer array, find the first missing positive integer. + +For example, +Given聽`[1,2,0]`聽return聽`3`, +and聽`[3,4,-1,1]`聽return聽`2`. + +Your algorithm should run in聽*O*(*n*) time and uses constant space. + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 + + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/51/note.md b/leetcode/51/note.md new file mode 100644 index 0000000..14feca9 --- /dev/null +++ b/leetcode/51/note.md @@ -0,0 +1,67 @@ +## 閾炬帴 + +51.N-Queens(Hard) + +## 棰樼洰 + +The聽*n*-queens puzzle is the problem of placing聽*n*聽queens on an聽*n*脳*n*聽chessboard such that no two queens attack each other. + +![img](http://www.leetcode.com/wp-content/uploads/2012/03/8-queens.png) + +Given an integer聽*n*, return all distinct solutions to the聽*n*-queens puzzle. + +Each solution contains a distinct board configuration of the聽*n*-queens' placement, where聽`'Q'`聽and聽`'.'`聽both indicate a queen and an empty space respectively. + +For example, +There exist two distinct solutions to the 4-queens puzzle: + +``` +[ + [".Q..", // Solution 1 + "...Q", + "Q...", + "..Q."], + + ["..Q.", // Solution 2 + "Q...", + "...Q", + ".Q.."] +] +``` + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 + + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file diff --git a/leetcode/65/note.md b/leetcode/65/note.md new file mode 100644 index 0000000..d9f2e33 --- /dev/null +++ b/leetcode/65/note.md @@ -0,0 +1,53 @@ +## 閾炬帴 + +65.Valid Number(Hard) + +## 棰樼洰 + +Validate if a given string is numeric. + +Some examples: +`"0"`聽=>聽`true` +`" 0.1 "`聽=>聽`true` +`"abc"`聽=>聽`false` +`"1 a"`聽=>聽`false` +`"2e10"`聽=>聽`true` + +**Note:**聽It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. + +## 閲婁箟 + + + + + + +## 琛ュ厖鎻忚堪 + + + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 + + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file From 3d7f6b6ae85b7ee8fd7dafe28b07c931a78c94d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E7=94=9F=E6=8A=80=E6=9C=AF=E8=8F=8C?= Date: Sat, 21 Jan 2017 09:51:57 +0800 Subject: [PATCH 61/72] Add note Median of Two Sorted Arrays --- leetcode/04/note_bajdcc.md | 174 +++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 leetcode/04/note_bajdcc.md diff --git a/leetcode/04/note_bajdcc.md b/leetcode/04/note_bajdcc.md new file mode 100644 index 0000000..77072a8 --- /dev/null +++ b/leetcode/04/note_bajdcc.md @@ -0,0 +1,174 @@ +## 閾炬帴 + +4.Median of Two Sorted Arrays(Hard) + +## 棰樼洰 + +There are two sorted arrays聽**nums1**聽and聽**nums2**聽of size m and n respectively. + +Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). + +**Example 1:** + +``` +nums1 = [1, 3] +nums2 = [2] + +The median is 2.0 + +``` + +**Example 2:** + +``` +nums1 = [1, 2] +nums2 = [3, 4] + +The median is (2 + 3)/2 = 2.5 +``` + +## 閲婁箟 + +灏嗕袱涓湁搴忔暟鍒楄繘琛屽綊骞讹紝鎵惧埌鍏朵腑浣嶆暟銆 + +鍏堣绠椾腑浣嶆暟锛 + +- 瀵规婚暱n涓哄鏁拌岃█锛屼腑浣嶆暟浣嶇疆涓簄/2 +- 瀵规婚暱n涓哄伓鏁拌岃█锛屼腑浣嶆暟浣嶇疆涓(n-1)/2鍜(n-1)/2+1 + +闂鍦ㄤ簬濡備綍姹傚嚭鎸囧畾浣嶇疆鐨勬暟銆 + +### 鐗逛緥 + +鐗逛緥鑲畾鏈夛紝鎯充竴鎯筹細 + +- 鍏朵腑涓涓暟鍒椾负绌猴紝閭d箞闂绠鍗曪紝鍦ㄥ彟涓涓暟鍒椾腑鎵惧嵆鍙 +- 涓涓暟鍒楃殑鏈澶у煎皬浜庡彟涓鏁板垪鐨勬渶灏忓硷紝鍗虫暟鍒1鏈鏈綅灏忎簬鏁板垪2鏈鍓嶄綅锛岃繖鏃跺皢鏁板垪1鍜2鐪嬩綔鏁翠綋锛屽啀杩涜鏌ユ壘 + +### 鍙﹀鎯呭喌 + +杩欐椂鍙兘浠庡墠闈㈠紑濮嬩竴涓釜姣旇緝銆傝褰撳墠闇瑕佹壘鍒扮index浣嶃 + +鍒嗗埆鐢ㄤ袱涓寚閽堣褰曞綋鍓嶆暟鍒1鍜2鐨勪綅缃傚摢杈圭殑鍊艰緝灏忥紝灏卞皢鐩稿簲鐨勪綅缃墠杩涗竴浣嶏紝index閫掑噺銆備笉鏂惊鐜 + +浠涔堟椂鍊欒烦鍑哄惊鐜細 + +- 鏌愪釜鏁板垪琚亶鍘嗗埌鏈熬浜嗭紝杩欐椂鍙渶瑕佹牴鎹墿浣欑殑index鍦ㄥ彟澶栦竴涓暟鍒椾腑鏌ユ壘鍗冲彲 +- index涓-1浜嗭紝鍗崇浉搴旂殑浣嶇疆宸茶鎵惧埌 + +涓嬮潰鏈夊嚑绉嶆儏鍐碉細 + +- 鍏朵腑涓涓暟鍒楅亶鍘嗗埌鏈熬銆傝嫢姝ゆ椂index涓-1锛岄偅涔堜綅缃氨鏄繖涓暟鍒楃殑鏈鏈綅锛屽鏋滄槸濂囨暟锛堜腑浣嶆暟鍙1浣嶏級锛屽氨鐩存帴杩斿洖浜嗭紱濡傛灉鏄伓鏁帮紝灏辩畻涓婂彟涓涓暟鍒楃殑褰撳墠浣嶇疆銆傝嫢姝ゆ椂index涓嶄负-1锛岄偅涔堣鏄庤繕瑕佸啀寰鍓嶉潰鎵惧嚑浣嶏紝閭d箞宸查亶鍘嗗畬鐨勯偅涓暟鍒楀氨out浜嗭紝鎺ヤ笅鏉ュ彧瑕佸湪鍙︿竴鏁板垪涓煡鎵惧嵆鍙 +- 鑻ヤ笉鏄笂闈㈢殑鎯呭喌锛屽氨鏄渶鍏稿瀷鐨勬儏鍐典簡銆傚厛鏍规嵁first鐭ラ亾涓婁竴涓兼槸鍦ㄥ摢涓涓暟鍒椾腑锛岀劧鑰屾壘鍑鸿繖涓糿um[ptr-1]锛屼负浠涔堣鍑忎竴鍛紝杩欐槸鍥犱负鍦╳hile寰幆涓璸tr闇瑕佸悜鍓嶇Щ涓浣嶃傚鏋滄槸濂囨暟锛岄偅涔堢洿鎺ヨ繑鍥炶繖涓硷紱濡傛灉鏄伓鏁帮紝閭i渶瑕佹壘鍑轰笅涓涓硷紝涓嬩竴涓煎湪褰撳墠鍊煎悗闈竴浣嶅拰鍙﹀涓鏁板垪褰撳墠浣嶇疆涓互杈冨皬鍊奸夋嫨鍑烘潵銆 + +涓轰粈涔堣繖棰橀毦搴︿负hard锛屾垜鎯充笉鏄洜涓烘濊矾鎯充笉鍒帮紙褰掑苟锛夛紝鑰屾槸鍥犱负鍏朵腑鐨勬楠ゅ緢鐑︼紝浠涔+1浠涔-1杩樿鍚勭鍒ゆ柇銆 + +褰撶劧锛岃繕鏈夊彲浠ヤ紭鍖栫殑鍦版柟銆傚鍏朵腑涓鏁板垪鍏冪礌鍙湁涓涓紝閭d箞褰掑苟灏卞彉鎴愪簡浜屽垎鎻掑叆锛岃兘鑺傜渷鏃堕棿銆 + + +## 琛ュ厖鎻忚堪 + +83.14% https://leetcode.com/submissions/detail/89933853/ + + + + +## 浠g爜 + + + + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 +#define MIN(a,b) (((a)<(b))?(a):(b)) + +class Solution { +public: + static double findIdx(const vector &nums1, const vector &nums2, int index, bool odd) + { + int len1 = nums1.size(), len2 = nums2.size(); + auto ptr1 = 0, ptr2 = 0; + if (len1 == 0) + { + return odd ? nums2[index] : ((nums2[index] + nums2[index + 1]) / 2.0); + } + if (len2 == 0) + { + return odd ? nums1[index] : ((nums1[index] + nums1[index + 1]) / 2.0); + } + if (nums1.back() <= nums2.front()) + { + if (odd) + return index < len1 ? nums1[index] : nums2[index - len1]; + if (index < len1 - 1) + return (nums1[index] + nums1[index + 1]) / 2.0; + if (index > len1 - 1) + return (nums2[index - len1] + nums2[index - len1 + 1]) / 2.0; + return (nums1[index] + nums2[0]) / 2.0; + } + if (nums2.back() <= nums1.front()) + { + if (odd) + return index < len2 ? nums2[index] : nums1[index - len2]; + if (index < len2 - 1) + return (nums2[index] + nums2[index + 1]) / 2.0; + if (index > len2 - 1) + return (nums1[index - len2] + nums1[index - len2 + 1]) / 2.0; + return (nums2[index] + nums1[0]) / 2.0; + } + auto first = true; + while (ptr1 < len1 && ptr2 < len2 && index-- >= 0) + { + first = nums1[ptr1] <= nums2[ptr2]; + first ? ptr1++ : ptr2++; + } + if (index == -1) + { + if (ptr1 == len1) + { + return odd ? nums1.back() : (nums2[ptr2] + nums1.back()) / 2.0; + } + if (ptr2 == len2) + { + return odd ? nums2.back() : (nums1[ptr1] + nums2.back()) / 2.0; + } + } + if (ptr1 == len1) + { + return odd ? nums2[ptr2 + index] : (nums2[ptr2 + index] + nums2[ptr2 + index + 1]) / 2.0; + } + if (ptr2 == len2) + { + return odd ? nums1[ptr1 + index] : (nums1[ptr1 + index] + nums1[ptr1 + index + 1]) / 2.0; + } + if (first) + { + if (odd) + return nums1[ptr1 - 1]; + return (nums1[ptr1 - 1] + MIN(nums1[ptr1], nums2[ptr2])) / 2.0; + } + if (odd) + return nums2[ptr2 - 1]; + return (nums2[ptr2 - 1] + MIN(nums2[ptr2], nums1[ptr1])) / 2.0; + } + + double findMedianSortedArrays(vector &nums1, vector &nums2) + { + auto count = nums1.size() + nums2.size(); + auto idx = (count - 1) / 2; + return findIdx(nums1, nums2, idx, count % 2 == 1); + } +}; + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file From 6624ec5676c76e0dd2e3cc4e16b8cb670887277d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E7=94=9F=E6=8A=80=E6=9C=AF=E8=8F=8C?= Date: Sun, 22 Jan 2017 00:50:17 +0800 Subject: [PATCH 62/72] Add note Regular Expression Matching --- leetcode/10/note_bajdcc.md | 293 +++++++++++++++++++++++++++++++++++++ 1 file changed, 293 insertions(+) create mode 100644 leetcode/10/note_bajdcc.md diff --git a/leetcode/10/note_bajdcc.md b/leetcode/10/note_bajdcc.md new file mode 100644 index 0000000..d20efe0 --- /dev/null +++ b/leetcode/10/note_bajdcc.md @@ -0,0 +1,293 @@ +## 閾炬帴 + +10.Regular Expression Matching(Hard) + +## 棰樼洰 + +Implement regular expression matching with support for聽`'.'`聽and聽`'*'`. + +``` +'.' Matches any single character. +'*' Matches zero or more of the preceding element. + +The matching should cover the entire input string (not partial). + +The function prototype should be: +bool isMatch(const char *s, const char *p) + +Some examples: +isMatch("aa","a") 鈫 false +isMatch("aa","aa") 鈫 true +isMatch("aaa","aa") 鈫 false +isMatch("aa", "a*") 鈫 true +isMatch("aa", ".*") 鈫 true +isMatch("ab", ".*") 鈫 true +isMatch("aab", "c*a*b") 鈫 true +``` + + + + +## 鏂规硶涓锛氬洖婧硶 + +杩欐槸鏈瀹规槗鎯冲埌鐨勬柟娉曘 + +璁炬ā寮忎覆涓簆at锛岃鍖归厤鐨勪覆涓簊tr銆 + +濡傛灉pat鍜宻tr褰撳墠鍏ㄦ槸瀛楁瘝锛岄偅灏变竴浣嶄綅鍖归厤鍗冲彲銆備絾杩欓噷鏈夐毦鐐癸紝涔熷氨鏄痯at鐨勫瓧姣嶅悗闈㈠彲鑳借窡\*锛岃繖涓猏*闈炲父鐨勭壒娈娿 + +姣斿a\*锛屽畠鍙互鍖归厤杩炵画鐨刟锛屼篃鍙互浠涔堥兘涓嶅尮閰嶏紝杩欐槸姣旇緝闅剧殑鍦版柟銆傛帴瑙﹁繃缂栬瘧鍘熺悊灏变細鐭ラ亾锛岃繖涓猏*瀵瑰簲鐨勬槸epsilon涓诧紝杩欎娇寰楁暣涓姸鎬佹満鍙樻垚涓嶇‘瀹氱殑锛屼笉纭畾鐘舵佹満鐨勫悗鏋滃氨鏄渶瑕佷笉鏂洖婧傚洖婧叾瀹炲彲浠ラ伩鍏嶏紝鐪嬫柟娉曚簩銆 + +鍥犳锛**鍋囧鐭ラ亾褰撳墠鐨勫瓧绗︼紝涓囦竴瀹冨悗闈㈣窡浜*鍛紵**鎵浠ワ紝闇瑕**鍚戝墠鐪嬩竴涓瓧绗**锛岃涓篴head銆 + +鎬濊矾灏卞嚭鏉ヤ簡銆傝閬嶅巻pat鐨勬寚閽堜负i锛岄亶鍘唖tr鐨勬寚閽堜负j銆 + +- 鍋囧褰撳墠瀛楁瘝鏄崟绾殑瀛楁瘝锛屽嵆鍚庨潰娌℃湁璺熺潃\*锛岄偅涔堝氨姣斿pat[i]鍜宻tr[j] +- 鍋囧褰撳墠鏄崟绾殑鍖归厤鍗曞瓧绗"."锛屽悗闈㈡病鏈夎窡鐫\*锛岄偅涔堜笉闇瑕佸姣旓紝i++ +- 鐗规畩鎯呭喌锛屽嵆鍗曠函瀛楁瘝"a~z"鎴栨槸鍖归厤鍗曞瓧绗"."鍚庨潰甯︿簡涓猏*锛岄偅涔堝氨杩涘叆鍥炴函妯″紡锛岄掑綊璋冪敤isMatch骞朵笖j++锛宨sMatch鑳借繑鍥炵湡锛岄偅涔堝尮閰嶆垚鍔燂紝鍚﹀垯缁х画鍖归厤 +- 浠ヤ笂鎯呭喌锛岃嫢璺冲嚭寰幆鏃秈鍜宩閮藉埌浜唒at鍜宻tr鐨勬湯灏撅紝鍒欏尮閰嶆垚鍔 + +涓轰簡杈ㄥ埆褰撳墠瀛楃鐨勭被鍨嬶紝鐢ㄤ簡鏌ユ壘鏁扮粍patMap銆 + +涓嬮潰鏄唬鐮侊紝浣嗚繕鏈夊彟澶栦竴绉嶅舰寮忓嵆鍏ㄩ掑綊鐨勶紝鐢变簬涓よ呭姛鑳界浉鍚岋紝鍥犳鐣ャ + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 +class Solution { + enum Pat + { + OTHER = 0, + ALPHABET, + DOT, + STAR, + END + }; + Pat patMap[0xff] = { OTHER }; // map pattern character + +public: + Solution() + { + for (auto i = 'a'; i <= 'z'; i++) + { + patMap[i] = ALPHABET; // [a-z] + } + patMap['.'] = DOT; // . + patMap['*'] = STAR; // * + patMap['#'] = END; // # + } + + bool isMatch(string s, string p) { + return isMatchImpl(s.c_str(), p.c_str(), s.length(), p.length()); // add stop(look ahead) + } + + using pstr = const char *; + + bool isMatchImpl(pstr s, pstr p, int slen, int plen) { + if (plen == 0) + return slen == 0; + auto c = '$', next = p[0]; + auto pat = OTHER, nextPat = patMap[next]; // look ahead + auto j = 0, i = 0, prev = -1; + for (; i < plen && j <= slen;) + { + if (prev == i - 1) + { + c = next; + pat = nextPat; + next = i + 1 == plen ? '#' : p[i + 1]; + nextPat = patMap[next]; + prev = i; + } + else if (prev < i) + { + c = p[i]; + pat = patMap[c]; + next = i + 1 == plen ? '#' : p[i + 1]; + nextPat = patMap[next]; + prev = i; + } + switch (pat) + { + case OTHER: + throw "invalid character"; + break; + case ALPHABET: + switch (nextPat) + { + case OTHER: + throw "invalid character"; + break; + case ALPHABET: + case DOT: + case END: + if (c != s[j]) + return false; // [a-z] not match + i++; j++; + break; + case STAR: + if (c != s[j]) + i += 2; // [a-z]* not match, skip pattern [a-z]* + else + { + // [a-z]* match, got epsilon, backtracking + if (isMatchImpl(s + j, p + i + 2, slen - j, plen - i - 2)) + return true; + j++; + if (j == slen) + i += 2; + } + break; + default: + break; + } + break; + case DOT: + switch (nextPat) + { + case OTHER: + throw "invalid character"; + break; + case ALPHABET: + case DOT: + case END: + i++; j++; // match . + break; + case STAR: + // got epsilon, use backtracking + if (i + 1 == plen) + { + return true; // .* match all + } + if (isMatchImpl(s + j, p + i + 2, slen - j, plen - i - 2)) + return true; + j++; + if (j == slen) + i += 2; + break; + default: + break; + } + break; + case STAR: + throw "invalid character"; // single * + break; + default: + break; + } + } + return i == plen && j == slen; + } +}; + +``` + + + +## 鏂规硶浜岋細鍔ㄦ佽鍒掓硶 + +鐢变簬鏂规硶涓瀛樺湪澶氫綑鐨勫洖婧棶棰橈紝鍥犳瑕侀伩鍏嶅洖婧紝閲囩敤DP鏂规硶銆 + +DP鏂规硶鐨勪紭鐐癸細涓嶉渶瑕侀掑綊锛堜笉澶嶅埗string锛夛紝鎬濊矾娓呮櫚锛岀敱鐘舵佽浆绉绘柟绋嬫帹瀵煎嚭锛堝厛閫昏緫楠岃瘉锛屼笉鏄撳嚭閿欙級锛屾晥鐜囬珮锛屽叾缁撴灉鏄畝鍖栫殑鐘舵佹満 + +DP鏂规硶鐨勭己鐐癸細鎺ㄥ杩囩▼澶嶆潅 + +浜岀淮DP鐨勪竴鑸楠わ細 + +1. 涓や釜鐩稿叧鍙橀噺鐨勮〃杩帮紝椤讳负鏁板瓧锛岃繖閲屾槸鍘熶覆鍜屾ā寮忎覆鐨勯暱搴︼紝鍚屾椂涔熷喅瀹氫簡dp琛ㄧ殑澶у皬浠ュ強绠楁硶鐨勫鏉傚害 +2. dp琛ㄧ殑鍊肩被鍨嬶紝杩欓噷鏄痓ool +3. 鏍规嵁棰樻剰鎬荤粨瑙勫垯 +4. 鏍规嵁瑙勫垯鍐欏嚭璇﹀敖鐨勭姸鎬佽浆绉绘柟绋 +5. 鍦ㄥ弻閲峟or寰幆涓疄鐜扮姸鎬佽浆绉 +6. 杩斿洖dp琛ㄧ殑鏈鍙充笅鍊 + +璁炬ā寮忎覆涓簆at锛岃鍖归厤鐨勪覆涓簊tr銆傚洜姝p鏁扮粍鐨勫ぇ灏忔槸(patlen+1) x (strlen+1)锛屽ぇ灏忎负瀛愪覆鐨勯暱搴︺ + +鎺ㄥ瑙勫垯锛 + +1. 绌轰覆鍖归厤绌轰覆锛歞p(0,0)=true +2. '.\*'缁勫悎鍖归厤绌轰覆锛氭秷闄'.\*'骞跺尮閰嶏紝dp(0,j)=dp(0,j-2) +3. '.\*'鎴'a\*'鍖归厤锛氱湅涓婃缁撴灉dp(i-1,j)锛岃嫢鐪熷垯鐪燂紱鑻ュ亣鍒欑湅dp(i,j-2) +4. 'a\*'鍖归厤锛氱湅涓婃缁撴灉dp(i-1,j)锛岃嫢鐪熷垯姣旇緝鏈鏂板尮閰嶄綅s(i-1)=p(j-2)锛涜嫢鍋囧垯鐪媎p(i,j-2) +5. 闈'\*'鍖归厤绌轰覆锛歞p(0,j)=false +6. '.'鍖归厤锛氱湅涓婃缁撴灉dp(i-1,j-1) +7. 'a'锛氱湅涓婃缁撴灉dp(i-1,j-1)锛屽悓鏃舵瘮杈冩渶鏂板尮閰嶄綅s(i-1)=p(j-1) + +浠g爜濡備笅锛岃楁椂鏄洖婧硶鐨1/3宸﹀彸銆 + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 +class Solution { +public: + bool isMatch(string s, string p) { + int slen = s.length(), plen = p.length(); + // dp[i,j] -> str[0..i-1] 鏄惁琚 pat[0..j-1] 鍖归厤 + vector> dp(slen + 1, vector(plen + 1, false)); + // 浜岄噸寰幆鏋勫缓dp鏁扮粍锛屾寜閮ㄥ氨鐝 + // dp[i,j] -> pat[0..j-1] matches str[0..i-1] + dp[0][0] = true; // empty matches empty + for (auto i = 0; i <= slen; i++) + { + // 杩欓噷鐪佺暐浜唈浠0寮濮嬬殑閮ㄥ垎锛屽洜涓簆at涓虹┖鏃讹紝闄ら潪str涓虹┖鍚﹀垯鍖归厤澶辫触 + for (auto j = 1; j <= plen; j++) + { + if (p[j - 1] == '*') // '*'涓嶅簲璇ュ湪绗竴浣嶏紝鍥犳j>1 + { + // 褰撳墠涓'*'锛岃繖閲屾儏鍐电◢鏄惧鏉 + if (p[j - 2] == '.') + { + if (i > 0 && dp[i - 1][j]) + // '.*' matches 'abc' => '.*' == 'ab' + dp[i][j] = true; + if (!dp[i][j]) // else + // 'a.*' matches 'ab' => 'a.*' == 'a' + dp[i][j] = dp[i][j - 2];; + } + else + { + if (i > 0 && dp[i - 1][j]) + // 'a*' matches 'aa' => 'a*' == 'a' && 'a' == 'a' + dp[i][j] = s[i - 1] == p[j - 2]; + if (!dp[i][j]) // else + // 'ab*' matches 'a' => 'a' == 'a' + dp[i][j] = dp[i][j - 2];; + } + } + else + { + // 褰撳墠pat[i]鏄痆a-z]鎴栬'.'锛屽垯鍗曞瓧绗﹀尮閰 + if (i == 0) + // 闈炵┖妯″紡涓蹭笉鑳藉尮閰嶇┖涓 + dp[0][j] = false; + else + { + if (p[j - 1] == '.') + { + // 褰撳墠涓'.' + // 'a.' matches 'aa' => 'a' == 'a' + dp[i][j] = dp[i - 1][j - 1]; + } + else + { + // 褰撳墠涓篬a-z] + // 'ab' matches 'ab' => 'a' == 'a' && 'b' == 'b' + dp[i][j] = dp[i - 1][j - 1] && s[i - 1] == p[j - 1]; + } + } + } + } + } + return dp[slen][plen]; + } +}; +``` + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file From 2347e79e5845ff39697378d95d4c2e85048012b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E7=94=9F=E6=8A=80=E6=9C=AF=E8=8F=8C?= Date: Sun, 22 Jan 2017 00:59:18 +0800 Subject: [PATCH 63/72] Update note MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 绠鍖栦唬鐮 --- leetcode/10/note_bajdcc.md | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/leetcode/10/note_bajdcc.md b/leetcode/10/note_bajdcc.md index d20efe0..94c3eeb 100644 --- a/leetcode/10/note_bajdcc.md +++ b/leetcode/10/note_bajdcc.md @@ -242,19 +242,16 @@ public: if (i > 0 && dp[i - 1][j]) // '.*' matches 'abc' => '.*' == 'ab' dp[i][j] = true; - if (!dp[i][j]) // else - // 'a.*' matches 'ab' => 'a.*' == 'a' - dp[i][j] = dp[i][j - 2];; } else { if (i > 0 && dp[i - 1][j]) // 'a*' matches 'aa' => 'a*' == 'a' && 'a' == 'a' dp[i][j] = s[i - 1] == p[j - 2]; - if (!dp[i][j]) // else - // 'ab*' matches 'a' => 'a' == 'a' - dp[i][j] = dp[i][j - 2];; } + if (!dp[i][j]) // retry + // 'a.*' matches 'a' => 'a' == 'a' + dp[i][j] = dp[i][j - 2];; } else { @@ -290,4 +287,4 @@ public: ![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) -PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From d413059a5b83c041473ad9c0c3f1e0d620b7d940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E7=94=9F=E6=8A=80=E6=9C=AF=E8=8F=8C?= Date: Sun, 22 Jan 2017 23:47:15 +0800 Subject: [PATCH 64/72] Add note First Missing Positive --- leetcode/41/note_bajdcc.md | 90 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 leetcode/41/note_bajdcc.md diff --git a/leetcode/41/note_bajdcc.md b/leetcode/41/note_bajdcc.md new file mode 100644 index 0000000..d2c6bb1 --- /dev/null +++ b/leetcode/41/note_bajdcc.md @@ -0,0 +1,90 @@ +## 閾炬帴 + +41.First Missing Positive(Hard) + +## 棰樼洰 + +Given an unsorted integer array, find the first missing positive integer. + +For example, +Given聽`[1,2,0]`聽return聽`3`, +and聽`[3,4,-1,1]`聽return聽`2`. + +Your algorithm should run in聽*O*(*n*) time and uses constant space. + + + +## 鏂规硶涓锛氭帓搴忔煡缂烘硶 + +鎬濊矾锛氬皢鍘熸暟缁勬帓搴忥紝鐒跺悗浠庡墠鍚戝悗鏌ユ壘缂烘紡鐨勶紝涓嶈繃姝ゆ柟娉曞鏉傚害涓虹嚎鎬у鏁伴樁锛屼笉婊¤冻棰樻剰锛屼絾涔熻兘AC銆 + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 + +class Solution { +public: + int firstMissingPositive(vector &nums) { + sort(nums.begin(), nums.end()); + auto min = 1; + for (auto &n : nums) { + if (n > 0) { + if (n == min) + min++; + else if (n > min) + return min; + } + } + return min; + } +}; +``` + +## 鏂规硶浜岋細鎸変綅缃崲娉 + +鍚杩囧笇灏斾集鐗规梾棣嗘倴璁哄惂锛熼偅涔堣繖閲岋紝浠庡墠寰鍚庨亶鍘唍ums锛屾暟缁勫ぇ灏弆en锛岃褰撳墠涓簄銆 + +杩欐椂鎬濊冿紝濡傛灉n<=0鎴栬卬>len锛岄偅涔坣蹇呯劧鏄棤鏁堢殑銆傚湪杩欎釜鑼冨洿鍐(1~len)鐨刵锛屾妸瀹冧滑鏀惧埌nums[n-1]鐨勪綅缃笂銆 + +闂鏉ヤ簡锛氭柊鐨勪綅缃笂鍙堟湁鍏朵粬鏁板瓧鎬庝箞鍔炲憿锛熺瓟妗堟槸鐢ㄩ掑綊鍘昏В鍐炽傞掑綊瑙e喅鍚庣劧鍚庡啀缃崲銆 + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 + +class Solution { +public: + int firstMissingPositive(vector &nums) { + for (auto &n : nums) { + firstMissingPositive(nums, n); + } + for (int i = 0; i < nums.size(); ++i) { + if (nums[i] != -2333) + return i + 1; + } + return nums.size() + 1; + } + + void firstMissingPositive(vector &nums, int& n) { + if (n > 0 && n <= nums.size()) { + auto j = nums[n-1], &k = nums[n-1]; + if (j < 1) { + k = -2333; + } else if (j != -2333) { + k = -666; + firstMissingPositive(nums, j); + k = -2333; + } + } + } +}; +``` + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file From b768dcd7f6c11e821546e332ea2ba7532eb7c1e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E7=94=9F=E6=8A=80=E6=9C=AF=E8=8F=8C?= Date: Mon, 23 Jan 2017 01:00:03 +0800 Subject: [PATCH 65/72] Add note Valid Number ** DFA table solution --- leetcode/65/note_bajdcc.md | 243 +++++++++++++++++++++++++++++++++++++ 1 file changed, 243 insertions(+) create mode 100644 leetcode/65/note_bajdcc.md diff --git a/leetcode/65/note_bajdcc.md b/leetcode/65/note_bajdcc.md new file mode 100644 index 0000000..81d3e91 --- /dev/null +++ b/leetcode/65/note_bajdcc.md @@ -0,0 +1,243 @@ +## 閾炬帴 + +65.Valid Number(Hard) + +## 棰樼洰 + +Validate if a given string is numeric. + +Some examples: +`"0"`聽=>聽`true` +`" 0.1 "`聽=>聽`true` +`"abc"`聽=>聽`false` +`"1 a"`聽=>聽`false` +`"2e10"`聽=>聽`true` + +**Note:**聽It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. + +## 鎬濊矾 + +杩欐槸鏁板瓧鐨勫尮閰嶏紝涓鑸秹鍙婄紪璇戝師鐞嗙殑棰樼洰閮芥槸hard闅惧害銆備竴鑸岃█锛屽彲浠ラ噰鐢ㄥ閲嶅祵濂梚f鍒ゆ柇鐨勬柟娉曡В鍐筹紝涓嶈繃杩欐牱娌℃湁鎸戞垬鎬э紝浠g爜涔熶笐锛岃兘涓嶈兘鍙﹁緹韫婂緞鍛紵 + +**涓嬮潰鐨勬柟娉曢潪鍏稿瀷锛屽垵瀛﹁呬細鎰熷埌鍚冨姏**銆傝瀛︿細杩欑鏂规硶锛岄渶瑕佺啛缁冩帉鎻℃鍒欒〃杈惧紡鐨勪娇鐢紝浠ュ強涓瀹氱殑缂栬瘧鍘熺悊鍩虹銆 + +姝e垯琛ㄨ揪寮忓彲浠ヨ〃绀3鍨嬫枃娉曪紝瀹冩湁涓瀹氱殑璇硶缁撴瀯锛岃繖鏍蜂笉缁嗚銆傚儚鐢靛瓙閭欢銆佺綉鍧銆佺數璇濆彿鐮併佺敤鎴峰悕绛夐兘鍙互鐢ㄦ鍒欒〃杈惧紡杩涜鍖归厤銆傝屾鍒欒〃杈惧紡甯哥敤鐨勫姛鑳戒篃灏辨槸鍖归厤鎴栬呮崟鑾枫 + +杩欓噷锛屾暟瀛楃殑姝e垯琛ㄨ揪寮忔槸`[ ]*[+-]?(\d*\.?\d+|\d+\.?\d*)([e][+-]?\d+)?[ ]*`銆 + +鏃㈢劧鏄3鍨嬫枃娉曪紝閭e氨鍙互鐢ㄧ‘瀹氭ф湁闄愯嚜鍔ㄦ満鏉ヨ〃绀恒傛牴鎹父鐢ㄦ柟娉曪紝瑕佸厛鐢诲嚭NFA锛岀劧鍚庡幓epsilon杈癸紝鍐嶈繘琛屾渶灏忓寲锛岄偅涔堣繖閲屼笉鍙兘鍐欏嚭杩欎箞澶嶆潅鐨勪唬鐮侊紝鍥犳鎴戝伔鎳掍簡 :) + +鍙傝鑷繁鐨(瑙i噴搴)[https://github.com/bajdcc/jMinilang]涓殑姝e垯鍖归厤閮ㄥ垎`priv.bajdcc.util.lexer.test.TestRegex`锛岀洿鎺ヨ繍琛屽畠锛岀劧鍚庤緭鍏ヤ笂杩版鍒欒〃杈惧紡锛岄偅涔堝叿浣撲俊鎭氨鍑烘潵浜嗐 + +璇︾粏淇℃伅锛堢▼搴忚嚜鍔ㄧ敓鎴愶級锛 + +> #### 姝e垯琛ㄨ揪寮忚娉曟爲 #### +> 搴忓垪 { +> 寰幆{0,-1} { +> 瀛楃 [\u0020,' '] +> } +> 寰幆{0,1} { +> 瀛楃 [\u002b,'+'],[\u002d,'-'] +> } +> 鍒嗘敮 { +> 搴忓垪 { +> 寰幆{0,-1} { +> 瀛楃 [\u0030,'0']-[\u0039,'9'] +> } +> 寰幆{0,1} { +> 瀛楃 [\u002e,'.'] +> } +> 寰幆{1,-1} { +> 瀛楃 [\u0030,'0']-[\u0039,'9'] +> } +> } +> 搴忓垪 { +> 寰幆{1,-1} { +> 瀛楃 [\u0030,'0']-[\u0039,'9'] +> } +> 寰幆{0,1} { +> 瀛楃 [\u002e,'.'] +> } +> 寰幆{0,-1} { +> 瀛楃 [\u0030,'0']-[\u0039,'9'] +> } +> } +> } +> 寰幆{0,1} { +> 搴忓垪 { +> 瀛楃 [\u0065,'e'] +> 寰幆{0,1} { +> 瀛楃 [\u002b,'+'],[\u002d,'-'] +> } +> 寰幆{1,-1} { +> 瀛楃 [\u0030,'0']-[\u0039,'9'] +> } +> } +> } +> 寰幆{0,-1} { +> 瀛楃 [\u0020,' '] +> } +> } + +> #### 鐘舵侀泦鍚 #### +> [\u0020,' '] +> [\u002b,'+'] +> [\u002d,'-'] +> [\u002e,'.'] +> [\u0030,'0']-[\u0039,'9'] +> [\u0065,'e'] + +> #### 鏈灏忓寲 #### +> 鐘舵乕0] => 0, +> 杈 => [1] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] +> 杈 => [0] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u0020,' '] +> 杈 => [2] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u002e,'.'] +> 杈 => [3] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u002b,'+'] +> 杈 => [3] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u002d,'-'] +> 鐘舵乕1][缁撴潫] => 3,4,6, +> 杈 => [4] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u002e,'.'] +> 杈 => [5] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u0065,'e'] +> 杈 => [6] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u0020,' '] +> 杈 => [1] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] +> 鐘舵乕2] => 5, +> 杈 => [7] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] +> 鐘舵乕3] => 2, +> 杈 => [2] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u002e,'.'] +> 杈 => [1] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] +> 鐘舵乕4][缁撴潫] => 5,8, +> 杈 => [5] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u0065,'e'] +> 杈 => [6] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u0020,' '] +> 杈 => [4] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] +> 鐘舵乕5] => 10, +> 杈 => [8] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] +> 杈 => [9] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u002b,'+'] +> 杈 => [9] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u002d,'-'] +> 鐘舵乕6][缁撴潫] => 11, +> 杈 => [6] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u0020,' '] +> 鐘舵乕7][缁撴潫] => 6, +> 杈 => [5] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u0065,'e'] +> 杈 => [7] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] +> 杈 => [6] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u0020,' '] +> 鐘舵乕8][缁撴潫] => 14, +> 杈 => [6] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u0020,' '] +> 杈 => [8] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] +> 鐘舵乕9] => 13, +> 杈 => [8] +> 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] + +> #### 鐘舵佽浆绉荤煩闃 #### +> 0 3 3 2 1 -1 +> 6 -1 -1 4 1 5 +> -1 -1 -1 -1 7 -1 +> -1 -1 -1 2 1 -1 +> 6 -1 -1 -1 4 5 +> -1 9 9 -1 8 -1 +> 6 -1 -1 -1 -1 -1 +> 6 -1 -1 -1 7 5 +> 6 -1 -1 -1 8 -1 +> -1 -1 -1 -1 8 -1 + +## 浠g爜 + +鍒╃敤DFA鐘舵佽浆绉昏〃杩涜鍖归厤銆 + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 +class Solution { + inline int getCharMap(const char& c) { + switch (c) { + case ' ': + return 0; + case '+': + return 1; + case '-': + return 2; + case '.': + return 3; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + return 4; + case 'e': + return 5; + } + return -1; + } + +public: + bool isNumber(string s) { + using t = int(*)[6]; + int mm[] = { + 0 ,3 ,3 ,2 ,1 ,-1, + 6 ,-1 ,-1 ,4 ,1 ,5, + -1 ,-1 ,-1 ,-1 ,7 ,-1, + -1 ,-1 ,-1 ,2 ,1 ,-1, + 6 ,-1 ,-1 ,-1 ,4 ,5, + -1 ,9 ,9 ,-1 ,8 ,-1, + 6 ,-1 ,-1 ,-1 ,-1 ,-1, + 6 ,-1 ,-1 ,-1 ,7 ,5, + 6 ,-1 ,-1 ,-1 ,8 ,-1, + -1 ,-1 ,-1 ,-1 ,8 ,-1, + }; + auto m = (t)mm; + bool final[] = {0, 1, 0, 0, 1, 0, 1, 1, 1, 0}; + int status = 0; + auto c = s.c_str(); + for (;;) { + auto local = *c++; + int charClass = getCharMap(local); + int refer = -1; + if (charClass != -1) { + refer = m[status][charClass]; + } + if (refer == -1) { + return local == 0 && final[status]; + } else { + status = refer; + } + } + } +}; + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file From bc04d1b57791f75789e928b605c2ef732187339d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E7=94=9F=E6=8A=80=E6=9C=AF=E8=8F=8C?= Date: Mon, 23 Jan 2017 01:04:53 +0800 Subject: [PATCH 66/72] Change typo --- leetcode/65/note_bajdcc.md | 263 +++++++++++++++++++------------------ 1 file changed, 132 insertions(+), 131 deletions(-) diff --git a/leetcode/65/note_bajdcc.md b/leetcode/65/note_bajdcc.md index 81d3e91..3cc5c9a 100644 --- a/leetcode/65/note_bajdcc.md +++ b/leetcode/65/note_bajdcc.md @@ -27,139 +27,140 @@ Some examples: 鏃㈢劧鏄3鍨嬫枃娉曪紝閭e氨鍙互鐢ㄧ‘瀹氭ф湁闄愯嚜鍔ㄦ満鏉ヨ〃绀恒傛牴鎹父鐢ㄦ柟娉曪紝瑕佸厛鐢诲嚭NFA锛岀劧鍚庡幓epsilon杈癸紝鍐嶈繘琛屾渶灏忓寲锛岄偅涔堣繖閲屼笉鍙兘鍐欏嚭杩欎箞澶嶆潅鐨勪唬鐮侊紝鍥犳鎴戝伔鎳掍簡 :) -鍙傝鑷繁鐨(瑙i噴搴)[https://github.com/bajdcc/jMinilang]涓殑姝e垯鍖归厤閮ㄥ垎`priv.bajdcc.util.lexer.test.TestRegex`锛岀洿鎺ヨ繍琛屽畠锛岀劧鍚庤緭鍏ヤ笂杩版鍒欒〃杈惧紡锛岄偅涔堝叿浣撲俊鎭氨鍑烘潵浜嗐 +鍙傝鑷繁鐨刐瑙i噴搴揮(https://github.com/bajdcc/jMinilang)涓殑姝e垯鍖归厤閮ㄥ垎`priv.bajdcc.util.lexer.test.TestRegex`锛岀洿鎺ヨ繍琛屽畠锛岀劧鍚庤緭鍏ヤ笂杩版鍒欒〃杈惧紡锛岄偅涔堝叿浣撲俊鎭氨鍑烘潵浜嗐 璇︾粏淇℃伅锛堢▼搴忚嚜鍔ㄧ敓鎴愶級锛 -> #### 姝e垯琛ㄨ揪寮忚娉曟爲 #### -> 搴忓垪 { -> 寰幆{0,-1} { -> 瀛楃 [\u0020,' '] -> } -> 寰幆{0,1} { -> 瀛楃 [\u002b,'+'],[\u002d,'-'] -> } -> 鍒嗘敮 { -> 搴忓垪 { -> 寰幆{0,-1} { -> 瀛楃 [\u0030,'0']-[\u0039,'9'] -> } -> 寰幆{0,1} { -> 瀛楃 [\u002e,'.'] -> } -> 寰幆{1,-1} { -> 瀛楃 [\u0030,'0']-[\u0039,'9'] -> } -> } -> 搴忓垪 { -> 寰幆{1,-1} { -> 瀛楃 [\u0030,'0']-[\u0039,'9'] -> } -> 寰幆{0,1} { -> 瀛楃 [\u002e,'.'] -> } -> 寰幆{0,-1} { -> 瀛楃 [\u0030,'0']-[\u0039,'9'] -> } -> } -> } -> 寰幆{0,1} { -> 搴忓垪 { -> 瀛楃 [\u0065,'e'] -> 寰幆{0,1} { -> 瀛楃 [\u002b,'+'],[\u002d,'-'] -> } -> 寰幆{1,-1} { -> 瀛楃 [\u0030,'0']-[\u0039,'9'] -> } -> } -> } -> 寰幆{0,-1} { -> 瀛楃 [\u0020,' '] -> } -> } - -> #### 鐘舵侀泦鍚 #### -> [\u0020,' '] -> [\u002b,'+'] -> [\u002d,'-'] -> [\u002e,'.'] -> [\u0030,'0']-[\u0039,'9'] -> [\u0065,'e'] - -> #### 鏈灏忓寲 #### -> 鐘舵乕0] => 0, -> 杈 => [1] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] -> 杈 => [0] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u0020,' '] -> 杈 => [2] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u002e,'.'] -> 杈 => [3] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u002b,'+'] -> 杈 => [3] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u002d,'-'] -> 鐘舵乕1][缁撴潫] => 3,4,6, -> 杈 => [4] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u002e,'.'] -> 杈 => [5] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u0065,'e'] -> 杈 => [6] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u0020,' '] -> 杈 => [1] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] -> 鐘舵乕2] => 5, -> 杈 => [7] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] -> 鐘舵乕3] => 2, -> 杈 => [2] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u002e,'.'] -> 杈 => [1] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] -> 鐘舵乕4][缁撴潫] => 5,8, -> 杈 => [5] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u0065,'e'] -> 杈 => [6] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u0020,' '] -> 杈 => [4] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] -> 鐘舵乕5] => 10, -> 杈 => [8] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] -> 杈 => [9] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u002b,'+'] -> 杈 => [9] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u002d,'-'] -> 鐘舵乕6][缁撴潫] => 11, -> 杈 => [6] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u0020,' '] -> 鐘舵乕7][缁撴潫] => 6, -> 杈 => [5] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u0065,'e'] -> 杈 => [7] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] -> 杈 => [6] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u0020,' '] -> 鐘舵乕8][缁撴潫] => 14, -> 杈 => [6] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u0020,' '] -> 杈 => [8] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] -> 鐘舵乕9] => 13, -> 杈 => [8] -> 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] - -> #### 鐘舵佽浆绉荤煩闃 #### -> 0 3 3 2 1 -1 -> 6 -1 -1 4 1 5 -> -1 -1 -1 -1 7 -1 -> -1 -1 -1 2 1 -1 -> 6 -1 -1 -1 4 5 -> -1 9 9 -1 8 -1 -> 6 -1 -1 -1 -1 -1 -> 6 -1 -1 -1 7 5 -> 6 -1 -1 -1 8 -1 -> -1 -1 -1 -1 8 -1 +```c++ +#### 姝e垯琛ㄨ揪寮忚娉曟爲 #### +搴忓垪 { + 寰幆{0,-1} { + 瀛楃 [\u0020,' '] + } + 寰幆{0,1} { + 瀛楃 [\u002b,'+'],[\u002d,'-'] + } + 鍒嗘敮 { + 搴忓垪 { + 寰幆{0,-1} { + 瀛楃 [\u0030,'0']-[\u0039,'9'] + } + 寰幆{0,1} { + 瀛楃 [\u002e,'.'] + } + 寰幆{1,-1} { + 瀛楃 [\u0030,'0']-[\u0039,'9'] + } + } + 搴忓垪 { + 寰幆{1,-1} { + 瀛楃 [\u0030,'0']-[\u0039,'9'] + } + 寰幆{0,1} { + 瀛楃 [\u002e,'.'] + } + 寰幆{0,-1} { + 瀛楃 [\u0030,'0']-[\u0039,'9'] + } + } + } + 寰幆{0,1} { + 搴忓垪 { + 瀛楃 [\u0065,'e'] + 寰幆{0,1} { + 瀛楃 [\u002b,'+'],[\u002d,'-'] + } + 寰幆{1,-1} { + 瀛楃 [\u0030,'0']-[\u0039,'9'] + } + } + } + 寰幆{0,-1} { + 瀛楃 [\u0020,' '] + } +} + +#### 鐘舵侀泦鍚 #### +[\u0020,' '] +[\u002b,'+'] +[\u002d,'-'] +[\u002e,'.'] +[\u0030,'0']-[\u0039,'9'] +[\u0065,'e'] +#### 鏈灏忓寲 #### +鐘舵乕0] => 0, + 杈 => [1] + 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] + 杈 => [0] + 绫诲瀷 => 瀛楃鍖洪棿 [\u0020,' '] + 杈 => [2] + 绫诲瀷 => 瀛楃鍖洪棿 [\u002e,'.'] + 杈 => [3] + 绫诲瀷 => 瀛楃鍖洪棿 [\u002b,'+'] + 杈 => [3] + 绫诲瀷 => 瀛楃鍖洪棿 [\u002d,'-'] +鐘舵乕1][缁撴潫] => 3,4,6, + 杈 => [4] + 绫诲瀷 => 瀛楃鍖洪棿 [\u002e,'.'] + 杈 => [5] + 绫诲瀷 => 瀛楃鍖洪棿 [\u0065,'e'] + 杈 => [6] + 绫诲瀷 => 瀛楃鍖洪棿 [\u0020,' '] + 杈 => [1] + 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] +鐘舵乕2] => 5, + 杈 => [7] + 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] +鐘舵乕3] => 2, + 杈 => [2] + 绫诲瀷 => 瀛楃鍖洪棿 [\u002e,'.'] + 杈 => [1] + 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] +鐘舵乕4][缁撴潫] => 5,8, + 杈 => [5] + 绫诲瀷 => 瀛楃鍖洪棿 [\u0065,'e'] + 杈 => [6] + 绫诲瀷 => 瀛楃鍖洪棿 [\u0020,' '] + 杈 => [4] + 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] +鐘舵乕5] => 10, + 杈 => [8] + 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] + 杈 => [9] + 绫诲瀷 => 瀛楃鍖洪棿 [\u002b,'+'] + 杈 => [9] + 绫诲瀷 => 瀛楃鍖洪棿 [\u002d,'-'] +鐘舵乕6][缁撴潫] => 11, + 杈 => [6] + 绫诲瀷 => 瀛楃鍖洪棿 [\u0020,' '] +鐘舵乕7][缁撴潫] => 6, + 杈 => [5] + 绫诲瀷 => 瀛楃鍖洪棿 [\u0065,'e'] + 杈 => [7] + 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] + 杈 => [6] + 绫诲瀷 => 瀛楃鍖洪棿 [\u0020,' '] +鐘舵乕8][缁撴潫] => 14, + 杈 => [6] + 绫诲瀷 => 瀛楃鍖洪棿 [\u0020,' '] + 杈 => [8] + 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] +鐘舵乕9] => 13, + 杈 => [8] + 绫诲瀷 => 瀛楃鍖洪棿 [\u0030,'0']-[\u0039,'9'] + +#### 鐘舵佽浆绉荤煩闃 #### + 0 3 3 2 1 -1 + 6 -1 -1 4 1 5 + -1 -1 -1 -1 7 -1 + -1 -1 -1 2 1 -1 + 6 -1 -1 -1 4 5 + -1 9 9 -1 8 -1 + 6 -1 -1 -1 -1 -1 + 6 -1 -1 -1 7 5 + 6 -1 -1 -1 8 -1 + -1 -1 -1 -1 8 -1 +``` ## 浠g爜 @@ -240,4 +241,4 @@ public: ![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) -PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ \ No newline at end of file +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From ba1e5c98988ae9b24c5881110e3af6239c5d0db8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BF=97=E5=B3=B0?= <380238062@qq.com> Date: Sun, 28 May 2017 08:37:27 +0800 Subject: [PATCH 67/72] Update README.md --- README.md | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/README.md b/README.md index 74e9c02..0b31c81 100644 --- a/README.md +++ b/README.md @@ -4,38 +4,6 @@ 2016骞12鏈05鏃ユ垜浠皢寮濮嬬涓娆℃椿鍔紝澶у涓璧锋潵瀹屾垚LeetCode绗09棰橈紝骞舵妸瑙i杩囩▼璁板綍涓嬫潵锛孭ull鍒拌繖涓粨搴撱 -# 娲诲姩璁板綍 - -## 20161215 鐧句汉鍒烽娲诲姩 - -杩欐鍒稬eetcode绗13棰 - -https://leetcode.com/problems/roman-to-integer/ - -澶у鍙互寰 leetcode/13 涓媝ull浠g爜 - -https://github.com/githubwoniu/learnprogram/tree/master/leetcode/13 - -## 20161205 鐧句汉鍒烽娲诲姩 - -瀛︿範缇ら噷鐨勫皬浼欎即浠粡甯搁棶鎴戯紝涓轰粈涔堝伐浣滃洓骞翠箣鍚庤繕瑕佸涔燙++銆 - -鍏跺疄瀛︿範C++骞朵笉鏄垜鐨勬渶缁堢洰鐨勶紝鎴戞槸甯屾湜鍊熷姪C++鏉ョ郴缁熺殑瀛︿範涓涓嬬紪绋嬭繖闂ㄥ绉戙 - -100澶〤++瀛︿範璁″垝鍙槸鎴戠殑绗竴姝ャ - -鎴鍒颁粖澶╋紙20161204锛夊凡缁忔槸鎵ц璁″垝鐨勭51澶╀簡锛屾垜鐨勬敹鑾锋湁锛 -1. 瀹屾垚浜嗗墠6绔犲拰绗8绔 -2. 鍒嗕韩浜30澶氱瘒璇讳功绗旇鍒扮煡涔庡拰涓汉鍏紬鍙 -3. 鍧氭寔6鐐瑰崐璧峰簥锛屾棭璧峰凡缁忎笉浼氭湁浠讳綍鐨勪笉閫 -4. 鐭ヤ箮鏈1490涓叧娉紝鍏紬鍙锋湁248涓叧娉紝c++瀛︿範缇ゅ垰濂100浜猴紝杩欎簺閮芥槸瀛︿範涓婄殑闄即锛屽彲浠ョ粡甯镐竴璧锋帰璁ㄩ棶棰橈紝鍒嗕韩瀛︿範鏂规硶锛岀浉浜掔洃鐫c - -涔熸槸涓轰簡璁板康瀛︿範缇ゆ弧100浜猴紝鍜屽皬浼欎即浠晢閲忕粍缁囦竴涓椿鍔細鐧句汉鍒穕eetcode澶т綔鎴橈紒锛 - -![](https://github.com/githubwoniu/learnprogram/blob/master/image/100leetcode.png) - -娆㈣繋澶у杞彂閭璇峰崱锛岄個璇峰ソ鍙嬩竴璧峰涔狅紝涓璧疯繘姝ャ - ### 鍏蜂綋鐨凣itHub鎿嶄綔濡備笅 #### 1. Fork From f5801dceb4727e41d7917d14b24f230386525ff0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BF=97=E5=B3=B0?= <380238062@qq.com> Date: Sun, 28 May 2017 08:44:37 +0800 Subject: [PATCH 68/72] =?UTF-8?q?Create=20=E8=AF=B4=E6=98=8E.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "leetcode/20/\350\257\264\346\230\216.md" | 48 +++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 "leetcode/20/\350\257\264\346\230\216.md" diff --git "a/leetcode/20/\350\257\264\346\230\216.md" "b/leetcode/20/\350\257\264\346\230\216.md" new file mode 100644 index 0000000..b22c1ec --- /dev/null +++ "b/leetcode/20/\350\257\264\346\230\216.md" @@ -0,0 +1,48 @@ +### 鍏蜂綋鐨凣itHub鎿嶄綔濡備笅 + +#### 1. Fork + +https://github.com/githubwoniu/learnprogram + +#### 2. 鍒涘缓涓涓柊鏂囦欢 + +1. 杩涘叆鑷繁鐨勪粨搴 +2. 杩涘叆鐩綍learnprogram/leetcode/09/ +3. 鐐瑰嚮Create New File +4. 鏂囦欢鍚嶅彲浠ユ槸`note_鐢ㄦ埛鍚.md`鐨勫舰寮忥紝濡俙note_githubwoniu.md` + +#### 3. 娣诲姞瑙i绗旇 + +杩欐瑕佽В鐨勯鐩槸 + +[leetcode 20](https://leetcode.com/problems/valid-parentheses/#/description) + +绗旇鏍煎紡鍙傝 + +note.md + +**涓嶈淇敼姝ゆ枃浠** + +#### 4. Commit + +#### 5. Pull Request + +# 鏇村github鎿嶄綔鎸囧崡 + +璇锋煡鐪嬶紝浠撳簱棣栭〉鐨勬枃绔 + +https://github.com/githubwoniu/learnprogram + +# 娉ㄦ剰 + +鍏紬鍙烽殢鏈洪夊彇鏈粨搴撶殑鎻愪氦渚涘ぇ瀹跺悙绯燂紝浠ヤ究鐩镐簰瀛︿範鎻愰珮銆 + +鍑℃彁浜ゅ埌鏈粨搴撶殑鏂囩珷鍗宠涓烘巿鏉冨叕浼楀彿浣跨敤 + +# 鍏虫敞鍏紬鍙 + +涓璧峰涔犲惂~ + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +鐢变簬寰俊缇や簩缁寸爜鍙湁涓冨ぉ鏈夋晥鏈燂紝杩欓噷灏变笉鏀句簡锛岃鍒板叕浼楀彿搴曢儴鑿滃崟鏍忔煡鎵俱 From 1696fc81ae1175399d794843b01c85ba5f4dd4cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BF=97=E5=B3=B0?= <380238062@qq.com> Date: Sun, 28 May 2017 08:49:03 +0800 Subject: [PATCH 69/72] =?UTF-8?q?Create=20=E7=AD=94=E9=A2=98=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=5F=E4=B8=8D=E8=A6=81=E4=BF=AE=E6=94=B9=E6=AD=A4?= =?UTF-8?q?=E6=96=87=E4=BB=B6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...71\346\255\244\346\226\207\344\273\266.md" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 "\347\255\224\351\242\230\346\240\274\345\274\217_\344\270\215\350\246\201\344\277\256\346\224\271\346\255\244\346\226\207\344\273\266.md" diff --git "a/\347\255\224\351\242\230\346\240\274\345\274\217_\344\270\215\350\246\201\344\277\256\346\224\271\346\255\244\346\226\207\344\273\266.md" "b/\347\255\224\351\242\230\346\240\274\345\274\217_\344\270\215\350\246\201\344\277\256\346\224\271\346\255\244\346\226\207\344\273\266.md" new file mode 100644 index 0000000..99d9a83 --- /dev/null +++ "b/\347\255\224\351\242\230\346\240\274\345\274\217_\344\270\215\350\246\201\344\277\256\346\224\271\346\255\244\346\226\207\344\273\266.md" @@ -0,0 +1,48 @@ +## 閾炬帴 + + +杩欓噷鏇存柊鏈棰樼洰鐨勯摼鎺 + + +## 棰樼洰 + +杩欓噷瀵归鐩仛鍑虹炕璇 + + + +## 閲婁箟 + +瀵硅棰樼洰鐨勭悊瑙 + + + + +## 琛ュ厖鎻忚堪 + + +鏈夊叾浠栨濊矾绛夊彲浠ュ啓鍦ㄨ繖閲 + + + +## 浠g爜 + + +鍏蜂綋鐨勫疄鐜颁唬鐮佹斁杩欙紝濡傛灉鏈夊涓疄鐜帮紝鍙鍒惰鍧楀浠斤紝骞舵坊鍔犵紪鍙凤紝濡傗## 浠g爜1鈥 + + + +```c++ + +//浠g爜鏀惧湪杩欎釜鍧楅噷闈紝鍙互楂樹寒鍏抽敭瀛 + + + +``` + + + +## 鏇村 + +![](https://github.com/githubwoniu/learnprogram/blob/master/image/erweima.png) + +PS: 璇蜂繚鐣欎簩缁寸爜閾炬帴锛屼互渚挎洿澶氫汉鍙備笌杩涙潵銆傝阿璋€ From fe7c9b3bf49834c149d55df7e53d70c824489cc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BF=97=E5=B3=B0?= <380238062@qq.com> Date: Sun, 28 May 2017 08:50:38 +0800 Subject: [PATCH 70/72] =?UTF-8?q?Update=20=E8=AF=B4=E6=98=8E.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "leetcode/20/\350\257\264\346\230\216.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/leetcode/20/\350\257\264\346\230\216.md" "b/leetcode/20/\350\257\264\346\230\216.md" index b22c1ec..692da96 100644 --- "a/leetcode/20/\350\257\264\346\230\216.md" +++ "b/leetcode/20/\350\257\264\346\230\216.md" @@ -19,7 +19,7 @@ https://github.com/githubwoniu/learnprogram 绗旇鏍煎紡鍙傝 -note.md +[绛旈鏍煎紡](https://github.com/githubwoniu/learnprogram/blob/master/%E7%AD%94%E9%A2%98%E6%A0%BC%E5%BC%8F_%E4%B8%8D%E8%A6%81%E4%BF%AE%E6%94%B9%E6%AD%A4%E6%96%87%E4%BB%B6.md) **涓嶈淇敼姝ゆ枃浠** From 35204b1b8f70c43b59c6f82c2472b2ee7781256d Mon Sep 17 00:00:00 2001 From: NashWong <791745839@qq.com> Date: Sat, 3 Jun 2017 13:25:24 +0800 Subject: [PATCH 71/72] Create node_Wanghonglu.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 涓嶅お浼氱敤github锛屼唬鐮佸啓鐨勪篃鏈夌偣low锛屼笉瑕佽绗戙傘傘傘 --- leetcode/20/node_Wanghonglu.md | 112 +++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 leetcode/20/node_Wanghonglu.md diff --git a/leetcode/20/node_Wanghonglu.md b/leetcode/20/node_Wanghonglu.md new file mode 100644 index 0000000..45d5a4d --- /dev/null +++ b/leetcode/20/node_Wanghonglu.md @@ -0,0 +1,112 @@ +class Solution { +public: + bool isValid(string s) { + char elem=0,left_elem=0,right_elem=0; + Stack *ptr = new Stack( (int)s.length() ); + for( int i=0;iPushStack(elem); + break; + case ']': + case ')': + case '}': + { + ptr->GetTopStack(left_elem); + switch( left_elem ) + { + case '[': + right_elem = ']'; + break; + case '(': + right_elem = ')'; + break; + case '{': + right_elem = '}'; + break; + } + if( right_elem == elem ) + { + ptr->PopStack(); + break; + } + else + { + delete ptr; + return false; + } + } + default: + { + delete ptr; + return false; + } + + } + } + if(ptr->IsEmptyStack()) + { + delete ptr; + return true; + } + delete ptr; + return false; + } +class Stack{ +public: + Stack( int size ) + { + m_size = size; + m_ptr = new char[size]; + memset( m_ptr, 0x00, size ); + m_top = 0; + } + ~Stack() + { + delete []m_ptr; + m_top = 0; + m_size = 0; + } + bool IsFullStack() + { + return m_size == m_top; + } + bool IsEmptyStack() + { + return m_top ==0; + } + bool PushStack( char elem ) + { + if( IsFullStack() ) + return false; + m_ptr[m_top] = elem; + m_top++; + return true; + } + bool PopStack( ) + { + if( IsEmptyStack()) + return false; + m_top--; + return true; + } + bool GetTopStack( char& elem ) + { + if( IsEmptyStack() ) + return false; + elem = m_ptr[m_top-1]; + return true; + } +private: + char* m_ptr; + int m_top; + int m_size; +}; + + +}; From 7e9d63649bcb28fc6cdd94af3c3a09e642754881 Mon Sep 17 00:00:00 2001 From: An Chen Date: Sat, 3 Jun 2017 14:01:33 +0800 Subject: [PATCH 72/72] =?UTF-8?q?=E6=9B=B4=E6=AD=A3=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/20/node_Wanghonglu.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/leetcode/20/node_Wanghonglu.md b/leetcode/20/node_Wanghonglu.md index 45d5a4d..180008e 100644 --- a/leetcode/20/node_Wanghonglu.md +++ b/leetcode/20/node_Wanghonglu.md @@ -1,3 +1,4 @@ +```c++ class Solution { public: bool isValid(string s) { @@ -110,3 +111,4 @@ private: }; +```