diff --git a/include/offer_05.hpp b/include/offer_05.hpp index 3b4976c..3c7abe2 100644 --- a/include/offer_05.hpp +++ b/include/offer_05.hpp @@ -5,7 +5,7 @@ using namespace std; -class Solution { +class Offer05 { public: string replaceSpace(string s); }; diff --git a/include/offer_58.hpp b/include/offer_58.hpp index 5817f4b..f49f324 100644 --- a/include/offer_58.hpp +++ b/include/offer_58.hpp @@ -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); diff --git a/include/s0001_two_sum.hpp b/include/s0001_two_sum.hpp index 9b5cc9a..175ee7d 100644 --- a/include/s0001_two_sum.hpp +++ b/include/s0001_two_sum.hpp @@ -5,7 +5,9 @@ #include #include -class Solution { +using namespace std; + +class S0001 { public: /** * @brief Two Sum diff --git a/include/s0002_add_two_numbers.hpp b/include/s0002_add_two_numbers.hpp index a7f4594..c6d0899 100644 --- a/include/s0002_add_two_numbers.hpp +++ b/include/s0002_add_two_numbers.hpp @@ -9,7 +9,7 @@ struct ListNode { ListNode(int x, ListNode* next) : val(x), next(next) {} }; -class Solution { +class S0002 { public: /** * @brief Add Two Numbers diff --git a/include/s0003_longest_substring_without_repeating_characters.hpp b/include/s0003_longest_substring_without_repeating_characters.hpp index e1f6f9b..07f2e88 100644 --- a/include/s0003_longest_substring_without_repeating_characters.hpp +++ b/include/s0003_longest_substring_without_repeating_characters.hpp @@ -4,7 +4,9 @@ #include #include -class Solution { +using namespace std; + +class S0003 { public: /** * @brief Longest Substring Without Repeating Characters diff --git a/include/s0004_median_of_two_sorted_arrays.hpp b/include/s0004_median_of_two_sorted_arrays.hpp index edf4260..e99c333 100644 --- a/include/s0004_median_of_two_sorted_arrays.hpp +++ b/include/s0004_median_of_two_sorted_arrays.hpp @@ -3,7 +3,9 @@ #include -class Solution { +using namespace std; + +class S0004 { public: /** * @brief Median of Two Sorted Arrays diff --git a/include/s0005_longest_palindromic_substring.hpp b/include/s0005_longest_palindromic_substring.hpp index 5b57ff1..064f245 100644 --- a/include/s0005_longest_palindromic_substring.hpp +++ b/include/s0005_longest_palindromic_substring.hpp @@ -1,26 +1,20 @@ #ifndef S0005_LONGEST_PALINDROMIC_SUBSTRING_HPP #define S0005_LONGEST_PALINDROMIC_SUBSTRING_HPP -#include #include +#include #include 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 diff --git a/include/s0006_zigzag_conversion.hpp b/include/s0006_zigzag_conversion.hpp index 7348532..7932ba6 100644 --- a/include/s0006_zigzag_conversion.hpp +++ b/include/s0006_zigzag_conversion.hpp @@ -5,7 +5,7 @@ using namespace std; -class Solution { +class S0006 { public: /** * @brief Zigzag Conversion diff --git a/include/s0007_reverse_integer.hpp b/include/s0007_reverse_integer.hpp index 19130d0..378c0dd 100644 --- a/include/s0007_reverse_integer.hpp +++ b/include/s0007_reverse_integer.hpp @@ -5,7 +5,9 @@ #include #include -class Solution { +using namespace std; + +class S0007 { public: int reverse(int x); }; diff --git a/include/s0008_string_to_integer.hpp b/include/s0008_string_to_integer.hpp index b36906a..95d8541 100644 --- a/include/s0008_string_to_integer.hpp +++ b/include/s0008_string_to_integer.hpp @@ -8,7 +8,7 @@ using namespace std; -class Solution { +class S0008 { public: int myAtoi(string s); }; diff --git a/include/s0009_palindrome_number.hpp b/include/s0009_palindrome_number.hpp index fe6a430..d30eeb1 100644 --- a/include/s0009_palindrome_number.hpp +++ b/include/s0009_palindrome_number.hpp @@ -6,7 +6,7 @@ using namespace std; -class Solution { +class S0009 { public: bool isPalindrome(int x); }; diff --git a/include/s0010_regular_expression_matching.hpp b/include/s0010_regular_expression_matching.hpp index 1908a47..af1ca90 100644 --- a/include/s0010_regular_expression_matching.hpp +++ b/include/s0010_regular_expression_matching.hpp @@ -6,7 +6,7 @@ using namespace std; -class Solution { +class S0010 { public: bool isMatch(string s, string p); }; diff --git a/include/s0011_container_with_most_water.hpp b/include/s0011_container_with_most_water.hpp index 6ac9311..e38a51c 100644 --- a/include/s0011_container_with_most_water.hpp +++ b/include/s0011_container_with_most_water.hpp @@ -5,7 +5,7 @@ using namespace std; -class Solution { +class S0011 { public: int maxArea(vector& height); }; diff --git a/include/s0012_integer_to_roman.hpp b/include/s0012_integer_to_roman.hpp index 7a6f2eb..513b28e 100644 --- a/include/s0012_integer_to_roman.hpp +++ b/include/s0012_integer_to_roman.hpp @@ -5,7 +5,7 @@ using namespace std; -class Solution { +class S0012 { public: string intToRoman(int num); }; diff --git a/include/s0013_roman_to_integer.hpp b/include/s0013_roman_to_integer.hpp index 1df6629..ef18cbe 100644 --- a/include/s0013_roman_to_integer.hpp +++ b/include/s0013_roman_to_integer.hpp @@ -6,7 +6,7 @@ using namespace std; -class Solution { +class S0013 { public: int romanToInt(string s); }; diff --git a/include/s0014_longest_common_prefix.hpp b/include/s0014_longest_common_prefix.hpp index 5180e5f..b19c333 100644 --- a/include/s0014_longest_common_prefix.hpp +++ b/include/s0014_longest_common_prefix.hpp @@ -6,7 +6,7 @@ using namespace std; -class Solution { +class S0014 { public: string longestCommonPrefix(vector& strs); }; diff --git a/include/s0015_3sum.hpp b/include/s0015_3sum.hpp index 54636c6..2283063 100644 --- a/include/s0015_3sum.hpp +++ b/include/s0015_3sum.hpp @@ -7,14 +7,10 @@ using namespace std; -class Solution1 { +class S0015 { public: - vector> threeSum(vector& nums); -}; - -class Solution2 { - public: - vector> threeSum(vector& nums); + vector> threeSum1(vector& nums); + vector> threeSum2(vector& nums); }; #endif diff --git a/include/s0016_3sum_closest.hpp b/include/s0016_3sum_closest.hpp index 68d16e4..eac9614 100644 --- a/include/s0016_3sum_closest.hpp +++ b/include/s0016_3sum_closest.hpp @@ -6,7 +6,7 @@ using namespace std; -class Solution { +class S0016 { public: int threeSumClosest(vector& nums, int target); }; diff --git a/include/s0017_letter_combinations_of_a_phone_number.hpp b/include/s0017_letter_combinations_of_a_phone_number.hpp index e877c65..8cd96f5 100644 --- a/include/s0017_letter_combinations_of_a_phone_number.hpp +++ b/include/s0017_letter_combinations_of_a_phone_number.hpp @@ -7,7 +7,7 @@ using namespace std; -class Solution { +class S0017 { public: vector letterCombinations(string digits); }; diff --git a/include/s0018_4sum.hpp b/include/s0018_4sum.hpp index bfa0570..bc7d048 100644 --- a/include/s0018_4sum.hpp +++ b/include/s0018_4sum.hpp @@ -6,7 +6,7 @@ using namespace std; -class Solution { +class S0018 { public: vector> fourSum(vector& nums, int target); }; diff --git a/include/s0019_remove_nth_node_from_end_of_list.hpp b/include/s0019_remove_nth_node_from_end_of_list.hpp index 17121be..204e2a2 100644 --- a/include/s0019_remove_nth_node_from_end_of_list.hpp +++ b/include/s0019_remove_nth_node_from_end_of_list.hpp @@ -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); }; diff --git a/include/s0020_valid_parentheses.hpp b/include/s0020_valid_parentheses.hpp index d370b18..f2568f3 100644 --- a/include/s0020_valid_parentheses.hpp +++ b/include/s0020_valid_parentheses.hpp @@ -6,9 +6,10 @@ using namespace std; -class Solution { +class S0020 { public: bool isValid(string s); + bool match(string s1, string s2); }; #endif diff --git a/include/s0021_merge_two_sorted_lists.hpp b/include/s0021_merge_two_sorted_lists.hpp index b0a388b..809025a 100644 --- a/include/s0021_merge_two_sorted_lists.hpp +++ b/include/s0021_merge_two_sorted_lists.hpp @@ -1,6 +1,10 @@ #ifndef S0021_MERGE_TWO_SORTED_LISTS_HPP #define S0021_MERGE_TWO_SORTED_LISTS_HPP +#include + +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 - -class Solution { +class S0021 { public: ListNode* mergeTwoLists(ListNode* list1, ListNode* list2); }; diff --git a/include/s0022_generate_parentheses.hpp b/include/s0022_generate_parentheses.hpp index 7a8a9b5..75fb1e8 100644 --- a/include/s0022_generate_parentheses.hpp +++ b/include/s0022_generate_parentheses.hpp @@ -7,9 +7,10 @@ using namespace std; -class Solution { +class S0022 { public: vector generateParenthesis(int n); + void dfs(string current, int left, int right, vector &result); }; #endif diff --git a/include/s0023_merge_k_sorted_lists.hpp b/include/s0023_merge_k_sorted_lists.hpp index aaa2f21..8687c49 100644 --- a/include/s0023_merge_k_sorted_lists.hpp +++ b/include/s0023_merge_k_sorted_lists.hpp @@ -7,15 +7,17 @@ using namespace std; struct ListNode { int val; - ListNode* next; + ListNode *next; ListNode() : val(0), 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: - ListNode* mergeKLists(vector& lists); + ListNode *mergeKLists(vector &lists); + ListNode *mergeTwoLists(ListNode *a, ListNode *b); + ListNode *merge(vector &lists, int l, int r); }; #endif diff --git a/include/s0024_swap_nodes_in_pairs.hpp b/include/s0024_swap_nodes_in_pairs.hpp index 38040df..eeba727 100644 --- a/include/s0024_swap_nodes_in_pairs.hpp +++ b/include/s0024_swap_nodes_in_pairs.hpp @@ -9,7 +9,7 @@ struct ListNode { ListNode(int x, ListNode* next) : val(x), next(next) {} }; -class Solution { +class S0024 { public: ListNode* swapPairs(ListNode* head); }; diff --git a/include/s0025_reverse_nodes_in_k-group.hpp b/include/s0025_reverse_nodes_in_k-group.hpp index 5d1b558..da55d74 100644 --- a/include/s0025_reverse_nodes_in_k-group.hpp +++ b/include/s0025_reverse_nodes_in_k-group.hpp @@ -1,8 +1,8 @@ #ifndef S0025_REVERSE_NODES_IN_K_GROUP_HPP #define S0025_REVERSE_NODES_IN_K_GROUP_HPP -#include #include +#include 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 myReverse(ListNode* head, ListNode* tail); }; #endif diff --git a/include/s0026_remove_duplicates_from_sorted_array.hpp b/include/s0026_remove_duplicates_from_sorted_array.hpp index bc7e688..575ea7e 100644 --- a/include/s0026_remove_duplicates_from_sorted_array.hpp +++ b/include/s0026_remove_duplicates_from_sorted_array.hpp @@ -5,7 +5,7 @@ using namespace std; -class Solution { +class S0026 { public: int removeDuplicates(vector& nums); }; diff --git a/include/s0027_remove_element.hpp b/include/s0027_remove_element.hpp index b5dd814..4bb2bbe 100644 --- a/include/s0027_remove_element.hpp +++ b/include/s0027_remove_element.hpp @@ -5,7 +5,7 @@ using namespace std; -class Solution { +class S0027 { public: int removeElement(vector& nums, int val); }; diff --git a/include/s0028_find_the_index_of_the_first_occurrence_in_a_string.hpp b/include/s0028_find_the_index_of_the_first_occurrence_in_a_string.hpp index 219e0f1..7f9d5b7 100644 --- a/include/s0028_find_the_index_of_the_first_occurrence_in_a_string.hpp +++ b/include/s0028_find_the_index_of_the_first_occurrence_in_a_string.hpp @@ -5,7 +5,7 @@ using namespace std; -class Solution { +class S0028 { public: int strStr(string haystack, string needle); }; diff --git a/include/s0029_divide_two_integers.hpp b/include/s0029_divide_two_integers.hpp index 4b9457c..06f817c 100644 --- a/include/s0029_divide_two_integers.hpp +++ b/include/s0029_divide_two_integers.hpp @@ -3,9 +3,12 @@ #include -class Solution { +using namespace std; + +class S0029 { public: int divide(int dividend, int divisor); + int div(long a, long b); }; #endif diff --git a/include/s0030_substring_with_concatenation_of_all_words.hpp b/include/s0030_substring_with_concatenation_of_all_words.hpp index 49797bf..2cc659c 100644 --- a/include/s0030_substring_with_concatenation_of_all_words.hpp +++ b/include/s0030_substring_with_concatenation_of_all_words.hpp @@ -7,7 +7,7 @@ using namespace std; -class Solution { +class S0030 { public: vector findSubstring(string s, vector& words); }; diff --git a/include/s0031_next_permutation.hpp b/include/s0031_next_permutation.hpp index 32f1fa5..495662e 100644 --- a/include/s0031_next_permutation.hpp +++ b/include/s0031_next_permutation.hpp @@ -6,7 +6,7 @@ using namespace std; -class Solution { +class S0031 { public: void nextPermutation(vector& nums); }; diff --git a/include/s0032_longest_valid_parentheses.hpp b/include/s0032_longest_valid_parentheses.hpp index 94eb9c3..c5aabcc 100644 --- a/include/s0032_longest_valid_parentheses.hpp +++ b/include/s0032_longest_valid_parentheses.hpp @@ -7,7 +7,7 @@ using namespace std; -class Solution { +class S0032 { public: int longestValidParentheses(string s); }; diff --git a/include/s0033_search_in_rotated_sorted_array.hpp b/include/s0033_search_in_rotated_sorted_array.hpp index 5a728c3..bd6a0c2 100644 --- a/include/s0033_search_in_rotated_sorted_array.hpp +++ b/include/s0033_search_in_rotated_sorted_array.hpp @@ -6,7 +6,7 @@ using namespace std; -class Solution { +class S0033 { public: int search(vector& nums, int target); }; diff --git a/include/s0034_find_first_and_last_position_of_element_in_sorted_array.hpp b/include/s0034_find_first_and_last_position_of_element_in_sorted_array.hpp index 1527800..d0bc0b7 100644 --- a/include/s0034_find_first_and_last_position_of_element_in_sorted_array.hpp +++ b/include/s0034_find_first_and_last_position_of_element_in_sorted_array.hpp @@ -5,13 +5,10 @@ using namespace std; -class Solution1 { +class S0034 { public: - vector searchRange(vector& nums, int target); + vector searchRange1(vector& nums, int target); + vector searchRange2(vector& nums, int target); }; -class Solution2 { - public: - vector searchRange(vector& nums, int target); -}; #endif diff --git a/include/s0035_search_insert_position.hpp b/include/s0035_search_insert_position.hpp index b55edad..e784dce 100644 --- a/include/s0035_search_insert_position.hpp +++ b/include/s0035_search_insert_position.hpp @@ -5,14 +5,10 @@ using namespace std; -class Solution1 { +class S0035 { public: - int searchInsert(vector& nums, int target); -}; - -class Solution2 { - public: - int searchInsert(vector& nums, int target); + int searchInsert1(vector& nums, int target); + int searchInsert2(vector& nums, int target); }; #endif diff --git a/include/s0036_valid_sudoku.hpp b/include/s0036_valid_sudoku.hpp index 73df99d..944af8f 100644 --- a/include/s0036_valid_sudoku.hpp +++ b/include/s0036_valid_sudoku.hpp @@ -7,7 +7,7 @@ using namespace std; -class Solution { +class S0036 { public: bool isValidSudoku(vector>& board); }; diff --git a/include/s0037_sudoku_solver.hpp b/include/s0037_sudoku_solver.hpp index 4cc4dac..c341545 100644 --- a/include/s0037_sudoku_solver.hpp +++ b/include/s0037_sudoku_solver.hpp @@ -1,16 +1,21 @@ #ifndef S0037_SUDOKU_SOLVER_HPP #define S0037_SUDOKU_SOLVER_HPP -#include -#include -#include #include +#include +#include +#include using namespace std; -class Solution { +class S0037 { public: - void solveSudoku(vector>& board); + void solveSudoku(vector> &board); + bool recusiveSolveSudoku(vector> &board, + vector> &rows, + vector> &cols, + vector> &grids, int row, + int col); }; #endif diff --git a/include/s0038_count_and_say.hpp b/include/s0038_count_and_say.hpp index d9a040f..d5bc4f8 100644 --- a/include/s0038_count_and_say.hpp +++ b/include/s0038_count_and_say.hpp @@ -6,7 +6,7 @@ using namespace std; -class Solution { +class S0038 { public: string countAndSay(int n); }; diff --git a/include/s0039_combination_sum.hpp b/include/s0039_combination_sum.hpp index 66dc6e2..45b83f4 100644 --- a/include/s0039_combination_sum.hpp +++ b/include/s0039_combination_sum.hpp @@ -1,15 +1,17 @@ #ifndef S0039_COMBINATION_SUM_HPP #define S0039_COMBINATION_SUM_HPP -#include #include #include +#include using namespace std; -class Solution { +class S0039 { public: vector> combinationSum(vector& candidates, int target); + void dfs(vector& candidates, int target, vector>& ans, + vector& combine, int idx); }; #endif diff --git a/include/s0076_minimum_window_substring.hpp b/include/s0076_minimum_window_substring.hpp index 2d73bff..970f276 100644 --- a/include/s0076_minimum_window_substring.hpp +++ b/include/s0076_minimum_window_substring.hpp @@ -7,7 +7,7 @@ using namespace std; -class Solution { +class S0076 { public: string minWindow(string s, string t); unordered_map ori, cnt; diff --git a/include/s0151_reverse_words_in_a_string.hpp b/include/s0151_reverse_words_in_a_string.hpp index e0dd25d..d9115b4 100644 --- a/include/s0151_reverse_words_in_a_string.hpp +++ b/include/s0151_reverse_words_in_a_string.hpp @@ -5,7 +5,7 @@ using namespace std; -class Solution { +class S0151 { public: void reverseSubStr(string &s, int begin, int end); string reverseWords(string s); diff --git a/include/s0209_minimum_size_subarray_sum.hpp b/include/s0209_minimum_size_subarray_sum.hpp index 9477e10..b5025e6 100644 --- a/include/s0209_minimum_size_subarray_sum.hpp +++ b/include/s0209_minimum_size_subarray_sum.hpp @@ -6,7 +6,7 @@ using namespace std; -class Solution { +class S0209 { public: int minSubArrayLen(int target, vector& nums); }; diff --git a/include/s0283_move_zeroes.hpp b/include/s0283_move_zeroes.hpp index ce0a36d..605668d 100644 --- a/include/s0283_move_zeroes.hpp +++ b/include/s0283_move_zeroes.hpp @@ -5,7 +5,7 @@ using namespace std; -class Solution { +class S0283 { public: void moveZeroes(vector& nums); }; diff --git a/include/s0704_binary_search.hpp b/include/s0704_binary_search.hpp index 805e66a..24dcab8 100644 --- a/include/s0704_binary_search.hpp +++ b/include/s0704_binary_search.hpp @@ -5,14 +5,10 @@ using namespace std; -class Solution1 { +class S0704 { public: - int binSearch(vector& nums, int target); -}; - -class Solution2 { - public: - int binSearch(vector& nums, int target); + int binSearch1(vector& nums, int target); + int binSearch2(vector& nums, int target); }; #endif diff --git a/src/offer_05.cpp b/src/offer_05.cpp index 86d9b1a..508652d 100644 --- a/src/offer_05.cpp +++ b/src/offer_05.cpp @@ -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) { diff --git a/src/offer_58.cpp b/src/offer_58.cpp index 4a79a4b..7bbf0eb 100644 --- a/src/offer_58.cpp +++ b/src/offer_58.cpp @@ -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); diff --git a/src/s0001_two_sum.cpp b/src/s0001_two_sum.cpp index c778865..ca09f19 100644 --- a/src/s0001_two_sum.cpp +++ b/src/s0001_two_sum.cpp @@ -1,7 +1,6 @@ #include "s0001_two_sum.hpp" -using namespace std; -vector Solution::twoSum(vector& nums, int target) { +vector S0001::twoSum(vector& nums, int target) { unordered_map hashtable; for (int i = 0; i < nums.size(); ++i) { auto it = hashtable.find(target - nums[i]); diff --git a/src/s0002_add_two_numbers.cpp b/src/s0002_add_two_numbers.cpp index ca024fd..f584aae 100644 --- a/src/s0002_add_two_numbers.cpp +++ b/src/s0002_add_two_numbers.cpp @@ -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 }; diff --git a/src/s0003_longest_substring_without_repeating_characters.cpp b/src/s0003_longest_substring_without_repeating_characters.cpp index b8e8a9b..aafaeba 100644 --- a/src/s0003_longest_substring_without_repeating_characters.cpp +++ b/src/s0003_longest_substring_without_repeating_characters.cpp @@ -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 // diff --git a/src/s0004_median_of_two_sorted_arrays.cpp b/src/s0004_median_of_two_sorted_arrays.cpp index 06fc867..c7316b7 100644 --- a/src/s0004_median_of_two_sorted_arrays.cpp +++ b/src/s0004_median_of_two_sorted_arrays.cpp @@ -1,6 +1,6 @@ #include "s0004_median_of_two_sorted_arrays.hpp" -int Solution::getKthElement(const std::vector& nums1, +int S0004::getKthElement(const std::vector& nums1, const std::vector& nums2, int k) { int m = nums1.size(); int n = nums2.size(); @@ -33,7 +33,7 @@ int Solution::getKthElement(const std::vector& nums1, } } -double Solution::findMedianSortedArrays(std::vector& nums1, +double S0004::findMedianSortedArrays(std::vector& nums1, std::vector& nums2) { int totalLength = nums1.size() + nums2.size(); if (totalLength % 2 == 1) { diff --git a/src/s0005_longest_palindromic_substring.cpp b/src/s0005_longest_palindromic_substring.cpp index 4a75938..d207dc4 100644 --- a/src/s0005_longest_palindromic_substring.cpp +++ b/src/s0005_longest_palindromic_substring.cpp @@ -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> dp(len, vector(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) { diff --git a/src/s0006_zigzag_conversion.cpp b/src/s0006_zigzag_conversion.cpp index 61699d3..82ba92d 100644 --- a/src/s0006_zigzag_conversion.cpp +++ b/src/s0006_zigzag_conversion.cpp @@ -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) { diff --git a/src/s0007_reverse_integer.cpp b/src/s0007_reverse_integer.cpp index e830cd5..b2a5ff4 100644 --- a/src/s0007_reverse_integer.cpp +++ b/src/s0007_reverse_integer.cpp @@ -1,6 +1,6 @@ #include "s0007_reverse_integer.hpp" -int Solution::reverse(int x) { +int S0007::reverse(int x) { int r{0}; std::queue queue; diff --git a/src/s0008_string_to_integer.cpp b/src/s0008_string_to_integer.cpp index ef3674f..5b81a11 100644 --- a/src/s0008_string_to_integer.cpp +++ b/src/s0008_string_to_integer.cpp @@ -2,7 +2,7 @@ // 当流程很复杂的时候,画流程图: https://paste.sainnhe.dev/NQxx.png -class Automaton { +class S0008Automaton { string state = "start"; unordered_map> 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; } diff --git a/src/s0009_palindrome_number.cpp b/src/s0009_palindrome_number.cpp index 63a3981..ad78820 100644 --- a/src/s0009_palindrome_number.cpp +++ b/src/s0009_palindrome_number.cpp @@ -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()); diff --git a/src/s0010_regular_expression_matching.cpp b/src/s0010_regular_expression_matching.cpp index 27e0820..e11063e 100644 --- a/src/s0010_regular_expression_matching.cpp +++ b/src/s0010_regular_expression_matching.cpp @@ -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(); diff --git a/src/s0011_container_with_most_water.cpp b/src/s0011_container_with_most_water.cpp index 1b0a353..d7339be 100644 --- a/src/s0011_container_with_most_water.cpp +++ b/src/s0011_container_with_most_water.cpp @@ -9,7 +9,7 @@ // 也就是说只有当移动小的指针时才有可能让储存的水量超过之前的 // 因此每次迭代我们移动较小的指针,然后统计出最大的储存水量 -int Solution::maxArea(vector& height) { +int S0011::maxArea(vector& height) { int max{0}; for (int l{0}, r{static_cast(height.size() - 1)}; r != l;) { if (height.at(l) > height.at(r)) { diff --git a/src/s0012_integer_to_roman.cpp b/src/s0012_integer_to_roman.cpp index bcb0ed5..e19126b 100644 --- a/src/s0012_integer_to_roman.cpp +++ b/src/s0012_integer_to_roman.cpp @@ -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"}; diff --git a/src/s0013_roman_to_integer.cpp b/src/s0013_roman_to_integer.cpp index 32a80bb..7b19aa1 100644 --- a/src/s0013_roman_to_integer.cpp +++ b/src/s0013_roman_to_integer.cpp @@ -1,6 +1,6 @@ #include "s0013_roman_to_integer.hpp" -int Solution::romanToInt(string s) { +int S0013::romanToInt(string s) { unordered_map map; map["I"] = 1; map["V"] = 5; diff --git a/src/s0014_longest_common_prefix.cpp b/src/s0014_longest_common_prefix.cpp index a8e0cca..c5b78ee 100644 --- a/src/s0014_longest_common_prefix.cpp +++ b/src/s0014_longest_common_prefix.cpp @@ -1,6 +1,6 @@ #include "s0014_longest_common_prefix.hpp" -string Solution::longestCommonPrefix(vector& strs) { +string S0014::longestCommonPrefix(vector& strs) { string o{""}; int size = strs.size(); int minLen = strs.at(0).length(); diff --git a/src/s0015_3sum.cpp b/src/s0015_3sum.cpp index c7f31a8..f08d030 100644 --- a/src/s0015_3sum.cpp +++ b/src/s0015_3sum.cpp @@ -6,7 +6,7 @@ // 去重的方式: // 对于每个找到的序列,先排序,如果不存在则添加到结果中。 -vector> Solution1::threeSum(vector& nums) { +vector> S0015::threeSum1(vector& nums) { unordered_map map; vector> result; int len = nums.size(); @@ -42,7 +42,7 @@ vector> Solution1::threeSum(vector& nums) { // 思路二:双指针 // 先对整个数组排序,然后固定一个数 a ,然后 b, c 从两边往中间靠。 -vector> Solution2::threeSum(vector& nums) { +vector> S0015::threeSum2(vector& nums) { int n = nums.size(); sort(nums.begin(), nums.end()); vector> ans; diff --git a/src/s0016_3sum_closest.cpp b/src/s0016_3sum_closest.cpp index eae47d2..59503f2 100644 --- a/src/s0016_3sum_closest.cpp +++ b/src/s0016_3sum_closest.cpp @@ -2,7 +2,7 @@ // 和上一道一样,排序 + 双指针 -int Solution::threeSumClosest(vector& nums, int target) { +int S0016::threeSumClosest(vector& nums, int target) { int sum = nums[0] + nums[1] + nums[2]; int len = nums.size(); sort(nums.begin(), nums.end()); diff --git a/src/s0017_letter_combinations_of_a_phone_number.cpp b/src/s0017_letter_combinations_of_a_phone_number.cpp index 4e6be4f..ad52f32 100644 --- a/src/s0017_letter_combinations_of_a_phone_number.cpp +++ b/src/s0017_letter_combinations_of_a_phone_number.cpp @@ -1,6 +1,6 @@ #include "s0017_letter_combinations_of_a_phone_number.hpp" -vector Solution::letterCombinations(string digits) { +vector S0017::letterCombinations(string digits) { unordered_map> map; map["2"] = vector{"a", "b", "c"}; map["3"] = vector{"d", "e", "f"}; diff --git a/src/s0018_4sum.cpp b/src/s0018_4sum.cpp index f203504..3182983 100644 --- a/src/s0018_4sum.cpp +++ b/src/s0018_4sum.cpp @@ -1,6 +1,6 @@ #include "s0018_4sum.hpp" -vector> Solution::fourSum(vector& nums, int target) { +vector> S0018::fourSum(vector& nums, int target) { int len = nums.size(); if (len < 4) { return {}; diff --git a/src/s0019_remove_nth_node_from_end_of_list.cpp b/src/s0019_remove_nth_node_from_end_of_list.cpp index f462fcd..39a32bd 100644 --- a/src/s0019_remove_nth_node_from_end_of_list.cpp +++ b/src/s0019_remove_nth_node_from_end_of_list.cpp @@ -6,7 +6,7 @@ // 步,等快指针走到头时,慢指针指向的的就是倒数第 n 个 // 双指针实现 -ListNode* Solution::removeNthFromEnd(ListNode* head, int n) { +ListNode* S0019::removeNthFromEnd(ListNode* head, int n) { // 一种常见的处理头指针的方法是添加一个哑指针,这个哑指针指向头指针 // 这样我们就不需要单独讨论头指针了。 ListNode dummy(0, head); diff --git a/src/s0020_valid_parentheses.cpp b/src/s0020_valid_parentheses.cpp index 9060bb7..e73a8dc 100644 --- a/src/s0020_valid_parentheses.cpp +++ b/src/s0020_valid_parentheses.cpp @@ -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 stack; int len = s.length(); for (int i{0}; i < len; i++) { diff --git a/src/s0021_merge_two_sorted_lists.cpp b/src/s0021_merge_two_sorted_lists.cpp index 73b83bd..12319fb 100644 --- a/src/s0021_merge_two_sorted_lists.cpp +++ b/src/s0021_merge_two_sorted_lists.cpp @@ -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) { diff --git a/src/s0022_generate_parentheses.cpp b/src/s0022_generate_parentheses.cpp index 54459f8..23dbf6e 100644 --- a/src/s0022_generate_parentheses.cpp +++ b/src/s0022_generate_parentheses.cpp @@ -3,7 +3,7 @@ // 深度优先遍历: // 接收参数为每个节点的状态 // 遍历结果可以用指针放在接收参数保存,也可以通过声明一个 class 的成员来保存 -void dfs(string current, int left, int right, vector &result) { +void S0022::dfs(string current, int left, int right, vector &result) { // 讨论边界条件(结束条件) // 不必讨论起始条件,因为初始化的工作会在 dfs 函数外完成。 if (left == 0 && right == 0) { @@ -22,7 +22,7 @@ void dfs(string current, int left, int right, vector &result) { } } -vector Solution::generateParenthesis(int n) { +vector S0022::generateParenthesis(int n) { // 初始化 vector result = {}; dfs("", n, n, result); diff --git a/src/s0023_merge_k_sorted_lists.cpp b/src/s0023_merge_k_sorted_lists.cpp index 6fa1f4b..06443e9 100644 --- a/src/s0023_merge_k_sorted_lists.cpp +++ b/src/s0023_merge_k_sorted_lists.cpp @@ -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 &lists, int l, int r) { +ListNode *S0023::merge(vector &lists, int l, int r) { if (l == r) { return lists[l]; } @@ -38,6 +38,6 @@ ListNode *merge(vector &lists, int l, int r) { return mergeTwoLists(merge(lists, 1, mid), merge(lists, mid + 1, r)); } -ListNode* Solution::mergeKLists(vector& lists) { +ListNode *S0023::mergeKLists(vector &lists) { return merge(lists, 0, lists.size() - 1); } diff --git a/src/s0024_swap_nodes_in_pairs.cpp b/src/s0024_swap_nodes_in_pairs.cpp index 3a59528..e252ca8 100644 --- a/src/s0024_swap_nodes_in_pairs.cpp +++ b/src/s0024_swap_nodes_in_pairs.cpp @@ -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; } diff --git a/src/s0025_reverse_nodes_in_k-group.cpp b/src/s0025_reverse_nodes_in_k-group.cpp index f383fce..daec0c0 100644 --- a/src/s0025_reverse_nodes_in_k-group.cpp +++ b/src/s0025_reverse_nodes_in_k-group.cpp @@ -1,7 +1,7 @@ #include "s0025_reverse_nodes_in_k-group.hpp" // 翻转一个子链表,并且返回新的头与尾 -pair myReverse(ListNode* head, ListNode* tail) { +pair S0025::myReverse(ListNode* head, ListNode* tail) { ListNode* prev = tail->next; ListNode* p = head; while (prev != tail) { @@ -13,7 +13,7 @@ pair 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; diff --git a/src/s0026_remove_duplicates_from_sorted_array.cpp b/src/s0026_remove_duplicates_from_sorted_array.cpp index 6e1e5dd..75a21e8 100644 --- a/src/s0026_remove_duplicates_from_sorted_array.cpp +++ b/src/s0026_remove_duplicates_from_sorted_array.cpp @@ -1,6 +1,6 @@ #include "s0026_remove_duplicates_from_sorted_array.hpp" -int Solution::removeDuplicates(vector& nums) { +int S0026::removeDuplicates(vector& nums) { int size = nums.size(); if (size == 0 || size == 1) { return size; diff --git a/src/s0027_remove_element.cpp b/src/s0027_remove_element.cpp index 71d475a..275e6a0 100644 --- a/src/s0027_remove_element.cpp +++ b/src/s0027_remove_element.cpp @@ -1,6 +1,6 @@ #include "s0027_remove_element.hpp" -int Solution::removeElement(vector& nums, int val) { +int S0027::removeElement(vector& nums, int val) { int len = nums.size(); int f{0}, s{0}; while (f < len) { diff --git a/src/s0028_find_the_index_of_the_first_occurrence_in_a_string.cpp b/src/s0028_find_the_index_of_the_first_occurrence_in_a_string.cpp index 0617f6d..f086475 100644 --- a/src/s0028_find_the_index_of_the_first_occurrence_in_a_string.cpp +++ b/src/s0028_find_the_index_of_the_first_occurrence_in_a_string.cpp @@ -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) { diff --git a/src/s0029_divide_two_integers.cpp b/src/s0029_divide_two_integers.cpp index 618836f..7db7bea 100644 --- a/src/s0029_divide_two_integers.cpp +++ b/src/s0029_divide_two_integers.cpp @@ -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) { diff --git a/src/s0030_substring_with_concatenation_of_all_words.cpp b/src/s0030_substring_with_concatenation_of_all_words.cpp index 5fc3584..1d22611 100644 --- a/src/s0030_substring_with_concatenation_of_all_words.cpp +++ b/src/s0030_substring_with_concatenation_of_all_words.cpp @@ -1,6 +1,6 @@ #include "s0030_substring_with_concatenation_of_all_words.hpp" -vector Solution::findSubstring(string s, vector& words) { +vector S0030::findSubstring(string s, vector& words) { vector res; int m = words.size(), n = words[0].size(), ls = s.size(); for (int i = 0; i < n && i + m * n <= ls; ++i) { diff --git a/src/s0031_next_permutation.cpp b/src/s0031_next_permutation.cpp index 76173b6..2f94c9a 100644 --- a/src/s0031_next_permutation.cpp +++ b/src/s0031_next_permutation.cpp @@ -1,6 +1,6 @@ #include "s0031_next_permutation.hpp" -void Solution::nextPermutation(vector& nums) { +void S0031::nextPermutation(vector& nums) { int i = nums.size() - 2; while (i >= 0 && nums[i] >= nums[i + 1]) { i--; diff --git a/src/s0032_longest_valid_parentheses.cpp b/src/s0032_longest_valid_parentheses.cpp index c146389..67d64cd 100644 --- a/src/s0032_longest_valid_parentheses.cpp +++ b/src/s0032_longest_valid_parentheses.cpp @@ -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 dp(n, 0); for (int i = 1; i < n; i++) { diff --git a/src/s0033_search_in_rotated_sorted_array.cpp b/src/s0033_search_in_rotated_sorted_array.cpp index 9d39d5c..79edd97 100644 --- a/src/s0033_search_in_rotated_sorted_array.cpp +++ b/src/s0033_search_in_rotated_sorted_array.cpp @@ -5,7 +5,7 @@ // 思路很简单,对数组二分,有一半一定是有序的,另一半可能有序可能无序 // 对有序的一半进行二分搜索,无序的部分再次进行二分,如此迭代 -int Solution::search(vector& nums, int target) { +int S0033::search(vector& nums, int target) { int size = nums.size(); int left{0}; int right = size - 1; diff --git a/src/s0034_find_first_and_last_position_of_element_in_sorted_array.cpp b/src/s0034_find_first_and_last_position_of_element_in_sorted_array.cpp index 0b4b2e1..6c37c4d 100644 --- a/src/s0034_find_first_and_last_position_of_element_in_sorted_array.cpp +++ b/src/s0034_find_first_and_last_position_of_element_in_sorted_array.cpp @@ -1,8 +1,8 @@ #include "s0034_find_first_and_last_position_of_element_in_sorted_array.hpp" // 闭区间写法 -vector Solution1::searchRange(vector& nums, int target) { - int len = nums.size(); +vector S0034::searchRange1(vector& nums, int target) { + int len = nums.size(); int l{0}; int r = len - 1; while (l <= r) { @@ -27,15 +27,15 @@ vector Solution1::searchRange(vector& nums, int target) { break; } } - return vector {l + 1, r - 1}; + return vector{l + 1, r - 1}; } } - return vector {-1, -1}; + return vector{-1, -1}; } // 开区间写法 -vector Solution2::searchRange(vector& nums, int target) { - int len = nums.size(); +vector S0034::searchRange2(vector& nums, int target) { + int len = nums.size(); int l{0}; int r = len; while (l < r) { @@ -60,8 +60,8 @@ vector Solution2::searchRange(vector& nums, int target) { break; } } - return vector {l + 1, r - 1}; + return vector{l + 1, r - 1}; } } - return vector {-1, -1}; + return vector{-1, -1}; } diff --git a/src/s0035_search_insert_position.cpp b/src/s0035_search_insert_position.cpp index a376baa..1b7681a 100644 --- a/src/s0035_search_insert_position.cpp +++ b/src/s0035_search_insert_position.cpp @@ -1,7 +1,7 @@ #include "s0035_search_insert_position.hpp" // 闭区间写法 -int Solution1::searchInsert(vector& nums, int target) { +int S0035::searchInsert1(vector& nums, int target) { int len = nums.size(); int l{0}; int r = len - 1; @@ -19,7 +19,7 @@ int Solution1::searchInsert(vector& nums, int target) { } // 开区间写法 -int Solution2::searchInsert(vector& nums, int target) { +int S0035::searchInsert2(vector& nums, int target) { int len = nums.size(); int l{0}; int r = len; diff --git a/src/s0036_valid_sudoku.cpp b/src/s0036_valid_sudoku.cpp index 4c6855b..59f30d0 100644 --- a/src/s0036_valid_sudoku.cpp +++ b/src/s0036_valid_sudoku.cpp @@ -1,6 +1,6 @@ #include "s0036_valid_sudoku.hpp" -bool Solution::isValidSudoku(vector>& board) { +bool S0036::isValidSudoku(vector>& board) { vector> rows; vector> columns; vector> grids; diff --git a/src/s0037_sudoku_solver.cpp b/src/s0037_sudoku_solver.cpp index 00c133b..6b5901e 100644 --- a/src/s0037_sudoku_solver.cpp +++ b/src/s0037_sudoku_solver.cpp @@ -1,11 +1,11 @@ #include "s0037_sudoku_solver.hpp" // 返回结果为当前传递进去的数独是否存在解 -bool recusiveSolveSudoku(vector> &board, - vector> &rows, - vector> &cols, - vector> &grids, int row, - int col) { +bool S0037::recusiveSolveSudoku(vector> &board, + vector> &rows, + vector> &cols, + vector> &grids, + int row, int col) { int size = board.size(); // 边界校验 if (col == size) { @@ -49,7 +49,7 @@ bool recusiveSolveSudoku(vector> &board, return false; } -void Solution::solveSudoku(vector> &board) { +void S0037::solveSudoku(vector> &board) { // 每个行/列/单元格中某个数字是否出现过 vector> rows; vector> cols; diff --git a/src/s0038_count_and_say.cpp b/src/s0038_count_and_say.cpp index a11ba97..50c3a47 100644 --- a/src/s0038_count_and_say.cpp +++ b/src/s0038_count_and_say.cpp @@ -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) { diff --git a/src/s0039_combination_sum.cpp b/src/s0039_combination_sum.cpp index 37407ec..f05a703 100644 --- a/src/s0039_combination_sum.cpp +++ b/src/s0039_combination_sum.cpp @@ -1,7 +1,7 @@ #include "s0039_combination_sum.hpp" -void dfs(vector& candidates, int target, vector>& ans, - vector& combine, int idx) { +void S0039::dfs(vector& candidates, int target, vector>& ans, + vector& combine, int idx) { if (idx == candidates.size()) { return; } @@ -19,8 +19,7 @@ void dfs(vector& candidates, int target, vector>& ans, } } -vector> Solution::combinationSum(vector& candidates, - int target) { +vector> S0039::combinationSum(vector& candidates, int target) { vector> ans; vector combine; dfs(candidates, target, ans, combine, 0); diff --git a/src/s0076_minimum_window_substring.cpp b/src/s0076_minimum_window_substring.cpp index 355a10d..65dcf96 100644 --- a/src/s0076_minimum_window_substring.cpp +++ b/src/s0076_minimum_window_substring.cpp @@ -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]; } diff --git a/src/s0151_reverse_words_in_a_string.cpp b/src/s0151_reverse_words_in_a_string.cpp index a224a51..1c8c786 100644 --- a/src/s0151_reverse_words_in_a_string.cpp +++ b/src/s0151_reverse_words_in_a_string.cpp @@ -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; } diff --git a/src/s0209_minimum_size_subarray_sum.cpp b/src/s0209_minimum_size_subarray_sum.cpp index ef0195c..cad657a 100644 --- a/src/s0209_minimum_size_subarray_sum.cpp +++ b/src/s0209_minimum_size_subarray_sum.cpp @@ -1,6 +1,6 @@ #include "s0209_minimum_size_subarray_sum.hpp" -int Solution::minSubArrayLen(int target, vector& nums) { +int S0209::minSubArrayLen(int target, vector& nums) { int len = nums.size(); int result{INT_MAX}; for (int begin{0}, end{0}, sum{0}; end < len; ++end) { diff --git a/src/s0283_move_zeroes.cpp b/src/s0283_move_zeroes.cpp index 3f58eef..4e50b57 100644 --- a/src/s0283_move_zeroes.cpp +++ b/src/s0283_move_zeroes.cpp @@ -1,6 +1,6 @@ #include "s0283_move_zeroes.hpp" -void Solution::moveZeroes(vector& nums) { +void S0283::moveZeroes(vector& nums) { int len = nums.size(); if (len <= 1) { return; diff --git a/src/s0704_binary_search.cpp b/src/s0704_binary_search.cpp index c0f9711..229c771 100644 --- a/src/s0704_binary_search.cpp +++ b/src/s0704_binary_search.cpp @@ -1,7 +1,7 @@ #include "s0704_binary_search.hpp" // 闭区间写法 -int Solution1::binSearch(vector& nums, int target) { +int S0704::binSearch1(vector& nums, int target) { int len = nums.size(); int l{0}; int r = len - 1; @@ -19,7 +19,7 @@ int Solution1::binSearch(vector& nums, int target) { } // 开区间写法 -int Solution2::binSearch(vector& nums, int target) { +int S0704::binSearch2(vector& nums, int target) { int len = nums.size(); int l{0}; int r = len; diff --git a/tests/offer_05.cpp b/tests/offer_05.cpp index cb83dc0..7c404c6 100644 --- a/tests/offer_05.cpp +++ b/tests/offer_05.cpp @@ -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); } diff --git a/tests/offer_58.cpp b/tests/offer_58.cpp index d86ee3b..7e84425 100644 --- a/tests/offer_58.cpp +++ b/tests/offer_58.cpp @@ -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); } diff --git a/tests/s0001_two_sum.cpp b/tests/s0001_two_sum.cpp index 8f54db0..c6bb8f9 100644 --- a/tests/s0001_two_sum.cpp +++ b/tests/s0001_two_sum.cpp @@ -6,7 +6,7 @@ TEST(Problem1, Case1) { std::vector input1{ 2, 7, 11, 15 }; int input2{ 9 }; - Solution solution; + S0001 solution; std::vector result = solution.twoSum(input1, input2); std::vector answer = std::vector{ 0, 1 }; EXPECT_EQ(result, answer); @@ -15,7 +15,7 @@ TEST(Problem1, Case1) { TEST(Problem1, Case2) { std::vector input1{ 3, 2, 4 }; int input2{ 6 }; - Solution solution; + S0001 solution; std::vector result = solution.twoSum(input1, input2); std::vector answer = std::vector{ 1, 2 }; EXPECT_EQ(result, answer); @@ -24,7 +24,7 @@ TEST(Problem1, Case2) { TEST(Problem1, Case3) { std::vector input1{ 3, 3 }; int input2{ 6 }; - Solution solution; + S0001 solution; std::vector result = solution.twoSum(input1, input2); std::vector answer = std::vector{ 0, 1 }; EXPECT_EQ(result, answer); diff --git a/tests/s0002_add_two_numbers.cpp b/tests/s0002_add_two_numbers.cpp index fd50af2..cdf4965 100644 --- a/tests/s0002_add_two_numbers.cpp +++ b/tests/s0002_add_two_numbers.cpp @@ -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); diff --git a/tests/s0003_longest_substring_without_repeating_characters.cpp b/tests/s0003_longest_substring_without_repeating_characters.cpp index cfbe224..220afcf 100644 --- a/tests/s0003_longest_substring_without_repeating_characters.cpp +++ b/tests/s0003_longest_substring_without_repeating_characters.cpp @@ -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); } diff --git a/tests/s0004_median_of_two_sorted_arrays.cpp b/tests/s0004_median_of_two_sorted_arrays.cpp index d1f509e..ba7f6e7 100644 --- a/tests/s0004_median_of_two_sorted_arrays.cpp +++ b/tests/s0004_median_of_two_sorted_arrays.cpp @@ -6,7 +6,7 @@ TEST(Problem4, Case1) { std::vector nums1{1, 3}; std::vector 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 nums1{1, 2}; std::vector nums2{3, 4}; double answer{2.5}; - Solution solution; + S0004 solution; EXPECT_EQ(solution.findMedianSortedArrays(nums1, nums2), answer); } diff --git a/tests/s0005_longest_palindromic_substring.cpp b/tests/s0005_longest_palindromic_substring.cpp index e6c7fb7..a020c3f 100644 --- a/tests/s0005_longest_palindromic_substring.cpp +++ b/tests/s0005_longest_palindromic_substring.cpp @@ -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); } diff --git a/tests/s0006_zigzag_conversion.cpp b/tests/s0006_zigzag_conversion.cpp index b9fc807..89b3d40 100644 --- a/tests/s0006_zigzag_conversion.cpp +++ b/tests/s0006_zigzag_conversion.cpp @@ -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); } diff --git a/tests/s0007_reverse_integer.cpp b/tests/s0007_reverse_integer.cpp index 2d9c4e0..b564c1e 100644 --- a/tests/s0007_reverse_integer.cpp +++ b/tests/s0007_reverse_integer.cpp @@ -5,27 +5,27 @@ TEST(Problem7, Case1) { int i = 123; int o = 321; - Solution solution; + S0007 solution; EXPECT_EQ(solution.reverse(i), o); } TEST(Problem7, Case2) { int i = -123; int o = -321; - Solution solution; + S0007 solution; EXPECT_EQ(solution.reverse(i), o); } TEST(Problem7, Case3) { int i = 120; int o = 21; - Solution solution; + S0007 solution; EXPECT_EQ(solution.reverse(i), o); } TEST(Problem7, Case4) { int i = -2147483648; int o = 0; - Solution solution; + S0007 solution; EXPECT_EQ(solution.reverse(i), o); } diff --git a/tests/s0008_string_to_integer.cpp b/tests/s0008_string_to_integer.cpp index 6ab0bb3..907d56c 100644 --- a/tests/s0008_string_to_integer.cpp +++ b/tests/s0008_string_to_integer.cpp @@ -5,20 +5,20 @@ TEST(Problem8, Case1) { string i = std::string("42"); int o = 42; - Solution solution; + S0008 solution; EXPECT_EQ(solution.myAtoi(i), o); } TEST(Problem8, Case2) { string i = std::string(" -42"); int o = -42; - Solution solution; + S0008 solution; EXPECT_EQ(solution.myAtoi(i), o); } TEST(Problem8, Case3) { string i = std::string("4193 with words"); int o = 4193; - Solution solution; + S0008 solution; EXPECT_EQ(solution.myAtoi(i), o); } diff --git a/tests/s0009_palindrome_number.cpp b/tests/s0009_palindrome_number.cpp index 48de79b..460915c 100644 --- a/tests/s0009_palindrome_number.cpp +++ b/tests/s0009_palindrome_number.cpp @@ -5,20 +5,20 @@ TEST(Problem9, Case1) { int i{121}; bool o{true}; - Solution solution; + S0009 solution; EXPECT_EQ(solution.isPalindrome(i), o); } TEST(Problem9, Case2) { int i{-121}; bool o{false}; - Solution solution; + S0009 solution; EXPECT_EQ(solution.isPalindrome(i), o); } TEST(Problem9, Case3) { int i{10}; bool o{false}; - Solution solution; + S0009 solution; EXPECT_EQ(solution.isPalindrome(i), o); } diff --git a/tests/s0010_regular_expression_matching.cpp b/tests/s0010_regular_expression_matching.cpp index d10bda4..8749ff3 100644 --- a/tests/s0010_regular_expression_matching.cpp +++ b/tests/s0010_regular_expression_matching.cpp @@ -5,20 +5,20 @@ TEST(Problem10, Case1) { string s{"aa"}; string p{"a"}; - Solution solution; + S0010 solution; EXPECT_FALSE(solution.isMatch(s, p)); } TEST(Problem10, Case2) { string s{"aa"}; string p{"a*"}; - Solution solution; + S0010 solution; EXPECT_TRUE(solution.isMatch(s, p)); } TEST(Problem10, Case3) { string s{"ab"}; string p{".*"}; - Solution solution; + S0010 solution; EXPECT_TRUE(solution.isMatch(s, p)); } diff --git a/tests/s0011_container_with_most_water.cpp b/tests/s0011_container_with_most_water.cpp index 7e86dda..3bdfe58 100644 --- a/tests/s0011_container_with_most_water.cpp +++ b/tests/s0011_container_with_most_water.cpp @@ -5,13 +5,13 @@ TEST(Problem11, Case1) { vector height{1, 8, 6, 2, 5, 4, 8, 3, 7}; int o{49}; - Solution solution; + S0011 solution; EXPECT_EQ(solution.maxArea(height), o); } TEST(Problem11, Case2) { vector height{1, 1}; int o{1}; - Solution solution; + S0011 solution; EXPECT_EQ(solution.maxArea(height), o); } diff --git a/tests/s0012_integer_to_roman.cpp b/tests/s0012_integer_to_roman.cpp index a76d705..09864d8 100644 --- a/tests/s0012_integer_to_roman.cpp +++ b/tests/s0012_integer_to_roman.cpp @@ -5,20 +5,20 @@ TEST(Problem12, Case1) { int i{3}; string o("III"); - Solution solution; + S0012 solution; EXPECT_EQ(solution.intToRoman(i), o); } TEST(Problem12, Case2) { int i{58}; string o("LVIII"); - Solution solution; + S0012 solution; EXPECT_EQ(solution.intToRoman(i), o); } TEST(Problem12, Case3) { int i{1994}; string o("MCMXCIV"); - Solution solution; + S0012 solution; EXPECT_EQ(solution.intToRoman(i), o); } diff --git a/tests/s0013_roman_to_integer.cpp b/tests/s0013_roman_to_integer.cpp index 941fce9..e13f412 100644 --- a/tests/s0013_roman_to_integer.cpp +++ b/tests/s0013_roman_to_integer.cpp @@ -5,20 +5,20 @@ TEST(Problem13, Case1) { string i("III"); int o{3}; - Solution solution; + S0013 solution; EXPECT_EQ(solution.romanToInt(i), o); } TEST(Problem13, Case2) { string i("LVIII"); int o{58}; - Solution solution; + S0013 solution; EXPECT_EQ(solution.romanToInt(i), o); } TEST(Problem13, Case3) { string i("MCMXCIV"); int o{1994}; - Solution solution; + S0013 solution; EXPECT_EQ(solution.romanToInt(i), o); } diff --git a/tests/s0014_longest_common_prefix.cpp b/tests/s0014_longest_common_prefix.cpp index f39a8fa..0658621 100644 --- a/tests/s0014_longest_common_prefix.cpp +++ b/tests/s0014_longest_common_prefix.cpp @@ -5,13 +5,13 @@ TEST(Problem14, Case1) { vector i{"flower","flow","flight"}; string o{"fl"}; - Solution solution; + S0014 solution; EXPECT_EQ(solution.longestCommonPrefix(i), o); } TEST(Problem14, Case2) { vector i{"dog","racecar","car"}; string o{""}; - Solution solution; + S0014 solution; EXPECT_EQ(solution.longestCommonPrefix(i), o); } diff --git a/tests/s0015_3sum.cpp b/tests/s0015_3sum.cpp index 34b4b76..6d9971e 100644 --- a/tests/s0015_3sum.cpp +++ b/tests/s0015_3sum.cpp @@ -6,44 +6,39 @@ TEST(Problem15, Case1) { vector i{-1, 0, 1, 2, -1, -4}; vector> o1{{-1, -1, 2}, {-1, 0, 1}}; vector> o2{{-1, 0, 1}, {-1, -1, 2}}; - Solution1 solution1; - Solution2 solution2; - EXPECT_TRUE(solution1.threeSum(i) == o1 || solution1.threeSum(i) == o2); - EXPECT_TRUE(solution2.threeSum(i) == o1 || solution2.threeSum(i) == o2); + S0015 solution; + EXPECT_TRUE(solution.threeSum1(i) == o1 || solution.threeSum1(i) == o2); + EXPECT_TRUE(solution.threeSum2(i) == o1 || solution.threeSum2(i) == o2); } TEST(Problem15, Case2) { vector i{0, 1, 1}; vector> o{}; - Solution1 solution1; - Solution2 solution2; - EXPECT_EQ(solution1.threeSum(i), o); - EXPECT_EQ(solution2.threeSum(i), o); + S0015 solution; + EXPECT_EQ(solution.threeSum1(i), o); + EXPECT_EQ(solution.threeSum2(i), o); } TEST(Problem15, Case3) { vector i{0, 0, 0}; vector> o{{0, 0, 0}}; - Solution1 solution1; - Solution2 solution2; - EXPECT_EQ(solution1.threeSum(i), o); - EXPECT_EQ(solution2.threeSum(i), o); + S0015 solution; + EXPECT_EQ(solution.threeSum1(i), o); + EXPECT_EQ(solution.threeSum2(i), o); } TEST(Problem15, Case4) { vector i{-1, 0, 1}; vector> o{{-1, 0, 1}}; - Solution1 solution1; - Solution2 solution2; - EXPECT_EQ(solution1.threeSum(i), o); - EXPECT_EQ(solution2.threeSum(i), o); + S0015 solution; + EXPECT_EQ(solution.threeSum1(i), o); + EXPECT_EQ(solution.threeSum2(i), o); } TEST(Problem15, Case5) { vector i{1, 2, -2, -1}; vector> o{}; - Solution1 solution1; - Solution2 solution2; - EXPECT_EQ(solution1.threeSum(i), o); - EXPECT_EQ(solution2.threeSum(i), o); + S0015 solution; + EXPECT_EQ(solution.threeSum1(i), o); + EXPECT_EQ(solution.threeSum2(i), o); } diff --git a/tests/s0016_3sum_closest.cpp b/tests/s0016_3sum_closest.cpp index 035b324..1845806 100644 --- a/tests/s0016_3sum_closest.cpp +++ b/tests/s0016_3sum_closest.cpp @@ -6,7 +6,7 @@ TEST(Problem16, Case1) { vector nums{-1, 2, 1, -4}; int target{1}; int o{2}; - Solution solution; + S0016 solution; EXPECT_EQ(solution.threeSumClosest(nums, target), o); } @@ -14,6 +14,6 @@ TEST(Problem16, Case2) { vector nums{0, 0, 0}; int target{1}; int o{0}; - Solution solution; + S0016 solution; EXPECT_EQ(solution.threeSumClosest(nums, target), o); } diff --git a/tests/s0017_letter_combinations_of_a_phone_number.cpp b/tests/s0017_letter_combinations_of_a_phone_number.cpp index 9690cca..00b4f18 100644 --- a/tests/s0017_letter_combinations_of_a_phone_number.cpp +++ b/tests/s0017_letter_combinations_of_a_phone_number.cpp @@ -5,20 +5,20 @@ TEST(Problem17, Case1) { string i{"23"}; vector o{"ad", "bd", "cd", "ae", "be", "ce", "af", "bf", "cf"}; - Solution solution; + S0017 solution; EXPECT_EQ(solution.letterCombinations(i), o); } TEST(Problem17, Case2) { string i{""}; vector o{}; - Solution solution; + S0017 solution; EXPECT_EQ(solution.letterCombinations(i), o); } TEST(Problem17, Case3) { string i{"2"}; vector o{"a", "b", "c"}; - Solution solution; + S0017 solution; EXPECT_EQ(solution.letterCombinations(i), o); } diff --git a/tests/s0018_4sum.cpp b/tests/s0018_4sum.cpp index 03269f4..2b4e428 100644 --- a/tests/s0018_4sum.cpp +++ b/tests/s0018_4sum.cpp @@ -6,7 +6,7 @@ TEST(Problem18, Case1) { vector nums{1, 0, -1, 0, -2, 2}; int target{0}; vector> sum{{-2, -1, 1, 2}, {-2, 0, 0, 2}, {-1, 0, 0, 1}}; - Solution solution; + S0018 solution; EXPECT_EQ(solution.fourSum(nums, target), sum); } @@ -14,6 +14,6 @@ TEST(Problem18, Case2) { vector nums{2, 2, 2, 2, 2}; int target{8}; vector> sum{{2, 2, 2, 2}}; - Solution solution; + S0018 solution; EXPECT_EQ(solution.fourSum(nums, target), sum); } diff --git a/tests/s0020_valid_parentheses.cpp b/tests/s0020_valid_parentheses.cpp index ff5bd3c..bb9fbf2 100644 --- a/tests/s0020_valid_parentheses.cpp +++ b/tests/s0020_valid_parentheses.cpp @@ -5,27 +5,27 @@ TEST(Problem20, Case1) { string i{"()"}; bool o{true}; - Solution solution; + S0020 solution; EXPECT_EQ(solution.isValid(i), o); } TEST(Problem20, Case2) { string i{"()[]{}"}; bool o{true}; - Solution solution; + S0020 solution; EXPECT_EQ(solution.isValid(i), o); } TEST(Problem20, Case3) { string i{"(]"}; bool o{false}; - Solution solution; + S0020 solution; EXPECT_EQ(solution.isValid(i), o); } TEST(Problem20, Case4) { string i{"(){}}{"}; bool o{false}; - Solution solution; + S0020 solution; EXPECT_EQ(solution.isValid(i), o); } diff --git a/tests/s0022_generate_parentheses.cpp b/tests/s0022_generate_parentheses.cpp index 402ea26..e00bb5d 100644 --- a/tests/s0022_generate_parentheses.cpp +++ b/tests/s0022_generate_parentheses.cpp @@ -5,20 +5,20 @@ TEST(Problem22, Case1) { int i{3}; vector o{"((()))", "(()())", "(())()", "()(())", "()()()"}; - Solution solution; + S0022 solution; EXPECT_EQ(solution.generateParenthesis(i), o); } TEST(Problem22, Case2) { int i{1}; vector o{"()"}; - Solution solution; + S0022 solution; EXPECT_EQ(solution.generateParenthesis(i), o); } TEST(Problem22, Case3) { int i{4}; vector o{"(((())))","((()()))","((())())","((()))()","(()(()))","(()()())","(()())()","(())(())","(())()()","()((()))","()(()())","()(())()","()()(())","()()()()"}; - Solution solution; + S0022 solution; EXPECT_EQ(solution.generateParenthesis(i), o); } diff --git a/tests/s0026_remove_duplicates_from_sorted_array.cpp b/tests/s0026_remove_duplicates_from_sorted_array.cpp index 883b3a7..6481388 100644 --- a/tests/s0026_remove_duplicates_from_sorted_array.cpp +++ b/tests/s0026_remove_duplicates_from_sorted_array.cpp @@ -5,13 +5,13 @@ TEST(Problem26, Case1) { vector i{1, 1, 2}; int o{2}; - Solution solution; + S0026 solution; EXPECT_EQ(solution.removeDuplicates(i), o); } TEST(Problem26, Case2) { vector i{0, 0, 1, 1, 1, 2, 2, 3, 3, 4}; int o{5}; - Solution solution; + S0026 solution; EXPECT_EQ(solution.removeDuplicates(i), o); } diff --git a/tests/s0027_remove_element.cpp b/tests/s0027_remove_element.cpp index 265fe10..ff0d1a0 100644 --- a/tests/s0027_remove_element.cpp +++ b/tests/s0027_remove_element.cpp @@ -6,7 +6,7 @@ TEST(Problem27, Case1) { vector nums{3, 2, 2, 3}; int val{3}; int o{2}; - Solution solution; + S0027 solution; EXPECT_EQ(solution.removeElement(nums, val), o); } @@ -14,6 +14,6 @@ TEST(Problem27, Case2) { vector nums{0, 1, 2, 2, 3, 0, 4, 2}; int val{2}; int o{5}; - Solution solution; + S0027 solution; EXPECT_EQ(solution.removeElement(nums, val), o); } diff --git a/tests/s0028_find_the_index_of_the_first_occurrence_in_a_string.cpp b/tests/s0028_find_the_index_of_the_first_occurrence_in_a_string.cpp index 6c83072..98f7f27 100644 --- a/tests/s0028_find_the_index_of_the_first_occurrence_in_a_string.cpp +++ b/tests/s0028_find_the_index_of_the_first_occurrence_in_a_string.cpp @@ -6,7 +6,7 @@ TEST(Problem28, Case1) { string haystack{"sadbutsad"}; string needle{"sad"}; int o{0}; - Solution solution; + S0028 solution; EXPECT_EQ(solution.strStr(haystack, needle), o); } @@ -14,6 +14,6 @@ TEST(Problem28, Case2) { string haystack{"leetcode"}; string needle{"leeto"}; int o{-1}; - Solution solution; + S0028 solution; EXPECT_EQ(solution.strStr(haystack, needle), o); } diff --git a/tests/s0029_divide_two_integers.cpp b/tests/s0029_divide_two_integers.cpp index c005832..6c30650 100644 --- a/tests/s0029_divide_two_integers.cpp +++ b/tests/s0029_divide_two_integers.cpp @@ -6,7 +6,7 @@ TEST(Problem29, Case1) { int dividend{10}; int divisor{3}; int o{3}; - Solution solution; + S0029 solution; EXPECT_EQ(solution.divide(dividend, divisor), o); } @@ -14,6 +14,6 @@ TEST(Problem29, Case2) { int dividend{7}; int divisor{-3}; int o{-2}; - Solution solution; + S0029 solution; EXPECT_EQ(solution.divide(dividend, divisor), o); } diff --git a/tests/s0030_substring_with_concatenation_of_all_words.cpp b/tests/s0030_substring_with_concatenation_of_all_words.cpp index 93bad95..676f175 100644 --- a/tests/s0030_substring_with_concatenation_of_all_words.cpp +++ b/tests/s0030_substring_with_concatenation_of_all_words.cpp @@ -6,7 +6,7 @@ TEST(Problem30, Case1) { string s{"barfoothefoobarman"}; vector words{"foo", "bar"}; vector o{0, 9}; - Solution solution; + S0030 solution; EXPECT_EQ(solution.findSubstring(s, words), o); } @@ -14,6 +14,6 @@ TEST(Problem30, Case2) { string s{"wordgoodgoodgoodbestword"}; vector words{"word","good","best","word"}; vector o{}; - Solution solution; + S0030 solution; EXPECT_EQ(solution.findSubstring(s, words), o); } diff --git a/tests/s0032_longest_valid_parentheses.cpp b/tests/s0032_longest_valid_parentheses.cpp index 49ee0b6..898e82a 100644 --- a/tests/s0032_longest_valid_parentheses.cpp +++ b/tests/s0032_longest_valid_parentheses.cpp @@ -5,27 +5,27 @@ TEST(Problem32, Case1) { string i{"(()"}; int o{2}; - Solution solution; + S0032 solution; EXPECT_EQ(solution.longestValidParentheses(i), o); } TEST(Problem32, Case2) { string i{")()())"}; int o{4}; - Solution solution; + S0032 solution; EXPECT_EQ(solution.longestValidParentheses(i), o); } TEST(Problem32, Case3) { string i{"())"}; int o{2}; - Solution solution; + S0032 solution; EXPECT_EQ(solution.longestValidParentheses(i), o); } TEST(Problem32, Case4) { string i{"()(()"}; int o{2}; - Solution solution; + S0032 solution; EXPECT_EQ(solution.longestValidParentheses(i), o); } diff --git a/tests/s0033_search_in_rotated_sorted_array.cpp b/tests/s0033_search_in_rotated_sorted_array.cpp index fc7b292..4cc7619 100644 --- a/tests/s0033_search_in_rotated_sorted_array.cpp +++ b/tests/s0033_search_in_rotated_sorted_array.cpp @@ -6,7 +6,7 @@ TEST(Problem33, Case1) { vector nums{4, 5, 6, 7, 0, 1, 2}; int target{0}; int o{4}; - Solution solution; + S0033 solution; EXPECT_EQ(solution.search(nums, target), o); } @@ -14,7 +14,7 @@ TEST(Problem33, Case2) { vector nums{4, 5, 6, 7, 0, 1, 2}; int target{3}; int o{-1}; - Solution solution; + S0033 solution; EXPECT_EQ(solution.search(nums, target), o); } @@ -22,6 +22,6 @@ TEST(Problem33, Case3) { vector nums{1}; int target{0}; int o{-1}; - Solution solution; + S0033 solution; EXPECT_EQ(solution.search(nums, target), o); } diff --git a/tests/s0034_find_first_and_last_position_of_element_in_sorted_array.cpp b/tests/s0034_find_first_and_last_position_of_element_in_sorted_array.cpp index 9ea6184..735443a 100644 --- a/tests/s0034_find_first_and_last_position_of_element_in_sorted_array.cpp +++ b/tests/s0034_find_first_and_last_position_of_element_in_sorted_array.cpp @@ -6,28 +6,25 @@ TEST(Problem34, Case1) { vector nums{5, 7, 7, 8, 8, 10}; int target{8}; vector o{3, 4}; - Solution1 solution1; - Solution2 solution2; - EXPECT_EQ(solution1.searchRange(nums, target), o); - EXPECT_EQ(solution2.searchRange(nums, target), o); + S0034 solution; + EXPECT_EQ(solution.searchRange1(nums, target), o); + EXPECT_EQ(solution.searchRange2(nums, target), o); } TEST(Problem34, Case2) { vector nums{5, 7, 7, 8, 8, 10}; int target{6}; vector o{-1, -1}; - Solution1 solution1; - Solution2 solution2; - EXPECT_EQ(solution1.searchRange(nums, target), o); - EXPECT_EQ(solution2.searchRange(nums, target), o); + S0034 solution; + EXPECT_EQ(solution.searchRange1(nums, target), o); + EXPECT_EQ(solution.searchRange2(nums, target), o); } TEST(Problem34, Case3) { vector nums{}; int target{0}; vector o{-1, -1}; - Solution1 solution1; - Solution2 solution2; - EXPECT_EQ(solution1.searchRange(nums, target), o); - EXPECT_EQ(solution2.searchRange(nums, target), o); + S0034 solution; + EXPECT_EQ(solution.searchRange1(nums, target), o); + EXPECT_EQ(solution.searchRange2(nums, target), o); } diff --git a/tests/s0035_search_insert_position.cpp b/tests/s0035_search_insert_position.cpp index c7d59a1..a951e06 100644 --- a/tests/s0035_search_insert_position.cpp +++ b/tests/s0035_search_insert_position.cpp @@ -6,28 +6,25 @@ TEST(Problem35, Case1) { vector nums{1, 3, 5, 6}; int target{5}; int o{2}; - Solution1 solution1; - Solution2 solution2; - EXPECT_EQ(solution1.searchInsert(nums, target), o); - EXPECT_EQ(solution2.searchInsert(nums, target), o); + S0035 solution; + EXPECT_EQ(solution.searchInsert1(nums, target), o); + EXPECT_EQ(solution.searchInsert2(nums, target), o); } TEST(Problem35, Case2) { vector nums{1, 3, 5, 6}; int target{2}; int o{1}; - Solution1 solution1; - Solution2 solution2; - EXPECT_EQ(solution1.searchInsert(nums, target), o); - EXPECT_EQ(solution2.searchInsert(nums, target), o); + S0035 solution; + EXPECT_EQ(solution.searchInsert1(nums, target), o); + EXPECT_EQ(solution.searchInsert2(nums, target), o); } TEST(Problem35, Case3) { vector nums{1, 3, 5, 6}; int target{7}; int o{4}; - Solution1 solution1; - Solution2 solution2; - EXPECT_EQ(solution1.searchInsert(nums, target), o); - EXPECT_EQ(solution2.searchInsert(nums, target), o); + S0035 solution; + EXPECT_EQ(solution.searchInsert1(nums, target), o); + EXPECT_EQ(solution.searchInsert2(nums, target), o); } diff --git a/tests/s0036_valid_sudoku.cpp b/tests/s0036_valid_sudoku.cpp index 614afb7..47b9cee 100644 --- a/tests/s0036_valid_sudoku.cpp +++ b/tests/s0036_valid_sudoku.cpp @@ -13,7 +13,7 @@ TEST(Problem36, Case1) { {'.', '.', '.', '4', '1', '9', '.', '.', '5'}, {'.', '.', '.', '.', '8', '.', '.', '7', '9'}}; bool o{true}; - Solution solution; + S0036 solution; EXPECT_EQ(solution.isValidSudoku(board), o); } @@ -28,6 +28,6 @@ TEST(Problem36, Case2) { {'.', '.', '.', '4', '1', '9', '.', '.', '5'}, {'.', '.', '.', '.', '8', '.', '.', '7', '9'}}; bool o{false}; - Solution solution; + S0036 solution; EXPECT_EQ(solution.isValidSudoku(board), o); } diff --git a/tests/s0037_sudoku_solver.cpp b/tests/s0037_sudoku_solver.cpp index 9033a47..1eddec9 100644 --- a/tests/s0037_sudoku_solver.cpp +++ b/tests/s0037_sudoku_solver.cpp @@ -21,7 +21,7 @@ TEST(Problem37, Case1) { {'9', '6', '1', '5', '3', '7', '2', '8', '4'}, {'2', '8', '7', '4', '1', '9', '6', '3', '5'}, {'3', '4', '5', '2', '8', '6', '1', '7', '9'}}; - Solution solution; + S0037 solution; solution.solveSudoku(board); EXPECT_EQ(board, expected); } diff --git a/tests/s0038_count_and_say.cpp b/tests/s0038_count_and_say.cpp index 77cba2c..24d10f4 100644 --- a/tests/s0038_count_and_say.cpp +++ b/tests/s0038_count_and_say.cpp @@ -5,13 +5,13 @@ TEST(Problem38, Case1) { int i{1}; string o{"1"}; - Solution solution; + S0038 solution; EXPECT_EQ(solution.countAndSay(i), o); } TEST(Problem38, Case2) { int i{4}; string o{"1211"}; - Solution solution; + S0038 solution; EXPECT_EQ(solution.countAndSay(i), o); } diff --git a/tests/s0039_combination_sum.cpp b/tests/s0039_combination_sum.cpp index 082519b..5960cf8 100644 --- a/tests/s0039_combination_sum.cpp +++ b/tests/s0039_combination_sum.cpp @@ -6,7 +6,7 @@ TEST(Problem39, Case1) { vector candidates{2, 3, 6, 7}; int target{7}; vector> o{{7}, {2, 2, 3}}; - Solution solution; + S0039 solution; EXPECT_EQ(solution.combinationSum(candidates, target), o); } @@ -14,7 +14,7 @@ TEST(Problem39, Case2) { vector candidates{2, 3, 5}; int target{8}; vector> o{{3, 5}, {2, 3, 3}, {2, 2, 2, 2}}; - Solution solution; + S0039 solution; EXPECT_EQ(solution.combinationSum(candidates, target), o); } @@ -22,6 +22,6 @@ TEST(Problem39, Case3) { vector candidates{2}; int target{1}; vector> o{}; - Solution solution; + S0039 solution; EXPECT_EQ(solution.combinationSum(candidates, target), o); } diff --git a/tests/s0076_minimum_window_substring.cpp b/tests/s0076_minimum_window_substring.cpp index 08ddc53..362ae42 100644 --- a/tests/s0076_minimum_window_substring.cpp +++ b/tests/s0076_minimum_window_substring.cpp @@ -6,7 +6,7 @@ TEST(Problem76, Case1) { string s{"ADOBECODEBANC"}; string t{"ABC"}; string expected{"BANC"}; - Solution solution; + S0076 solution; EXPECT_EQ(solution.minWindow(s, t), expected); } @@ -14,7 +14,7 @@ TEST(Problem76, Case2) { string s{"a"}; string t{"a"}; string expected{"a"}; - Solution solution; + S0076 solution; EXPECT_EQ(solution.minWindow(s, t), expected); } @@ -22,6 +22,6 @@ TEST(Problem76, Case3) { string s{"a"}; string t{"aa"}; string expected{""}; - Solution solution; + S0076 solution; EXPECT_EQ(solution.minWindow(s, t), expected); } diff --git a/tests/s0151_reverse_words_in_a_string.cpp b/tests/s0151_reverse_words_in_a_string.cpp index a69ae8a..81d306c 100644 --- a/tests/s0151_reverse_words_in_a_string.cpp +++ b/tests/s0151_reverse_words_in_a_string.cpp @@ -5,27 +5,27 @@ TEST(Problem151, Case1) { string s{"the sky is blue"}; string expected{"blue is sky the"}; - Solution solution; + S0151 solution; EXPECT_EQ(solution.reverseWords(s), expected); } TEST(Problem151, Case2) { string s{" hello world "}; string expected{"world hello"}; - Solution solution; + S0151 solution; EXPECT_EQ(solution.reverseWords(s), expected); } TEST(Problem151, Case3) { string s{"a good example"}; string expected{"example good a"}; - Solution solution; + S0151 solution; EXPECT_EQ(solution.reverseWords(s), expected); } TEST(Problem151, Case4) { string s{"F R I E N D S "}; string expected{"S D N E I R F"}; - Solution solution; + S0151 solution; EXPECT_EQ(solution.reverseWords(s), expected); } diff --git a/tests/s0209_minimum_size_subarray_sum.cpp b/tests/s0209_minimum_size_subarray_sum.cpp index edeb553..b780848 100644 --- a/tests/s0209_minimum_size_subarray_sum.cpp +++ b/tests/s0209_minimum_size_subarray_sum.cpp @@ -6,7 +6,7 @@ TEST(Problem209, Case1) { int target{7}; vector nums{2, 3, 1, 2, 4, 3}; int o{2}; - Solution solution; + S0209 solution; EXPECT_EQ(solution.minSubArrayLen(target, nums), o); } @@ -14,7 +14,7 @@ TEST(Problem209, Case2) { int target{4}; vector nums{1, 4, 4}; int o{1}; - Solution solution; + S0209 solution; EXPECT_EQ(solution.minSubArrayLen(target, nums), o); } @@ -22,7 +22,7 @@ TEST(Problem209, Case3) { int target{11}; vector nums{1, 1, 1, 1, 1, 1, 1, 1}; int o{0}; - Solution solution; + S0209 solution; EXPECT_EQ(solution.minSubArrayLen(target, nums), o); } @@ -30,6 +30,6 @@ TEST(Problem209, Case4) { int target{11}; vector nums{1, 2, 3, 4, 5}; int o{3}; - Solution solution; + S0209 solution; EXPECT_EQ(solution.minSubArrayLen(target, nums), o); } diff --git a/tests/s0283_move_zeroes.cpp b/tests/s0283_move_zeroes.cpp index 346e0ec..5e87c1c 100644 --- a/tests/s0283_move_zeroes.cpp +++ b/tests/s0283_move_zeroes.cpp @@ -5,7 +5,7 @@ TEST(Problem283, Case1) { vector i{0, 1, 0, 3, 12}; vector o{1, 3, 12, 0, 0}; - Solution solution; + S0283 solution; solution.moveZeroes(i); EXPECT_EQ(i, o); } @@ -13,7 +13,7 @@ TEST(Problem283, Case1) { TEST(Problem283, Case2) { vector i{0}; vector o{0}; - Solution solution; + S0283 solution; solution.moveZeroes(i); EXPECT_EQ(i, o); } diff --git a/tests/s0704_binary_search.cpp b/tests/s0704_binary_search.cpp index 00be0bf..3e69ac3 100644 --- a/tests/s0704_binary_search.cpp +++ b/tests/s0704_binary_search.cpp @@ -6,18 +6,16 @@ TEST(Problem704, Case1) { vector nums{-1, 0, 3, 5, 9, 12}; int target{9}; int o{4}; - Solution1 solution1; - Solution2 solution2; - EXPECT_EQ(solution1.binSearch(nums, target), o); - EXPECT_EQ(solution2.binSearch(nums, target), o); + S0704 solution; + EXPECT_EQ(solution.binSearch1(nums, target), o); + EXPECT_EQ(solution.binSearch2(nums, target), o); } TEST(Problem704, Case2) { vector nums{-1, 0, 3, 5, 9, 12}; int target{2}; int o{-1}; - Solution1 solution1; - Solution2 solution2; - EXPECT_EQ(solution1.binSearch(nums, target), o); - EXPECT_EQ(solution2.binSearch(nums, target), o); + S0704 solution; + EXPECT_EQ(solution.binSearch1(nums, target), o); + EXPECT_EQ(solution.binSearch2(nums, target), o); }