Skip to content

Commit 684c3c7

Browse files
committed
add C++ indicator before code
1 parent 3a9ad83 commit 684c3c7

File tree

90 files changed

+110
-110
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+110
-110
lines changed

solutions/118. Pascal's Triangle.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# 思路
33
首先明白题目要求返回的是一个vector,其元素也是vector,按照题目规律构造每个vector即可。
44
# C++
5-
```
5+
``` C++
66
class Solution {
77
public:
88
vector<vector<int>> generate(int numRows) {

solutions/119. Pascal's Triangle II.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
在得到第i-1行结果时,可以从前往后依次更新数组里的值来得到第i行的结果,这是内层循环。
66
注意用一个pre来记录第i-1行里面第j个元素的前一个元素的值。
77
# C++
8-
```
8+
``` C++
99
class Solution {
1010
public:
1111
vector<int> getRow(int rowIndex) {

solutions/121. Best Time to Buy and Sell Stock.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
因此prices[j]肯定是prices[i]之前的元素中最小的那一个,所以从前往后遍历,并用min_price记录当前位置前的元素中最小的一个。
55
另外注意单独判断空数组的情况。
66
# C++
7-
```
7+
``` C++
88
class Solution {
99
public:
1010
int maxProfit(vector<int>& prices) {

solutions/122. Best Time to Buy and Sell Stock II.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ max(dp[j] + max(prices[i+1] - prices[k])), 其中k属于j~i+1
1010

1111
# C++
1212
改进前:
13-
```
13+
``` C++
1414
class Solution {
1515
public:
1616
int maxProfit(vector<int>& prices) {
@@ -29,7 +29,7 @@ public:
2929
};
3030
```
3131
改进后:
32-
```
32+
``` C++
3333
class Solution {
3434
public:
3535
int maxProfit(vector<int>& prices) {

solutions/125. Valid Palindrome.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
然后再用两个指针low和high从两头往中间遍历并比较大小即可。
1010
时间复杂度O(n),空间复杂度O(1)
1111
# C++
12-
```
12+
``` C++
1313
class Solution {
1414
private:
1515
int transformer(char c){ // 将所有字符映射到整数以方便比较

solutions/13. Roman to Integer.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
用res代表结果,将代表罗马数字的字符串s从前往后遍历,如果当前字母对应的数大于等于下一个字母对应的数,则res加上对应的数,否则减。
55
注意:根据题意是没有可能发生s[i] < s[i + 1] < s[i + 2]的。
66
# C++
7-
```
7+
``` C++
88
class Solution {
99
public:
1010
int romanToInt(string s) {

solutions/136. Single Number.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ unordered_map: 时间复杂度O(n), 空间复杂度O(N)
1010
时间复杂度O(n), 空间复杂度O(1), 完美
1111
# C++
1212
## 思路二
13-
```
13+
``` C++
1414
class Solution {
1515
public:
1616
int singleNumber(vector<int>& nums) {

solutions/14. Longest Common Prefix.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
注意:
66
append函数可以用来在字符串的末尾追加字符和字符串。由于string重载了运算符,也可以用+=操作实现。
77
# C++
8-
```
8+
``` C++
99
class Solution {
1010
public:
1111
string longestCommonPrefix(vector<string>& strs) {

solutions/141. Linked List Cycle.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
设置两个指针p1和p2,用步长分别为1和2从前往后遍历,若链表是环则p1和p2总会相遇。
55
时间复杂度O(n),空间复杂度O(1)
66
# C++
7-
```
7+
``` C++
88
/**
99
* Definition for singly-linked list.
1010
* struct ListNode {

solutions/155. Min Stack.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
实现最小栈,要求所有操作的时间复杂度都为O(1)。
44
可考虑用两个站S1、S2,S1就正常记录minStack的值,而S2的栈顶记录了当前Minstack的最小值。S2的更新规则见代码。
55
# C++
6-
```
6+
``` C++
77
class MinStack {
88
private:
99
stack<int> s1;

solutions/160. Intersection of Two Linked Lists.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
直到p1 == p2即到达所求节点c1或者遇到NULL。
66
时间复杂度O(n), 空间复杂度O(1)
77
# C++
8-
```
8+
``` C++
99
/**
1010
* Definition for singly-linked list.
1111
* struct ListNode {

solutions/167. Two Sum II - Input array is sorted.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ map查找的复杂度为O(logn),所以总的时间复杂度O(nlogn), 空间复
1010
此时时间复杂度O(n), 空间复杂度O(1).
1111
# C++
1212
## 思路一
13-
```
13+
``` C++
1414
class Solution {
1515
public:
1616
vector<int> twoSum(vector<int>& numbers, int target) {
@@ -28,7 +28,7 @@ public:
2828
};
2929
```
3030
## 思路二
31-
```
31+
``` C++
3232
class Solution {
3333
public:
3434
vector<int> twoSum(vector<int>& numbers, int target) {

solutions/168. Excel Sheet Column Title.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
题目的要求相当于是十进制转二十六进制。用一个循环每次对n取模然后n除26进入下一次循环即可。
44
不过需要注意的是,题目给的是1-26对应A-Z而不是0-25对应A-Z,所以每次循环时都要对n作自减操作。
55
# C++
6-
```
6+
``` C++
77
class Solution {
88
public:
99
string convertToTitle(int n) {

solutions/169. Majority Element.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
时间复杂度O(n)。
1414
# C++
1515
## 思路一
16-
```
16+
``` C++
1717
// 提交结果为16ms
1818
class Solution {
1919
public:
@@ -24,7 +24,7 @@ public:
2424
};
2525
```
2626
## 思路二
27-
```
27+
``` C++
2828
// 提交结果为12ms,较思路一有提升
2929
class Solution {
3030
public:
@@ -40,7 +40,7 @@ public:
4040
};
4141
```
4242
## 思路三
43-
```
43+
``` C++
4444
// 提交结果20ms
4545
class Solution {
4646
public:

solutions/171. Excel Sheet Column Number.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[168题](https://leetcode.com/problems/excel-sheet-column-title/description/)是将10进制转换为26进制,这题是将26进制转换为10进制.
44
例:将k进制数"abcd"转换为10进制数:`res = d * k^0 + c * k^1 + b * k^2 + a * k^3`.
55
# C++
6-
```
6+
``` C++
77
class Solution {
88
public:
99
int titleToNumber(string s) {

solutions/172. Factorial Trailing Zeroes.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ n = 4617.
1515
所以 4617! 有 923 + 184 + 36 + 7 + 1 = 1151 个尾0.
1616
[参考](https://leetcode.com/problems/factorial-trailing-zeroes/discuss/52373/Simple-CC++-Solution-(with-detailed-explaination))
1717
# C++
18-
```
18+
``` C++
1919
class Solution {
2020
public:
2121
int trailingZeroes(int n) {

solutions/189. Rotate Array.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
再对后面的元素翻转:【5,6,7,1,2,3,4]
88
时间复杂度O(n), 空间复杂度O(1)。
99
# C++
10-
```
10+
``` C++
1111
class Solution {
1212
public:
1313
void rotate(vector<int>& nums, int k) {

solutions/190. Reverse Bits.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
# C++
1919
## 思路一
20-
```
20+
``` C++
2121
class Solution {
2222
public:
2323
uint32_t reverseBits(uint32_t n) {
@@ -33,7 +33,7 @@ public:
3333
};
3434
```
3535
## 思路二
36-
```
36+
``` C++
3737
class Solution {
3838
public:
3939
uint32_t reverseBits(uint32_t n) {

solutions/191. Number of 1 Bits.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
依然是循环,但是每次循环不是得到最低位的值,而是每次循环去掉一个1,用一个count计数即可得到答案。
99
# C++
1010
## 思路一
11-
```
11+
``` C++
1212
// 方法1
1313
class Solution {
1414
public:
@@ -37,7 +37,7 @@ public:
3737
};
3838
```
3939
## 思路二*
40-
```
40+
``` C++
4141
class Solution {
4242
public:
4343
int hammingWeight(uint32_t n) {

solutions/198. House Robber.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
## 空间优化且不改变原数组
1313
用pre记录dp[i-1],这样既不改变原数组nums也使得空间复杂度为o(1), 完美
1414
# C++
15-
```
15+
``` C++
1616
class Solution {
1717
public:
1818
int rob(vector<int>& nums){
@@ -27,7 +27,7 @@ public:
2727
};
2828
```
2929
## 空间优化
30-
```
30+
``` C++
3131
class Solution {
3232
public:
3333
int rob(vector<int>& nums){
@@ -40,7 +40,7 @@ public:
4040
};
4141
```
4242
## 空间优化且不修改原数组
43-
```
43+
``` C++
4444
class Solution {
4545
public:
4646
int rob(vector<int>& nums){

solutions/20. Valid Parentheses.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
退出循环后,若栈不空,说明还剩下未配对的左括号,则应该返回false。
55
时间复杂度O(n), 空间复杂度O(n)
66
# C++
7-
```
7+
``` C++
88
class Solution {
99
private:
1010
bool isLegal(const char& a, const char&b){

solutions/202. Happy Number.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# 思路
33
按照题目的意思进行循环,判断每次循环结果是否为1,若是则返回true,否则用map记录结果,然后修改n进行下一次循环。
44
# C++
5-
```
5+
``` C++
66
class Solution {
77
private:
88
int count_sum(int n){ // 定义题目中所述的求和函数

solutions/203. Remove Linked List Elements.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
最后将p指向pre的next。
99
为了操作方便,可以设置一个头结点。
1010
# C++
11-
```
11+
``` C++
1212
/**
1313
* Definition for singly-linked list.
1414
* struct ListNode {

solutions/204. Count Primes.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
# C++
1616
## 思路一
17-
```
17+
``` C++
1818
class Solution {
1919
private:
2020
bool isPrime(int n){
@@ -33,7 +33,7 @@ public:
3333
};
3434
```
3535
## 思路二
36-
```
36+
``` C++
3737
class Solution {
3838
public:
3939
int countPrimes(int n) {

solutions/205. Isomorphic Strings.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
为了记录是否出现过,用map来实现,此外还用一个count计数。
77
时间复杂度O(nlogn)
88
# C++
9-
```
9+
``` C++
1010
class Solution {
1111
public:
1212
bool isIsomorphic(string s, string t) {

solutions/206. Reverse Linked List.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
# C++
1212
## 思路一
13-
```
13+
``` C++
1414
/**
1515
* Definition for singly-linked list.
1616
* struct ListNode {
@@ -38,7 +38,7 @@ public:
3838
};
3939
```
4040
## 思路二
41-
```
41+
``` C++
4242
/**
4343
* Definition for singly-linked list.
4444
* struct ListNode {

solutions/21. Merge Two Sorted Lists.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
合并两个已有序的链表,注意题目给的链表没有头结点,所以为了操作方便可以自己设一个头结点,最后返回头结点的下一个节点即可。两个链表的工作指针就用传进来的l1和
44
l2即可。
55
# C++
6-
```
6+
``` C++
77
/**
88
* Definition for singly-linked list.
99
* struct ListNode {

solutions/217. Contains Duplicate.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# 思路
33
先对数组进行排序,排序后重复的数一定是相邻的,再遍历一遍即可。
44
# C++
5-
```
5+
``` C++
66
class Solution {
77
public:
88
bool containsDuplicate(vector<int>& nums) {

solutions/219. Contains Duplicate II.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
判断数组是否有重复元素,而且重复元素下标差的绝对值不大于k,首先简单的思路就是用map,这里给出另一种解法:
44
定义一个结构体num_with_index记录数组元素的值和下标,然后再对结构体按照值进行排序,排序后重复元素肯定相邻,再判断下标是否满足条件即可。
55
# C++
6-
```
6+
``` C++
77
class Solution {
88
struct num_with_index{
99
int num;

solutions/225. Implement Stack using Queues.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
因为pop和top都要求找到栈顶元素,采用前者思路的话会产生混乱。
66
注意学习stl中queue的一些操作:push、pop、front
77
# C++
8-
```
8+
``` C++
99
class MyStack {
1010
private:
1111
queue<int>q;

solutions/231. Power of Two.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
2. 若n是不为1的奇数返回false;
66
3. 令n = n / 2,若n=1则返回true否则返回第2步。
77
# C++
8-
```
8+
``` C++
99
class Solution {
1010
public:
1111
bool isPowerOfTwo(int n) {

solutions/232. Implement Queue using Stacks.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
若stk1不空,对队列入队的话直接对stk1入栈即可,否则要将stk2中所有元素pop到stk1中后再对stk1入栈。
66
若stk2不空,对队列出队的话直接对stk2出栈即可,否则要将stk1中所有元素pop到stk2中后再对stk2出栈。
77
# C++
8-
```
8+
``` C++
99
class MyQueue {
1010
private:
1111
stack<int>stk1;

solutions/234. Palindrome Linked List.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
判断所给链表是不是回文的,要求线性时间复杂度且空间复杂度为O(1)。
44
可以考虑现将链表的后半部分(或前半部分)反转一下,然后再设置两个指针p和q,初始分别指向前、后半部分的第一个节点,然后同时往后移动并判断值是否相等。
55

6-
```
6+
``` C++
77
/**
88
* Definition for singly-linked list.
99
* struct ListNode {

0 commit comments

Comments
 (0)