From c76d6555d396bf0d1135865f6badde7751e5d77a Mon Sep 17 00:00:00 2001 From: JZFamily Date: Fri, 6 Jul 2018 12:58:21 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E5=88=B7=E9=A2=98.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LeetCode-CN/刷题.md | 2028 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 1905 insertions(+), 123 deletions(-) diff --git a/LeetCode-CN/刷题.md b/LeetCode-CN/刷题.md index b07519f..e84299d 100644 --- a/LeetCode-CN/刷题.md +++ b/LeetCode-CN/刷题.md @@ -2,7 +2,6 @@ [TOC] # 作业 -[TOC] ## 627. 交换工资 @2018/06/20 ### jzf :517ms @@ -23,73 +22,73 @@ update salary ### 北河 -* 第一版 :392ms -```c++ -class Solution { -public: - int lengthOfLongestSubstring(string s) { - map v; - int max_num = 0; - for (int i = 0; i < s.size(); ++i) { - if (v.find(s[i]) != v.end()) { - if (max_num < v.size()) { - max_num = v.size(); - } - i = v.find(s[i])->second + 1; - v.clear(); - } - if (i >= s.size()) { - break; - } - v[s[i]] = i; - if (max_num < v.size()) { - max_num = v.size(); - } - } - return max_num; - } -}; - -``` -* 第2版 :32ms -```c++ -class Solution { -public: - int lengthOfLongestSubstring(string s) { - map v; - int max_num = 0; - for (int i = 0; i < s.size(); ++i) { - if (v.find(s[i]) != v.end()) { - if (max_num < v.size()) { - max_num = v.size(); - } - ++i; - while (i < s.size()) { - if (v.find(s[i]) != v.end()) { - ++i; - continue; + * 第一版 :392ms + ```c++ + class Solution { + public: + int lengthOfLongestSubstring(string s) { + map v; + int max_num = 0; + for (int i = 0; i < s.size(); ++i) { + if (v.find(s[i]) != v.end()) { + if (max_num < v.size()) { + max_num = v.size(); } - --i; - break; + i = v.find(s[i])->second + 1; + v.clear(); } if (i >= s.size()) { - return max_num; + break; + } + v[s[i]] = i; + if (max_num < v.size()) { + max_num = v.size(); } - i = v.find(s[i])->second + 1; - v.clear(); - } - if (i >= s.size()) { - break; - } - v[s[i]] = i; - if (max_num < v.size()) { - max_num = v.size(); } + return max_num; } - return max_num; - } -}; -``` + }; + + ``` + * 第2版 :32ms + ```c++ + class Solution { + public: + int lengthOfLongestSubstring(string s) { + map v; + int max_num = 0; + for (int i = 0; i < s.size(); ++i) { + if (v.find(s[i]) != v.end()) { + if (max_num < v.size()) { + max_num = v.size(); + } + ++i; + while (i < s.size()) { + if (v.find(s[i]) != v.end()) { + ++i; + continue; + } + --i; + break; + } + if (i >= s.size()) { + return max_num; + } + i = v.find(s[i])->second + 1; + v.clear(); + } + if (i >= s.size()) { + break; + } + v[s[i]] = i; + if (max_num < v.size()) { + max_num = v.size(); + } + } + return max_num; + } + }; + ``` ### jzf :252 ms 8 ```c++ class Solution { @@ -232,35 +231,35 @@ public: ### jzf -* 第一版 超时 复杂度 n*2^n -```c++ -class Solution { -public: int findTargetSumWays(vector& nums, int S) { - int n = 0; - int sum = 0; - int b = 0; - int size =nums.size(); - for (size_t k = 0; k & nums, int S) { + int n = 0; + int sum = 0; + int b = 0; + int size =nums.size(); + for (size_t k = 0; k vec; - if(x <0) - { - return false; - } - if(x ==0) - { - return true; - } - if(x%10 ==0) - { - return false; - } - for(size_t i =0;sh>0;i++) - { - vec.push_back(sh%10); - sh = sh/10; - } - vector::iterator i=vec.begin(); - vector::iterator j=vec.end()-1; - for(;i vec; + if(x <0) { return false; } + if(x ==0) + { + return true; + } + if(x%10 ==0) + { + return false; + } + for(size_t i =0;sh>0;i++) + { + vec.push_back(sh%10); + sh = sh/10; + } + vector::iterator i=vec.begin(); + vector::iterator j=vec.end()-1; + for(;i +class MyStack { +public: + /** Initialize your data structure here. */ + MyStack() { + + } + + /** Push element x onto stack. */ + void push(int x) { + lst.push_back(x); + } + + /** Removes the element on top of the stack and returns that element. */ + int pop() { + int t = lst.back(); + lst.pop_back(); + return t; + } + + /** Get the top element. */ + int top() { + return lst.back(); + } + + /** Returns whether the stack is empty. */ + bool empty() { + return lst.empty(); + } +private: + std::list lst; +}; +``` +## 28. 实现strStr() @2018/06/23 ### jzf @@ -680,14 +714,1762 @@ class Solution } }; ``` -### 北河 ## 823. Binary Trees With Factors@2018/06/23 ## 196. 删除重复的电子邮箱@2018/06/23 ### 北河 + * ```sql delete from Person where id in (select c.id from (select a.id from Person a join Person b on a.Email = b.Email and a.Id > b.Id) c); ``` +## 48. 旋转图像@2018/06/24 +### jzf +* 4ms +```c++ +class Solution { +public: + void rotate(vector>& matrix) { + + int tmp; + for(size_t i=0; i>& matrix) { + int n = matrix.size(); + if (n % 2) // odd number, easy + { + int mid = n / 2; + + for (int dis = 1; dis <= mid; dis++) + { + // do chain swap at distance:dis + int swap_n = 2 * dis + 1; + // save right col + vector tvec; + int begin_pos = mid - dis; + for (int i = 0; i < swap_n; i++) + { + tvec.push_back(matrix[begin_pos + i][mid + dis]); + } + // start rotate + // RIGHT <- UPPER + for (int i = 0; i < swap_n; i++) + { + //printf("matrix[%d][%d]<-matrix[%d][%d]\n", begin_pos + i, mid + dis, mid - dis, begin_pos + i); + matrix[begin_pos + i][mid + dis] = matrix[mid - dis][begin_pos + i]; + } + // UPPER <- LEFT + for (int i = 0; i < swap_n; i++) + { + //printf("matrix[%d][%d]<-matrix[%d][%d]\n", mid - dis, begin_pos + i, begin_pos + i, mid - dis); + matrix[mid - dis][begin_pos + (swap_n - i - 1)] = matrix[begin_pos + i][mid - dis]; + } + // LEFT <- DOWNER + for (int i = 0; i < swap_n; i++) + { + //printf("matrix[%d][%d]<-matrix[%d][%d]\n", begin_pos + i, mid - dis, mid + dis, begin_pos + i); + matrix[begin_pos + i][mid - dis] = matrix[mid + dis][begin_pos + i]; + } + // DOWNER <- TEMP + for (int i = 0; i < swap_n; i++) + { + //printf("matrix[%d][%d]<-tvec[%d]\n", mid + dis, begin_pos + i, i); + matrix[mid + dis][begin_pos + (swap_n - i - 1)] = tvec[i]; + } + + // debug + //print(matrix); + } + } + else // oh, this stuff sucks. + { + int mid = n / 2 ; + + for (int dis = 1; dis <= mid ; dis++) + { + // do chain swap at distance:dis + int swap_n = 2 * dis; + // save right col + vector tvec; + int begin_pos = mid - dis; + for (int i = 0; i < swap_n; i++) + { + tvec.push_back(matrix[begin_pos + i][mid + dis - 1]); + } + // start rotate + // RIGHT <- UPPER + for (int i = 0; i < swap_n; i++) + { + //printf("matrix[%d][%d]<-matrix[%d][%d]\n", begin_pos + i, mid + dis, mid - dis, begin_pos + i); + matrix[begin_pos + i][mid + dis - 1] = matrix[mid - dis][begin_pos + i]; + } + // UPPER <- LEFT + for (int i = 0; i < swap_n; i++) + { + //printf("matrix[%d][%d]<-matrix[%d][%d]\n", mid - dis, begin_pos + i, begin_pos + i, mid - dis); + matrix[mid - dis][begin_pos + (swap_n - i - 1)] = matrix[begin_pos + i][mid - dis]; + } + // LEFT <- DOWNER + for (int i = 0; i < swap_n; i++) + { + //printf("matrix[%d][%d]<-matrix[%d][%d]\n", begin_pos + i, mid - dis, mid + dis, begin_pos + i); + matrix[begin_pos + i][mid - dis] = matrix[mid + dis -1][begin_pos + i]; + } + // DOWNER <- TEMP + for (int i = 0; i < swap_n; i++) + { + //printf("matrix[%d][%d]<-tvec[%d]\n", mid + dis, begin_pos + i, i); + matrix[mid + dis -1][begin_pos + (swap_n - i - 1)] = tvec[i]; + } + + // debug + //print(matrix); + } + } + } +}; +``` +### puck + +```c++ +#include + +class Solution +{ + public: + void rotate(vector> &matrix) + { + auto n = matrix.size(); + auto max = n - 1; + auto half = n / 2; + + for (int i{}; i < half; ++i) + { + for (int j{}; j < half; ++j) + { + std::swap(matrix[i][j], matrix[j][max - i]); + std::swap(matrix[i][j], matrix[max - i][max - j]); + std::swap(matrix[i][j], matrix[max - j][i]); + } + } + + if (n == 2 * half + 1) + { + for (int i{}; i < half; ++i) + { + std::swap(matrix[i][half], matrix[half][max - i]); + std::swap(matrix[i][half], matrix[max - i][half]); + std::swap(matrix[i][half], matrix[half][i]); + } + } + } +}; +``` +## 4. 两个排序数组的中位数@2018/06/24 + +### 种子 似乎不符合复杂度限制 +```c++ +class Solution { +public: + double findMedianSortedArrays(vector& nums1, vector& nums2) { + copy(nums2.begin(),nums2.end(),back_inserter(nums1)); + sort(nums1.begin(),nums1.end()); + return nums1.size()%2?\ + double(nums1[nums1.size()/2]):\ + double(nums1[nums1.size()/2]+nums1[nums1.size()/2-1])/2; + } +}; +``` +### 北河 48ms +```c++ +class Solution { +public: + double findMedianSortedArrays(vector& nums1, vector& nums2) { + std::multiset v; + copy(nums1.begin(), nums1.end(), inserter(v, v.begin())); + copy(nums2.begin(), nums2.end(), inserter(v, v.begin())); + std::multiset::iterator p, q; + p = q = v.begin(); + return v.size() % 2 == 0 ? ((double)*(advance(p, v.size() / 2 - 1), p) + (double)*(advance(q, v.size() / 2), q))/ 2 : (double)*(advance(p, v.size() / 2), p); + } +}; +``` +### kiritow + +```c++ +#include +class Solution { +public: + void forward_until(vector& nums1, vector& nums2, + int lenA,int lenB, + int& idxA, int& idxB, int& passed, int midLen) + { + while (idxA < lenA&&idxB < lenB && passed < midLen) + { + if (nums1[idxA] < nums2[idxB]) + { + idxA++; + passed++; + } + else + { + idxB++; + passed++; + } + } + while (idxA < lenA&&passed < midLen) + { + idxA++; + passed++; + } + while (idxB < lenB&&passed < midLen) + { + idxB++; + passed++; + } + } + + int getCurrent(vector& nums1, vector& nums2, + int lenA,int lenB, + int idxA, int idxB) + { + if (idxA < lenA&&idxB < lenB) + { + return std::min(nums1[idxA], nums2[idxB]); + } + else if (idxA < lenA) + { + return nums1[idxA]; + } + else + { + return nums2[idxB]; + } + } + + double findMedianSortedArrays(vector& nums1, vector& nums2) { + int lenA = nums1.size(); + int lenB = nums2.size(); + int sumLen = lenA + lenB; + int midLen = sumLen / 2 - (1 - (sumLen % 2)); + int idxA = 0; + int idxB = 0; + int passed = 0; + + forward_until(nums1, nums2, lenA, lenB, idxA, idxB, passed, midLen); + + if (sumLen % 2) + { + return getCurrent(nums1, nums2, lenA, lenB, idxA, idxB); + } + else + { + int step1 = getCurrent(nums1, nums2, lenA, lenB, idxA, idxB); + + forward_until(nums1, nums2, lenA, lenB, idxA, idxB, passed, midLen + 1); + + int step2 = getCurrent(nums1, nums2, lenA, lenB, idxA, idxB); + + return (step1 + step2) / 2.0; + } + } +}; + +``` +### jzf + +算了,我就算是抄上边几个的吧。 + +### 小白 + +* 第一版 40ms +```c++ + +class Solution { +public: + + double findMedianSortedArrays(vector& nums1, vector& nums2) { + vector ab; + + std::merge(nums1.begin(), nums1.end(), nums2.begin(), nums2.end(), std::back_inserter(ab)); + + return (double(ab[(ab.size() >> 1) - !(ab.size() & 1)]) + double(ab[(ab.size() >> 1)])) *0.5; + } +}; +``` +* 第二版 + +```c++ +class Solution { +public: + + double findMedianSortedArrays(vector& nums1, vector& nums2) { + vector ab; + + std::merge(nums1.begin(), nums1.end(), nums2.begin(), nums2.end(), std::back_inserter(ab)); + + return (double(ab[(ab.size() >> 1) - !(ab.size() & 1)]) + double(ab[(ab.size() >> 1)])) *0.5; + } +}; +``` +* 第三版 40ms +```c++ + +class Solution { + +public: + + double findMedianSortedArrays(vector& nums1, vector& nums2) { + + vector ab; + + ab.reserve(nums1.size() + nums2.size()); + + std::merge(nums1.begin(), nums1.end(), nums2.begin(), nums2.end(), std::back_inserter(ab)); + + return double(ab[(ab.size() >> 1) - !(ab.size() & 1)] + ab[(ab.size() >> 1)]) * 0.5; + + } + +}; +``` + +### 总结 + +1,北河和种子的体现了使用STL的巨大优势,利用现有的工具,确实好很多。 + +而我在解题的过程中出现了自创轮子没本事放弃,和不熟悉STL等问题 + +2,算法还是要学的,STL的复杂度也要会分析 + +### 知识点 +1, std::merge +2,copy + +## 724. 寻找数组的中心索引@2018/06/25 + +### jzf 28ms +```c++ +class Solution { +public: + int pivotIndex(vector& nums) { + vector::iterator iter; + int sum =0; + int ret =-1; + for(iter =nums.begin();iter!=nums.end();iter++) + { + sum+=*iter; + } + + int tmp =0; + + for(size_t i=0;i=len?-1:i; +}; +``` +```c++ + +class Solution { +public: + int pivotIndex(vector& nums) { + int r = 0,l=0,len = nums.size(),i; + if(len<2)return -1; + for( i = 1; i < len; i++ )r += nums[i]; + for(i=0; l!=r && i=len?-1:i; + } +}; +``` +### puck +```c++ +#include + +class Solution +{ +public: + int pivotIndex(vector &nums) + { + int n{-1}; + auto iter = std::find_if(nums.begin(), nums.end(), [&](const int a) { + return ++n, std::accumulate(nums.begin(), nums.begin() + n, 0) == + std::accumulate(nums.begin() + n + 1, nums.end(), 0); + }); + + if (iter == nums.end()) + { + return -1; + } + return iter - nums.begin(); + } +}; +``` + +### 小白 +```c++ + +``` +## 5. 最长回文子串@2018/06/25 +### jzf +* 第一版 超时 +```c++ +class Solution { +public: + string longestPalindrome(string s) { + int len =s.size(); + string retStr=""; + for(size_t i = len; i>0;i--) + { + for(size_t j =0;j+i<=len;j++) + { + string str =s.substr(j,i); + string str2 =str; + reverse(str2.begin(),str2.end()); + + if(!str.compare(str2)) + { + retStr =str; + return retStr; + } + + } + } + return retStr; + } +}; +``` + +* 第二版 超时 +```c++ +class Solution { +public: + string longestPalindrome(string s) { + int len =s.size(); + string retStr=s.substr(0,1); + for(size_t i = len; i>1;i--) + { + for(size_t j =0;j+i<=len;j++) + { + string str =s.substr(j,i); + string str2 =str; + reverse(str2.begin(),str2.end()); + + if(!str.compare(str2)) + { + retStr =str; + return retStr; + } + + } + } + return retStr; + } +}; +``` +* 8ms +```c++ +class Solution { +public: +int func(string &str, int n) +{ + int ret = 1; + size_t k = 1; + for ( ;k <= n && (n + k)<=str.size(); k++) + { + if (str[n - k] == str[n + k + 1]) + { + ret ++; + } + else + { + break; + } + } + return ret; +} +int func2(string &str, int n) +{ + int ret = 1; + size_t k = 1; + for (; k <= n && (n + k+3) <= str.size(); k++) + { + if (str[n - k] == str[n + k+2]) + { + ret++; + } + else + { + break; + } + } + return ret; +} +string longestPalindrome(string s) { + int len = s.size(); + int max = 0; + int center = 0; + int f = 0; + if (len<2) + { + return s; + } + if (len == 2) + { + if (s[0] == s[1]) + { + return s; + } + else + { + return s.substr(0, 1); + } + } + + std::vector vec1; + for (size_t i = 0; imax) + { + max = tmp; + center = vec1[i]; + f = 1; + } + if (max + i>s.size()) + { + break; + } + } + std::vector vec2; + for (size_t i = 0; imax) + { + max = tmp ; + center = vec2[i]; + f = 2; + } + if (max + i>s.size()) + { + break; + } + } + if (f == 0) + { + return s.substr(0,1); + } + if (f == 1) + { + return s.substr(center - max+1, max * 2); + } + if (f == 2) + { + return s.substr(center -max+1, max * 2 + 1); + } +} +}; + +``` +### 小白 +* 第一版 超时 +```c++ +std::pair longestPalindrome(string::const_iterator a, string::const_iterator b) + +{ + + if (a==b || std::mismatch(a, b, make_reverse_iterator(b)).first == b) + + return std::make_pair(a, b); + + auto str1 = longestPalindrome(a + 1, b), str2 = longestPalindrome(a, b - 1); + + return (str1.second - str1.first) > (str2.second - str2.first) ? str1 : str2; + +} + +string longestPalindrome(const string &s) { + + auto pr = longestPalindrome(s.begin(), s.end()); + + return std::string(pr.first, pr.second); + +} +``` +```c++ + +class Solution { +public: + string longestPalindrome(string s) { + auto lr_max = std::make_pair( s.crend(), s.cbegin() ); + auto lr_dist_comp = [](decltype(lr_max) a, decltype(lr_max) b) + {return std::distance(a.first.base(), a.second) < + std::distance(b.first.base(), b.second); }; + for (auto cent = lr_max; cent != std::make_pair(s.crbegin(), s.cend()); std::distance(cent.first.base(), cent.second) == 0 ? ++cent.second : (--cent.first).base() ) + lr_max = std::max(lr_max, std::mismatch((cent.first - 1), s.crend(), cent.second), lr_dist_comp); + return std::string(lr_max.first.base(), lr_max.second); + } +}; + +``` + +## 384. 打乱数组@2018/06/26 +### jzf +```c++ +class Solution { + vector nums; +public: + Solution(vector nums) { + this->nums = nums; + } + + /** Resets the array to its original configuration and return it. */ + vector reset() { + return nums; + } + + /** Returns a random shuffling of the array. */ + vector shuffle() { + vector result(nums); + for (int i = 0;i < result.size();i++) { + int pos = rand()%(result.size()-i); + swap(result[i+pos], result[i]); + } + return result; + } +}; +``` +### 北河 +```c++ +class Solution { +public: + Solution(vector nums) :m_nums(nums.begin(), nums.end()) { + } + + /** Resets the array to its original configuration and return it. */ + vector reset() { + return m_nums; + } + + /** Returns a random shuffling of the array. */ + vector shuffle() { + vector v(m_nums.begin(), m_nums.end()); + random_shuffle(v.begin(), v.end()); + return v; + } +private: + vector m_nums; +}; +``` + +### 总结 +1, 北河的STL确实知道的很多,random_shuffle 惊艳了我 + +### 知识点 + +随机数 + +## 843. 猜猜这个单词@2018/06/26 +### 北河 +```c++ + +class Solution { + vector nums; +public: + Solution(vector nums) { + this->nums = nums; + } + + /** Resets the array to its original configuration and return it. */ + vector reset() { + return nums; + } + + /** Returns a random shuffling of the array. */ + vector shuffle() { + vector result(nums); + for (int i = 0;i < result.size();i++) { + int pos = rand()%(result.size()-i); + swap(result[i+pos], result[i]); + } + return result; + } +}; +``` +## 6. Z字形变换@2018/06/26 +### jzf +```c++ + +``` +## 180. 连续出现的数字@2018/06/26 +### jzf +```sql +select distinct s1.Num as ConsecutiveNums from Logs as s1 ,Logs as s2,Logs as s3 + where(( (s1.id= s2.id-1 and s1.id =s3.id-2) + ||(s1.id =s2.id+1 and s1.id =s3.id-1) + ||(s1.id =s2.id+2 and s1.id =s3.id+1)) + and(s1.Num =s2.Num and s1.Num =s3.Num)) +``` + +## 7. 反转整数@2018/06/27 +### jzf + +* 第一版 +```c++ +class Solution { +public: + int reverse(int x) { + char str[16]={0}; + sprintf(str,"%d",x); + string s =str; + if(s[0]=='-') + { + std::reverse(s.begin()+1,s.end()); + } + else + { + std::reverse(s.begin(),s.end()); + } + long ret =atol(s.c_str()); + + if(ret >INT_MAX||ret INT_MAX) ? 0 : res; + } +}; +``` +* 第三版 +```c++ +``` +### 小白 +* 第一版 出错 +```c++ +class Solution { +public: +int reverse(int x) { + + char str[12], *p = str + sprintf(str, "%d", x); + + std::reverse(str + (x < 0), p); + + int r = strtol(str, NULL, 10); + + return errno == ERANGE ? (errno = 0) : r; + +} +}; +``` + +* 第二版 +```c++ +int reverse(int x) { + auto str = to_string(x); + std::reverse(str.begin(), str.end()); + try + { + return str.back() == '-' ? -std::stoi(str): std::stoi(str); + } + catch (...) + { + return 0; + } + } +``` + +### 总结 + +1,他这边用 strtol 和spritnf("%ld")出错 + 原因不知 + +### 知识点 + +1,这边atoi strtoi 遇见不合适的字符会跳过,和停止 + +## 147. 对链表进行插入排序@2018/06/27 + +### 北河 16ms +```c++ +class Solution { +public: + ListNode* insertionSortList(ListNode* head) { + list v; + ListNode *p = head; + while (p) { v.push_back(p->val); p = p->next;} + v.sort(); + p = head; + for (list::iterator it = v.begin(); it != v.end(); ++it, p = p->next) { + p->val = *it; + } + return head; + } +}; +``` +### jzf +```c++ +class Solution { +public: + ListNode* insertionSortList(ListNode* head) { + ListNode* node =head; + if(head ==NULL) + { + return head; + } + vector vec; + while(node != NULL) + { + vec.push_back(node); + node =node->next; + } + sort(vec.begin(),vec.end(),[](ListNode*a,ListNode*b){return a->val val;}); + for(auto iter =vec.begin();iter!= vec.end();iter++) + { + if(iter==vec.end()-1) + { + (*iter)->next =NULL; + break; + } + (*iter)->next =*(iter+1); + + } + return *vec.begin(); + } +}; +``` +### puck + +```c++ + +class Solution +{ + public: + ListNode *insertionSortList(ListNode *head) + { + if (nullptr == head) + { + return nullptr; + } + + ListNode *fast{head->next}; + ListNode *slow{head}; + ListNode *new_head{head}; + ListNode *tmp[2]{}; + + while (nullptr != fast) + { + if (slow->val > fast->val) + { + // delete the node + slow->next = fast->next; + + if (new_head->val > fast->val) + { + fast->next = new_head; + new_head = fast; + } + else + { + tmp[0] = new_head; + tmp[1] = new_head->next; + while (nullptr != tmp[1]) + { + if (tmp[1]->val > fast->val) + { + fast->next = tmp[1]; + tmp[0]->next = fast; + break; + } + tmp[0] = tmp[1]; + tmp[1] = tmp[1]->next; + } + } + } + + slow = fast; + fast = fast->next; + } + + return new_head; + } +}; +``` + + + + +## 828. 独特字符串@2018/06/28 +### 北河 +```c++ + +``` +### jzf + +* 第一版超时 +```c++ +class Solution { +public: +int uniqueLetterString(string S) { + int sum = 0; + vector> vec; + for(size_t i =0;i> vecTmp; + vecTmp =vec; + vec.clear(); + string str=S.substr(i,1); + vec.push_back(make_pair(1,str)); + sum++; + if(i ==0) + { + continue; + } + for(size_t j=0;j> vec; + for(size_t i =0;i +#include + +class Solution +{ +public: + int uniqueLetterString(string S) + { + std::size_t sz = S.size(); + int result{}; + + auto ed = S.end(); + bool flg{true}; + for (auto iter{ S.begin() }; iter != ed; ++iter) + { + for (auto ite2{ 1 + iter }; flg; ++ite2) + { + if (ite2 == ed) + { + flg = false; + } + result += uniqueNum(string(iter, ite2)); + } + flg = true; + } + + return result; + } + +private: + int uniqueNum(string s) + { + //std::unordered_map map; + //for (string::value_type ch : s) + //{ + // ++map[ch]; + //} + + //int num{}; + //for (auto &pair : map) + //{ + // if (1 == pair.second) + // { + // ++num; + // } + //} + + //return num; + + short a[128]{}; + + for (string::value_type ch : s) + { + ++a[ch]; + } + + return std::count(std::begin(a), std::end(a), 1); + } +}; +``` + +## 693. 交替位二进制数@2018/06/29 + +### jzf +* 第一版 错误 +```c++ +class Solution { +public: + bool hasAlternatingBits(int n) { + // #define IS_TARGET(x) ((x&0xaaaaaaaa)&&(x&0x55555555)) + int x =n; + + if( ((x&0xaaaaaaaa)==x)&&((x|0xaaaaaaaa)==0xaaaaaaaa)) + { + int x =~n; + if( ((x&0x55555555)==x)&&((x|0x55555555)==0x55555555)) + { + return true; + } + else + { + return false; + } + } + + if( ((x&0x55555555)==x)&&((x|0x55555555)==0x55555555)) + { + int x =~n; + if( ((x&0xaaaaaaaa)==x)&&((x|0xaaaaaaaa)==0xaaaaaaaa)) + { + return true; + } + else + { + return false; + } + } + return false; + } +}; +``` + +* 第二版 0s +```c++ +class Solution { +public: + bool hasAlternatingBits(int n) { + + int tmp=0; + for(size_t i= n&1?0:1;i<31;) + { + tmp |=1<n) + { + return false; + } + else if(tmp ==n) + { + return true; + } + i +=2; + } + + return false; + + } +}; +``` + + +### 总结 + +1, 想通过按位与等方式解决问题的时候,对于异或同或之类的转换忘记了,结果绕了很大的弯子。数学还是要学一学的。 + +2,那个神奇的代码对提升速度真的很有效. + +## 63. 不同路径 II@2018/06/30 +### 北河 0ms +```c++ + +static auto XXX = []() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + return 0; +}(); + +class Solution { +public: + int uniquePathsWithObstacles(vector>& obstacleGrid) { + int m = obstacleGrid.size(); + int n = obstacleGrid[0].size(); + int v[m][n]; + memset(v, 0, m * n * sizeof(int)); + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (obstacleGrid[i][j] != 0) { + continue; + } + if (i == 0 && j != 0) { + v[i][j] = v[i][j - 1]; + } else if (j == 0 && i != 0) { + v[i][j] = v[i - 1][j]; + } else if ( i != 0 && j != 0) { + v[i][j] += obstacleGrid[i - 1][j] > 0 ? 0 : v[i - 1][j]; + v[i][j] += obstacleGrid[i][j - 1] > 0 ? 0 : v[i][j - 1]; + } else { + v[i][j] = 1; + } + } + } + return v[m - 1][n - 1]; + } +}; +``` +### jzf +```c++ +class Solution { +public: + int uniquePathsWithObstacles(vector>& obstacleGrid) { + int dp[101] ={0}; + for(int i =0;i& A, vector& B) { + int max=0; + + + for(size_t i =0 ;imax) + { + max =tmp; + } + } + } + return max; + } +}; +``` +* 第二版 dp +```c++ + +``` +### 北河 +```c++ +static auto XX = []() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + return 0; +}(); + +class Solution { +public: + int findLength(vector& A, vector& B) { + vector dp(1001); + int m = 0; + for (int i = A.size() - 1; i >= 0; --i) { + for (size_t j = 0; j < B.size(); ++j) { + if (A[i] == B[j]) { + dp[j] = dp[j + 1] + 1; + if (dp[j] > m) { + m = dp[j]; + } + } else { + dp[j] = 0; + } + } + } + return m; + } +}; + +``` +## 406. 根据身高重建队列@2018/07/02 未备份 + + +## 455. 分发饼干@2018/07/03 +### jzf +```c++ +class Solution { +public: + int findContentChildren(vector& g, vector& s) { + sort(g.begin(),g.end()); + sort(s.begin(),s.end()); + + int j =0; + int i=0; + int t =0; + for(;i10;q/=10) + { + s*=10; + } + for(;s>0;s/=10) + { + *(p++)=tmp/s+'0'; + tmp=tmp%s; + } + } + int monotoneIncreasingDigits(int N) { + // tostring(N); + char str[11]={0}; + tostr(N,str); + + int t=0; + for(size_t i=0;i<11 &&str[i]!=0;i++) + { + if(str[i] > str[i+1]) + { + t =i; + break; + } + } + if(t==0) + { + str[0]-=1; + for(int i= t+1;i<11 &&str[i]!=0;i++) + { + str[i] ='9'; + } + return turn2Int(str+t); + } + else if(str[t+1]==0) + { + return N;cout<<12; + } + else + { + int tmp =t; + while(t>0) + { + if(str[t]>str[t-1]) + { + str[t]-=1; + for(int i= t+1;i<11 &&str[i]!=0;i++) + { + str[i] ='9'; + } + return turn2Int(str); + } + t--; + } + + if(t==0) + { + + str[0]-=1; + for(int i= t+1;i<11 &&str[i]!=0;i++) + { + str[i] ='9'; + } + return turn2Int(str+t); + } + + } + return turn2Int(str+t); + } +}; +``` +## 313. 超级丑数@2018/07/05 无人做 +## 20. 有效的括号@2018/07/06 +### 羽柔子 +```javascript +var isValid = function (s) { + var l = new Array(); + return s.split("").every(e => { + switch (e) { + case '(': return l.push(1); + case ')': return l.pop() == 1; + case '{': return l.push(2); + case '}': return l.pop() == 2; + case '[': return l.push(3); + case ']': return l.pop() == 3; + }; + }) ? !l.length : false; +}; +``` +### jzf +* 第一版错误 +```c++ +class Solution { +public: + bool isClosed(char c, char dst) + { + switch (c) + { + case '[': + return dst == ']'; + case '{': + return dst == '}'; + case '(': + return dst == ')'; + default: + return false; + } + } + bool isOpen(char c) + { + switch (c) + { + case '[': + + case '{': + + case '(': + return true; + default: + return false; + } + } + bool check(size_t &i, string &s) + { + size_t j = i + 1; + bool ret=false; + for (; j0; k--) + { + + if (!isClosed(s[i + k], s[j - k])) + { + return false; + } + } + break; + } + } + i = j; + return ret; + } + bool isValid(string s) { + // '[''{''('')''}'']' + for (size_t i = 0; ip = (char *)malloc(size * sizeof(char)); + memset(pStack->p, 0, size); + pStack->iPos= 0; +} + +void DestroyStack(Stack *pStack) +{ + free(pStack->p); +} + +int Push(Stack *pStack, char ch) +{ + if (pStack->iPos >= 0) + { + *(pStack->p + pStack->iPos) = ch; + ++(pStack->iPos); + return 0; + } + return -1; +} + +int Pop(Stack *pStack) +{ + --(pStack->iPos); + if (pStack->iPos >= 0) + { + *(pStack->p + pStack->iPos) = '\0'; + return 0; + } + return -1; +} +char GetPairChar(char ch) +{ + switch (ch) + { + case ')': + return '('; + case '}': + return '{'; + + case ']': + return '['; + default: + return '\0'; + } +} + +bool IsPush(char ch) +{ + if (ch == '(' || ch == '{' || ch == '[') + { + return true; + } + return false; +} + +bool isValid(char *s) +{ + if ('\0' == *s) + { + return true; + } + int n = 0; + char chPre = *s; + if (!IsPush(chPre)) + { + return false; + } + + Stack myStack; + BulidStack(&myStack, strlen(s) + 1); + while (*s != '\0') + { + char chCur = *s; + if (IsPush(chCur)) + { + if (0 != Push(&myStack, chCur)) + { + return false; + } + } + else + { + if ((myStack.iPos >= 1) && + (myStack.p[myStack.iPos - 1] == GetPairChar(chCur))) + { + if (0 != Pop(&myStack)) + { + return false; + } + } + else + { + + if (0 != Push(&myStack, chCur)) + { + return false; + } + } + } + chPre = *s; + ++s; + } + if (0 == myStack.iPos) + { + DestroyStack(&myStack); + return true; + } + else + { + DestroyStack(&myStack); + return false; + } + +} +``` +### 总结 +1,我在做这道题的时候,出现了许多次的分析失误,疏忽了一些情形。 +2,发现 s.size()的返回类型是size_t ,遇到了一个比较悲惨的越界错误。 +size_t是unsigned int类型的 +当 for(int i=0; i