Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ListNode* build(const std::vector<int>& vec) {
if (vec.empty()) return nullptr;
ListNode* head = new ListNode(vec[0]);
ListNode* cur = head;
for (int i = 1; i < vec.size(); i++) {
for (size_t i = 1; i < vec.size(); i++) {
cur->next = new ListNode(vec[i]);
cur = cur->next;
}
Expand Down
2 changes: 1 addition & 1 deletion src/1-twoSum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> hash;
for (int i = 0; i < nums.size(); i++) {
for (int i = 0; i < static_cast<int>(nums.size()); i++) {
if (hash.contains(target - nums[i])) {
return {hash[target - nums[i]], i};
}
Expand Down
5 changes: 3 additions & 2 deletions src/13-romanToInt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ class Solution {
m['M'] = 1000;

int res = 0;
for (int i = 0; i < s.size(); i++) {
if (i < s.size() - 1 && m[s[i]] < m[s[i + 1]]) {
const int n = static_cast<int>(s.size());
for (int i = 0; i < n; i++) {
if (i < n - 1 && m[s[i]] < m[s[i + 1]]) {
res -= m[s[i]];
} else {
res += m[s[i]];
Expand Down
5 changes: 3 additions & 2 deletions src/14-longestCommonPrefix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ using namespace std;
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
for (int i = 0; i < strs[0].size(); i++) {
const int n = static_cast<int>(strs[0].size());
for (int i = 0; i < n; i++) {
for (const auto& str : strs) {
if (i >= str.size() || str[i] != strs[0][i]) {
if (i >= n || str[i] != strs[0][i]) {
return strs[0].substr(0, i);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/15-threeSum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ class Solution {
}
vector<vector<int>> res;
ranges::sort(nums);
for (int i = 0; i < nums.size() - 2; i++) {
const int n = static_cast<int>(nums.size());
for (int i = 0; i < n - 2; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int left = i + 1, right = nums.size() - 1;
int left = i + 1, right = n - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
if (sum == 0) {
Expand Down
5 changes: 3 additions & 2 deletions src/16-threeSumClosest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ class Solution {
int threeSumClosest(vector<int>& nums, int target) {
int res = nums[0] + nums[1] + nums[2];
ranges::sort(nums);
for (int i = 0; i < nums.size() - 2; i++) {
const int n = static_cast<int>(nums.size());
for (int i = 0; i < n - 2; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int left = i + 1, right = nums.size() - 1;
int left = i + 1, right = n - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
if (sum == target) {
Expand Down
2 changes: 1 addition & 1 deletion src/17-letterCombinations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Solution {
const vector<string> m{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

void dfs(const string& digits, int index, string&& path, vector<string>& res) {
if (index == digits.size()) {
if (index == static_cast<int>(digits.size())) {
res.push_back(path);
return;
}
Expand Down
9 changes: 5 additions & 4 deletions src/18-fourSum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ class Solution {

private:
vector<vector<int>> kSum(vector<int>& nums, int k, int start, long target) {
if (nums.size() < k || k < 2) return {};
const int n = static_cast<int>(nums.size());
if (n < k || k < 2) return {};
vector<vector<int>> res;
if (k == 2) {
int left = start, right = nums.size() - 1;
int left = start, right = n - 1;
while (left < right) {
long sum = nums[left] + nums[right];
if (sum == target) {
Expand All @@ -33,13 +34,13 @@ class Solution {
}
}
} else {
for (int i = start; i < nums.size(); i++) {
for (int i = start; i < n; i++) {
auto sub = kSum(nums, k - 1, i + 1, target - nums[i]);
for (auto& v : sub) {
v.push_back(nums[i]);
res.push_back(v);
}
while (i < nums.size() - 1 && nums[i] == nums[i + 1]) i++;
while (i < n - 1 && nums[i] == nums[i + 1]) i++;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/26-removeDuplicates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Solution {
int removeDuplicates(vector<int>& nums) {
if (nums.empty()) return 0;
int res = 0;
for (int i = 1; i < nums.size(); i++) {
for (int i = 1; i < static_cast<int>(nums.size()); i++) {
if (nums[res] != nums[i]) {
nums[++res] = nums[i];
}
Expand All @@ -24,12 +24,12 @@ void test() {
Solution s;
vector<int> nums = {1, 1, 2};
vector<int> expected = {1, 2};
assert(s.removeDuplicates(nums) == expected.size());
assert(s.removeDuplicates(nums) == static_cast<int>(expected.size()));
assert(vector<int>(nums.begin(), nums.begin() + expected.size()) == expected);

nums = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
expected = {0, 1, 2, 3, 4};
assert(s.removeDuplicates(nums) == expected.size());
assert(s.removeDuplicates(nums) == static_cast<int>(expected.size()));
assert(vector<int>(nums.begin(), nums.begin() + expected.size()) == expected);
}

Expand Down
8 changes: 4 additions & 4 deletions src/27-removeElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Solution {
int removeElement(vector<int>& nums, int val) {
if (nums.empty()) return 0;
int res = -1;
for (int i = 0; i < nums.size(); i++) {
for (int i = 0; i < static_cast<int>(nums.size()); i++) {
if (nums[i] != val) {
nums[++res] = nums[i];
}
Expand All @@ -24,17 +24,17 @@ void test() {
Solution s;
vector<int> nums = {3, 2, 2, 3};
vector<int> expected = {2, 2};
assert(s.removeElement(nums, 3) == expected.size());
assert(s.removeElement(nums, 3) == static_cast<int>(expected.size()));
assert(vector<int>(nums.begin(), nums.begin() + expected.size()) == expected);

nums = {0, 1, 2, 2, 3, 0, 4, 2};
expected = {0, 1, 3, 0, 4};
assert(s.removeElement(nums, 2) == expected.size());
assert(s.removeElement(nums, 2) == static_cast<int>(expected.size()));
assert(vector<int>(nums.begin(), nums.begin() + expected.size()) == expected);

nums = {};
expected = {};
assert(s.removeElement(nums, 1) == expected.size());
assert(s.removeElement(nums, 1) == static_cast<int>(expected.size()));
assert(vector<int>(nums.begin(), nums.begin() + expected.size()) == expected);
}

Expand Down
13 changes: 8 additions & 5 deletions src/3-lengthOfLongestSubstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ using namespace std;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
char chs[128] = {0};
int chs[128] = {0};
int res = 0;
for (int left = 0, right = 0; right < s.size(); right++) {
chs[s[right]]++;
while (chs[s[right]] > 1) {
chs[s[left++]]--;
for (int left = 0, right = 0; right < static_cast<int>(s.size()); right++) {
auto c = static_cast<unsigned char>(s[right]);
chs[c]++;
while (chs[c] > 1) {
auto lc = static_cast<unsigned char>(s[left]);
chs[lc]--;
left++;
}
res = max(res, right - left + 1);
}
Expand Down
2 changes: 1 addition & 1 deletion src/34-searchRange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
const int l = static_cast<int>(ranges::lower_bound(nums, target) - nums.begin()); // find first >= target
if (l == nums.size() || nums[l] != target) return {-1, -1};
if (l == static_cast<int>(nums.size()) || nums[l] != target) return {-1, -1};
const int r = static_cast<int>(ranges::upper_bound(nums, target) - nums.begin() - 1); // find first > target
return {l, r};
}
Expand Down
5 changes: 3 additions & 2 deletions src/38-countAndSay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ class Solution {
string res = "1";
while (--n) {
string tmp;
for (int i = 0; i < res.size(); ++i) {
const int m = static_cast<int>(res.size());
for (int i = 0; i < m; ++i) {
int cnt = 1;
while (i < res.size() - 1 && res[i] == res[i + 1]) {
while (i < m - 1 && res[i] == res[i + 1]) {
cnt++;
i++;
}
Expand Down
2 changes: 1 addition & 1 deletion src/39-combinationSum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Solution {
res.emplace_back(path);
return;
}
for (int i = index; i < candidates.size(); i++) {
for (int i = index; i < static_cast<int>(candidates.size()); i++) {
if (candidates[i] > target) break;
path.emplace_back(candidates[i]);
dfs(candidates, target - candidates[i], i, std::move(path), res);
Expand Down
2 changes: 1 addition & 1 deletion src/40-combinationSum2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Solution {
res.emplace_back(path);
return;
}
for (int i = index; i < candidates.size(); i++) {
for (int i = index; i < static_cast<int>(candidates.size()); i++) {
if (candidates[i] > target) break;
if (i > index && candidates[i] == candidates[i - 1]) continue; // 剪枝 重点理解这个 i>index 的作用
path.emplace_back(candidates[i]);
Expand Down
9 changes: 5 additions & 4 deletions src/43-multiply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ class Solution {
public:
string multiply(string num1, string num2) {
string res(num1.size() + num2.size(), '0');
for (int i = num1.size() - 1; i >= 0; i--) {
for (int j = num2.size() - 1; j >= 0; j--) {
const int m = static_cast<int>(num1.size()), n = static_cast<int>(num2.size());
for (int i = m - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
const int mult = (num1[i] - '0') * (num2[j] - '0');
const int sum = mult + (res[i + j + 1] - '0');
res[i + j + 1] = (sum % 10) + '0';
res[i + j + 1] = static_cast<char>((sum % 10) + '0');
res[i + j] += sum / 10;
}
}
const int idx = res.find_first_not_of('0');
auto idx = res.find_first_not_of('0');
return idx == string::npos ? "0" : res.substr(idx);
}
};
Expand Down
5 changes: 3 additions & 2 deletions src/45-jump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ class Solution {
int res = 0;
int end = 0;
int farthest = 0;
for (int i = 0; i < nums.size() - 1; i++) {
const int n = static_cast<int>(nums.size());
for (int i = 0; i < n - 1; i++) {
farthest = max(farthest, i + nums[i]);
if (farthest >= nums.size() - 1) {
if (farthest >= n - 1) {
res++;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/46-permute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Solution {
res.emplace_back(path);
return;
}
for (int i = 0; i < nums.size(); ++i) {
for (int i = 0; i < static_cast<int>(nums.size()); ++i) {
if (used[i]) continue;
used[i] = true;
path.emplace_back(nums[i]);
Expand Down
2 changes: 1 addition & 1 deletion src/47-permuteUnique.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Solution {
res.emplace_back(path);
return;
}
for (int i = 0; i < nums.size(); ++i) {
for (int i = 0; i < static_cast<int>(nums.size()); ++i) {
if (used[i]) continue;
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) continue;
used[i] = true;
Expand Down
4 changes: 2 additions & 2 deletions src/48-rotate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ using namespace std;
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
const int m = matrix.size();
const int n = matrix[0].size();
for (int i = 0; i < m; i++) {
for (int j = i; j < n; j++) {
swap(matrix[i][j], matrix[j][i]); // 转置
Expand Down
4 changes: 2 additions & 2 deletions src/5-longestPalindrome.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Solution {
public:
string longestPalindrome(string s) {
pair<int, int> index{0, 0};
for (int i = 0; i < s.size(); i++) {
for (int i = 0; i < static_cast<int>(s.size()); i++) {
auto index1 = findPalindrome(s, i, i);
auto index2 = findPalindrome(s, i, i + 1);
index = index1.second - index1.first > index.second - index.first ? index1 : index;
Expand All @@ -21,7 +21,7 @@ class Solution {

private:
static pair<int, int> findPalindrome(string s, int left, int right) {
while (left >= 0 && right < s.size() && s[left] == s[right]) {
while (left >= 0 && right < static_cast<int>(s.size()) && s[left] == s[right]) {
left--;
right++;
}
Expand Down
2 changes: 1 addition & 1 deletion src/53-maxSubArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Solution {
int maxSubArray(vector<int>& nums) {
int res = INT_MIN;
int sum = 0;
for (int i = 0; i < nums.size(); i++) {
for (int i = 0; i < static_cast<int>(nums.size()); i++) {
sum += nums[i];
res = max(res, sum);
if (sum < 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/54-spiralOrder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ using namespace std;
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
const int m = static_cast<int>(matrix.size());
const int n = static_cast<int>(matrix[0].size());
vector<int> res;
res.reserve(m * n);
int i = 0, j = 0;
while (res.size() < m * n) {
while (static_cast<int>(res.size()) < m * n) {
while (i >= 0 && i < m && j >= 0 && j < n && matrix[i][j] != INT_MIN) {
res.push_back(matrix[i][j]);
matrix[i][j] = INT_MIN;
Expand Down
5 changes: 3 additions & 2 deletions src/55-canJump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ class Solution {
public:
bool canJump(vector<int>& nums) {
int reach = 0;
for (int i = 0; i < nums.size() && i <= reach; ++i) {
const int n = static_cast<int>(nums.size());
for (int i = 0; i < n && i <= reach; ++i) {
reach = max(reach, i + nums[i]);
if (reach >= nums.size() - 1) return true;
if (reach >= n - 1) return true;
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/6-convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Solution {
return s;
}
vector<vector<char>> m(numRows);
for (int i = 0, row = 0, dir = 1; i < s.size(); i++) {
for (int i = 0, row = 0, dir = 1; i < static_cast<int>(s.size()); i++) {
if (dir == 1) {
m[row++].push_back(s[i]);
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/64-minPathSum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ using namespace std;
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
const int m = grid.size();
const int n = grid[0].size();
const int m = static_cast<int>(grid.size());
const int n = static_cast<int>(grid[0].size());
vector<vector<int>> dp(m, vector<int>(n, 0));
dp[0][0] = grid[0][0];
for (int i = 1; i < m; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion src/66-plusOne.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using namespace std;
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
for (int i = digits.size() - 1; i >= 0; --i) {
for (int i = static_cast<int>(digits.size()) - 1; i >= 0; --i) {
if (digits[i] < 9) {
digits[i]++;
return digits;
Expand Down
4 changes: 2 additions & 2 deletions src/67-addBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class Solution {
public:
string addBinary(string a, string b) {
string res;
int i = a.size() - 1, j = b.size() - 1, carry = 0;
int i = static_cast<int>(a.size()) - 1, j = static_cast<int>(b.size()) - 1, carry = 0;
while (i >= 0 || j >= 0 || carry) {
const int x = i >= 0 ? a[i--] - '0' : 0;
const int y = j >= 0 ? b[j--] - '0' : 0;
const int sum = x + y + carry;
res.push_back(sum % 2 + '0');
res.push_back(static_cast<char>(sum % 2 + '0'));
carry = sum / 2;
}
ranges::reverse(res);
Expand Down
2 changes: 1 addition & 1 deletion src/69-mySqrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Solution {
int left = 1, right = x / 2;
int res = 0;
while (left <= right) {
int mid = left + ((right - left) >> 1);
const int mid = left + ((right - left) >> 1);
if (mid <= x / mid) {
res = mid;
left = mid + 1;
Expand Down
Loading