Refactor
This commit is contained in:
		@@ -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,8 +1,8 @@
 | 
			
		||||
#include "s0034_find_first_and_last_position_of_element_in_sorted_array.hpp"
 | 
			
		||||
 | 
			
		||||
// 闭区间写法
 | 
			
		||||
vector<int> Solution1::searchRange(vector<int>& nums, int target) {
 | 
			
		||||
  int len  = nums.size();
 | 
			
		||||
vector<int> S0034::searchRange1(vector<int>& nums, int target) {
 | 
			
		||||
  int len = nums.size();
 | 
			
		||||
  int l{0};
 | 
			
		||||
  int r = len - 1;
 | 
			
		||||
  while (l <= r) {
 | 
			
		||||
@@ -27,15 +27,15 @@ vector<int> Solution1::searchRange(vector<int>& nums, int target) {
 | 
			
		||||
          break;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      return vector<int> {l + 1, r - 1};
 | 
			
		||||
      return vector<int>{l + 1, r - 1};
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return vector<int> {-1, -1};
 | 
			
		||||
  return vector<int>{-1, -1};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 开区间写法
 | 
			
		||||
vector<int> Solution2::searchRange(vector<int>& nums, int target) {
 | 
			
		||||
  int len  = nums.size();
 | 
			
		||||
vector<int> S0034::searchRange2(vector<int>& nums, int target) {
 | 
			
		||||
  int len = nums.size();
 | 
			
		||||
  int l{0};
 | 
			
		||||
  int r = len;
 | 
			
		||||
  while (l < r) {
 | 
			
		||||
@@ -60,8 +60,8 @@ vector<int> Solution2::searchRange(vector<int>& nums, int target) {
 | 
			
		||||
          break;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      return vector<int> {l + 1, r - 1};
 | 
			
		||||
      return vector<int>{l + 1, r - 1};
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return vector<int> {-1, -1};
 | 
			
		||||
  return vector<int>{-1, -1};
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
#include "s0035_search_insert_position.hpp"
 | 
			
		||||
 | 
			
		||||
// 闭区间写法
 | 
			
		||||
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,
 | 
			
		||||
                         vector<unordered_map<char, bool>> &rows,
 | 
			
		||||
                         vector<unordered_map<char, bool>> &cols,
 | 
			
		||||
                         vector<unordered_map<char, bool>> &grids, int row,
 | 
			
		||||
                         int col) {
 | 
			
		||||
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) {
 | 
			
		||||
  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,7 +1,7 @@
 | 
			
		||||
#include "s0039_combination_sum.hpp"
 | 
			
		||||
 | 
			
		||||
void dfs(vector<int>& candidates, int target, vector<vector<int>>& ans,
 | 
			
		||||
         vector<int>& combine, int idx) {
 | 
			
		||||
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;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user