1. 括号生成
# include <iostream>
# include <vector>
# include <string>
# include <unordered_map>
# include <algorithm>
using namespace std;
class Solution {
public :
vector< string> res;
void dfs ( const string& str, int left, int right) {
if ( left < 0 || left > right) {
return ;
}
if ( left == 0 && right == 0 ) {
res. push_back ( str) ;
return ;
}
dfs ( str + '[' , left - 1 , right) ;
dfs ( str + ']' , left, right - 1 ) ;
}
vector< string> generateParenthesis ( int n) {
dfs ( "" , n, n) ;
return res;
}
} ;
int main ( ) {
vector< string> ans;
Solution s1;
int n;
cin >> n;
ans = s1. generateParenthesis ( n) ;
for ( int i = 0 ; i < ans. size ( ) ; ++ i) {
cout << ans[ i] << endl;
}
return 0 ;
}
2. 两两交换链表中的节点
class Solution {
public :
ListNode* swapPairs ( ListNode* head) {
if ( head == nullptr || head-> next == nullptr ) {
return head;
}
ListNode* newhead = head-> next;
head-> next = swapPairs ( newhead-> next) ;
newhead-> next = head;
return newhead;
ListNode* dummyHead = new ListNode ( 0 ) ;
dummyHead-> next = head;
ListNode* temp = dummyHead;
while ( temp-> next != nullptr && temp-> next-> next != nullptr ) {
ListNode* node1 = temp-> next;
ListNode* node2 = temp-> next-> next;
temp-> next = node2;
node1-> next = node2-> next;
node2-> next = node1;
temp = node1;
}
ListNode* ans = dummyHead-> next;
delete dummyHead;
return ans;
}
} ;
3. 删除有序数组中的重复项
class Solution {
public :
int removeDuplicates ( vector< int > & nums) {
int n = nums. size ( ) ;
if ( n == 0 ) {
return 0 ;
}
int fast = 1 , slow = 1 ;
while ( fast < n) {
if ( nums[ fast] != nums[ fast - 1 ] ) {
nums[ slow] = nums[ fast] ;
++ slow;
}
++ fast;
}
return slow;
}
} ;
4. 移除元素
class Solution {
public :
int removeElement ( vector< int > & nums, int val) {
int n = nums. size ( ) ;
int left = 0 ;
for ( int right = 0 ; right < n; right++ ) {
if ( nums[ right] != val) {
nums[ left] = nums[ right] ;
left++ ;
}
}
return left;
}
} ;
5. 找出字符串中第一个匹配项的下标
class Solution {
public :
int strStr ( string haystack, string needle) {
int n = haystack. size ( ) , m = needle. size ( ) ;
for ( int i = 0 ; i <= n - m; i++ ) {
int j = i, k = 0 ;
while ( k < m && haystack[ j] == needle[ k] ) {
j++ ;
k++ ;
}
if ( k == m) {
return i;
}
}
return - 1 ;
}
} ;
6. 下一个排列
# include <iostream>
# include <vector>
# include <unordered_map>
# include <algorithm>
using namespace std;
class Solution {
public :
void nextPermutation ( vector< int > & nums) {
int n = nums. size ( ) ;
int left = n - 2 ;
while ( left >= 0 && nums[ left] >= nums[ left + 1 ] ) {
left-- ;
}
if ( left >= 0 ) {
int right = n - 1 ;
while ( nums[ right] <= nums[ left] ) {
right-- ;
}
swap ( nums[ left] , nums[ right] ) ;
reverse ( nums. begin ( ) + left + 1 , nums. end ( ) ) ;
}
else {
reverse ( nums. begin ( ) , nums. end ( ) ) ;
}
return ;
}
} ;
int main ( ) {
vector< int > nums = { 1 , 2 , 3 } ;
Solution s1;
s1. nextPermutation ( nums) ;
for ( int i = 0 ; i < nums. size ( ) ; ++ i) {
cout << nums[ i] << endl;
}
return 0 ;
}
3. 搜索旋转排序数组
class Solution {
public :
int search ( vector< int > & nums, int target) {
int n = nums. size ( ) ;
for ( int i = 0 ; i < n; i++ )
{
if ( nums[ i] == target)
return i;
}
return - 1 ;
}
} ;
4. 在排序数组中查找元素的第一个和最后一个位置
# include <iostream>
# include <vector>
# include <unordered_map>
using namespace std;
class Solution {
public :
int binarySearch ( vector< int > & nums, int target, bool lower) {
int left = 0 , right = ( int ) nums. size ( ) - 1 , ans = nums. size ( ) ;
while ( left <= right) {
int mid = ( left + right) / 2 ;
if ( nums[ mid] > target || ( lower && nums[ mid] >= target) ) {
right = mid - 1 ;
ans = mid;
}
else {
left = mid + 1 ;
}
}
return ans;
}
vector< int > searchRange ( vector< int > & nums, int target) {
int leftIdx = binarySearch ( nums, target, true ) ;
int rightIdx = binarySearch ( nums, target, false ) - 1 ;
if ( leftIdx <= rightIdx && rightIdx < nums. size ( ) && nums[ leftIdx] == target && nums[ rightIdx] == target) {
return vector< int > { leftIdx, rightIdx} ;
}
return vector< int > { - 1 , - 1 } ;
}
} ;
int main ( ) {
int tgt = 8 ;
vector< int > nums = { 5 , 7 , 7 , 8 , 8 , 10 } ;
Solution s1;
vector< int > ans;
ans = s1. searchRange ( nums, tgt) ;
for ( auto ans_item : ans) {
cout << ans_item << " " ;
}
system ( "pause" ) ;
return 0 ;
}
5. 组合总和
# include <iostream>
# include <vector>
# include <unordered_map>
# include <algorithm>
using namespace std;
class Solution {
public :
void backtrack ( vector< int > & state, int target, vector< int > & choices, int start, vector< vector< int >> & res) {
if ( target == 0 ) {
res. push_back ( state) ;
return ;
}
for ( int i = start; i < choices. size ( ) ; ++ i) {
if ( target - choices[ i] < 0 ) {
break ;
}
state. push_back ( choices[ i] ) ;
backtrack ( state, target - choices[ i] , choices, i, res) ;
state. pop_back ( ) ;
}
}
vector< vector< int >> combinationSum ( vector< int > & candidates, int target) {
vector< int > state;
sort ( candidates. begin ( ) , candidates. end ( ) ) ;
int start = 0 ;
vector< vector< int >> res;
backtrack ( state, target, candidates, start, res) ;
return res;
}
} ;
int main ( ) {
int tgt = 7 ;
vector< int > nums = { 2 , 3 , 6 , 7 } ;
Solution s1;
vector< vector< int >> ans;
ans = s1. combinationSum ( nums, tgt) ;
for ( auto ans_item : ans) {
for ( auto it : ans_item) {
cout << it << " " ;
}
cout << endl;
}
system ( "pause" ) ;
return 0 ;
}
6. 全排列
# include <iostream>
# include <vector>
# include <unordered_map>
# include <algorithm>
using namespace std;
class Solution {
public :
vector< vector< int >> res;
void dfs ( vector< int > & nums, int x) {
if ( x == nums. size ( ) - 1 ) {
res. push_back ( nums) ;
return ;
}
for ( int i = x; i < nums. size ( ) ; ++ i) {
swap ( nums[ i] , nums[ x] ) ;
dfs ( nums, x + 1 ) ;
swap ( nums[ i] , nums[ x] ) ;
}
}
vector< vector< int >> permute ( vector< int > & nums) {
dfs ( nums, 0 ) ;
return res;
}
} ;
int main ( ) {
vector< int > nums = { 1 , 2 , 3 } ;
Solution s1;
vector< vector< int >> ans;
ans = s1. permute ( nums) ;
for ( auto ans_item : ans) {
for ( auto it : ans_item) {
cout << it << " " ;
}
cout << endl;
}
system ( "pause" ) ;
return 0 ;
}
7. 旋转图像
# include <iostream>
# include <vector>
# include <unordered_map>
# include <algorithm>
using namespace std;
class Solution {
public :
void rotate ( vector< vector< int >> & matrix) {
int n = matrix. size ( ) ;
for ( int i = 0 ; i < n / 2 ; ++ i) {
for ( int j = 0 ; j < ( n + 1 ) / 2 ; ++ j) {
int temp = matrix[ i] [ j] ;
matrix[ i] [ j] = matrix[ n - j - 1 ] [ i] ;
matrix[ n - j - 1 ] [ i] = matrix[ n - i - 1 ] [ n - j - 1 ] ;
matrix[ n - i - 1 ] [ n - j - 1 ] = matrix[ j] [ n - i - 1 ] ;
matrix[ j] [ n - i - 1 ] = temp;
}
}
}
} ;
int main ( ) {
vector< vector< int >> nums = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ;
for ( auto ans_item : nums) {
for ( auto it : ans_item) {
cout << it << " " ;
}
cout << endl;
}
Solution s1;
s1. rotate ( nums) ;
for ( auto ans_item : nums) {
for ( auto it : ans_item) {
cout << it << " " ;
}
cout << endl;
}
system ( "pause" ) ;
return 0 ;
}
8. 最大子数组和
# include <iostream>
# include <vector>
# include <unordered_map>
# include <algorithm>
using namespace std;
class Solution {
public :
int maxSubArray ( vector< int > & nums) {
int pre = 0 , maxAns = nums[ 0 ] ;
for ( auto & x : nums) {
pre = max ( pre + x, x) ;
maxAns = max ( pre, maxAns) ;
}
return maxAns;
}
} ;
int main ( ) {
vector< int > nums = { - 2 , 1 , - 3 , 4 , - 1 , 2 , 1 , - 5 , 4 } ;
int ans = 0 ;
Solution s1;
ans = s1. maxSubArray ( nums) ;
cout << ans << endl;
system ( "pause" ) ;
return 0 ;
}
9. 跳跃游戏
# include <iostream>
# include <vector>
# include <unordered_map>
# include <algorithm>
using namespace std;
class Solution {
public :
bool canJump ( vector< int > & nums) {
int n = nums. size ( ) ;
int rightmost = 0 ;
for ( int i = 0 ; i < n; ++ i) {
if ( i <= rightmost) {
rightmost = max ( rightmost, i + nums[ i] ) ;
if ( rightmost >= n - 1 ) {
return true ;
}
}
}
return false ;
}
} ;
int main ( ) {
vector< int > nums = { 2 , 3 , 1 , 1 , 4 } ;
bool ans;
Solution s1;
ans = s1. canJump ( nums) ;
cout << ans << endl;
system ( "pause" ) ;
return 0 ;
}
10. 合并区间
# include <iostream>
# include <vector>
# include <unordered_map>
# include <algorithm>
using namespace std;
class Solution {
public :
vector< vector< int >> merge ( vector< vector< int >> & intervals) {
sort ( intervals. begin ( ) , intervals. end ( ) ) ;
vector< vector< int >> ans;
for ( int i = 0 ; i < intervals. size ( ) ; ) {
int t = intervals[ i] [ 1 ] ;
int j = i + 1 ;
while ( j < intervals. size ( ) && intervals[ j] [ 0 ] <= t) {
t = max ( t, intervals[ j] [ 1 ] ) ;
j++ ;
}
ans. push_back ( { intervals[ i] [ 0 ] , t} ) ;
i = j;
}
return ans;
}
} ;
int main ( ) {
vector< vector< int >> nums = { { 1 , 3 } , { 2 , 6 } , { 8 , 10 } , { 15 , 18 } } ;
vector< vector< int >> ans;
Solution s1;
ans = s1. merge ( nums) ;
for ( auto ans_item : ans) {
for ( auto it : ans_item) {
cout << it << " " ;
}
cout << endl;
}
system ( "pause" ) ;
return 0 ;
}
11. 不同路径
# include <iostream>
# include <vector>
# include <unordered_map>
# include <algorithm>
using namespace std;
class Solution {
public :
int uniquePaths ( int m, int n) {
vector< vector< int >> f ( m, vector < int > ( n) ) ;
for ( int i = 0 ; i < m; ++ i) {
f[ i] [ 0 ] = 1 ;
}
for ( int j = 0 ; j < n; ++ j) {
f[ 0 ] [ j] = 1 ;
}
for ( int i = 1 ; i < m; ++ i) {
for ( int j = 1 ; j < n; ++ j) {
f[ i] [ j] = f[ i - 1 ] [ j] + f[ i] [ j - 1 ] ;
}
}
return f[ m - 1 ] [ n - 1 ] ;
}
} ;
int main ( ) {
int m = 3 , n = 7 ;
int ans;
Solution s1;
ans = s1. uniquePaths ( m, n) ;
cout << ans << endl;
system ( "pause" ) ;
return 0 ;
}
12. 最小路径和
# include <iostream>
# include <vector>
# include <unordered_map>
# include <algorithm>
using namespace std;
class Solution {
public :
int minPathSum ( vector< vector< int >> & grid) {
int rows = grid. size ( ) , columns = grid[ 0 ] . size ( ) ;
if ( rows == 0 || columns == 0 ) {
return 0 ;
}
vector< vector< int >> dp = vector < vector< int >> ( rows, vector < int > ( columns) ) ;
dp[ 0 ] [ 0 ] = grid[ 0 ] [ 0 ] ;
for ( int i = 1 ; i < rows; ++ i) {
dp[ i] [ 0 ] = dp[ i - 1 ] [ 0 ] + grid[ i] [ 0 ] ;
}
for ( int j = 1 ; j < columns; j++ ) {
dp[ 0 ] [ j] = dp[ 0 ] [ j - 1 ] + grid[ 0 ] [ j] ;
}
for ( int i = 1 ; i < rows; ++ i) {
for ( int j = 1 ; j < columns; ++ j) {
dp[ i] [ j] = min ( dp[ i - 1 ] [ j] , dp[ i] [ j - 1 ] ) + grid[ i] [ j] ;
}
}
return dp[ rows - 1 ] [ columns - 1 ] ;
}
} ;
int main ( ) {
vector< vector< int >> nums = { { 1 , 3 , 1 } , { 1 , 5 , 1 } , { 4 , 2 , 1 } } ;
int ans = 0 ;
Solution s1;
ans = s1. minPathSum ( nums) ;
cout << ans << endl;
system ( "pause" ) ;
return 0 ;
}