Refactor
This commit is contained in:
parent
b13cfa00bb
commit
72555b19e0
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class Offer05 {
|
||||||
public:
|
public:
|
||||||
string replaceSpace(string s);
|
string replaceSpace(string s);
|
||||||
};
|
};
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class Offer58 {
|
||||||
public:
|
public:
|
||||||
string reverseLeftWords(string s, int n);
|
string reverseLeftWords(string s, int n);
|
||||||
void reverseSubStr(string &s, int begin, int end);
|
void reverseSubStr(string &s, int begin, int end);
|
||||||
|
@ -5,7 +5,9 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class Solution {
|
using namespace std;
|
||||||
|
|
||||||
|
class S0001 {
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Two Sum
|
* @brief Two Sum
|
||||||
|
@ -9,7 +9,7 @@ struct ListNode {
|
|||||||
ListNode(int x, ListNode* next) : val(x), next(next) {}
|
ListNode(int x, ListNode* next) : val(x), next(next) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Solution {
|
class S0002 {
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Add Two Numbers
|
* @brief Add Two Numbers
|
||||||
|
@ -4,7 +4,9 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class Solution {
|
using namespace std;
|
||||||
|
|
||||||
|
class S0003 {
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Longest Substring Without Repeating Characters
|
* @brief Longest Substring Without Repeating Characters
|
||||||
|
@ -3,7 +3,9 @@
|
|||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class Solution {
|
using namespace std;
|
||||||
|
|
||||||
|
class S0004 {
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Median of Two Sorted Arrays
|
* @brief Median of Two Sorted Arrays
|
||||||
|
@ -1,26 +1,20 @@
|
|||||||
#ifndef S0005_LONGEST_PALINDROMIC_SUBSTRING_HPP
|
#ifndef S0005_LONGEST_PALINDROMIC_SUBSTRING_HPP
|
||||||
#define S0005_LONGEST_PALINDROMIC_SUBSTRING_HPP
|
#define S0005_LONGEST_PALINDROMIC_SUBSTRING_HPP
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution1 {
|
typedef struct S0005ResultStruct {
|
||||||
public:
|
int left1; // 边界情况为长度为 1 的字符时,扩展完成后的左边界
|
||||||
/**
|
int right1; // 边界情况为长度为 1 的字符时,扩展完成后的右边界
|
||||||
* @brief Longest Palindromic Substring
|
int left2; // 边界情况为长度为 2 的字符串时,扩展完成后的左边界
|
||||||
*
|
int right2; // 边界情况为长度为 2 的字符串时,扩展完成后的右边界
|
||||||
* Given a string s, return the longest palindromic substring in s.
|
} S0005Result;
|
||||||
*
|
|
||||||
* @param s the given string
|
|
||||||
* @return longest palindromic substring
|
|
||||||
*/
|
|
||||||
string longestPalindrome(string s);
|
|
||||||
};
|
|
||||||
|
|
||||||
class Solution2 {
|
class S0005 {
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Longest Palindromic Substring
|
* @brief Longest Palindromic Substring
|
||||||
@ -30,7 +24,17 @@ class Solution2 {
|
|||||||
* @param s the given string
|
* @param s the given string
|
||||||
* @return longest palindromic substring
|
* @return longest palindromic substring
|
||||||
*/
|
*/
|
||||||
string longestPalindrome(string s);
|
string longestPalindrome1(string s);
|
||||||
|
/**
|
||||||
|
* @brief Longest Palindromic Substring
|
||||||
|
*
|
||||||
|
* Given a string s, return the longest palindromic substring in s.
|
||||||
|
*
|
||||||
|
* @param s the given string
|
||||||
|
* @return longest palindromic substring
|
||||||
|
*/
|
||||||
|
string longestPalindrome2(string s);
|
||||||
|
S0005Result expand(string s, int i);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0006 {
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Zigzag Conversion
|
* @brief Zigzag Conversion
|
||||||
|
@ -5,7 +5,9 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <climits>
|
#include <climits>
|
||||||
|
|
||||||
class Solution {
|
using namespace std;
|
||||||
|
|
||||||
|
class S0007 {
|
||||||
public:
|
public:
|
||||||
int reverse(int x);
|
int reverse(int x);
|
||||||
};
|
};
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0008 {
|
||||||
public:
|
public:
|
||||||
int myAtoi(string s);
|
int myAtoi(string s);
|
||||||
};
|
};
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0009 {
|
||||||
public:
|
public:
|
||||||
bool isPalindrome(int x);
|
bool isPalindrome(int x);
|
||||||
};
|
};
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0010 {
|
||||||
public:
|
public:
|
||||||
bool isMatch(string s, string p);
|
bool isMatch(string s, string p);
|
||||||
};
|
};
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0011 {
|
||||||
public:
|
public:
|
||||||
int maxArea(vector<int>& height);
|
int maxArea(vector<int>& height);
|
||||||
};
|
};
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0012 {
|
||||||
public:
|
public:
|
||||||
string intToRoman(int num);
|
string intToRoman(int num);
|
||||||
};
|
};
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0013 {
|
||||||
public:
|
public:
|
||||||
int romanToInt(string s);
|
int romanToInt(string s);
|
||||||
};
|
};
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0014 {
|
||||||
public:
|
public:
|
||||||
string longestCommonPrefix(vector<string>& strs);
|
string longestCommonPrefix(vector<string>& strs);
|
||||||
};
|
};
|
||||||
|
@ -7,14 +7,10 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution1 {
|
class S0015 {
|
||||||
public:
|
public:
|
||||||
vector<vector<int>> threeSum(vector<int>& nums);
|
vector<vector<int>> threeSum1(vector<int>& nums);
|
||||||
};
|
vector<vector<int>> threeSum2(vector<int>& nums);
|
||||||
|
|
||||||
class Solution2 {
|
|
||||||
public:
|
|
||||||
vector<vector<int>> threeSum(vector<int>& nums);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0016 {
|
||||||
public:
|
public:
|
||||||
int threeSumClosest(vector<int>& nums, int target);
|
int threeSumClosest(vector<int>& nums, int target);
|
||||||
};
|
};
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0017 {
|
||||||
public:
|
public:
|
||||||
vector<string> letterCombinations(string digits);
|
vector<string> letterCombinations(string digits);
|
||||||
};
|
};
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0018 {
|
||||||
public:
|
public:
|
||||||
vector<vector<int>> fourSum(vector<int>& nums, int target);
|
vector<vector<int>> fourSum(vector<int>& nums, int target);
|
||||||
};
|
};
|
||||||
|
@ -9,7 +9,7 @@ struct ListNode {
|
|||||||
ListNode(int x, ListNode* next) : val(x), next(next) {}
|
ListNode(int x, ListNode* next) : val(x), next(next) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Solution {
|
class S0019 {
|
||||||
public:
|
public:
|
||||||
ListNode* removeNthFromEnd(ListNode* head, int n);
|
ListNode* removeNthFromEnd(ListNode* head, int n);
|
||||||
};
|
};
|
||||||
|
@ -6,9 +6,10 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0020 {
|
||||||
public:
|
public:
|
||||||
bool isValid(string s);
|
bool isValid(string s);
|
||||||
|
bool match(string s1, string s2);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
#ifndef S0021_MERGE_TWO_SORTED_LISTS_HPP
|
#ifndef S0021_MERGE_TWO_SORTED_LISTS_HPP
|
||||||
#define S0021_MERGE_TWO_SORTED_LISTS_HPP
|
#define S0021_MERGE_TWO_SORTED_LISTS_HPP
|
||||||
|
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
struct ListNode {
|
struct ListNode {
|
||||||
int val;
|
int val;
|
||||||
ListNode* next;
|
ListNode* next;
|
||||||
@ -9,9 +13,7 @@ struct ListNode {
|
|||||||
ListNode(int x, ListNode* next) : val(x), next(next) {}
|
ListNode(int x, ListNode* next) : val(x), next(next) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
#include <limits>
|
class S0021 {
|
||||||
|
|
||||||
class Solution {
|
|
||||||
public:
|
public:
|
||||||
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2);
|
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2);
|
||||||
};
|
};
|
||||||
|
@ -7,9 +7,10 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0022 {
|
||||||
public:
|
public:
|
||||||
vector<string> generateParenthesis(int n);
|
vector<string> generateParenthesis(int n);
|
||||||
|
void dfs(string current, int left, int right, vector<string> &result);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -7,15 +7,17 @@ using namespace std;
|
|||||||
|
|
||||||
struct ListNode {
|
struct ListNode {
|
||||||
int val;
|
int val;
|
||||||
ListNode* next;
|
ListNode *next;
|
||||||
ListNode() : val(0), next(nullptr) {}
|
ListNode() : val(0), next(nullptr) {}
|
||||||
ListNode(int x) : val(x), next(nullptr) {}
|
ListNode(int x) : val(x), next(nullptr) {}
|
||||||
ListNode(int x, ListNode* next) : val(x), next(next) {}
|
ListNode(int x, ListNode *next) : val(x), next(next) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Solution {
|
class S0023 {
|
||||||
public:
|
public:
|
||||||
ListNode* mergeKLists(vector<ListNode*>& lists);
|
ListNode *mergeKLists(vector<ListNode *> &lists);
|
||||||
|
ListNode *mergeTwoLists(ListNode *a, ListNode *b);
|
||||||
|
ListNode *merge(vector<ListNode *> &lists, int l, int r);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -9,7 +9,7 @@ struct ListNode {
|
|||||||
ListNode(int x, ListNode* next) : val(x), next(next) {}
|
ListNode(int x, ListNode* next) : val(x), next(next) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Solution {
|
class S0024 {
|
||||||
public:
|
public:
|
||||||
ListNode* swapPairs(ListNode* head);
|
ListNode* swapPairs(ListNode* head);
|
||||||
};
|
};
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
#ifndef S0025_REVERSE_NODES_IN_K_GROUP_HPP
|
#ifndef S0025_REVERSE_NODES_IN_K_GROUP_HPP
|
||||||
#define S0025_REVERSE_NODES_IN_K_GROUP_HPP
|
#define S0025_REVERSE_NODES_IN_K_GROUP_HPP
|
||||||
|
|
||||||
#include <utility>
|
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@ -14,9 +14,10 @@ struct ListNode {
|
|||||||
ListNode(int x, ListNode* next) : val(x), next(next) {}
|
ListNode(int x, ListNode* next) : val(x), next(next) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Solution {
|
class S0025 {
|
||||||
public:
|
public:
|
||||||
ListNode* reverseKGroup(ListNode* head, int k);
|
ListNode* reverseKGroup(ListNode* head, int k);
|
||||||
|
pair<ListNode*, ListNode*> myReverse(ListNode* head, ListNode* tail);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0026 {
|
||||||
public:
|
public:
|
||||||
int removeDuplicates(vector<int>& nums);
|
int removeDuplicates(vector<int>& nums);
|
||||||
};
|
};
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0027 {
|
||||||
public:
|
public:
|
||||||
int removeElement(vector<int>& nums, int val);
|
int removeElement(vector<int>& nums, int val);
|
||||||
};
|
};
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0028 {
|
||||||
public:
|
public:
|
||||||
int strStr(string haystack, string needle);
|
int strStr(string haystack, string needle);
|
||||||
};
|
};
|
||||||
|
@ -3,9 +3,12 @@
|
|||||||
|
|
||||||
#include <climits>
|
#include <climits>
|
||||||
|
|
||||||
class Solution {
|
using namespace std;
|
||||||
|
|
||||||
|
class S0029 {
|
||||||
public:
|
public:
|
||||||
int divide(int dividend, int divisor);
|
int divide(int dividend, int divisor);
|
||||||
|
int div(long a, long b);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0030 {
|
||||||
public:
|
public:
|
||||||
vector<int> findSubstring(string s, vector<string>& words);
|
vector<int> findSubstring(string s, vector<string>& words);
|
||||||
};
|
};
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0031 {
|
||||||
public:
|
public:
|
||||||
void nextPermutation(vector<int>& nums);
|
void nextPermutation(vector<int>& nums);
|
||||||
};
|
};
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0032 {
|
||||||
public:
|
public:
|
||||||
int longestValidParentheses(string s);
|
int longestValidParentheses(string s);
|
||||||
};
|
};
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0033 {
|
||||||
public:
|
public:
|
||||||
int search(vector<int>& nums, int target);
|
int search(vector<int>& nums, int target);
|
||||||
};
|
};
|
||||||
|
@ -5,13 +5,10 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution1 {
|
class S0034 {
|
||||||
public:
|
public:
|
||||||
vector<int> searchRange(vector<int>& nums, int target);
|
vector<int> searchRange1(vector<int>& nums, int target);
|
||||||
|
vector<int> searchRange2(vector<int>& nums, int target);
|
||||||
};
|
};
|
||||||
|
|
||||||
class Solution2 {
|
|
||||||
public:
|
|
||||||
vector<int> searchRange(vector<int>& nums, int target);
|
|
||||||
};
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -5,14 +5,10 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution1 {
|
class S0035 {
|
||||||
public:
|
public:
|
||||||
int searchInsert(vector<int>& nums, int target);
|
int searchInsert1(vector<int>& nums, int target);
|
||||||
};
|
int searchInsert2(vector<int>& nums, int target);
|
||||||
|
|
||||||
class Solution2 {
|
|
||||||
public:
|
|
||||||
int searchInsert(vector<int>& nums, int target);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0036 {
|
||||||
public:
|
public:
|
||||||
bool isValidSudoku(vector<vector<char>>& board);
|
bool isValidSudoku(vector<vector<char>>& board);
|
||||||
};
|
};
|
||||||
|
@ -1,16 +1,21 @@
|
|||||||
#ifndef S0037_SUDOKU_SOLVER_HPP
|
#ifndef S0037_SUDOKU_SOLVER_HPP
|
||||||
#define S0037_SUDOKU_SOLVER_HPP
|
#define S0037_SUDOKU_SOLVER_HPP
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <unordered_map>
|
|
||||||
#include <cmath>
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <cmath>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0037 {
|
||||||
public:
|
public:
|
||||||
void solveSudoku(vector<vector<char>>& board);
|
void solveSudoku(vector<vector<char>> &board);
|
||||||
|
bool recusiveSolveSudoku(vector<vector<char>> &board,
|
||||||
|
vector<unordered_map<char, bool>> &rows,
|
||||||
|
vector<unordered_map<char, bool>> &cols,
|
||||||
|
vector<unordered_map<char, bool>> &grids, int row,
|
||||||
|
int col);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0038 {
|
||||||
public:
|
public:
|
||||||
string countAndSay(int n);
|
string countAndSay(int n);
|
||||||
};
|
};
|
||||||
|
@ -1,15 +1,17 @@
|
|||||||
#ifndef S0039_COMBINATION_SUM_HPP
|
#ifndef S0039_COMBINATION_SUM_HPP
|
||||||
#define S0039_COMBINATION_SUM_HPP
|
#define S0039_COMBINATION_SUM_HPP
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0039 {
|
||||||
public:
|
public:
|
||||||
vector<vector<int>> combinationSum(vector<int>& candidates, int target);
|
vector<vector<int>> combinationSum(vector<int>& candidates, int target);
|
||||||
|
void dfs(vector<int>& candidates, int target, vector<vector<int>>& ans,
|
||||||
|
vector<int>& combine, int idx);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0076 {
|
||||||
public:
|
public:
|
||||||
string minWindow(string s, string t);
|
string minWindow(string s, string t);
|
||||||
unordered_map<char, int> ori, cnt;
|
unordered_map<char, int> ori, cnt;
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0151 {
|
||||||
public:
|
public:
|
||||||
void reverseSubStr(string &s, int begin, int end);
|
void reverseSubStr(string &s, int begin, int end);
|
||||||
string reverseWords(string s);
|
string reverseWords(string s);
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0209 {
|
||||||
public:
|
public:
|
||||||
int minSubArrayLen(int target, vector<int>& nums);
|
int minSubArrayLen(int target, vector<int>& nums);
|
||||||
};
|
};
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class S0283 {
|
||||||
public:
|
public:
|
||||||
void moveZeroes(vector<int>& nums);
|
void moveZeroes(vector<int>& nums);
|
||||||
};
|
};
|
||||||
|
@ -5,14 +5,10 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution1 {
|
class S0704 {
|
||||||
public:
|
public:
|
||||||
int binSearch(vector<int>& nums, int target);
|
int binSearch1(vector<int>& nums, int target);
|
||||||
};
|
int binSearch2(vector<int>& nums, int target);
|
||||||
|
|
||||||
class Solution2 {
|
|
||||||
public:
|
|
||||||
int binSearch(vector<int>& nums, int target);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "offer_05.hpp"
|
#include "offer_05.hpp"
|
||||||
|
|
||||||
string Solution::replaceSpace(string s) {
|
string Offer05::replaceSpace(string s) {
|
||||||
int len = s.length();
|
int len = s.length();
|
||||||
int cnt{0};
|
int cnt{0};
|
||||||
for (int i{0}; i < len; ++i) {
|
for (int i{0}; i < len; ++i) {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "offer_58.hpp"
|
#include "offer_58.hpp"
|
||||||
|
|
||||||
void Solution::reverseSubStr(string &s, int begin, int end) {
|
void Offer58::reverseSubStr(string &s, int begin, int end) {
|
||||||
for (; begin < end; ++begin, --end) {
|
for (; begin < end; ++begin, --end) {
|
||||||
char tmp = s[begin];
|
char tmp = s[begin];
|
||||||
s[begin] = s[end];
|
s[begin] = s[end];
|
||||||
@ -8,7 +8,7 @@ void Solution::reverseSubStr(string &s, int begin, int end) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
string Solution::reverseLeftWords(string s, int n) {
|
string Offer58::reverseLeftWords(string s, int n) {
|
||||||
int len = s.length();
|
int len = s.length();
|
||||||
reverseSubStr(s, 0, n - 1);
|
reverseSubStr(s, 0, n - 1);
|
||||||
reverseSubStr(s, n, len - 1);
|
reverseSubStr(s, n, len - 1);
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
#include "s0001_two_sum.hpp"
|
#include "s0001_two_sum.hpp"
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
vector<int> Solution::twoSum(vector<int>& nums, int target) {
|
vector<int> S0001::twoSum(vector<int>& nums, int target) {
|
||||||
unordered_map<int, int> hashtable;
|
unordered_map<int, int> hashtable;
|
||||||
for (int i = 0; i < nums.size(); ++i) {
|
for (int i = 0; i < nums.size(); ++i) {
|
||||||
auto it = hashtable.find(target - nums[i]);
|
auto it = hashtable.find(target - nums[i]);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0002_add_two_numbers.hpp"
|
#include "s0002_add_two_numbers.hpp"
|
||||||
|
|
||||||
ListNode* Solution::addTwoNumbers(ListNode* l1, ListNode* l2) {
|
ListNode* S0002::addTwoNumbers(ListNode* l1, ListNode* l2) {
|
||||||
ListNode *l_begin = new ListNode((l1->val + l2->val) % 10);
|
ListNode *l_begin = new ListNode((l1->val + l2->val) % 10);
|
||||||
ListNode *l_end = l_begin;
|
ListNode *l_end = l_begin;
|
||||||
int carry{ (l1->val + l2->val) >= 10 ? 1 : 0 };
|
int carry{ (l1->val + l2->val) >= 10 ? 1 : 0 };
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0003_longest_substring_without_repeating_characters.hpp"
|
#include "s0003_longest_substring_without_repeating_characters.hpp"
|
||||||
|
|
||||||
int Solution::lengthOfLongestSubstring(std::string s) {
|
int S0003::lengthOfLongestSubstring(std::string s) {
|
||||||
// index_forward points to the frontmost element
|
// index_forward points to the frontmost element
|
||||||
// index_backward points to the last repeated element
|
// index_backward points to the last repeated element
|
||||||
//
|
//
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0004_median_of_two_sorted_arrays.hpp"
|
#include "s0004_median_of_two_sorted_arrays.hpp"
|
||||||
|
|
||||||
int Solution::getKthElement(const std::vector<int>& nums1,
|
int S0004::getKthElement(const std::vector<int>& nums1,
|
||||||
const std::vector<int>& nums2, int k) {
|
const std::vector<int>& nums2, int k) {
|
||||||
int m = nums1.size();
|
int m = nums1.size();
|
||||||
int n = nums2.size();
|
int n = nums2.size();
|
||||||
@ -33,7 +33,7 @@ int Solution::getKthElement(const std::vector<int>& nums1,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
double Solution::findMedianSortedArrays(std::vector<int>& nums1,
|
double S0004::findMedianSortedArrays(std::vector<int>& nums1,
|
||||||
std::vector<int>& nums2) {
|
std::vector<int>& nums2) {
|
||||||
int totalLength = nums1.size() + nums2.size();
|
int totalLength = nums1.size() + nums2.size();
|
||||||
if (totalLength % 2 == 1) {
|
if (totalLength % 2 == 1) {
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
// 把 dp 这张二维数组的表全部填好,然后在里面找最长的子字符串。
|
// 把 dp 这张二维数组的表全部填好,然后在里面找最长的子字符串。
|
||||||
// 时间复杂度:O(n^2) 因为总共有 n^2 个状态,计算每个状态需要的时间为 O(1)
|
// 时间复杂度:O(n^2) 因为总共有 n^2 个状态,计算每个状态需要的时间为 O(1)
|
||||||
// 空间复杂度:O(n^2) 因为总共需要存储 n^2 个状态。
|
// 空间复杂度:O(n^2) 因为总共需要存储 n^2 个状态。
|
||||||
string Solution1::longestPalindrome(string s) {
|
string S0005::longestPalindrome1(string s) {
|
||||||
int len = s.length();
|
int len = s.length();
|
||||||
vector<vector<int>> dp(len, vector<int>(len));
|
vector<vector<int>> dp(len, vector<int>(len));
|
||||||
|
|
||||||
@ -79,20 +79,13 @@ string Solution1::longestPalindrome(string s) {
|
|||||||
// “回文中心”实际上是两种边界情况,也就是说每个可能的结果都是由边界情况扩展开来的
|
// “回文中心”实际上是两种边界情况,也就是说每个可能的结果都是由边界情况扩展开来的
|
||||||
// 因此我们可以遍历每个边界情况,对它进行扩展
|
// 因此我们可以遍历每个边界情况,对它进行扩展
|
||||||
|
|
||||||
typedef struct ResultStruct {
|
|
||||||
int left1; // 边界情况为长度为 1 的字符时,扩展完成后的左边界
|
|
||||||
int right1; // 边界情况为长度为 1 的字符时,扩展完成后的右边界
|
|
||||||
int left2; // 边界情况为长度为 2 的字符串时,扩展完成后的左边界
|
|
||||||
int right2; // 边界情况为长度为 2 的字符串时,扩展完成后的右边界
|
|
||||||
} Result;
|
|
||||||
|
|
||||||
// 输入为字符串和边界情况的那一点
|
// 输入为字符串和边界情况的那一点
|
||||||
// 返回值包含为扩展的结果
|
// 返回值包含为扩展的结果
|
||||||
Result expand(string s, int i) {
|
S0005Result S0005::expand(string s, int i) {
|
||||||
int len = s.length();
|
int len = s.length();
|
||||||
|
|
||||||
if (i == len - 1) {
|
if (i == len - 1) {
|
||||||
return Result{i, i, i, i};
|
return S0005Result{i, i, i, i};
|
||||||
}
|
}
|
||||||
|
|
||||||
// 边界情况为长度为 1 的字符时
|
// 边界情况为长度为 1 的字符时
|
||||||
@ -117,13 +110,13 @@ Result expand(string s, int i) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Result{left1, right1, left2, right2};
|
return S0005Result{left1, right1, left2, right2};
|
||||||
}
|
}
|
||||||
|
|
||||||
string Solution2::longestPalindrome(string s) {
|
string S0005::longestPalindrome2(string s) {
|
||||||
int len = s.length();
|
int len = s.length();
|
||||||
int left = 0, right = 0;
|
int left = 0, right = 0;
|
||||||
Result result{0, 0, 0, 0};
|
S0005Result result{0, 0, 0, 0};
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
result = expand(s, i);
|
result = expand(s, i);
|
||||||
if (result.right1 - result.left1 > right - left) {
|
if (result.right1 - result.left1 > right - left) {
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
// 2 4 8 10 2*(n-1)*col+row, 2*(n-1)*col-row
|
// 2 4 8 10 2*(n-1)*col+row, 2*(n-1)*col-row
|
||||||
// 3 9 2*(n-1)*col + (n-1)
|
// 3 9 2*(n-1)*col + (n-1)
|
||||||
|
|
||||||
string Solution::convert(string s, int numRows) {
|
string S0006::convert(string s, int numRows) {
|
||||||
string r = "";
|
string r = "";
|
||||||
int l = s.length();
|
int l = s.length();
|
||||||
if (l == 1 || numRows == 1) {
|
if (l == 1 || numRows == 1) {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0007_reverse_integer.hpp"
|
#include "s0007_reverse_integer.hpp"
|
||||||
|
|
||||||
int Solution::reverse(int x) {
|
int S0007::reverse(int x) {
|
||||||
int r{0};
|
int r{0};
|
||||||
std::queue<int> queue;
|
std::queue<int> queue;
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
// 当流程很复杂的时候,画流程图: https://paste.sainnhe.dev/NQxx.png
|
// 当流程很复杂的时候,画流程图: https://paste.sainnhe.dev/NQxx.png
|
||||||
|
|
||||||
class Automaton {
|
class S0008Automaton {
|
||||||
string state = "start";
|
string state = "start";
|
||||||
unordered_map<string, vector<string>> table = {
|
unordered_map<string, vector<string>> table = {
|
||||||
{"start", {"start", "signed", "in_number", "end"}},
|
{"start", {"start", "signed", "in_number", "end"}},
|
||||||
@ -32,8 +32,8 @@ class Automaton {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int Solution::myAtoi(string str) {
|
int S0008::myAtoi(string str) {
|
||||||
Automaton automaton;
|
S0008Automaton automaton;
|
||||||
for (char c : str) automaton.get(c);
|
for (char c : str) automaton.get(c);
|
||||||
return automaton.sign * automaton.ans;
|
return automaton.sign * automaton.ans;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0009_palindrome_number.hpp"
|
#include "s0009_palindrome_number.hpp"
|
||||||
|
|
||||||
bool Solution::isPalindrome(int x) {
|
bool S0009::isPalindrome(int x) {
|
||||||
string s = to_string(x);
|
string s = to_string(x);
|
||||||
string r = s;
|
string r = s;
|
||||||
reverse(r.begin(), r.end());
|
reverse(r.begin(), r.end());
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
// 动态规划
|
// 动态规划
|
||||||
// 当前匹配成功 == 之前匹配成功 && 当前字符匹配成功
|
// 当前匹配成功 == 之前匹配成功 && 当前字符匹配成功
|
||||||
|
|
||||||
bool Solution::isMatch(string s, string p) {
|
bool S0010::isMatch(string s, string p) {
|
||||||
int m = s.size();
|
int m = s.size();
|
||||||
int n = p.size();
|
int n = p.size();
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
// 也就是说只有当移动小的指针时才有可能让储存的水量超过之前的
|
// 也就是说只有当移动小的指针时才有可能让储存的水量超过之前的
|
||||||
// 因此每次迭代我们移动较小的指针,然后统计出最大的储存水量
|
// 因此每次迭代我们移动较小的指针,然后统计出最大的储存水量
|
||||||
|
|
||||||
int Solution::maxArea(vector<int>& height) {
|
int S0011::maxArea(vector<int>& height) {
|
||||||
int max{0};
|
int max{0};
|
||||||
for (int l{0}, r{static_cast<int>(height.size() - 1)}; r != l;) {
|
for (int l{0}, r{static_cast<int>(height.size() - 1)}; r != l;) {
|
||||||
if (height.at(l) > height.at(r)) {
|
if (height.at(l) > height.at(r)) {
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
// 先硬编码一个哈希表
|
// 先硬编码一个哈希表
|
||||||
// 然后当数值大于一个值的时候,就直接把对应的字符串加上去
|
// 然后当数值大于一个值的时候,就直接把对应的字符串加上去
|
||||||
|
|
||||||
string Solution::intToRoman(int num) {
|
string S0012::intToRoman(int num) {
|
||||||
int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
|
int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
|
||||||
string reps[] = {"M", "CM", "D", "CD", "C", "XC", "L",
|
string reps[] = {"M", "CM", "D", "CD", "C", "XC", "L",
|
||||||
"XL", "X", "IX", "V", "IV", "I"};
|
"XL", "X", "IX", "V", "IV", "I"};
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0013_roman_to_integer.hpp"
|
#include "s0013_roman_to_integer.hpp"
|
||||||
|
|
||||||
int Solution::romanToInt(string s) {
|
int S0013::romanToInt(string s) {
|
||||||
unordered_map<string, int> map;
|
unordered_map<string, int> map;
|
||||||
map["I"] = 1;
|
map["I"] = 1;
|
||||||
map["V"] = 5;
|
map["V"] = 5;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0014_longest_common_prefix.hpp"
|
#include "s0014_longest_common_prefix.hpp"
|
||||||
|
|
||||||
string Solution::longestCommonPrefix(vector<string>& strs) {
|
string S0014::longestCommonPrefix(vector<string>& strs) {
|
||||||
string o{""};
|
string o{""};
|
||||||
int size = strs.size();
|
int size = strs.size();
|
||||||
int minLen = strs.at(0).length();
|
int minLen = strs.at(0).length();
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
// 去重的方式:
|
// 去重的方式:
|
||||||
// 对于每个找到的序列,先排序,如果不存在则添加到结果中。
|
// 对于每个找到的序列,先排序,如果不存在则添加到结果中。
|
||||||
|
|
||||||
vector<vector<int>> Solution1::threeSum(vector<int>& nums) {
|
vector<vector<int>> S0015::threeSum1(vector<int>& nums) {
|
||||||
unordered_map<int, int> map;
|
unordered_map<int, int> map;
|
||||||
vector<vector<int>> result;
|
vector<vector<int>> result;
|
||||||
int len = nums.size();
|
int len = nums.size();
|
||||||
@ -42,7 +42,7 @@ vector<vector<int>> Solution1::threeSum(vector<int>& nums) {
|
|||||||
|
|
||||||
// 思路二:双指针
|
// 思路二:双指针
|
||||||
// 先对整个数组排序,然后固定一个数 a ,然后 b, c 从两边往中间靠。
|
// 先对整个数组排序,然后固定一个数 a ,然后 b, c 从两边往中间靠。
|
||||||
vector<vector<int>> Solution2::threeSum(vector<int>& nums) {
|
vector<vector<int>> S0015::threeSum2(vector<int>& nums) {
|
||||||
int n = nums.size();
|
int n = nums.size();
|
||||||
sort(nums.begin(), nums.end());
|
sort(nums.begin(), nums.end());
|
||||||
vector<vector<int>> ans;
|
vector<vector<int>> ans;
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
// 和上一道一样,排序 + 双指针
|
// 和上一道一样,排序 + 双指针
|
||||||
|
|
||||||
int Solution::threeSumClosest(vector<int>& nums, int target) {
|
int S0016::threeSumClosest(vector<int>& nums, int target) {
|
||||||
int sum = nums[0] + nums[1] + nums[2];
|
int sum = nums[0] + nums[1] + nums[2];
|
||||||
int len = nums.size();
|
int len = nums.size();
|
||||||
sort(nums.begin(), nums.end());
|
sort(nums.begin(), nums.end());
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0017_letter_combinations_of_a_phone_number.hpp"
|
#include "s0017_letter_combinations_of_a_phone_number.hpp"
|
||||||
|
|
||||||
vector<string> Solution::letterCombinations(string digits) {
|
vector<string> S0017::letterCombinations(string digits) {
|
||||||
unordered_map<string, vector<string>> map;
|
unordered_map<string, vector<string>> map;
|
||||||
map["2"] = vector<string>{"a", "b", "c"};
|
map["2"] = vector<string>{"a", "b", "c"};
|
||||||
map["3"] = vector<string>{"d", "e", "f"};
|
map["3"] = vector<string>{"d", "e", "f"};
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0018_4sum.hpp"
|
#include "s0018_4sum.hpp"
|
||||||
|
|
||||||
vector<vector<int>> Solution::fourSum(vector<int>& nums, int target) {
|
vector<vector<int>> S0018::fourSum(vector<int>& nums, int target) {
|
||||||
int len = nums.size();
|
int len = nums.size();
|
||||||
if (len < 4) {
|
if (len < 4) {
|
||||||
return {};
|
return {};
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
// 步,等快指针走到头时,慢指针指向的的就是倒数第 n 个
|
// 步,等快指针走到头时,慢指针指向的的就是倒数第 n 个
|
||||||
|
|
||||||
// 双指针实现
|
// 双指针实现
|
||||||
ListNode* Solution::removeNthFromEnd(ListNode* head, int n) {
|
ListNode* S0019::removeNthFromEnd(ListNode* head, int n) {
|
||||||
// 一种常见的处理头指针的方法是添加一个哑指针,这个哑指针指向头指针
|
// 一种常见的处理头指针的方法是添加一个哑指针,这个哑指针指向头指针
|
||||||
// 这样我们就不需要单独讨论头指针了。
|
// 这样我们就不需要单独讨论头指针了。
|
||||||
ListNode dummy(0, head);
|
ListNode dummy(0, head);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0020_valid_parentheses.hpp"
|
#include "s0020_valid_parentheses.hpp"
|
||||||
|
|
||||||
bool match(string s1, string s2) {
|
bool S0020::match(string s1, string s2) {
|
||||||
return s1 == "(" && s2 == ")" ||
|
return s1 == "(" && s2 == ")" ||
|
||||||
s1 == "[" && s2 == "]" ||
|
s1 == "[" && s2 == "]" ||
|
||||||
s1 == "{" && s2 == "}";
|
s1 == "{" && s2 == "}";
|
||||||
@ -8,7 +8,7 @@ bool match(string s1, string s2) {
|
|||||||
|
|
||||||
// 直接用栈
|
// 直接用栈
|
||||||
|
|
||||||
bool Solution::isValid(string s) {
|
bool S0020::isValid(string s) {
|
||||||
stack<string> stack;
|
stack<string> stack;
|
||||||
int len = s.length();
|
int len = s.length();
|
||||||
for (int i{0}; i < len; i++) {
|
for (int i{0}; i < len; i++) {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0021_merge_two_sorted_lists.hpp"
|
#include "s0021_merge_two_sorted_lists.hpp"
|
||||||
|
|
||||||
ListNode* Solution::mergeTwoLists(ListNode* list1, ListNode* list2) {
|
ListNode* S0021::mergeTwoLists(ListNode* list1, ListNode* list2) {
|
||||||
if (list1 == nullptr) {
|
if (list1 == nullptr) {
|
||||||
return list2;
|
return list2;
|
||||||
} else if (list2 == nullptr) {
|
} else if (list2 == nullptr) {
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
// 深度优先遍历:
|
// 深度优先遍历:
|
||||||
// 接收参数为每个节点的状态
|
// 接收参数为每个节点的状态
|
||||||
// 遍历结果可以用指针放在接收参数保存,也可以通过声明一个 class 的成员来保存
|
// 遍历结果可以用指针放在接收参数保存,也可以通过声明一个 class 的成员来保存
|
||||||
void dfs(string current, int left, int right, vector<string> &result) {
|
void S0022::dfs(string current, int left, int right, vector<string> &result) {
|
||||||
// 讨论边界条件(结束条件)
|
// 讨论边界条件(结束条件)
|
||||||
// 不必讨论起始条件,因为初始化的工作会在 dfs 函数外完成。
|
// 不必讨论起始条件,因为初始化的工作会在 dfs 函数外完成。
|
||||||
if (left == 0 && right == 0) {
|
if (left == 0 && right == 0) {
|
||||||
@ -22,7 +22,7 @@ void dfs(string current, int left, int right, vector<string> &result) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<string> Solution::generateParenthesis(int n) {
|
vector<string> S0022::generateParenthesis(int n) {
|
||||||
// 初始化
|
// 初始化
|
||||||
vector<string> result = {};
|
vector<string> result = {};
|
||||||
dfs("", n, n, result);
|
dfs("", n, n, result);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0023_merge_k_sorted_lists.hpp"
|
#include "s0023_merge_k_sorted_lists.hpp"
|
||||||
|
|
||||||
ListNode *mergeTwoLists(ListNode *a, ListNode *b) {
|
ListNode *S0023::mergeTwoLists(ListNode *a, ListNode *b) {
|
||||||
if (a == nullptr) {
|
if (a == nullptr) {
|
||||||
return b;
|
return b;
|
||||||
} else if (b == nullptr) {
|
} else if (b == nullptr) {
|
||||||
@ -27,7 +27,7 @@ ListNode *mergeTwoLists(ListNode *a, ListNode *b) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 分治合并
|
// 分治合并
|
||||||
ListNode *merge(vector<ListNode*> &lists, int l, int r) {
|
ListNode *S0023::merge(vector<ListNode *> &lists, int l, int r) {
|
||||||
if (l == r) {
|
if (l == r) {
|
||||||
return lists[l];
|
return lists[l];
|
||||||
}
|
}
|
||||||
@ -38,6 +38,6 @@ ListNode *merge(vector<ListNode*> &lists, int l, int r) {
|
|||||||
return mergeTwoLists(merge(lists, 1, mid), merge(lists, mid + 1, r));
|
return mergeTwoLists(merge(lists, 1, mid), merge(lists, mid + 1, r));
|
||||||
}
|
}
|
||||||
|
|
||||||
ListNode* Solution::mergeKLists(vector<ListNode*>& lists) {
|
ListNode *S0023::mergeKLists(vector<ListNode *> &lists) {
|
||||||
return merge(lists, 0, lists.size() - 1);
|
return merge(lists, 0, lists.size() - 1);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0024_swap_nodes_in_pairs.hpp"
|
#include "s0024_swap_nodes_in_pairs.hpp"
|
||||||
|
|
||||||
ListNode* swapPairs(ListNode* head) {
|
ListNode* S0024::swapPairs(ListNode* head) {
|
||||||
if (head == nullptr || head->next == nullptr) {
|
if (head == nullptr || head->next == nullptr) {
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#include "s0025_reverse_nodes_in_k-group.hpp"
|
#include "s0025_reverse_nodes_in_k-group.hpp"
|
||||||
|
|
||||||
// 翻转一个子链表,并且返回新的头与尾
|
// 翻转一个子链表,并且返回新的头与尾
|
||||||
pair<ListNode*, ListNode*> myReverse(ListNode* head, ListNode* tail) {
|
pair<ListNode*, ListNode*> S0025::myReverse(ListNode* head, ListNode* tail) {
|
||||||
ListNode* prev = tail->next;
|
ListNode* prev = tail->next;
|
||||||
ListNode* p = head;
|
ListNode* p = head;
|
||||||
while (prev != tail) {
|
while (prev != tail) {
|
||||||
@ -13,7 +13,7 @@ pair<ListNode*, ListNode*> myReverse(ListNode* head, ListNode* tail) {
|
|||||||
return {tail, head};
|
return {tail, head};
|
||||||
}
|
}
|
||||||
|
|
||||||
ListNode* Solution::reverseKGroup(ListNode* head, int k) {
|
ListNode* S0025::reverseKGroup(ListNode* head, int k) {
|
||||||
ListNode* hair = new ListNode(0);
|
ListNode* hair = new ListNode(0);
|
||||||
hair->next = head;
|
hair->next = head;
|
||||||
ListNode* pre = hair;
|
ListNode* pre = hair;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0026_remove_duplicates_from_sorted_array.hpp"
|
#include "s0026_remove_duplicates_from_sorted_array.hpp"
|
||||||
|
|
||||||
int Solution::removeDuplicates(vector<int>& nums) {
|
int S0026::removeDuplicates(vector<int>& nums) {
|
||||||
int size = nums.size();
|
int size = nums.size();
|
||||||
if (size == 0 || size == 1) {
|
if (size == 0 || size == 1) {
|
||||||
return size;
|
return size;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0027_remove_element.hpp"
|
#include "s0027_remove_element.hpp"
|
||||||
|
|
||||||
int Solution::removeElement(vector<int>& nums, int val) {
|
int S0027::removeElement(vector<int>& nums, int val) {
|
||||||
int len = nums.size();
|
int len = nums.size();
|
||||||
int f{0}, s{0};
|
int f{0}, s{0};
|
||||||
while (f < len) {
|
while (f < len) {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0028_find_the_index_of_the_first_occurrence_in_a_string.hpp"
|
#include "s0028_find_the_index_of_the_first_occurrence_in_a_string.hpp"
|
||||||
|
|
||||||
int Solution::strStr(string haystack, string needle) {
|
int S0028::strStr(string haystack, string needle) {
|
||||||
int haystackLen = haystack.length();
|
int haystackLen = haystack.length();
|
||||||
int needleLen = needle.length();
|
int needleLen = needle.length();
|
||||||
for (int i{0}; i < haystackLen; ++i) {
|
for (int i{0}; i < haystackLen; ++i) {
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
// 首先11比3大,结果至少是1,
|
// 首先11比3大,结果至少是1,
|
||||||
// 然后我让3翻倍,就是6,发现11比3翻倍后还要大,那么结果就至少是2了,那我让这个6再翻倍,得12,11不比12大,吓死我了,差点让就让刚才的最小解2也翻倍得到4了。但是我知道最终结果肯定在2和4之间。也就是说2再加上某个数,这个数是多少呢?我让11减去刚才最后一次的结果6,剩下5,我们计算5是3的几倍,也就是除法,看,递归出现了。
|
// 然后我让3翻倍,就是6,发现11比3翻倍后还要大,那么结果就至少是2了,那我让这个6再翻倍,得12,11不比12大,吓死我了,差点让就让刚才的最小解2也翻倍得到4了。但是我知道最终结果肯定在2和4之间。也就是说2再加上某个数,这个数是多少呢?我让11减去刚才最后一次的结果6,剩下5,我们计算5是3的几倍,也就是除法,看,递归出现了。
|
||||||
|
|
||||||
int div(long a, long b) { // 似乎精髓和难点就在于下面这几句
|
int S0029::div(long a, long b) { // 似乎精髓和难点就在于下面这几句
|
||||||
if (a < b) return 0;
|
if (a < b) return 0;
|
||||||
long count = 1;
|
long count = 1;
|
||||||
long tb = b; // 在后面的代码中不更新b
|
long tb = b; // 在后面的代码中不更新b
|
||||||
@ -15,7 +15,7 @@ int div(long a, long b) { // 似乎精髓和难点就在于下面这几句
|
|||||||
return count + div(a - tb, b);
|
return count + div(a - tb, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Solution::divide(int dividend, int divisor) {
|
int S0029::divide(int dividend, int divisor) {
|
||||||
if (dividend == 0) return 0;
|
if (dividend == 0) return 0;
|
||||||
if (divisor == 1) return dividend;
|
if (divisor == 1) return dividend;
|
||||||
if (divisor == -1) {
|
if (divisor == -1) {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0030_substring_with_concatenation_of_all_words.hpp"
|
#include "s0030_substring_with_concatenation_of_all_words.hpp"
|
||||||
|
|
||||||
vector<int> Solution::findSubstring(string s, vector<string>& words) {
|
vector<int> S0030::findSubstring(string s, vector<string>& words) {
|
||||||
vector<int> res;
|
vector<int> res;
|
||||||
int m = words.size(), n = words[0].size(), ls = s.size();
|
int m = words.size(), n = words[0].size(), ls = s.size();
|
||||||
for (int i = 0; i < n && i + m * n <= ls; ++i) {
|
for (int i = 0; i < n && i + m * n <= ls; ++i) {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0031_next_permutation.hpp"
|
#include "s0031_next_permutation.hpp"
|
||||||
|
|
||||||
void Solution::nextPermutation(vector<int>& nums) {
|
void S0031::nextPermutation(vector<int>& nums) {
|
||||||
int i = nums.size() - 2;
|
int i = nums.size() - 2;
|
||||||
while (i >= 0 && nums[i] >= nums[i + 1]) {
|
while (i >= 0 && nums[i] >= nums[i + 1]) {
|
||||||
i--;
|
i--;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0032_longest_valid_parentheses.hpp"
|
#include "s0032_longest_valid_parentheses.hpp"
|
||||||
|
|
||||||
int Solution::longestValidParentheses(string s) {
|
int S0032::longestValidParentheses(string s) {
|
||||||
int maxans = 0, n = s.length();
|
int maxans = 0, n = s.length();
|
||||||
vector<int> dp(n, 0);
|
vector<int> dp(n, 0);
|
||||||
for (int i = 1; i < n; i++) {
|
for (int i = 1; i < n; i++) {
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
// 思路很简单,对数组二分,有一半一定是有序的,另一半可能有序可能无序
|
// 思路很简单,对数组二分,有一半一定是有序的,另一半可能有序可能无序
|
||||||
// 对有序的一半进行二分搜索,无序的部分再次进行二分,如此迭代
|
// 对有序的一半进行二分搜索,无序的部分再次进行二分,如此迭代
|
||||||
|
|
||||||
int Solution::search(vector<int>& nums, int target) {
|
int S0033::search(vector<int>& nums, int target) {
|
||||||
int size = nums.size();
|
int size = nums.size();
|
||||||
int left{0};
|
int left{0};
|
||||||
int right = size - 1;
|
int right = size - 1;
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
#include "s0034_find_first_and_last_position_of_element_in_sorted_array.hpp"
|
#include "s0034_find_first_and_last_position_of_element_in_sorted_array.hpp"
|
||||||
|
|
||||||
// 闭区间写法
|
// 闭区间写法
|
||||||
vector<int> Solution1::searchRange(vector<int>& nums, int target) {
|
vector<int> S0034::searchRange1(vector<int>& nums, int target) {
|
||||||
int len = nums.size();
|
int len = nums.size();
|
||||||
int l{0};
|
int l{0};
|
||||||
int r = len - 1;
|
int r = len - 1;
|
||||||
while (l <= r) {
|
while (l <= r) {
|
||||||
@ -27,15 +27,15 @@ vector<int> Solution1::searchRange(vector<int>& nums, int target) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return vector<int> {l + 1, r - 1};
|
return vector<int>{l + 1, r - 1};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return vector<int> {-1, -1};
|
return vector<int>{-1, -1};
|
||||||
}
|
}
|
||||||
|
|
||||||
// 开区间写法
|
// 开区间写法
|
||||||
vector<int> Solution2::searchRange(vector<int>& nums, int target) {
|
vector<int> S0034::searchRange2(vector<int>& nums, int target) {
|
||||||
int len = nums.size();
|
int len = nums.size();
|
||||||
int l{0};
|
int l{0};
|
||||||
int r = len;
|
int r = len;
|
||||||
while (l < r) {
|
while (l < r) {
|
||||||
@ -60,8 +60,8 @@ vector<int> Solution2::searchRange(vector<int>& nums, int target) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return vector<int> {l + 1, r - 1};
|
return vector<int>{l + 1, r - 1};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return vector<int> {-1, -1};
|
return vector<int>{-1, -1};
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#include "s0035_search_insert_position.hpp"
|
#include "s0035_search_insert_position.hpp"
|
||||||
|
|
||||||
// 闭区间写法
|
// 闭区间写法
|
||||||
int Solution1::searchInsert(vector<int>& nums, int target) {
|
int S0035::searchInsert1(vector<int>& nums, int target) {
|
||||||
int len = nums.size();
|
int len = nums.size();
|
||||||
int l{0};
|
int l{0};
|
||||||
int r = len - 1;
|
int r = len - 1;
|
||||||
@ -19,7 +19,7 @@ int Solution1::searchInsert(vector<int>& nums, int target) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 开区间写法
|
// 开区间写法
|
||||||
int Solution2::searchInsert(vector<int>& nums, int target) {
|
int S0035::searchInsert2(vector<int>& nums, int target) {
|
||||||
int len = nums.size();
|
int len = nums.size();
|
||||||
int l{0};
|
int l{0};
|
||||||
int r = len;
|
int r = len;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0036_valid_sudoku.hpp"
|
#include "s0036_valid_sudoku.hpp"
|
||||||
|
|
||||||
bool Solution::isValidSudoku(vector<vector<char>>& board) {
|
bool S0036::isValidSudoku(vector<vector<char>>& board) {
|
||||||
vector<unordered_map<char, int>> rows;
|
vector<unordered_map<char, int>> rows;
|
||||||
vector<unordered_map<char, int>> columns;
|
vector<unordered_map<char, int>> columns;
|
||||||
vector<unordered_map<char, int>> grids;
|
vector<unordered_map<char, int>> grids;
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
#include "s0037_sudoku_solver.hpp"
|
#include "s0037_sudoku_solver.hpp"
|
||||||
|
|
||||||
// 返回结果为当前传递进去的数独是否存在解
|
// 返回结果为当前传递进去的数独是否存在解
|
||||||
bool recusiveSolveSudoku(vector<vector<char>> &board,
|
bool S0037::recusiveSolveSudoku(vector<vector<char>> &board,
|
||||||
vector<unordered_map<char, bool>> &rows,
|
vector<unordered_map<char, bool>> &rows,
|
||||||
vector<unordered_map<char, bool>> &cols,
|
vector<unordered_map<char, bool>> &cols,
|
||||||
vector<unordered_map<char, bool>> &grids, int row,
|
vector<unordered_map<char, bool>> &grids,
|
||||||
int col) {
|
int row, int col) {
|
||||||
int size = board.size();
|
int size = board.size();
|
||||||
// 边界校验
|
// 边界校验
|
||||||
if (col == size) {
|
if (col == size) {
|
||||||
@ -49,7 +49,7 @@ bool recusiveSolveSudoku(vector<vector<char>> &board,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Solution::solveSudoku(vector<vector<char>> &board) {
|
void S0037::solveSudoku(vector<vector<char>> &board) {
|
||||||
// 每个行/列/单元格中某个数字是否出现过
|
// 每个行/列/单元格中某个数字是否出现过
|
||||||
vector<unordered_map<char, bool>> rows;
|
vector<unordered_map<char, bool>> rows;
|
||||||
vector<unordered_map<char, bool>> cols;
|
vector<unordered_map<char, bool>> cols;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0038_count_and_say.hpp"
|
#include "s0038_count_and_say.hpp"
|
||||||
|
|
||||||
string Solution::countAndSay(int n) {
|
string S0038::countAndSay(int n) {
|
||||||
if (n == 1) {
|
if (n == 1) {
|
||||||
return "1";
|
return "1";
|
||||||
} else if (n <= 0) {
|
} else if (n <= 0) {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#include "s0039_combination_sum.hpp"
|
#include "s0039_combination_sum.hpp"
|
||||||
|
|
||||||
void dfs(vector<int>& candidates, int target, vector<vector<int>>& ans,
|
void S0039::dfs(vector<int>& candidates, int target, vector<vector<int>>& ans,
|
||||||
vector<int>& combine, int idx) {
|
vector<int>& combine, int idx) {
|
||||||
if (idx == candidates.size()) {
|
if (idx == candidates.size()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -19,8 +19,7 @@ void dfs(vector<int>& candidates, int target, vector<vector<int>>& ans,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<vector<int>> Solution::combinationSum(vector<int>& candidates,
|
vector<vector<int>> S0039::combinationSum(vector<int>& candidates, int target) {
|
||||||
int target) {
|
|
||||||
vector<vector<int>> ans;
|
vector<vector<int>> ans;
|
||||||
vector<int> combine;
|
vector<int> combine;
|
||||||
dfs(candidates, target, ans, combine, 0);
|
dfs(candidates, target, ans, combine, 0);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0076_minimum_window_substring.hpp"
|
#include "s0076_minimum_window_substring.hpp"
|
||||||
|
|
||||||
bool Solution::check() {
|
bool S0076::check() {
|
||||||
for (const auto &p : ori) {
|
for (const auto &p : ori) {
|
||||||
if (cnt[p.first] < p.second) {
|
if (cnt[p.first] < p.second) {
|
||||||
return false;
|
return false;
|
||||||
@ -11,7 +11,7 @@ bool Solution::check() {
|
|||||||
|
|
||||||
// 不包含 t 中所有字符,r 右移
|
// 不包含 t 中所有字符,r 右移
|
||||||
// 包含 t 中所有字符,l 右移
|
// 包含 t 中所有字符,l 右移
|
||||||
string Solution::minWindow(string s, string t) {
|
string S0076::minWindow(string s, string t) {
|
||||||
for (const auto &c : t) {
|
for (const auto &c : t) {
|
||||||
++ori[c];
|
++ori[c];
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,14 @@
|
|||||||
#include "s0151_reverse_words_in_a_string.hpp"
|
#include "s0151_reverse_words_in_a_string.hpp"
|
||||||
|
|
||||||
string Solution::reverseWords(string s) {
|
void S0151::reverseSubStr(string &s, int begin, int end) {
|
||||||
|
for (; begin < end; ++begin, --end) {
|
||||||
|
char tmp = s[begin];
|
||||||
|
s[begin] = s[end];
|
||||||
|
s[end] = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
string S0151::reverseWords(string s) {
|
||||||
if (s.length() == 0) {
|
if (s.length() == 0) {
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0209_minimum_size_subarray_sum.hpp"
|
#include "s0209_minimum_size_subarray_sum.hpp"
|
||||||
|
|
||||||
int Solution::minSubArrayLen(int target, vector<int>& nums) {
|
int S0209::minSubArrayLen(int target, vector<int>& nums) {
|
||||||
int len = nums.size();
|
int len = nums.size();
|
||||||
int result{INT_MAX};
|
int result{INT_MAX};
|
||||||
for (int begin{0}, end{0}, sum{0}; end < len; ++end) {
|
for (int begin{0}, end{0}, sum{0}; end < len; ++end) {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include "s0283_move_zeroes.hpp"
|
#include "s0283_move_zeroes.hpp"
|
||||||
|
|
||||||
void Solution::moveZeroes(vector<int>& nums) {
|
void S0283::moveZeroes(vector<int>& nums) {
|
||||||
int len = nums.size();
|
int len = nums.size();
|
||||||
if (len <= 1) {
|
if (len <= 1) {
|
||||||
return;
|
return;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#include "s0704_binary_search.hpp"
|
#include "s0704_binary_search.hpp"
|
||||||
|
|
||||||
// 闭区间写法
|
// 闭区间写法
|
||||||
int Solution1::binSearch(vector<int>& nums, int target) {
|
int S0704::binSearch1(vector<int>& nums, int target) {
|
||||||
int len = nums.size();
|
int len = nums.size();
|
||||||
int l{0};
|
int l{0};
|
||||||
int r = len - 1;
|
int r = len - 1;
|
||||||
@ -19,7 +19,7 @@ int Solution1::binSearch(vector<int>& nums, int target) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 开区间写法
|
// 开区间写法
|
||||||
int Solution2::binSearch(vector<int>& nums, int target) {
|
int S0704::binSearch2(vector<int>& nums, int target) {
|
||||||
int len = nums.size();
|
int len = nums.size();
|
||||||
int l{0};
|
int l{0};
|
||||||
int r = len;
|
int r = len;
|
||||||
|
@ -5,6 +5,6 @@
|
|||||||
TEST(Offer05, Case1) {
|
TEST(Offer05, Case1) {
|
||||||
string s{"We are happy."};
|
string s{"We are happy."};
|
||||||
string expected{"We%20are%20happy."};
|
string expected{"We%20are%20happy."};
|
||||||
Solution solution;
|
Offer05 solution;
|
||||||
EXPECT_EQ(solution.replaceSpace(s), expected);
|
EXPECT_EQ(solution.replaceSpace(s), expected);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ TEST(Offer58, Case1) {
|
|||||||
string s{"abcdefg"};
|
string s{"abcdefg"};
|
||||||
int k{2};
|
int k{2};
|
||||||
string expected{"cdefgab"};
|
string expected{"cdefgab"};
|
||||||
Solution solution;
|
Offer58 solution;
|
||||||
EXPECT_EQ(solution.reverseLeftWords(s, k), expected);
|
EXPECT_EQ(solution.reverseLeftWords(s, k), expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -14,6 +14,6 @@ TEST(Offer58, Case2) {
|
|||||||
string s{"lrloseumgh"};
|
string s{"lrloseumgh"};
|
||||||
int k{6};
|
int k{6};
|
||||||
string expected{"umghlrlose"};
|
string expected{"umghlrlose"};
|
||||||
Solution solution;
|
Offer58 solution;
|
||||||
EXPECT_EQ(solution.reverseLeftWords(s, k), expected);
|
EXPECT_EQ(solution.reverseLeftWords(s, k), expected);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
TEST(Problem1, Case1) {
|
TEST(Problem1, Case1) {
|
||||||
std::vector<int> input1{ 2, 7, 11, 15 };
|
std::vector<int> input1{ 2, 7, 11, 15 };
|
||||||
int input2{ 9 };
|
int input2{ 9 };
|
||||||
Solution solution;
|
S0001 solution;
|
||||||
std::vector<int> result = solution.twoSum(input1, input2);
|
std::vector<int> result = solution.twoSum(input1, input2);
|
||||||
std::vector<int> answer = std::vector<int>{ 0, 1 };
|
std::vector<int> answer = std::vector<int>{ 0, 1 };
|
||||||
EXPECT_EQ(result, answer);
|
EXPECT_EQ(result, answer);
|
||||||
@ -15,7 +15,7 @@ TEST(Problem1, Case1) {
|
|||||||
TEST(Problem1, Case2) {
|
TEST(Problem1, Case2) {
|
||||||
std::vector<int> input1{ 3, 2, 4 };
|
std::vector<int> input1{ 3, 2, 4 };
|
||||||
int input2{ 6 };
|
int input2{ 6 };
|
||||||
Solution solution;
|
S0001 solution;
|
||||||
std::vector<int> result = solution.twoSum(input1, input2);
|
std::vector<int> result = solution.twoSum(input1, input2);
|
||||||
std::vector<int> answer = std::vector<int>{ 1, 2 };
|
std::vector<int> answer = std::vector<int>{ 1, 2 };
|
||||||
EXPECT_EQ(result, answer);
|
EXPECT_EQ(result, answer);
|
||||||
@ -24,7 +24,7 @@ TEST(Problem1, Case2) {
|
|||||||
TEST(Problem1, Case3) {
|
TEST(Problem1, Case3) {
|
||||||
std::vector<int> input1{ 3, 3 };
|
std::vector<int> input1{ 3, 3 };
|
||||||
int input2{ 6 };
|
int input2{ 6 };
|
||||||
Solution solution;
|
S0001 solution;
|
||||||
std::vector<int> result = solution.twoSum(input1, input2);
|
std::vector<int> result = solution.twoSum(input1, input2);
|
||||||
std::vector<int> answer = std::vector<int>{ 0, 1 };
|
std::vector<int> answer = std::vector<int>{ 0, 1 };
|
||||||
EXPECT_EQ(result, answer);
|
EXPECT_EQ(result, answer);
|
||||||
|
@ -11,7 +11,7 @@ TEST(Problem2, Case1) {
|
|||||||
new ListNode(5,
|
new ListNode(5,
|
||||||
new ListNode(6,
|
new ListNode(6,
|
||||||
new ListNode(4)));
|
new ListNode(4)));
|
||||||
Solution solution;
|
S0002 solution;
|
||||||
ListNode *l = solution.addTwoNumbers(l1, l2);
|
ListNode *l = solution.addTwoNumbers(l1, l2);
|
||||||
EXPECT_EQ(l->val, 7);
|
EXPECT_EQ(l->val, 7);
|
||||||
EXPECT_EQ(l->next->val, 0);
|
EXPECT_EQ(l->next->val, 0);
|
||||||
@ -21,7 +21,7 @@ TEST(Problem2, Case1) {
|
|||||||
TEST(Problem2, Case2) {
|
TEST(Problem2, Case2) {
|
||||||
ListNode *l1 = new ListNode(0);
|
ListNode *l1 = new ListNode(0);
|
||||||
ListNode *l2 = new ListNode(0);
|
ListNode *l2 = new ListNode(0);
|
||||||
Solution solution;
|
S0002 solution;
|
||||||
ListNode *l = solution.addTwoNumbers(l1, l2);
|
ListNode *l = solution.addTwoNumbers(l1, l2);
|
||||||
EXPECT_EQ(l->val, 0);
|
EXPECT_EQ(l->val, 0);
|
||||||
}
|
}
|
||||||
@ -40,7 +40,7 @@ TEST(Problem2, Case3) {
|
|||||||
new ListNode(9,
|
new ListNode(9,
|
||||||
new ListNode(9,
|
new ListNode(9,
|
||||||
new ListNode(9))));
|
new ListNode(9))));
|
||||||
Solution solution;
|
S0002 solution;
|
||||||
ListNode *l = solution.addTwoNumbers(l1, l2);
|
ListNode *l = solution.addTwoNumbers(l1, l2);
|
||||||
EXPECT_EQ(l->val, 8);
|
EXPECT_EQ(l->val, 8);
|
||||||
EXPECT_EQ(l->next->val, 9);
|
EXPECT_EQ(l->next->val, 9);
|
||||||
|
@ -4,18 +4,18 @@
|
|||||||
|
|
||||||
TEST(Problem3, Case1) {
|
TEST(Problem3, Case1) {
|
||||||
std::string s{std::string("abcabcbb")};
|
std::string s{std::string("abcabcbb")};
|
||||||
Solution solution;
|
S0003 solution;
|
||||||
EXPECT_EQ(solution.lengthOfLongestSubstring(s), 3);
|
EXPECT_EQ(solution.lengthOfLongestSubstring(s), 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Problem3, Case2) {
|
TEST(Problem3, Case2) {
|
||||||
std::string s{std::string("bbbbb")};
|
std::string s{std::string("bbbbb")};
|
||||||
Solution solution;
|
S0003 solution;
|
||||||
EXPECT_EQ(solution.lengthOfLongestSubstring(s), 1);
|
EXPECT_EQ(solution.lengthOfLongestSubstring(s), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Problem3, Case3) {
|
TEST(Problem3, Case3) {
|
||||||
std::string s{std::string("pwwkew")};
|
std::string s{std::string("pwwkew")};
|
||||||
Solution solution;
|
S0003 solution;
|
||||||
EXPECT_EQ(solution.lengthOfLongestSubstring(s), 3);
|
EXPECT_EQ(solution.lengthOfLongestSubstring(s), 3);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ TEST(Problem4, Case1) {
|
|||||||
std::vector<int> nums1{1, 3};
|
std::vector<int> nums1{1, 3};
|
||||||
std::vector<int> nums2{2};
|
std::vector<int> nums2{2};
|
||||||
double answer{2};
|
double answer{2};
|
||||||
Solution solution;
|
S0004 solution;
|
||||||
EXPECT_EQ(solution.findMedianSortedArrays(nums1, nums2), answer);
|
EXPECT_EQ(solution.findMedianSortedArrays(nums1, nums2), answer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -14,6 +14,6 @@ TEST(Problem4, Case2) {
|
|||||||
std::vector<int> nums1{1, 2};
|
std::vector<int> nums1{1, 2};
|
||||||
std::vector<int> nums2{3, 4};
|
std::vector<int> nums2{3, 4};
|
||||||
double answer{2.5};
|
double answer{2.5};
|
||||||
Solution solution;
|
S0004 solution;
|
||||||
EXPECT_EQ(solution.findMedianSortedArrays(nums1, nums2), answer);
|
EXPECT_EQ(solution.findMedianSortedArrays(nums1, nums2), answer);
|
||||||
}
|
}
|
||||||
|
@ -6,17 +6,15 @@ TEST(Problem5, Case1) {
|
|||||||
std::string s("babad");
|
std::string s("babad");
|
||||||
std::string r1("bab");
|
std::string r1("bab");
|
||||||
std::string r2("aba");
|
std::string r2("aba");
|
||||||
Solution1 solution1;
|
S0005 solution;
|
||||||
Solution2 solution2;
|
EXPECT_TRUE(solution.longestPalindrome1(s) == r1 || solution.longestPalindrome1(s) == r2);
|
||||||
EXPECT_TRUE(solution1.longestPalindrome(s) == r1 || solution1.longestPalindrome(s) == r2);
|
EXPECT_TRUE(solution.longestPalindrome2(s) == r1 || solution.longestPalindrome2(s) == r2);
|
||||||
EXPECT_TRUE(solution2.longestPalindrome(s) == r1 || solution2.longestPalindrome(s) == r2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Problem5, Case2) {
|
TEST(Problem5, Case2) {
|
||||||
std::string s("cbbd");
|
std::string s("cbbd");
|
||||||
std::string r("bb");
|
std::string r("bb");
|
||||||
Solution1 solution1;
|
S0005 solution;
|
||||||
Solution2 solution2;
|
EXPECT_EQ(r.compare(solution.longestPalindrome1(s)), 0);
|
||||||
EXPECT_EQ(r.compare(solution1.longestPalindrome(s)), 0);
|
EXPECT_EQ(r.compare(solution.longestPalindrome2(s)), 0);
|
||||||
EXPECT_EQ(r.compare(solution2.longestPalindrome(s)), 0);
|
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ TEST(Problem6, Case1) {
|
|||||||
string s("PAYPALISHIRING");
|
string s("PAYPALISHIRING");
|
||||||
int rows = 3;
|
int rows = 3;
|
||||||
string r("PAHNAPLSIIGYIR");
|
string r("PAHNAPLSIIGYIR");
|
||||||
Solution solution;
|
S0006 solution;
|
||||||
EXPECT_TRUE(solution.convert(s, rows) == r);
|
EXPECT_TRUE(solution.convert(s, rows) == r);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -14,7 +14,7 @@ TEST(Problem6, Case2) {
|
|||||||
string s("PAYPALISHIRING");
|
string s("PAYPALISHIRING");
|
||||||
int rows = 4;
|
int rows = 4;
|
||||||
string r("PINALSIGYAHRPI");
|
string r("PINALSIGYAHRPI");
|
||||||
Solution solution;
|
S0006 solution;
|
||||||
EXPECT_TRUE(solution.convert(s, rows) == r);
|
EXPECT_TRUE(solution.convert(s, rows) == r);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22,6 +22,6 @@ TEST(Problem6, Case3) {
|
|||||||
string s("AB");
|
string s("AB");
|
||||||
int rows = 4;
|
int rows = 4;
|
||||||
string r("AB");
|
string r("AB");
|
||||||
Solution solution;
|
S0006 solution;
|
||||||
EXPECT_TRUE(solution.convert(s, rows) == r);
|
EXPECT_TRUE(solution.convert(s, rows) == r);
|
||||||
}
|
}
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user