From 684c3c7a358a035f03b312b6364048dc556ff58d Mon Sep 17 00:00:00 2001 From: ShusenTang Date: Fri, 13 Sep 2019 23:08:41 +0800 Subject: [PATCH] add C++ indicator before code --- solutions/118. Pascal's Triangle.md | 2 +- solutions/119. Pascal's Triangle II.md | 2 +- solutions/121. Best Time to Buy and Sell Stock.md | 2 +- solutions/122. Best Time to Buy and Sell Stock II.md | 4 ++-- solutions/125. Valid Palindrome.md | 2 +- solutions/13. Roman to Integer.md | 2 +- solutions/136. Single Number.md | 2 +- solutions/14. Longest Common Prefix.md | 2 +- solutions/141. Linked List Cycle.md | 2 +- solutions/155. Min Stack.md | 2 +- solutions/160. Intersection of Two Linked Lists.md | 2 +- solutions/167. Two Sum II - Input array is sorted.md | 4 ++-- solutions/168. Excel Sheet Column Title.md | 2 +- solutions/169. Majority Element.md | 6 +++--- solutions/171. Excel Sheet Column Number.md | 2 +- solutions/172. Factorial Trailing Zeroes.md | 2 +- solutions/189. Rotate Array.md | 2 +- solutions/190. Reverse Bits.md | 4 ++-- solutions/191. Number of 1 Bits.md | 4 ++-- solutions/198. House Robber.md | 6 +++--- solutions/20. Valid Parentheses.md | 2 +- solutions/202. Happy Number.md | 2 +- solutions/203. Remove Linked List Elements.md | 2 +- solutions/204. Count Primes.md | 4 ++-- solutions/205. Isomorphic Strings.md | 2 +- solutions/206. Reverse Linked List.md | 4 ++-- solutions/21. Merge Two Sorted Lists.md | 2 +- solutions/217. Contains Duplicate.md | 2 +- solutions/219. Contains Duplicate II.md | 2 +- solutions/225. Implement Stack using Queues.md | 2 +- solutions/231. Power of Two.md | 2 +- solutions/232. Implement Queue using Stacks.md | 2 +- solutions/234. Palindrome Linked List.md | 2 +- solutions/237. Delete Node in a Linked List.md | 4 ++-- solutions/242. Valid Anagram.md | 4 ++-- solutions/258. Add Digits.md | 2 +- solutions/263. Ugly Number.md | 2 +- solutions/268. Missing Number.md | 4 ++-- solutions/27. Remove Element.md | 2 +- solutions/278. First Bad Version.md | 2 +- solutions/283. Move Zeroes.md | 2 +- solutions/290. Word Pattern.md | 2 +- solutions/303. Range Sum Query - Immutable.md | 2 +- solutions/326. Power of Three.md | 2 +- solutions/344. Reverse String.md | 2 +- solutions/345. Reverse Vowels of a String.md | 2 +- solutions/349. Intersection of Two Arrays.md | 2 +- solutions/35. Search Insert Position.md | 2 +- solutions/350. Intersection of Two Arrays II.md | 2 +- solutions/367. Valid Perfect Square.md | 2 +- solutions/374. Guess Number Higher or Lower.md | 2 +- solutions/38. Count and Say.md | 2 +- solutions/383. Ransom Note.md | 2 +- solutions/387. First Unique Character in a String.md | 2 +- solutions/389. Find the Difference.md | 2 +- solutions/400. Nth Digit.md | 2 +- solutions/409. Longest Palindrome.md | 2 +- solutions/414. Third Maximum Number.md | 2 +- solutions/415. Add Strings.md | 2 +- solutions/434. Number of Segments in a String.md | 2 +- solutions/438. Find All Anagrams in a String.md | 4 ++-- solutions/441. Arranging Coins.md | 4 ++-- solutions/443. String Compression.md | 2 +- solutions/447. Number of Boomerangs.md | 2 +- solutions/448. Find All Numbers Disappeared in an Array.md | 4 ++-- solutions/453. Minimum Moves to Equal Array Elements.md | 2 +- solutions/455. Assign Cookies.md | 2 +- solutions/459. Repeated Substring Pattern.md | 2 +- solutions/463. Island Perimeter.md | 2 +- solutions/475. Heaters.md | 6 +++--- solutions/485. Max Consecutive Ones.md | 2 +- solutions/496. Next Greater Element I.md | 2 +- solutions/500. Keyboard Row.md | 2 +- solutions/53. Maximum Subarray.md | 2 +- solutions/532. K-diff Pairs in an Array.md | 2 +- solutions/561. Array Partition I.md | 4 ++-- solutions/566. Reshape the Matrix.md | 2 +- solutions/58. Length of Last Word.md | 2 +- solutions/581. Shortest Unsorted Continuous Subarray.md | 2 +- solutions/605. Can Place Flowers.md | 2 +- solutions/628. Maximum Product of Three Numbers.md | 2 +- solutions/643. Maximum Average Subarray I.md | 2 +- solutions/66. Plus One.md | 2 +- solutions/661. Image Smoother.md | 2 +- solutions/665. Non-decreasing Array.md | 2 +- solutions/67. Add Binary.md | 2 +- solutions/69. Sqrt(x).md | 2 +- solutions/70. Climbing Stairs.md | 4 ++-- solutions/83. Remove Duplicates from Sorted List.md | 2 +- solutions/88. Merge Sorted Array.md | 2 +- 90 files changed, 110 insertions(+), 110 deletions(-) diff --git a/solutions/118. Pascal's Triangle.md b/solutions/118. Pascal's Triangle.md index f68f837..37ea361 100644 --- a/solutions/118. Pascal's Triangle.md +++ b/solutions/118. Pascal's Triangle.md @@ -2,7 +2,7 @@ # 思路 首先明白题目要求返回的是一个vector,其元素也是vector,按照题目规律构造每个vector即可。 # C++ -``` +``` C++ class Solution { public: vector> generate(int numRows) { diff --git a/solutions/119. Pascal's Triangle II.md b/solutions/119. Pascal's Triangle II.md index d978532..57547db 100644 --- a/solutions/119. Pascal's Triangle II.md +++ b/solutions/119. Pascal's Triangle II.md @@ -5,7 +5,7 @@ 在得到第i-1行结果时,可以从前往后依次更新数组里的值来得到第i行的结果,这是内层循环。 注意用一个pre来记录第i-1行里面第j个元素的前一个元素的值。 # C++ -``` +``` C++ class Solution { public: vector getRow(int rowIndex) { diff --git a/solutions/121. Best Time to Buy and Sell Stock.md b/solutions/121. Best Time to Buy and Sell Stock.md index 2dd6c81..b2ee9a5 100644 --- a/solutions/121. Best Time to Buy and Sell Stock.md +++ b/solutions/121. Best Time to Buy and Sell Stock.md @@ -4,7 +4,7 @@ 因此prices[j]肯定是prices[i]之前的元素中最小的那一个,所以从前往后遍历,并用min_price记录当前位置前的元素中最小的一个。 另外注意单独判断空数组的情况。 # C++ -``` +``` C++ class Solution { public: int maxProfit(vector& prices) { diff --git a/solutions/122. Best Time to Buy and Sell Stock II.md b/solutions/122. Best Time to Buy and Sell Stock II.md index 0f678b5..f6fbf90 100644 --- a/solutions/122. Best Time to Buy and Sell Stock II.md +++ b/solutions/122. Best Time to Buy and Sell Stock II.md @@ -10,7 +10,7 @@ max(dp[j] + max(prices[i+1] - prices[k])), 其中k属于j~i+1 # C++ 改进前: -``` +``` C++ class Solution { public: int maxProfit(vector& prices) { @@ -29,7 +29,7 @@ public: }; ``` 改进后: -``` +``` C++ class Solution { public: int maxProfit(vector& prices) { diff --git a/solutions/125. Valid Palindrome.md b/solutions/125. Valid Palindrome.md index d20ce90..e794ea2 100644 --- a/solutions/125. Valid Palindrome.md +++ b/solutions/125. Valid Palindrome.md @@ -9,7 +9,7 @@ 然后再用两个指针low和high从两头往中间遍历并比较大小即可。 时间复杂度O(n),空间复杂度O(1) # C++ -``` +``` C++ class Solution { private: int transformer(char c){ // 将所有字符映射到整数以方便比较 diff --git a/solutions/13. Roman to Integer.md b/solutions/13. Roman to Integer.md index 7b47fe3..1526a97 100644 --- a/solutions/13. Roman to Integer.md +++ b/solutions/13. Roman to Integer.md @@ -4,7 +4,7 @@ 用res代表结果,将代表罗马数字的字符串s从前往后遍历,如果当前字母对应的数大于等于下一个字母对应的数,则res加上对应的数,否则减。 注意:根据题意是没有可能发生s[i] < s[i + 1] < s[i + 2]的。 # C++ -``` +``` C++ class Solution { public: int romanToInt(string s) { diff --git a/solutions/136. Single Number.md b/solutions/136. Single Number.md index b96dd3a..95aeacf 100644 --- a/solutions/136. Single Number.md +++ b/solutions/136. Single Number.md @@ -10,7 +10,7 @@ unordered_map: 时间复杂度O(n), 空间复杂度O(N) 时间复杂度O(n), 空间复杂度O(1), 完美 # C++ ## 思路二 -``` +``` C++ class Solution { public: int singleNumber(vector& nums) { diff --git a/solutions/14. Longest Common Prefix.md b/solutions/14. Longest Common Prefix.md index bb579df..409eb85 100644 --- a/solutions/14. Longest Common Prefix.md +++ b/solutions/14. Longest Common Prefix.md @@ -5,7 +5,7 @@ 注意: append函数可以用来在字符串的末尾追加字符和字符串。由于string重载了运算符,也可以用+=操作实现。 # C++ -``` +``` C++ class Solution { public: string longestCommonPrefix(vector& strs) { diff --git a/solutions/141. Linked List Cycle.md b/solutions/141. Linked List Cycle.md index b2fd454..10a773f 100644 --- a/solutions/141. Linked List Cycle.md +++ b/solutions/141. Linked List Cycle.md @@ -4,7 +4,7 @@ 设置两个指针p1和p2,用步长分别为1和2从前往后遍历,若链表是环则p1和p2总会相遇。 时间复杂度O(n),空间复杂度O(1) # C++ - ``` + ``` C++ /** * Definition for singly-linked list. * struct ListNode { diff --git a/solutions/155. Min Stack.md b/solutions/155. Min Stack.md index 3645c6f..eda65ba 100644 --- a/solutions/155. Min Stack.md +++ b/solutions/155. Min Stack.md @@ -3,7 +3,7 @@ 实现最小栈,要求所有操作的时间复杂度都为O(1)。 可考虑用两个站S1、S2,S1就正常记录minStack的值,而S2的栈顶记录了当前Minstack的最小值。S2的更新规则见代码。 # C++ -``` +``` C++ class MinStack { private: stack s1; diff --git a/solutions/160. Intersection of Two Linked Lists.md b/solutions/160. Intersection of Two Linked Lists.md index 5743db3..63c11bc 100644 --- a/solutions/160. Intersection of Two Linked Lists.md +++ b/solutions/160. Intersection of Two Linked Lists.md @@ -5,7 +5,7 @@ 直到p1 == p2即到达所求节点c1或者遇到NULL。 时间复杂度O(n), 空间复杂度O(1) # C++ -``` +``` C++ /** * Definition for singly-linked list. * struct ListNode { diff --git a/solutions/167. Two Sum II - Input array is sorted.md b/solutions/167. Two Sum II - Input array is sorted.md index 3ddfde1..81623ac 100644 --- a/solutions/167. Two Sum II - Input array is sorted.md +++ b/solutions/167. Two Sum II - Input array is sorted.md @@ -10,7 +10,7 @@ map查找的复杂度为O(logn),所以总的时间复杂度O(nlogn), 空间复 此时时间复杂度O(n), 空间复杂度O(1). # C++ ## 思路一 -``` +``` C++ class Solution { public: vector twoSum(vector& numbers, int target) { @@ -28,7 +28,7 @@ public: }; ``` ## 思路二 -``` +``` C++ class Solution { public: vector twoSum(vector& numbers, int target) { diff --git a/solutions/168. Excel Sheet Column Title.md b/solutions/168. Excel Sheet Column Title.md index fbd00a7..b13d2c1 100644 --- a/solutions/168. Excel Sheet Column Title.md +++ b/solutions/168. Excel Sheet Column Title.md @@ -3,7 +3,7 @@ 题目的要求相当于是十进制转二十六进制。用一个循环每次对n取模然后n除26进入下一次循环即可。 不过需要注意的是,题目给的是1-26对应A-Z而不是0-25对应A-Z,所以每次循环时都要对n作自减操作。 # C++ -``` +``` C++ class Solution { public: string convertToTitle(int n) { diff --git a/solutions/169. Majority Element.md b/solutions/169. Majority Element.md index 2c0937c..69245b3 100644 --- a/solutions/169. Majority Element.md +++ b/solutions/169. Majority Element.md @@ -13,7 +13,7 @@ 时间复杂度O(n)。 # C++ ## 思路一 -``` +``` C++ // 提交结果为16ms class Solution { public: @@ -24,7 +24,7 @@ public: }; ``` ## 思路二 -``` +``` C++ // 提交结果为12ms,较思路一有提升 class Solution { public: @@ -40,7 +40,7 @@ public: }; ``` ## 思路三 -``` +``` C++ // 提交结果20ms class Solution { public: diff --git a/solutions/171. Excel Sheet Column Number.md b/solutions/171. Excel Sheet Column Number.md index 4c5cbd2..649e6f8 100644 --- a/solutions/171. Excel Sheet Column Number.md +++ b/solutions/171. Excel Sheet Column Number.md @@ -3,7 +3,7 @@ 第[168题](https://leetcode.com/problems/excel-sheet-column-title/description/)是将10进制转换为26进制,这题是将26进制转换为10进制. 例:将k进制数"abcd"转换为10进制数:`res = d * k^0 + c * k^1 + b * k^2 + a * k^3`. # C++ -``` +``` C++ class Solution { public: int titleToNumber(string s) { diff --git a/solutions/172. Factorial Trailing Zeroes.md b/solutions/172. Factorial Trailing Zeroes.md index d16ef00..261b3fc 100644 --- a/solutions/172. Factorial Trailing Zeroes.md +++ b/solutions/172. Factorial Trailing Zeroes.md @@ -15,7 +15,7 @@ n = 4617. 所以 4617! 有 923 + 184 + 36 + 7 + 1 = 1151 个尾0. [参考](https://leetcode.com/problems/factorial-trailing-zeroes/discuss/52373/Simple-CC++-Solution-(with-detailed-explaination)) # C++ -``` +``` C++ class Solution { public: int trailingZeroes(int n) { diff --git a/solutions/189. Rotate Array.md b/solutions/189. Rotate Array.md index b4593b2..7164ab3 100644 --- a/solutions/189. Rotate Array.md +++ b/solutions/189. Rotate Array.md @@ -7,7 +7,7 @@ 再对后面的元素翻转:【5,6,7,1,2,3,4]。 时间复杂度O(n), 空间复杂度O(1)。 # C++ -``` +``` C++ class Solution { public: void rotate(vector& nums, int k) { diff --git a/solutions/190. Reverse Bits.md b/solutions/190. Reverse Bits.md index 94bd9c9..0b11b8c 100644 --- a/solutions/190. Reverse Bits.md +++ b/solutions/190. Reverse Bits.md @@ -17,7 +17,7 @@ # C++ ## 思路一 -``` +``` C++ class Solution { public: uint32_t reverseBits(uint32_t n) { @@ -33,7 +33,7 @@ public: }; ``` ## 思路二 -``` +``` C++ class Solution { public: uint32_t reverseBits(uint32_t n) { diff --git a/solutions/191. Number of 1 Bits.md b/solutions/191. Number of 1 Bits.md index 25c300f..dc70916 100644 --- a/solutions/191. Number of 1 Bits.md +++ b/solutions/191. Number of 1 Bits.md @@ -8,7 +8,7 @@ 依然是循环,但是每次循环不是得到最低位的值,而是每次循环去掉一个1,用一个count计数即可得到答案。 # C++ ## 思路一 -``` +``` C++ // 方法1 class Solution { public: @@ -37,7 +37,7 @@ public: }; ``` ## 思路二* -``` +``` C++ class Solution { public: int hammingWeight(uint32_t n) { diff --git a/solutions/198. House Robber.md b/solutions/198. House Robber.md index 021df32..b0cc76c 100644 --- a/solutions/198. House Robber.md +++ b/solutions/198. House Robber.md @@ -12,7 +12,7 @@ ## 空间优化且不改变原数组 用pre记录dp[i-1],这样既不改变原数组nums也使得空间复杂度为o(1), 完美 # C++ -``` +``` C++ class Solution { public: int rob(vector& nums){ @@ -27,7 +27,7 @@ public: }; ``` ## 空间优化 -``` +``` C++ class Solution { public: int rob(vector& nums){ @@ -40,7 +40,7 @@ public: }; ``` ## 空间优化且不修改原数组 -``` +``` C++ class Solution { public: int rob(vector& nums){ diff --git a/solutions/20. Valid Parentheses.md b/solutions/20. Valid Parentheses.md index 24d7b42..63c51ee 100644 --- a/solutions/20. Valid Parentheses.md +++ b/solutions/20. Valid Parentheses.md @@ -4,7 +4,7 @@ 退出循环后,若栈不空,说明还剩下未配对的左括号,则应该返回false。 时间复杂度O(n), 空间复杂度O(n) # C++ -``` +``` C++ class Solution { private: bool isLegal(const char& a, const char&b){ diff --git a/solutions/202. Happy Number.md b/solutions/202. Happy Number.md index e1ca4a4..74da967 100644 --- a/solutions/202. Happy Number.md +++ b/solutions/202. Happy Number.md @@ -2,7 +2,7 @@ # 思路 按照题目的意思进行循环,判断每次循环结果是否为1,若是则返回true,否则用map记录结果,然后修改n进行下一次循环。 # C++ -``` +``` C++ class Solution { private: int count_sum(int n){ // 定义题目中所述的求和函数 diff --git a/solutions/203. Remove Linked List Elements.md b/solutions/203. Remove Linked List Elements.md index 60a01ea..2f88d0f 100644 --- a/solutions/203. Remove Linked List Elements.md +++ b/solutions/203. Remove Linked List Elements.md @@ -8,7 +8,7 @@ 最后将p指向pre的next。 为了操作方便,可以设置一个头结点。 # C++ -``` +``` C++ /** * Definition for singly-linked list. * struct ListNode { diff --git a/solutions/204. Count Primes.md b/solutions/204. Count Primes.md index 08aeb8c..3e414d4 100644 --- a/solutions/204. Count Primes.md +++ b/solutions/204. Count Primes.md @@ -14,7 +14,7 @@ # C++ ## 思路一 -``` +``` C++ class Solution { private: bool isPrime(int n){ @@ -33,7 +33,7 @@ public: }; ``` ## 思路二 -``` +``` C++ class Solution { public: int countPrimes(int n) { diff --git a/solutions/205. Isomorphic Strings.md b/solutions/205. Isomorphic Strings.md index 87463ad..c47c3ec 100644 --- a/solutions/205. Isomorphic Strings.md +++ b/solutions/205. Isomorphic Strings.md @@ -6,7 +6,7 @@ 为了记录是否出现过,用map来实现,此外还用一个count计数。 时间复杂度O(nlogn) # C++ -``` +``` C++ class Solution { public: bool isIsomorphic(string s, string t) { diff --git a/solutions/206. Reverse Linked List.md b/solutions/206. Reverse Linked List.md index 8e35276..e7f0432 100644 --- a/solutions/206. Reverse Linked List.md +++ b/solutions/206. Reverse Linked List.md @@ -10,7 +10,7 @@ # C++ ## 思路一 -``` +``` C++ /** * Definition for singly-linked list. * struct ListNode { @@ -38,7 +38,7 @@ public: }; ``` ## 思路二 -``` +``` C++ /** * Definition for singly-linked list. * struct ListNode { diff --git a/solutions/21. Merge Two Sorted Lists.md b/solutions/21. Merge Two Sorted Lists.md index 3c90b43..1cd342a 100644 --- a/solutions/21. Merge Two Sorted Lists.md +++ b/solutions/21. Merge Two Sorted Lists.md @@ -3,7 +3,7 @@ 合并两个已有序的链表,注意题目给的链表没有头结点,所以为了操作方便可以自己设一个头结点,最后返回头结点的下一个节点即可。两个链表的工作指针就用传进来的l1和 l2即可。 # C++ -``` +``` C++ /** * Definition for singly-linked list. * struct ListNode { diff --git a/solutions/217. Contains Duplicate.md b/solutions/217. Contains Duplicate.md index 7a783b0..51b2b85 100644 --- a/solutions/217. Contains Duplicate.md +++ b/solutions/217. Contains Duplicate.md @@ -2,7 +2,7 @@ # 思路 先对数组进行排序,排序后重复的数一定是相邻的,再遍历一遍即可。 # C++ -``` +``` C++ class Solution { public: bool containsDuplicate(vector& nums) { diff --git a/solutions/219. Contains Duplicate II.md b/solutions/219. Contains Duplicate II.md index 75505b0..c6a4685 100644 --- a/solutions/219. Contains Duplicate II.md +++ b/solutions/219. Contains Duplicate II.md @@ -3,7 +3,7 @@ 判断数组是否有重复元素,而且重复元素下标差的绝对值不大于k,首先简单的思路就是用map,这里给出另一种解法: 定义一个结构体num_with_index记录数组元素的值和下标,然后再对结构体按照值进行排序,排序后重复元素肯定相邻,再判断下标是否满足条件即可。 # C++ -``` +``` C++ class Solution { struct num_with_index{ int num; diff --git a/solutions/225. Implement Stack using Queues.md b/solutions/225. Implement Stack using Queues.md index 00f83cf..ca6168e 100644 --- a/solutions/225. Implement Stack using Queues.md +++ b/solutions/225. Implement Stack using Queues.md @@ -5,7 +5,7 @@ 因为pop和top都要求找到栈顶元素,采用前者思路的话会产生混乱。 注意学习stl中queue的一些操作:push、pop、front # C++ -``` +``` C++ class MyStack { private: queueq; diff --git a/solutions/231. Power of Two.md b/solutions/231. Power of Two.md index 3084a99..c80b902 100644 --- a/solutions/231. Power of Two.md +++ b/solutions/231. Power of Two.md @@ -5,7 +5,7 @@ 2. 若n是不为1的奇数返回false; 3. 令n = n / 2,若n=1则返回true否则返回第2步。 # C++ -``` +``` C++ class Solution { public: bool isPowerOfTwo(int n) { diff --git a/solutions/232. Implement Queue using Stacks.md b/solutions/232. Implement Queue using Stacks.md index 5d63dc6..e8ba148 100644 --- a/solutions/232. Implement Queue using Stacks.md +++ b/solutions/232. Implement Queue using Stacks.md @@ -5,7 +5,7 @@ 若stk1不空,对队列入队的话直接对stk1入栈即可,否则要将stk2中所有元素pop到stk1中后再对stk1入栈。 若stk2不空,对队列出队的话直接对stk2出栈即可,否则要将stk1中所有元素pop到stk2中后再对stk2出栈。 # C++ -``` +``` C++ class MyQueue { private: stackstk1; diff --git a/solutions/234. Palindrome Linked List.md b/solutions/234. Palindrome Linked List.md index 46ceaf5..0c0841c 100644 --- a/solutions/234. Palindrome Linked List.md +++ b/solutions/234. Palindrome Linked List.md @@ -3,7 +3,7 @@ 判断所给链表是不是回文的,要求线性时间复杂度且空间复杂度为O(1)。 可以考虑现将链表的后半部分(或前半部分)反转一下,然后再设置两个指针p和q,初始分别指向前、后半部分的第一个节点,然后同时往后移动并判断值是否相等。 -``` +``` C++ /** * Definition for singly-linked list. * struct ListNode { diff --git a/solutions/237. Delete Node in a Linked List.md b/solutions/237. Delete Node in a Linked List.md index 0c3291f..a99303c 100644 --- a/solutions/237. Delete Node in a Linked List.md +++ b/solutions/237. Delete Node in a Linked List.md @@ -8,7 +8,7 @@ 为什么要移动所有节点的值呢,我们仅仅需要移动node后面第一个元素值就可以了。即令node -> val = node -> next -> val, 然后删除node->next即可。 # C++ ## 思路一 -``` +``` C++ /** * Definition for singly-linked list. * struct ListNode { @@ -32,7 +32,7 @@ public: }; ``` ## 思路二 -``` +```C++ /** * Definition for singly-linked list. * struct ListNode { diff --git a/solutions/242. Valid Anagram.md b/solutions/242. Valid Anagram.md index 7b2aef5..10221cf 100644 --- a/solutions/242. Valid Anagram.md +++ b/solutions/242. Valid Anagram.md @@ -8,7 +8,7 @@ 由此可见,只用分配一个数组即可,对s中出现的字母进行次数累加,对t中的出现的字母进行次数累减。 # C++ ## 思路一 -``` +``` C++ class Solution { public: bool isAnagram(string s, string t) { @@ -26,7 +26,7 @@ public: }; ``` ## 思路二 -``` +``` C++ class Solution { public: bool isAnagram(string s, string t) { diff --git a/solutions/258. Add Digits.md b/solutions/258. Add Digits.md index 04429d0..8e900bb 100644 --- a/solutions/258. Add Digits.md +++ b/solutions/258. Add Digits.md @@ -2,7 +2,7 @@ # 思路 没什么好说的,按照题目的意思,用两个while循环即可。 # C++ -``` +``` C++ class Solution { public: int addDigits(int num) { diff --git a/solutions/263. Ugly Number.md b/solutions/263. Ugly Number.md index 3916dc4..f6a10c7 100644 --- a/solutions/263. Ugly Number.md +++ b/solutions/263. Ugly Number.md @@ -3,7 +3,7 @@ 判断一个整数是否是‘丑陋的’。若一个整数是正数且其质因子仅包括2、3、5, 那么这个数是‘丑陋的’。特例,1也是’丑陋的‘。 首先若n非正,则直接返回false。否则将其分别除以2、3、5直到不能除进,若最后的结果是1则返回true否则返回false。 # C++ -``` +``` C++ class Solution { public: bool isUgly(int num) { diff --git a/solutions/268. Missing Number.md b/solutions/268. Missing Number.md index caffda2..4e78160 100644 --- a/solutions/268. Missing Number.md +++ b/solutions/268. Missing Number.md @@ -18,7 +18,7 @@ index: 0 1 2 3 4 5 6 此算法就肯定不会溢出了, 时间复杂度O(n), 空间复杂度O(1) # C++ ## 思路二 -``` +``` C++ class Solution { public: int missingNumber(vector& nums) { @@ -38,7 +38,7 @@ public: }; ``` ## 思路三 -``` +```C++ class Solution { public: int missingNumber(vector& nums) { diff --git a/solutions/27. Remove Element.md b/solutions/27. Remove Element.md index bb56161..0653d21 100644 --- a/solutions/27. Remove Element.md +++ b/solutions/27. Remove Element.md @@ -1,7 +1,7 @@ # 思路 类似第26题, 用count记录非val的个数,从前往后遍历,如果值为val则跳过,否则令nums[count并=nums[i]并自增count # C++ -``` +``` C++ class Solution { public: int removeElement(vector& nums, int val) { diff --git a/solutions/278. First Bad Version.md b/solutions/278. First Bad Version.md index 239080d..ab12f4d 100644 --- a/solutions/278. First Bad Version.md +++ b/solutions/278. First Bad Version.md @@ -5,7 +5,7 @@ 所以,我们不应该用`mid = (high + low) / 2`来更新mid而应该`mid = low + (high - low) / 2`。 > **以后的二分法都应该这样更新mid以防溢出!!!** # C++ -``` +```C++ // Forward declaration of isBadVersion API. bool isBadVersion(int version); diff --git a/solutions/283. Move Zeroes.md b/solutions/283. Move Zeroes.md index 953d558..85b5776 100644 --- a/solutions/283. Move Zeroes.md +++ b/solutions/283. Move Zeroes.md @@ -5,7 +5,7 @@ 为了找到这个合适的位置,用变量not_0记录当前元素之前有多少非0元素,若当前元素也是非0元素,则将该元素移到下标为not_0位置即可。 时间复杂度O(n),空间复杂度O(1) # C++ -``` +```C++ class Solution { public: void moveZeroes(vector& nums) { diff --git a/solutions/290. Word Pattern.md b/solutions/290. Word Pattern.md index 0db4c62..c70e8b3 100644 --- a/solutions/290. Word Pattern.md +++ b/solutions/290. Word Pattern.md @@ -4,7 +4,7 @@ 将pattern和str中个每个元素都用一个数代替,这个数代表了该元素是第几个出现的(如 "abba" -> 1221, "dog cat cat dog" -> 1221), 则结果应该是一样的。 为了记录是否出现过,pattern用一个长度为26的数组实现,str用map来实现,此外还用一个count计数。 # C++ -``` +```C++ class Solution { public: bool wordPattern(string pattern, string str) { diff --git a/solutions/303. Range Sum Query - Immutable.md b/solutions/303. Range Sum Query - Immutable.md index 1e658e0..994c216 100644 --- a/solutions/303. Range Sum Query - Immutable.md +++ b/solutions/303. Range Sum Query - Immutable.md @@ -3,7 +3,7 @@ 用一个数组sums记录和,sums[i]代表nums[0]到nums[i]的和,那么sumRange(i, j)就应该等于sum[i] - sum[i-1], 注意单独判断i得0时。 注意这种面向对象的代码风格。 # C++ -``` +```C++ class NumArray { private: vector sums; diff --git a/solutions/326. Power of Three.md b/solutions/326. Power of Three.md index f550d01..5855279 100644 --- a/solutions/326. Power of Three.md +++ b/solutions/326. Power of Three.md @@ -6,7 +6,7 @@ **以上方法适用于判断某数是否是某个质数(如2、3、5..)的幂的问题!!!** 更多解法参考[此处](https://leetcode.com/problems/power-of-three/discuss/77876/**-A-summary-of-all-solutions-(new-method-included-at-15:30pm-Jan-8th)) # C++ -``` +```C++ class Solution { public: bool isPowerOfThree(int n) { diff --git a/solutions/344. Reverse String.md b/solutions/344. Reverse String.md index 6bcaf40..9f9d8ab 100644 --- a/solutions/344. Reverse String.md +++ b/solutions/344. Reverse String.md @@ -3,7 +3,7 @@ 翻转字符串。 注意:交换元素时,最好用标准库里的swap,快一些。 # C++ -``` +```C++ class Solution { public: string reverseString(string s) { diff --git a/solutions/345. Reverse Vowels of a String.md b/solutions/345. Reverse Vowels of a String.md index 2a25e32..b3aaacc 100644 --- a/solutions/345. Reverse Vowels of a String.md +++ b/solutions/345. Reverse Vowels of a String.md @@ -2,7 +2,7 @@ # 思路 翻转字符串中的元音字母,即A、E、I、O、U、a、e、i、o、u。常规题 # C++ -``` +```C++ class Solution { private: bool isVowel(char c){ diff --git a/solutions/349. Intersection of Two Arrays.md b/solutions/349. Intersection of Two Arrays.md index 0027315..8a85d78 100644 --- a/solutions/349. Intersection of Two Arrays.md +++ b/solutions/349. Intersection of Two Arrays.md @@ -5,7 +5,7 @@ 时间复杂度O(nlogn) 当然也可以用map来查看是否有相同元素。 # C++ -``` +```C++ class Solution { public: vector intersection(vector& nums1, vector& nums2) { diff --git a/solutions/35. Search Insert Position.md b/solutions/35. Search Insert Position.md index f0bcabb..2e14544 100644 --- a/solutions/35. Search Insert Position.md +++ b/solutions/35. Search Insert Position.md @@ -2,7 +2,7 @@ # 思路 排序数组查找肯定就是二分法啦,但是题目没说一定是增序,根据结果来看应该全是增序。 # C++ -``` +``` C++ class Solution { public: int removeElement(vector& nums, int val) { diff --git a/solutions/350. Intersection of Two Arrays II.md b/solutions/350. Intersection of Two Arrays II.md index ff1af5c..73edd5f 100644 --- a/solutions/350. Intersection of Two Arrays II.md +++ b/solutions/350. Intersection of Two Arrays II.md @@ -4,7 +4,7 @@ 则先对两个数组进行排序,然后再用两个指针遍历一遍数组,合理更新指针即可获得两个数组的相同元素。 时间复杂度O(nlogn) # C++ -``` +```C++ class Solution { public: vector intersect(vector& nums1, vector& nums2) { diff --git a/solutions/367. Valid Perfect Square.md b/solutions/367. Valid Perfect Square.md index b9d9775..7886f9e 100644 --- a/solutions/367. Valid Perfect Square.md +++ b/solutions/367. Valid Perfect Square.md @@ -4,7 +4,7 @@ 可以考虑二分搜索1到num/2范围的数mid, 判断mid的平方是否等于num。 注意:如果直接计算`if(mid * mid == num)` 会存在溢出的情况,所以应该`if(num % mid == 0 && num / mid == mid)`。 # C++ -``` +```C++ class Solution { public: bool isPerfectSquare(int num) { diff --git a/solutions/374. Guess Number Higher or Lower.md b/solutions/374. Guess Number Higher or Lower.md index fa6afaf..20b55f6 100644 --- a/solutions/374. Guess Number Higher or Lower.md +++ b/solutions/374. Guess Number Higher or Lower.md @@ -3,7 +3,7 @@ 二分法。 注意计算mid的时候防止溢出,见代码。 # C++ -``` +```C++ // Forward declaration of guess API. // @param num, your guess // @return -1 if my number is lower, 1 if my number is higher, otherwise return 0 diff --git a/solutions/38. Count and Say.md b/solutions/38. Count and Say.md index 84e7faf..404bc3f 100644 --- a/solutions/38. Count and Say.md +++ b/solutions/38. Count and Say.md @@ -9,7 +9,7 @@ 搞懂意思后,按照规则模拟即可。 # C++ -``` +``` C++ class Solution { public: string countAndSay(int n) { diff --git a/solutions/383. Ransom Note.md b/solutions/383. Ransom Note.md index 8107a15..aea83d2 100644 --- a/solutions/383. Ransom Note.md +++ b/solutions/383. Ransom Note.md @@ -2,7 +2,7 @@ # 思路 用一个大小为26的数组count记录magazine中26个字母的出现次数,只要每个字母出现次数不小于ransomNote对应字母次数就行了。 # C++ -``` +```C++ class Solution { public: bool canConstruct(string ransomNote, string magazine) { diff --git a/solutions/387. First Unique Character in a String.md b/solutions/387. First Unique Character in a String.md index 8a8f8e8..017f810 100644 --- a/solutions/387. First Unique Character in a String.md +++ b/solutions/387. First Unique Character in a String.md @@ -4,7 +4,7 @@ 由于题目说了全部字符都是小写字母,所以开辟一个大小为26的数组记录字符出现次数即可。 时间复杂度O(n) # C++ -``` +```C++ class Solution { public: int firstUniqChar(string s) { diff --git a/solutions/389. Find the Difference.md b/solutions/389. Find the Difference.md index f7137de..6fd210a 100644 --- a/solutions/389. Find the Difference.md +++ b/solutions/389. Find the Difference.md @@ -5,7 +5,7 @@ 最后次数为-1的对应的字母即所求。 时间复杂度O(n), 空间复杂度O(1) # C++ -``` +```C++ class Solution { public: char findTheDifference(string s, string t) { diff --git a/solutions/400. Nth Digit.md b/solutions/400. Nth Digit.md index 0b13a71..a060b06 100644 --- a/solutions/400. Nth Digit.md +++ b/solutions/400. Nth Digit.md @@ -11,7 +11,7 @@ 注意: 当我们算base * k 的时候,结果可能超过int的表示范围,所以要定义成long long型。 # C++ -``` +```C++ class Solution { public: int findNthDigit(int n) { diff --git a/solutions/409. Longest Palindrome.md b/solutions/409. Longest Palindrome.md index 9dec7cf..1ead62d 100644 --- a/solutions/409. Longest Palindrome.md +++ b/solutions/409. Longest Palindrome.md @@ -5,7 +5,7 @@ 不过需要注意的是,如果出现了出现次数为奇数的字母,则最后的回文串长度按照上诉思路计算后还应该加1,此时回文串长度为奇数,例如dccaccd。 时间复杂度O(n), 空间复杂度O(1) # C++ -``` +```C++ class Solution { public: int longestPalindrome(string s) { diff --git a/solutions/414. Third Maximum Number.md b/solutions/414. Third Maximum Number.md index 4007f1b..cb5bf28 100644 --- a/solutions/414. Third Maximum Number.md +++ b/solutions/414. Third Maximum Number.md @@ -3,7 +3,7 @@ 题意就是找出数组第三大的数,注意相同的数算作一个数,可以考虑遍历三次,第一次得到最大的数max1,第二次得到第二大的数max2,第三次就得到了第三大的数max3。 注意可以用INT_MIN(在limits.h里)表示int型最小的数,另外需要用一个tag记录是否找到max3,否则当max3==INT_MIN时无法判断max3是否是真的第三大的值还是初始值。 # C++ -``` +```C++ class Solution { public: int thirdMax(vector& nums) { diff --git a/solutions/415. Add Strings.md b/solutions/415. Add Strings.md index 5fb7227..2a9381b 100644 --- a/solutions/415. Add Strings.md +++ b/solutions/415. Add Strings.md @@ -3,7 +3,7 @@ 题目要求计算两个大数的和。 可以先定义一位的加法digit_add,然后再从后往前遍历num1和num2并不断调用digit_add即可完成大数相加。 # C++ -``` +```C++ class Solution { private: int digit_add(const int d1, const int d2, int &cin){ diff --git a/solutions/434. Number of Segments in a String.md b/solutions/434. Number of Segments in a String.md index 92ce97d..0decaac 100644 --- a/solutions/434. Number of Segments in a String.md +++ b/solutions/434. Number of Segments in a String.md @@ -3,7 +3,7 @@ 计算一个字符串被空格分成了多少部分。 从头至尾遍历字符串,用tag标记当前字符的前一个字符是否是空格,若tag=1则前面是空格否则不是。若当前字符不是空格且tag==1则count应该加1。 # C++ -``` +```C++ class Solution { public: int countSegments(string s) { diff --git a/solutions/438. Find All Anagrams in a String.md b/solutions/438. Find All Anagrams in a String.md index 1d17b7d..df8d2f9 100644 --- a/solutions/438. Find All Anagrams in a String.md +++ b/solutions/438. Find All Anagrams in a String.md @@ -12,7 +12,7 @@ 这样,当char_num == 0的时候,表明我们的窗口中包含了p中的全部字符,得到一个结果。 # C++ ## 思路一 -``` +```C++ class Solution { private: bool isOK(vectorcount){ @@ -43,7 +43,7 @@ public: ``` ## 思路二 -``` +```C++ class Solution { public: vector findAnagrams(string s, string p) { diff --git a/solutions/441. Arranging Coins.md b/solutions/441. Arranging Coins.md index b6c6a45..2b7841b 100644 --- a/solutions/441. Arranging Coins.md +++ b/solutions/441. Arranging Coins.md @@ -10,7 +10,7 @@ # C++ ## 思路一 -``` +```C++ class Solution { public: int arrangeCoins(int n) { @@ -25,7 +25,7 @@ public: }; ``` ## 思路二 -``` +```C++ class Solution { public: int arrangeCoins(int n) { diff --git a/solutions/443. String Compression.md b/solutions/443. String Compression.md index fc5c216..43cf621 100644 --- a/solutions/443. String Compression.md +++ b/solutions/443. String Compression.md @@ -3,7 +3,7 @@ 根据题意压缩字符串。 用res记录当前处理过的字符串压缩后的长度,num记录当前字符出现了多少次。 # C++ -``` +```C++ class Solution { public: int compress(vector& chars) { diff --git a/solutions/447. Number of Boomerangs.md b/solutions/447. Number of Boomerangs.md index e56de55..402300f 100644 --- a/solutions/447. Number of Boomerangs.md +++ b/solutions/447. Number of Boomerangs.md @@ -6,7 +6,7 @@ 那么我们问题就变成了遍历所有点,让每个点都做一次点a,然后遍历其他所有点,统计和a距离相等的点有多少个,然后分别带入n(n-1)计算结果并累加到res中。 时间复杂度O(n^2) # C++ -``` +```C++ class Solution { public: int numberOfBoomerangs(vector>& points) { diff --git a/solutions/448. Find All Numbers Disappeared in an Array.md b/solutions/448. Find All Numbers Disappeared in an Array.md index 14e32cb..1a9e557 100644 --- a/solutions/448. Find All Numbers Disappeared in an Array.md +++ b/solutions/448. Find All Numbers Disappeared in an Array.md @@ -24,7 +24,7 @@ 两次遍历,所以时间复杂度O(n) # C++ ## 思路一 -``` +```C++ class Solution { public: vector findDisappearedNumbers(vector& nums) { @@ -50,7 +50,7 @@ public: }; ``` ## 思路二 -``` +```C++ class Solution { public: vector findDisappearedNumbers(vector& nums) { diff --git a/solutions/453. Minimum Moves to Equal Array Elements.md b/solutions/453. Minimum Moves to Equal Array Elements.md index 2d7fcd3..540a2a9 100644 --- a/solutions/453. Minimum Moves to Equal Array Elements.md +++ b/solutions/453. Minimum Moves to Equal Array Elements.md @@ -4,7 +4,7 @@ 所以这题就转换成每次将一个元素减1,需要进行多少次才能让所有元素相等。由于只能减,所以最终元素都应该变成原数组的最小值,而所求次数则是所有元素与最小值的差的和。 时间复杂度O(n),空间复杂度O(1) # C++ -``` +```C++ class Solution { public: int minMoves(vector& nums) { diff --git a/solutions/455. Assign Cookies.md b/solutions/455. Assign Cookies.md index 0316d95..822a5d3 100644 --- a/solutions/455. Assign Cookies.md +++ b/solutions/455. Assign Cookies.md @@ -7,7 +7,7 @@ 时间复杂度O(n)。 # C++ -``` +```C++ class Solution { public: int findContentChildren(vector& g, vector& s) { diff --git a/solutions/459. Repeated Substring Pattern.md b/solutions/459. Repeated Substring Pattern.md index 2fa122d..1955c84 100644 --- a/solutions/459. Repeated Substring Pattern.md +++ b/solutions/459. Repeated Substring Pattern.md @@ -17,7 +17,7 @@ substr(pos, len): 从位置pos开始,跨越len个字符(或直到字符串 # C++ ## 思路一 -``` +```C++ class Solution { private: // bool isOK(string s, const int &sub_len){ diff --git a/solutions/463. Island Perimeter.md b/solutions/463. Island Perimeter.md index 435ff7b..2b4a0a0 100644 --- a/solutions/463. Island Perimeter.md +++ b/solutions/463. Island Perimeter.md @@ -4,7 +4,7 @@ 我们只需要遍历一遍grid,对于每个方块,根据是否是边界和周围方块的情况进行合适的周长累加即可。 时间复杂度O(n) # C++ -``` +```C++ class Solution { public: int islandPerimeter(vector>& grid) { diff --git a/solutions/475. Heaters.md b/solutions/475. Heaters.md index 93b7a62..542ec0f 100644 --- a/solutions/475. Heaters.md +++ b/solutions/475. Heaters.md @@ -19,7 +19,7 @@ lower_bound(begin_it, end_it, target)返回的是处于两个迭代器begin_it # C++ ## 思路一 -``` +```C++ class Solution { private: int find_max_dist(vector&sorted_heaters, int &h){ # 二分查找h @@ -48,7 +48,7 @@ public: }; ``` ## 思路二 -``` +```C++ class Solution { private: int find_max_dist(vector&sorted_heaters, int &h){ @@ -70,7 +70,7 @@ public: }; ``` ## 思路三* -``` +```C++ class Solution { public: int findRadius(vector& houses, vector& heaters) { diff --git a/solutions/485. Max Consecutive Ones.md b/solutions/485. Max Consecutive Ones.md index 0745266..52d3182 100644 --- a/solutions/485. Max Consecutive Ones.md +++ b/solutions/485. Max Consecutive Ones.md @@ -7,7 +7,7 @@ 注意跳出循环后也要判断一次是否更新max。 # C++ -``` +```C++ class Solution { public: int findMaxConsecutiveOnes(vector& nums) { diff --git a/solutions/496. Next Greater Element I.md b/solutions/496. Next Greater Element I.md index 94569f8..3629cc3 100644 --- a/solutions/496. Next Greater Element I.md +++ b/solutions/496. Next Greater Element I.md @@ -8,7 +8,7 @@ 所以栈里的元素肯定是自底向上递减的,例如若nums=[1 6 5 3 4],则当遍历到3时,栈s为[6,5],而3 < s.top() = 5所以直到此时依然没有找到5的Next-Greater-Element,即5不应该pop出来。遍历到4时,s为[6,5,3],而4 > s.top() = 3, 所以3的Next-Greater-Element找到了,应该将其pop出来,然后4 < s.top() = 5,所以5不应该pop。 时间复杂度O(n) # C++ -``` +```C++ class Solution { public: vector nextGreaterElement(vector& findNums, vector& nums) { diff --git a/solutions/500. Keyboard Row.md b/solutions/500. Keyboard Row.md index c999175..4da3d72 100644 --- a/solutions/500. Keyboard Row.md +++ b/solutions/500. Keyboard Row.md @@ -4,7 +4,7 @@ 我们可以将字母按照键盘布局分类,从上到下分别为0、1、2类。对于每个字符串,遍历一遍,若属于同一类则可输出。 时间复杂度O(n) # C++ -``` +```C++ class Solution { private: int count_hash_index(char c){ diff --git a/solutions/53. Maximum Subarray.md b/solutions/53. Maximum Subarray.md index 4699cae..d67be57 100644 --- a/solutions/53. Maximum Subarray.md +++ b/solutions/53. Maximum Subarray.md @@ -4,7 +4,7 @@ 如果currsum大于0,那么currsum += nums[i]; 否则currsum = nums[i] # C++ -``` +``` C++ class Solution { public: int maxSubArray(vector& nums) { diff --git a/solutions/532. K-diff Pairs in an Array.md b/solutions/532. K-diff Pairs in an Array.md index 37846da..8e2a911 100644 --- a/solutions/532. K-diff Pairs in an Array.md +++ b/solutions/532. K-diff Pairs in an Array.md @@ -9,7 +9,7 @@ * k = 0时,上述方法会失效,不过提前增加一个判断即可,见代码。 * 没说输入的k一定非负,但k应该满足非负才有意义,所以当k小于0时直接返回0即可。 # C++ -``` +```C++ class Solution { public: int findPairs(vector& nums, int k) { diff --git a/solutions/561. Array Partition I.md b/solutions/561. Array Partition I.md index 6e81f64..4ba3934 100644 --- a/solutions/561. Array Partition I.md +++ b/solutions/561. Array Partition I.md @@ -10,7 +10,7 @@ min(ai, bi)操作可以看做是去掉一个二者之间较大的数,即我们 # C++ ## 思路一 -``` +```C++ class Solution { public: int arrayPairSum(vector& nums) { @@ -23,7 +23,7 @@ public: }; ``` ## 思路二 -``` +```C++ class Solution { public: int arrayPairSum(vector& nums) { diff --git a/solutions/566. Reshape the Matrix.md b/solutions/566. Reshape the Matrix.md index c1f4c6d..00632e7 100644 --- a/solutions/566. Reshape the Matrix.md +++ b/solutions/566. Reshape the Matrix.md @@ -3,7 +3,7 @@ 题意就是将一个二维数组按要求reshape成另一个二维数组使得有同样的row-traversing顺序,即按照行优先遍历应该得到同样的结果。 用一个count计数,然后对原数组遍历一遍将每个元素填到新数组合适的位置即可。 # C++ -``` +```C++ class Solution { public: vector> matrixReshape(vector>& nums, int r, int c) { diff --git a/solutions/58. Length of Last Word.md b/solutions/58. Length of Last Word.md index c6445f3..f507025 100644 --- a/solutions/58. Length of Last Word.md +++ b/solutions/58. Length of Last Word.md @@ -4,7 +4,7 @@ 用high表示字符串中从右开始第一个非空格字符所在index或者high==-1表示没有字母,low表示high从左边起第一个空格的index或者low==-1表示high左边没有空格了。 即high表示最后一个单词的结尾字母的index,low表示最后一个单词的第一个字母的前一个index,这样的话,所求结果就是high - low。 # C++ -``` +``` C++ class Solution { public: int lengthOfLastWord(string s) { diff --git a/solutions/581. Shortest Unsorted Continuous Subarray.md b/solutions/581. Shortest Unsorted Continuous Subarray.md index 5cab940..e78f586 100644 --- a/solutions/581. Shortest Unsorted Continuous Subarray.md +++ b/solutions/581. Shortest Unsorted Continuous Subarray.md @@ -19,7 +19,7 @@ * 4、5两步由于是在有序表中查找,所以用**二分查找**更快。 # C++ ## 思路二 -``` +```C++ class Solution { public: int findUnsortedSubarray(vector& nums) { diff --git a/solutions/605. Can Place Flowers.md b/solutions/605. Can Place Flowers.md index 1df42fb..2b11b14 100644 --- a/solutions/605. Can Place Flowers.md +++ b/solutions/605. Can Place Flowers.md @@ -4,7 +4,7 @@ 使用贪心策略即可,从前往后遍历数组,遇到能变为1的0就将其变为1,同时用count计数,当count达到n即可返回true。 注意数组首尾的特殊情况。 # C++ -``` +```C++ class Solution { public: bool canPlaceFlowers(vector& flowerbed, int n) { diff --git a/solutions/628. Maximum Product of Three Numbers.md b/solutions/628. Maximum Product of Three Numbers.md index 46a75a8..3924be1 100644 --- a/solutions/628. Maximum Product of Three Numbers.md +++ b/solutions/628. Maximum Product of Three Numbers.md @@ -10,7 +10,7 @@ 所以找出最大的三个数与最小的两个数,然后再比较两种乘积的大小即可。 时间复杂度O(n) # C++ -``` +```C++ class Solution { public: int maximumProduct(vector& nums) { diff --git a/solutions/643. Maximum Average Subarray I.md b/solutions/643. Maximum Average Subarray I.md index 70b9eb5..fd7f68d 100644 --- a/solutions/643. Maximum Average Subarray I.md +++ b/solutions/643. Maximum Average Subarray I.md @@ -3,7 +3,7 @@ 用一个sum记录当前窗口的和,向右移动窗口找出最大的sum 注意:亲测`if(max_sum < sum) max_sum = sum;`比`max_sum = max(max_sum, sum)`慢一些 # C++ -``` +```C++ class Solution { public: double findMaxAverage(vector& nums, int k) { diff --git a/solutions/66. Plus One.md b/solutions/66. Plus One.md index 375dbe5..d7332b1 100644 --- a/solutions/66. Plus One.md +++ b/solutions/66. Plus One.md @@ -4,7 +4,7 @@ 从后往前遍历,用一个c记录每一个digit所加的数,初试为1,以后每个c为前一次加法的进位。 注意如果最后有进位的话会使数组size加1 # C++ -``` +``` C++ class Solution { public: vector plusOne(vector& digits) { diff --git a/solutions/661. Image Smoother.md b/solutions/661. Image Smoother.md index ae75932..1ebbdd7 100644 --- a/solutions/661. Image Smoother.md +++ b/solutions/661. Image Smoother.md @@ -14,7 +14,7 @@ 由于元素值为0-255, 所以最多8位,所以可以先用int型的高8位存放新的值,这样就可以使空间复杂度为O(1)。 # C++ -``` +```C++ class Solution { public: bool islegal(const int& x, const int& y, const int& r, const int &c){ diff --git a/solutions/665. Non-decreasing Array.md b/solutions/665. Non-decreasing Array.md index 90167cd..8c8ba92 100644 --- a/solutions/665. Non-decreasing Array.md +++ b/solutions/665. Non-decreasing Array.md @@ -8,7 +8,7 @@ 时间复杂度O(n), 空间复杂度O(1) # C++ -``` +```C++ class Solution { public: bool checkPossibility(vector& nums) { diff --git a/solutions/67. Add Binary.md b/solutions/67. Add Binary.md index 2775a4d..d2119bf 100644 --- a/solutions/67. Add Binary.md +++ b/solutions/67. Add Binary.md @@ -3,7 +3,7 @@ 题目要求实现多位二进制加法器。可以先考虑先实现一个一位二进制加法器,然后再运用这个一位二进制加法串接成多位的二进制加法,注意不要忘了处理进位。 注意学习如何将char型的vector转成string: `string str(vc.begin(), vc.end())` # C++ -``` +``` C++ class Solution { private: int bit_add(const int a, const int b, int &cin){ // 一位加法器,cin是进位 diff --git a/solutions/69. Sqrt(x).md b/solutions/69. Sqrt(x).md index ee46714..72231b9 100644 --- a/solutions/69. Sqrt(x).md +++ b/solutions/69. Sqrt(x).md @@ -4,7 +4,7 @@ 考虑采用二分法。 注意:为了防止溢出,判断mid * mid与 x 的值时用除法,即判断 mid 与 x / mid的大小关系。 # C++ -``` +``` C++ class Solution { public: int mySqrt(int x) { diff --git a/solutions/70. Climbing Stairs.md b/solutions/70. Climbing Stairs.md index f522502..1d0541c 100644 --- a/solutions/70. Climbing Stairs.md +++ b/solutions/70. Climbing Stairs.md @@ -7,7 +7,7 @@ 即若dp[i]代表跨到第i步阶梯的情况数,那么`dp[i] = dp[i - 1] + dp[i - 2]`。 时间复杂度和空间复杂度都为O(n),可将空间复杂度优化为O(1) # C++ -``` +``` C++ class Solution { public: int climbStairs(int n) { @@ -22,7 +22,7 @@ public: }; ``` 空间优化后的版本: -``` +``` C++ class Solution { public: int climbStairs(int n) { diff --git a/solutions/83. Remove Duplicates from Sorted List.md b/solutions/83. Remove Duplicates from Sorted List.md index e3632dd..fb05afa 100644 --- a/solutions/83. Remove Duplicates from Sorted List.md +++ b/solutions/83. Remove Duplicates from Sorted List.md @@ -3,7 +3,7 @@ 去除链表中的重复元素。 设置两个相邻的指针pre和p,pre代表已处理部分的最后一个节点,p代表待处理节点,比较pre和p的值来决定是否删除p; # C++ -``` +``` C++ /** * Definition for singly-linked list. * struct ListNode { diff --git a/solutions/88. Merge Sorted Array.md b/solutions/88. Merge Sorted Array.md index 4997fb0..94878a7 100644 --- a/solutions/88. Merge Sorted Array.md +++ b/solutions/88. Merge Sorted Array.md @@ -6,7 +6,7 @@ 最大的数放在nums1[n+m-1],次大的数放在nums1[n+m-2],以此类推直到其中一个数组遍历结束,然后将未遍历的部分依次复制到nums1即可。 所以当nums1先遍历完成时,将nums2剩余的数依次复制到nums1对应的位置即可;当nums2先遍历完成时,不用做任何操作。 # C++ -``` +``` C++ class Solution { public: void merge(vector& nums1, int m, vector& nums2, int n) {