Refactor
This commit is contained in:
parent
b13cfa00bb
commit
72555b19e0
@ -5,7 +5,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class Offer05 {
|
||||
public:
|
||||
string replaceSpace(string s);
|
||||
};
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class Offer58 {
|
||||
public:
|
||||
string reverseLeftWords(string s, int n);
|
||||
void reverseSubStr(string &s, int begin, int end);
|
||||
|
@ -5,7 +5,9 @@
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
using namespace std;
|
||||
|
||||
class S0001 {
|
||||
public:
|
||||
/**
|
||||
* @brief Two Sum
|
||||
|
@ -9,7 +9,7 @@ struct ListNode {
|
||||
ListNode(int x, ListNode* next) : val(x), next(next) {}
|
||||
};
|
||||
|
||||
class Solution {
|
||||
class S0002 {
|
||||
public:
|
||||
/**
|
||||
* @brief Add Two Numbers
|
||||
|
@ -4,7 +4,9 @@
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
class Solution {
|
||||
using namespace std;
|
||||
|
||||
class S0003 {
|
||||
public:
|
||||
/**
|
||||
* @brief Longest Substring Without Repeating Characters
|
||||
|
@ -3,7 +3,9 @@
|
||||
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
using namespace std;
|
||||
|
||||
class S0004 {
|
||||
public:
|
||||
/**
|
||||
* @brief Median of Two Sorted Arrays
|
||||
|
@ -1,26 +1,20 @@
|
||||
#ifndef S0005_LONGEST_PALINDROMIC_SUBSTRING_HPP
|
||||
#define S0005_LONGEST_PALINDROMIC_SUBSTRING_HPP
|
||||
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution1 {
|
||||
public:
|
||||
/**
|
||||
* @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 longestPalindrome(string s);
|
||||
};
|
||||
typedef struct S0005ResultStruct {
|
||||
int left1; // 边界情况为长度为 1 的字符时,扩展完成后的左边界
|
||||
int right1; // 边界情况为长度为 1 的字符时,扩展完成后的右边界
|
||||
int left2; // 边界情况为长度为 2 的字符串时,扩展完成后的左边界
|
||||
int right2; // 边界情况为长度为 2 的字符串时,扩展完成后的右边界
|
||||
} S0005Result;
|
||||
|
||||
class Solution2 {
|
||||
class S0005 {
|
||||
public:
|
||||
/**
|
||||
* @brief Longest Palindromic Substring
|
||||
@ -30,7 +24,17 @@ class Solution2 {
|
||||
* @param s the given string
|
||||
* @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
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0006 {
|
||||
public:
|
||||
/**
|
||||
* @brief Zigzag Conversion
|
||||
|
@ -5,7 +5,9 @@
|
||||
#include <cmath>
|
||||
#include <climits>
|
||||
|
||||
class Solution {
|
||||
using namespace std;
|
||||
|
||||
class S0007 {
|
||||
public:
|
||||
int reverse(int x);
|
||||
};
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0008 {
|
||||
public:
|
||||
int myAtoi(string s);
|
||||
};
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0009 {
|
||||
public:
|
||||
bool isPalindrome(int x);
|
||||
};
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0010 {
|
||||
public:
|
||||
bool isMatch(string s, string p);
|
||||
};
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0011 {
|
||||
public:
|
||||
int maxArea(vector<int>& height);
|
||||
};
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0012 {
|
||||
public:
|
||||
string intToRoman(int num);
|
||||
};
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0013 {
|
||||
public:
|
||||
int romanToInt(string s);
|
||||
};
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0014 {
|
||||
public:
|
||||
string longestCommonPrefix(vector<string>& strs);
|
||||
};
|
||||
|
@ -7,14 +7,10 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution1 {
|
||||
class S0015 {
|
||||
public:
|
||||
vector<vector<int>> threeSum(vector<int>& nums);
|
||||
};
|
||||
|
||||
class Solution2 {
|
||||
public:
|
||||
vector<vector<int>> threeSum(vector<int>& nums);
|
||||
vector<vector<int>> threeSum1(vector<int>& nums);
|
||||
vector<vector<int>> threeSum2(vector<int>& nums);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0016 {
|
||||
public:
|
||||
int threeSumClosest(vector<int>& nums, int target);
|
||||
};
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0017 {
|
||||
public:
|
||||
vector<string> letterCombinations(string digits);
|
||||
};
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0018 {
|
||||
public:
|
||||
vector<vector<int>> fourSum(vector<int>& nums, int target);
|
||||
};
|
||||
|
@ -9,7 +9,7 @@ struct ListNode {
|
||||
ListNode(int x, ListNode* next) : val(x), next(next) {}
|
||||
};
|
||||
|
||||
class Solution {
|
||||
class S0019 {
|
||||
public:
|
||||
ListNode* removeNthFromEnd(ListNode* head, int n);
|
||||
};
|
||||
|
@ -6,9 +6,10 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0020 {
|
||||
public:
|
||||
bool isValid(string s);
|
||||
bool match(string s1, string s2);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -1,6 +1,10 @@
|
||||
#ifndef S0021_MERGE_TWO_SORTED_LISTS_HPP
|
||||
#define S0021_MERGE_TWO_SORTED_LISTS_HPP
|
||||
|
||||
#include <limits>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode* next;
|
||||
@ -9,9 +13,7 @@ struct ListNode {
|
||||
ListNode(int x, ListNode* next) : val(x), next(next) {}
|
||||
};
|
||||
|
||||
#include <limits>
|
||||
|
||||
class Solution {
|
||||
class S0021 {
|
||||
public:
|
||||
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2);
|
||||
};
|
||||
|
@ -7,9 +7,10 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0022 {
|
||||
public:
|
||||
vector<string> generateParenthesis(int n);
|
||||
void dfs(string current, int left, int right, vector<string> &result);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -13,9 +13,11 @@ struct ListNode {
|
||||
ListNode(int x, ListNode *next) : val(x), next(next) {}
|
||||
};
|
||||
|
||||
class Solution {
|
||||
class S0023 {
|
||||
public:
|
||||
ListNode *mergeKLists(vector<ListNode *> &lists);
|
||||
ListNode *mergeTwoLists(ListNode *a, ListNode *b);
|
||||
ListNode *merge(vector<ListNode *> &lists, int l, int r);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -9,7 +9,7 @@ struct ListNode {
|
||||
ListNode(int x, ListNode* next) : val(x), next(next) {}
|
||||
};
|
||||
|
||||
class Solution {
|
||||
class S0024 {
|
||||
public:
|
||||
ListNode* swapPairs(ListNode* head);
|
||||
};
|
||||
|
@ -1,8 +1,8 @@
|
||||
#ifndef S0025_REVERSE_NODES_IN_K_GROUP_HPP
|
||||
#define S0025_REVERSE_NODES_IN_K_GROUP_HPP
|
||||
|
||||
#include <utility>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@ -14,9 +14,10 @@ struct ListNode {
|
||||
ListNode(int x, ListNode* next) : val(x), next(next) {}
|
||||
};
|
||||
|
||||
class Solution {
|
||||
class S0025 {
|
||||
public:
|
||||
ListNode* reverseKGroup(ListNode* head, int k);
|
||||
pair<ListNode*, ListNode*> myReverse(ListNode* head, ListNode* tail);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0026 {
|
||||
public:
|
||||
int removeDuplicates(vector<int>& nums);
|
||||
};
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0027 {
|
||||
public:
|
||||
int removeElement(vector<int>& nums, int val);
|
||||
};
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0028 {
|
||||
public:
|
||||
int strStr(string haystack, string needle);
|
||||
};
|
||||
|
@ -3,9 +3,12 @@
|
||||
|
||||
#include <climits>
|
||||
|
||||
class Solution {
|
||||
using namespace std;
|
||||
|
||||
class S0029 {
|
||||
public:
|
||||
int divide(int dividend, int divisor);
|
||||
int div(long a, long b);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0030 {
|
||||
public:
|
||||
vector<int> findSubstring(string s, vector<string>& words);
|
||||
};
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0031 {
|
||||
public:
|
||||
void nextPermutation(vector<int>& nums);
|
||||
};
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0032 {
|
||||
public:
|
||||
int longestValidParentheses(string s);
|
||||
};
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0033 {
|
||||
public:
|
||||
int search(vector<int>& nums, int target);
|
||||
};
|
||||
|
@ -5,13 +5,10 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution1 {
|
||||
class S0034 {
|
||||
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
|
||||
|
@ -5,14 +5,10 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution1 {
|
||||
class S0035 {
|
||||
public:
|
||||
int searchInsert(vector<int>& nums, int target);
|
||||
};
|
||||
|
||||
class Solution2 {
|
||||
public:
|
||||
int searchInsert(vector<int>& nums, int target);
|
||||
int searchInsert1(vector<int>& nums, int target);
|
||||
int searchInsert2(vector<int>& nums, int target);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0036 {
|
||||
public:
|
||||
bool isValidSudoku(vector<vector<char>>& board);
|
||||
};
|
||||
|
@ -1,16 +1,21 @@
|
||||
#ifndef S0037_SUDOKU_SOLVER_HPP
|
||||
#define S0037_SUDOKU_SOLVER_HPP
|
||||
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0037 {
|
||||
public:
|
||||
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
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0038 {
|
||||
public:
|
||||
string countAndSay(int n);
|
||||
};
|
||||
|
@ -1,15 +1,17 @@
|
||||
#ifndef S0039_COMBINATION_SUM_HPP
|
||||
#define S0039_COMBINATION_SUM_HPP
|
||||
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0039 {
|
||||
public:
|
||||
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
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0076 {
|
||||
public:
|
||||
string minWindow(string s, string t);
|
||||
unordered_map<char, int> ori, cnt;
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0151 {
|
||||
public:
|
||||
void reverseSubStr(string &s, int begin, int end);
|
||||
string reverseWords(string s);
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0209 {
|
||||
public:
|
||||
int minSubArrayLen(int target, vector<int>& nums);
|
||||
};
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
class S0283 {
|
||||
public:
|
||||
void moveZeroes(vector<int>& nums);
|
||||
};
|
||||
|
@ -5,14 +5,10 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution1 {
|
||||
class S0704 {
|
||||
public:
|
||||
int binSearch(vector<int>& nums, int target);
|
||||
};
|
||||
|
||||
class Solution2 {
|
||||
public:
|
||||
int binSearch(vector<int>& nums, int target);
|
||||
int binSearch1(vector<int>& nums, int target);
|
||||
int binSearch2(vector<int>& nums, int target);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "offer_05.hpp"
|
||||
|
||||
string Solution::replaceSpace(string s) {
|
||||
string Offer05::replaceSpace(string s) {
|
||||
int len = s.length();
|
||||
int cnt{0};
|
||||
for (int i{0}; i < len; ++i) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
#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) {
|
||||
char tmp = s[begin];
|
||||
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();
|
||||
reverseSubStr(s, 0, n - 1);
|
||||
reverseSubStr(s, n, len - 1);
|
||||
|
@ -1,7 +1,6 @@
|
||||
#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;
|
||||
for (int i = 0; i < nums.size(); ++i) {
|
||||
auto it = hashtable.find(target - nums[i]);
|
||||
|
@ -1,6 +1,6 @@
|
||||
#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_end = l_begin;
|
||||
int carry{ (l1->val + l2->val) >= 10 ? 1 : 0 };
|
||||
|
@ -1,6 +1,6 @@
|
||||
#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_backward points to the last repeated element
|
||||
//
|
||||
|
@ -1,6 +1,6 @@
|
||||
#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) {
|
||||
int m = nums1.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) {
|
||||
int totalLength = nums1.size() + nums2.size();
|
||||
if (totalLength % 2 == 1) {
|
||||
|
@ -17,7 +17,7 @@
|
||||
// 把 dp 这张二维数组的表全部填好,然后在里面找最长的子字符串。
|
||||
// 时间复杂度:O(n^2) 因为总共有 n^2 个状态,计算每个状态需要的时间为 O(1)
|
||||
// 空间复杂度:O(n^2) 因为总共需要存储 n^2 个状态。
|
||||
string Solution1::longestPalindrome(string s) {
|
||||
string S0005::longestPalindrome1(string s) {
|
||||
int len = s.length();
|
||||
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();
|
||||
|
||||
if (i == len - 1) {
|
||||
return Result{i, i, i, i};
|
||||
return S0005Result{i, i, i, i};
|
||||
}
|
||||
|
||||
// 边界情况为长度为 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 left = 0, right = 0;
|
||||
Result result{0, 0, 0, 0};
|
||||
S0005Result result{0, 0, 0, 0};
|
||||
for (int i = 0; i < len; i++) {
|
||||
result = expand(s, i);
|
||||
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
|
||||
// 3 9 2*(n-1)*col + (n-1)
|
||||
|
||||
string Solution::convert(string s, int numRows) {
|
||||
string S0006::convert(string s, int numRows) {
|
||||
string r = "";
|
||||
int l = s.length();
|
||||
if (l == 1 || numRows == 1) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "s0007_reverse_integer.hpp"
|
||||
|
||||
int Solution::reverse(int x) {
|
||||
int S0007::reverse(int x) {
|
||||
int r{0};
|
||||
std::queue<int> queue;
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
// 当流程很复杂的时候,画流程图: https://paste.sainnhe.dev/NQxx.png
|
||||
|
||||
class Automaton {
|
||||
class S0008Automaton {
|
||||
string state = "start";
|
||||
unordered_map<string, vector<string>> table = {
|
||||
{"start", {"start", "signed", "in_number", "end"}},
|
||||
@ -32,8 +32,8 @@ class Automaton {
|
||||
}
|
||||
};
|
||||
|
||||
int Solution::myAtoi(string str) {
|
||||
Automaton automaton;
|
||||
int S0008::myAtoi(string str) {
|
||||
S0008Automaton automaton;
|
||||
for (char c : str) automaton.get(c);
|
||||
return automaton.sign * automaton.ans;
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "s0009_palindrome_number.hpp"
|
||||
|
||||
bool Solution::isPalindrome(int x) {
|
||||
bool S0009::isPalindrome(int x) {
|
||||
string s = to_string(x);
|
||||
string r = s;
|
||||
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 n = p.size();
|
||||
|
||||
|
@ -9,7 +9,7 @@
|
||||
// 也就是说只有当移动小的指针时才有可能让储存的水量超过之前的
|
||||
// 因此每次迭代我们移动较小的指针,然后统计出最大的储存水量
|
||||
|
||||
int Solution::maxArea(vector<int>& height) {
|
||||
int S0011::maxArea(vector<int>& height) {
|
||||
int max{0};
|
||||
for (int l{0}, r{static_cast<int>(height.size() - 1)}; r != l;) {
|
||||
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};
|
||||
string reps[] = {"M", "CM", "D", "CD", "C", "XC", "L",
|
||||
"XL", "X", "IX", "V", "IV", "I"};
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "s0013_roman_to_integer.hpp"
|
||||
|
||||
int Solution::romanToInt(string s) {
|
||||
int S0013::romanToInt(string s) {
|
||||
unordered_map<string, int> map;
|
||||
map["I"] = 1;
|
||||
map["V"] = 5;
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "s0014_longest_common_prefix.hpp"
|
||||
|
||||
string Solution::longestCommonPrefix(vector<string>& strs) {
|
||||
string S0014::longestCommonPrefix(vector<string>& strs) {
|
||||
string o{""};
|
||||
int size = strs.size();
|
||||
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;
|
||||
vector<vector<int>> result;
|
||||
int len = nums.size();
|
||||
@ -42,7 +42,7 @@ vector<vector<int>> Solution1::threeSum(vector<int>& nums) {
|
||||
|
||||
// 思路二:双指针
|
||||
// 先对整个数组排序,然后固定一个数 a ,然后 b, c 从两边往中间靠。
|
||||
vector<vector<int>> Solution2::threeSum(vector<int>& nums) {
|
||||
vector<vector<int>> S0015::threeSum2(vector<int>& nums) {
|
||||
int n = nums.size();
|
||||
sort(nums.begin(), nums.end());
|
||||
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 len = nums.size();
|
||||
sort(nums.begin(), nums.end());
|
||||
|
@ -1,6 +1,6 @@
|
||||
#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;
|
||||
map["2"] = vector<string>{"a", "b", "c"};
|
||||
map["3"] = vector<string>{"d", "e", "f"};
|
||||
|
@ -1,6 +1,6 @@
|
||||
#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();
|
||||
if (len < 4) {
|
||||
return {};
|
||||
|
@ -6,7 +6,7 @@
|
||||
// 步,等快指针走到头时,慢指针指向的的就是倒数第 n 个
|
||||
|
||||
// 双指针实现
|
||||
ListNode* Solution::removeNthFromEnd(ListNode* head, int n) {
|
||||
ListNode* S0019::removeNthFromEnd(ListNode* head, int n) {
|
||||
// 一种常见的处理头指针的方法是添加一个哑指针,这个哑指针指向头指针
|
||||
// 这样我们就不需要单独讨论头指针了。
|
||||
ListNode dummy(0, head);
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "s0020_valid_parentheses.hpp"
|
||||
|
||||
bool match(string s1, string s2) {
|
||||
bool S0020::match(string s1, string s2) {
|
||||
return 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;
|
||||
int len = s.length();
|
||||
for (int i{0}; i < len; i++) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "s0021_merge_two_sorted_lists.hpp"
|
||||
|
||||
ListNode* Solution::mergeTwoLists(ListNode* list1, ListNode* list2) {
|
||||
ListNode* S0021::mergeTwoLists(ListNode* list1, ListNode* list2) {
|
||||
if (list1 == nullptr) {
|
||||
return list2;
|
||||
} else if (list2 == nullptr) {
|
||||
|
@ -3,7 +3,7 @@
|
||||
// 深度优先遍历:
|
||||
// 接收参数为每个节点的状态
|
||||
// 遍历结果可以用指针放在接收参数保存,也可以通过声明一个 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 函数外完成。
|
||||
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 = {};
|
||||
dfs("", n, n, result);
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "s0023_merge_k_sorted_lists.hpp"
|
||||
|
||||
ListNode *mergeTwoLists(ListNode *a, ListNode *b) {
|
||||
ListNode *S0023::mergeTwoLists(ListNode *a, ListNode *b) {
|
||||
if (a == nullptr) {
|
||||
return b;
|
||||
} 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) {
|
||||
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));
|
||||
}
|
||||
|
||||
ListNode* Solution::mergeKLists(vector<ListNode*>& lists) {
|
||||
ListNode *S0023::mergeKLists(vector<ListNode *> &lists) {
|
||||
return merge(lists, 0, lists.size() - 1);
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "s0024_swap_nodes_in_pairs.hpp"
|
||||
|
||||
ListNode* swapPairs(ListNode* head) {
|
||||
ListNode* S0024::swapPairs(ListNode* head) {
|
||||
if (head == nullptr || head->next == nullptr) {
|
||||
return head;
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
#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* p = head;
|
||||
while (prev != tail) {
|
||||
@ -13,7 +13,7 @@ pair<ListNode*, ListNode*> myReverse(ListNode* head, ListNode* tail) {
|
||||
return {tail, head};
|
||||
}
|
||||
|
||||
ListNode* Solution::reverseKGroup(ListNode* head, int k) {
|
||||
ListNode* S0025::reverseKGroup(ListNode* head, int k) {
|
||||
ListNode* hair = new ListNode(0);
|
||||
hair->next = head;
|
||||
ListNode* pre = hair;
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "s0026_remove_duplicates_from_sorted_array.hpp"
|
||||
|
||||
int Solution::removeDuplicates(vector<int>& nums) {
|
||||
int S0026::removeDuplicates(vector<int>& nums) {
|
||||
int size = nums.size();
|
||||
if (size == 0 || size == 1) {
|
||||
return size;
|
||||
|
@ -1,6 +1,6 @@
|
||||
#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 f{0}, s{0};
|
||||
while (f < len) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
#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 needleLen = needle.length();
|
||||
for (int i{0}; i < haystackLen; ++i) {
|
||||
|
@ -4,7 +4,7 @@
|
||||
// 首先11比3大,结果至少是1,
|
||||
// 然后我让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;
|
||||
long count = 1;
|
||||
long tb = b; // 在后面的代码中不更新b
|
||||
@ -15,7 +15,7 @@ int div(long a, long 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 (divisor == 1) return dividend;
|
||||
if (divisor == -1) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
#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;
|
||||
int m = words.size(), n = words[0].size(), ls = s.size();
|
||||
for (int i = 0; i < n && i + m * n <= ls; ++i) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "s0031_next_permutation.hpp"
|
||||
|
||||
void Solution::nextPermutation(vector<int>& nums) {
|
||||
void S0031::nextPermutation(vector<int>& nums) {
|
||||
int i = nums.size() - 2;
|
||||
while (i >= 0 && nums[i] >= nums[i + 1]) {
|
||||
i--;
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "s0032_longest_valid_parentheses.hpp"
|
||||
|
||||
int Solution::longestValidParentheses(string s) {
|
||||
int S0032::longestValidParentheses(string s) {
|
||||
int maxans = 0, n = s.length();
|
||||
vector<int> dp(n, 0);
|
||||
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 left{0};
|
||||
int right = size - 1;
|
||||
|
@ -1,7 +1,7 @@
|
||||
#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 l{0};
|
||||
int r = len - 1;
|
||||
@ -34,7 +34,7 @@ vector<int> Solution1::searchRange(vector<int>& nums, int target) {
|
||||
}
|
||||
|
||||
// 开区间写法
|
||||
vector<int> Solution2::searchRange(vector<int>& nums, int target) {
|
||||
vector<int> S0034::searchRange2(vector<int>& nums, int target) {
|
||||
int len = nums.size();
|
||||
int l{0};
|
||||
int r = len;
|
||||
|
@ -1,7 +1,7 @@
|
||||
#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 l{0};
|
||||
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 l{0};
|
||||
int r = len;
|
||||
|
@ -1,6 +1,6 @@
|
||||
#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>> columns;
|
||||
vector<unordered_map<char, int>> grids;
|
||||
|
@ -1,11 +1,11 @@
|
||||
#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>> &cols,
|
||||
vector<unordered_map<char, bool>> &grids, int row,
|
||||
int col) {
|
||||
vector<unordered_map<char, bool>> &grids,
|
||||
int row, int col) {
|
||||
int size = board.size();
|
||||
// 边界校验
|
||||
if (col == size) {
|
||||
@ -49,7 +49,7 @@ bool recusiveSolveSudoku(vector<vector<char>> &board,
|
||||
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>> cols;
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "s0038_count_and_say.hpp"
|
||||
|
||||
string Solution::countAndSay(int n) {
|
||||
string S0038::countAndSay(int n) {
|
||||
if (n == 1) {
|
||||
return "1";
|
||||
} else if (n <= 0) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
#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) {
|
||||
if (idx == candidates.size()) {
|
||||
return;
|
||||
@ -19,8 +19,7 @@ void dfs(vector<int>& candidates, int target, vector<vector<int>>& ans,
|
||||
}
|
||||
}
|
||||
|
||||
vector<vector<int>> Solution::combinationSum(vector<int>& candidates,
|
||||
int target) {
|
||||
vector<vector<int>> S0039::combinationSum(vector<int>& candidates, int target) {
|
||||
vector<vector<int>> ans;
|
||||
vector<int> combine;
|
||||
dfs(candidates, target, ans, combine, 0);
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "s0076_minimum_window_substring.hpp"
|
||||
|
||||
bool Solution::check() {
|
||||
bool S0076::check() {
|
||||
for (const auto &p : ori) {
|
||||
if (cnt[p.first] < p.second) {
|
||||
return false;
|
||||
@ -11,7 +11,7 @@ bool Solution::check() {
|
||||
|
||||
// 不包含 t 中所有字符,r 右移
|
||||
// 包含 t 中所有字符,l 右移
|
||||
string Solution::minWindow(string s, string t) {
|
||||
string S0076::minWindow(string s, string t) {
|
||||
for (const auto &c : t) {
|
||||
++ori[c];
|
||||
}
|
||||
|
@ -1,6 +1,14 @@
|
||||
#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) {
|
||||
return s;
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
#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 result{INT_MAX};
|
||||
for (int begin{0}, end{0}, sum{0}; end < len; ++end) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "s0283_move_zeroes.hpp"
|
||||
|
||||
void Solution::moveZeroes(vector<int>& nums) {
|
||||
void S0283::moveZeroes(vector<int>& nums) {
|
||||
int len = nums.size();
|
||||
if (len <= 1) {
|
||||
return;
|
||||
|
@ -1,7 +1,7 @@
|
||||
#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 l{0};
|
||||
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 l{0};
|
||||
int r = len;
|
||||
|
@ -5,6 +5,6 @@
|
||||
TEST(Offer05, Case1) {
|
||||
string s{"We are happy."};
|
||||
string expected{"We%20are%20happy."};
|
||||
Solution solution;
|
||||
Offer05 solution;
|
||||
EXPECT_EQ(solution.replaceSpace(s), expected);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ TEST(Offer58, Case1) {
|
||||
string s{"abcdefg"};
|
||||
int k{2};
|
||||
string expected{"cdefgab"};
|
||||
Solution solution;
|
||||
Offer58 solution;
|
||||
EXPECT_EQ(solution.reverseLeftWords(s, k), expected);
|
||||
}
|
||||
|
||||
@ -14,6 +14,6 @@ TEST(Offer58, Case2) {
|
||||
string s{"lrloseumgh"};
|
||||
int k{6};
|
||||
string expected{"umghlrlose"};
|
||||
Solution solution;
|
||||
Offer58 solution;
|
||||
EXPECT_EQ(solution.reverseLeftWords(s, k), expected);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
TEST(Problem1, Case1) {
|
||||
std::vector<int> input1{ 2, 7, 11, 15 };
|
||||
int input2{ 9 };
|
||||
Solution solution;
|
||||
S0001 solution;
|
||||
std::vector<int> result = solution.twoSum(input1, input2);
|
||||
std::vector<int> answer = std::vector<int>{ 0, 1 };
|
||||
EXPECT_EQ(result, answer);
|
||||
@ -15,7 +15,7 @@ TEST(Problem1, Case1) {
|
||||
TEST(Problem1, Case2) {
|
||||
std::vector<int> input1{ 3, 2, 4 };
|
||||
int input2{ 6 };
|
||||
Solution solution;
|
||||
S0001 solution;
|
||||
std::vector<int> result = solution.twoSum(input1, input2);
|
||||
std::vector<int> answer = std::vector<int>{ 1, 2 };
|
||||
EXPECT_EQ(result, answer);
|
||||
@ -24,7 +24,7 @@ TEST(Problem1, Case2) {
|
||||
TEST(Problem1, Case3) {
|
||||
std::vector<int> input1{ 3, 3 };
|
||||
int input2{ 6 };
|
||||
Solution solution;
|
||||
S0001 solution;
|
||||
std::vector<int> result = solution.twoSum(input1, input2);
|
||||
std::vector<int> answer = std::vector<int>{ 0, 1 };
|
||||
EXPECT_EQ(result, answer);
|
||||
|
@ -11,7 +11,7 @@ TEST(Problem2, Case1) {
|
||||
new ListNode(5,
|
||||
new ListNode(6,
|
||||
new ListNode(4)));
|
||||
Solution solution;
|
||||
S0002 solution;
|
||||
ListNode *l = solution.addTwoNumbers(l1, l2);
|
||||
EXPECT_EQ(l->val, 7);
|
||||
EXPECT_EQ(l->next->val, 0);
|
||||
@ -21,7 +21,7 @@ TEST(Problem2, Case1) {
|
||||
TEST(Problem2, Case2) {
|
||||
ListNode *l1 = new ListNode(0);
|
||||
ListNode *l2 = new ListNode(0);
|
||||
Solution solution;
|
||||
S0002 solution;
|
||||
ListNode *l = solution.addTwoNumbers(l1, l2);
|
||||
EXPECT_EQ(l->val, 0);
|
||||
}
|
||||
@ -40,7 +40,7 @@ TEST(Problem2, Case3) {
|
||||
new ListNode(9,
|
||||
new ListNode(9,
|
||||
new ListNode(9))));
|
||||
Solution solution;
|
||||
S0002 solution;
|
||||
ListNode *l = solution.addTwoNumbers(l1, l2);
|
||||
EXPECT_EQ(l->val, 8);
|
||||
EXPECT_EQ(l->next->val, 9);
|
||||
|
@ -4,18 +4,18 @@
|
||||
|
||||
TEST(Problem3, Case1) {
|
||||
std::string s{std::string("abcabcbb")};
|
||||
Solution solution;
|
||||
S0003 solution;
|
||||
EXPECT_EQ(solution.lengthOfLongestSubstring(s), 3);
|
||||
}
|
||||
|
||||
TEST(Problem3, Case2) {
|
||||
std::string s{std::string("bbbbb")};
|
||||
Solution solution;
|
||||
S0003 solution;
|
||||
EXPECT_EQ(solution.lengthOfLongestSubstring(s), 1);
|
||||
}
|
||||
|
||||
TEST(Problem3, Case3) {
|
||||
std::string s{std::string("pwwkew")};
|
||||
Solution solution;
|
||||
S0003 solution;
|
||||
EXPECT_EQ(solution.lengthOfLongestSubstring(s), 3);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ TEST(Problem4, Case1) {
|
||||
std::vector<int> nums1{1, 3};
|
||||
std::vector<int> nums2{2};
|
||||
double answer{2};
|
||||
Solution solution;
|
||||
S0004 solution;
|
||||
EXPECT_EQ(solution.findMedianSortedArrays(nums1, nums2), answer);
|
||||
}
|
||||
|
||||
@ -14,6 +14,6 @@ TEST(Problem4, Case2) {
|
||||
std::vector<int> nums1{1, 2};
|
||||
std::vector<int> nums2{3, 4};
|
||||
double answer{2.5};
|
||||
Solution solution;
|
||||
S0004 solution;
|
||||
EXPECT_EQ(solution.findMedianSortedArrays(nums1, nums2), answer);
|
||||
}
|
||||
|
@ -6,17 +6,15 @@ TEST(Problem5, Case1) {
|
||||
std::string s("babad");
|
||||
std::string r1("bab");
|
||||
std::string r2("aba");
|
||||
Solution1 solution1;
|
||||
Solution2 solution2;
|
||||
EXPECT_TRUE(solution1.longestPalindrome(s) == r1 || solution1.longestPalindrome(s) == r2);
|
||||
EXPECT_TRUE(solution2.longestPalindrome(s) == r1 || solution2.longestPalindrome(s) == r2);
|
||||
S0005 solution;
|
||||
EXPECT_TRUE(solution.longestPalindrome1(s) == r1 || solution.longestPalindrome1(s) == r2);
|
||||
EXPECT_TRUE(solution.longestPalindrome2(s) == r1 || solution.longestPalindrome2(s) == r2);
|
||||
}
|
||||
|
||||
TEST(Problem5, Case2) {
|
||||
std::string s("cbbd");
|
||||
std::string r("bb");
|
||||
Solution1 solution1;
|
||||
Solution2 solution2;
|
||||
EXPECT_EQ(r.compare(solution1.longestPalindrome(s)), 0);
|
||||
EXPECT_EQ(r.compare(solution2.longestPalindrome(s)), 0);
|
||||
S0005 solution;
|
||||
EXPECT_EQ(r.compare(solution.longestPalindrome1(s)), 0);
|
||||
EXPECT_EQ(r.compare(solution.longestPalindrome2(s)), 0);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ TEST(Problem6, Case1) {
|
||||
string s("PAYPALISHIRING");
|
||||
int rows = 3;
|
||||
string r("PAHNAPLSIIGYIR");
|
||||
Solution solution;
|
||||
S0006 solution;
|
||||
EXPECT_TRUE(solution.convert(s, rows) == r);
|
||||
}
|
||||
|
||||
@ -14,7 +14,7 @@ TEST(Problem6, Case2) {
|
||||
string s("PAYPALISHIRING");
|
||||
int rows = 4;
|
||||
string r("PINALSIGYAHRPI");
|
||||
Solution solution;
|
||||
S0006 solution;
|
||||
EXPECT_TRUE(solution.convert(s, rows) == r);
|
||||
}
|
||||
|
||||
@ -22,6 +22,6 @@ TEST(Problem6, Case3) {
|
||||
string s("AB");
|
||||
int rows = 4;
|
||||
string r("AB");
|
||||
Solution solution;
|
||||
S0006 solution;
|
||||
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