自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(46)
  • 收藏
  • 关注

原创 LeetCode #203 移除链表元素

生成一个指向链表第一个节点的头节点,避免单独处理。注意对指针的手动释放。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x.

2021-12-12 02:00:57 247

原创 剑指Offer #29 顺时针打印矩阵

注意矩阵为空的情况。class Solution { public: vector<int> spiralOrder(vector<vector<int>>& matrix) { if (matrix.size() == 0 || matrix[0].size() == 0) { return {}; } vector<int> res; int .

2021-12-11 14:47:02 351

原创 LeetCode #59 螺旋矩阵 II

解法与LeetCode #54 螺旋矩阵类似,记录上下左右边界的位置。class Solution { public: vector<vector<int>> generateMatrix(int n) { vector<vector<int>> res(n, vector<int>(n, 0)); int up = 0; int down = n - 1; int..

2021-12-11 14:31:33 251

原创 LeetCode #54 螺旋矩阵

记录上下左右边界的位置,按照螺旋的顺序依次填充结果数组res。若上下或左右边界交错,表示填充完毕,退出即可。class Solution { public: vector<int> spiralOrder(vector<vector<int>>& matrix) { vector<int> res; int m = matrix.size(); // 行数 int n = ma..

2021-12-11 09:48:35 127

原创 LeetCode #9 回文数

方法一:使用哈希表存储每一位的数字。class Solution { public: bool isPalindrome(int x) { if (x < 0) { return false; } unordered_map<int, int> res; int k = 1; while (x / 10 != 0) { res[k++] = ..

2021-12-10 20:10:40 276

原创 LeetCode #414 第三大的数

方法一:注意lambda表达式的使用。class Solution { public: int thirdMax(vector<int>& nums) { sort(nums.begin(), nums.end(), [](int x, int y) { return x >= y; }); if (nums.size() <= 2) { return nums[0]; } .

2021-12-10 19:32:30 70

原创 LeetCode #119 杨辉三角 II

class Solution { public: vector<int> getRow(int rowIndex) { vector<vector<int>> res; for (int i = 1; i <= rowIndex + 1; i++) { vector<int> a(i, 1); for (int j = 1; j < i - 1; j...

2021-12-10 19:12:37 187

原创 十大经典排序算法

目录0.算法概述1.冒泡排序2.快速排序3.插入排序4.选择排序5.希尔排序6.归并排序7.堆排序8.计数排序9.桶排序10.基数排序0.算法概述十种常见排序算法可以分为两大类:比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O(nlogn),因此也称为非线性时间比较类排序。非比较类排序:不通过比较来决定元素间的相对次序,它可以突破基于比较排序的时间下界,以线性时间运行,因此也称为线性时间非比较类排序。1.冒泡排序冒泡排序是一种简单的排序算法。它重复地走访过要排序.

2021-12-10 15:24:31 69

原创 LeetCode #118 杨辉三角

class Solution { public: vector<vector<int>> generate(int numRows) { vector<vector<int>> res; for (int i = 1; i <= numRows; i++) { vector<int> a(i, 1); for (int j = 1; j &lt...

2021-12-10 15:04:48 103

原创 LeetCode #66 加一

分三种情况进行分析:1.末位不为9,直接末位加一即可。2.末位有几位都是9,从后往前找到第一个非9的数字加一,将后面置为0。3.每一位都是9,直接生成一个新的数组,长度比原来多1,第一位置为1,后面置为0。class Solution { public: vector<int> plusOne(vector<int>& digits) { for (int i = digits.size() - 1; i >= 0; i--) {.

2021-12-10 14:51:29 113

原创 LeetCode #904 水果成篮

滑动窗口核心思想是利用滑动窗口不断向前试探,如果试探发现水果种类超过2,就更新left。用num表示当前水果的种类,如果num为1,那么下一个水果取什么都可以,根据下一个水果的种类更新num和type;如果num为2,若下一个水果和前面的种类一致,则继续向前,若不一致,那么就将left移动到可能的最靠右的位置。class Solution { public: int totalFruit(vector<int>& fruits) { if (frui..

2021-12-10 09:46:18 83

原创 LeetCode #209 长度最小的子数组

滑动窗口固定模板void slidwindow(vector<int> nums){ int left = 0, right = 0; while(right < nums.size()) { ...//扩大右边界并更新窗口状态 right++; while(需要收缩)//窗口到达什么状态需要收缩 { ...//缩小左边界并更新窗口状态 left+.

2021-12-10 09:01:17 74

原创 LeetCode #16 最接近的三数之和

双指针首先固定一个数,剩余两个数可以用双指针限定范围。class Solution { public: int threeSumClosest(vector<int>& nums, int target) { if (nums.size() == 3) { return accumulate(nums.begin(), nums.end(), 0); } sort(nums.begin(), nu.

2021-12-09 21:51:48 373

原创 LeetCode #977 有序数组的平方

方法一:暴力方法。先将每个元素变成平方,然后排序。class Solution { public: vector<int> sortedSquares(vector<int>& nums) { for (int i = 0; i < nums.size(); i++) { nums[i] = nums[i] * nums[i]; } sort(nums.begin(), nums.

2021-12-09 17:40:12 359

原创 LeetCode #844 比较含退格的字符串

方法一:利用栈的特性处理字符串。class Solution { public: bool backspaceCompare(string s, string t) { return processing(s) == processing(t); } string processing(string str) { string ret; for (auto ch : str) { if (ch !=..

2021-12-09 17:27:25 65

原创 LeetCode #283 移动零

方法一:利用双指针将所有非零元素排列,然后将后面的位置补零。class Solution { public: void moveZeroes(vector<int>& nums) { int slow = 0; int fast = 0; while (fast < nums.size()) { if (nums[fast] != 0) { nums[slow.

2021-12-09 16:48:34 178

原创 LeetCode #26 删除有序数组中的重复项

快慢指针写法一:class Solution { public: int removeDuplicates(vector<int>& nums) { if (nums.size() <= 1) { return nums.size(); } int slow = 0; int fast = 0; while (fast < nums.size()) {..

2021-12-09 16:28:51 152

原创 LeetCode #27 移除元素

方法1:从头开始遍历,每次遇到指定元素,就从数组末端取元素将其覆盖。class Solution { public: int removeElement(vector<int>& nums, int val) { int len = nums.size(); for (int i = 0; i < len; i++) { if (nums[i] == val) { len-..

2021-12-09 16:09:54 174

原创 LeetCode #367 有效的完全平方数

class Solution { public: bool isPerfectSquare(int num) { int left = 1; int right = num; while (left <= right) { // 避免int越界 int mid = left + (right - left) / 2; int t = num / mid; .

2021-12-08 22:28:39 146

原创 LeetCode #34 在排序数组中查找元素的第一个和最后一个位置

class Solution { public: vector<int> searchRange(vector<int>& nums, int target) { if (nums.size() == 0) { return {-1, -1}; } vector<int> res = {-1, -1}; int left = 0; int ri..

2021-12-08 22:15:46 175

原创 LeetCode #35 搜索插入位置

class Solution { public: int searchInsert(vector<int>& nums, int target) { int left = 0; int right = nums.size() - 1; int mid = -1; while (left <= right) { mid = (left + right) / 2; ..

2021-12-08 21:18:18 73

原创 LeetCode #704 二分查找

class Solution { public: int search(vector<int>& nums, int target) { if (nums.size() == 1) { if (target == nums[0]) { return 0; } else { return -1; } } .

2021-12-08 20:54:51 61

原创 LeetCode #506 相对名次

class Solution { public: vector<string> findRelativeRanks(vector<int>& score) { vector<int> score_1(score); sort(score_1.begin(), score_1.end(), greater<int>()); unordered_map<int, int> ran..

2021-12-02 18:16:10 72

原创 LeetCode #1005 K 次取反后最大化的数组和

class Solution { public: int largestSumAfterKNegations(vector<int>& nums, int k) { sort(nums.begin(), nums.end()); int sum = accumulate(nums.begin(), nums.end(), 0); int cnt_1 = 0; for (int i = 0; i < nu.

2021-11-03 20:07:49 53

原创 LeetCode #1974 使用特殊打字机键入单词的最少时间

class Solution { public: int minTimeToType(string word) { int res = 0; char pos = 'a'; for (int i = 0; i < word.size(); i++) { res += min(abs(word[i] - pos), 26 - abs(word[i] - pos)); pos = word[i].

2021-11-03 19:32:45 72

原创 LeetCode #1013 将数组分成和相等的三个部分

class Solution { public: bool canThreePartsEqualSum(vector<int>& arr) { int sum = accumulate(arr.begin(), arr.end(), 0); if (sum % 3 != 0) { return false; } int avg = sum / 3; int pos_1 .

2021-11-03 19:18:48 52

原创 LeetCode #1217 玩筹码

class Solution { public: int minCostToMoveChips(vector<int>& position) { int m = 0; int n = 0; for (int num : position) { if (num % 2 == 0) { m++; } else { n+.

2021-11-03 19:05:52 70

原创 LeetCode #942 增减字符串匹配

class Solution { public: vector<int> diStringMatch(string s) { vector<int> res; int N = s.size(); int min = 0; int max = N; for (int i = 0; i < N; i++) { if (s[i] == 'I') { .

2021-11-03 00:27:17 69

原创 LeetCode #237 删除链表中的节点

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution { public: void deleteNode(ListNode* node) { node->val = node-&..

2021-11-03 00:21:53 60

原创 LeetCode #69 Sqrt(x)

class Solution { public: int mySqrt(int x) { long left = 0, right = x; int res = -1; while (left <= right) { long mid = (left + right) / 2; long pro = mid * mid; if (pro <= x) { .

2021-11-01 21:03:05 83

原创 LeetCode #524 通过删除字母匹配到字典里最长单词

class Solution { public: string findLongestWord(string s, vector<string>& dictionary) { vector<string> res; for (string x : dictionary) { int j = 0; for (int i = 0; i < s.size(); i++) { .

2021-11-01 20:57:38 67

原创 LeetCode #680 验证回文字符串 Ⅱ

class Solution { public: bool isRight(string s, int left, int right) { for (int i = left, j = right; i <= j; i++, j--) { if (s[i] != s[j]) { return false; } } return true; } .

2021-11-01 20:52:39 57

原创 LeetCode #633 平方数之和

class Solution { public: bool judgeSquareSum(int c) { long left = 0, right = sqrt(c); while (left <= right) { long sum = left * left + right * right; if (sum > c) { right--; .

2021-11-01 20:47:51 66

原创 LeetCode #575 分糖果

class Solution { public: int distributeCandies(vector<int>& candyType) { unordered_map<int, int> hs; for (int c : candyType) { hs[c]++; } return min(hs.size(), candyType.size() / 2); }.

2021-11-01 20:43:15 88

原创 LeetCode #136 只出现一次的数字

class Solution { public: int singleNumber(vector<int>& nums) { int res = 0; for (int num : nums) { res ^= num; } return res; }};

2021-10-30 19:46:14 75

原创 LeetCode #260 只出现一次的数字 III

class Solution { public: vector<int> singleNumber(vector<int>& nums) { unordered_map<int, int> hs; for (int num : nums) { hs[num]++; } vector<int> res; for (const auto&.

2021-10-30 19:40:50 50

原创 LeetCode #860 柠檬水找零

class Solution { public: bool lemonadeChange(vector<int>& bills) { int five = 0; int ten = 0; for (int i = 0; i < bills.size(); i++) { if (bills[i] == 5) { five++; } else .

2021-10-30 19:23:00 55

原创 LeetCode #76 最小覆盖子串

滑动窗口class Solution { public: string minWindow(string s, string t) { unordered_map<char, int> hs, ht; for (int i = 0; i < t.length(); i++) { ht[t[i]]++; } int left = 0; int right = 0; .

2021-10-30 19:07:52 60

原创 LeetCode #142 环形链表 II

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution{public: ListNode *detectCycle(ListNode *head) { ListNode *slow .

2021-10-27 17:41:46 55

原创 LeetCode #88 合并两个有序数组

class Solution { public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { for (int i = m; i < m + n; i++) { nums1[i] = nums2[i - m]; } sort(nums1.begin(), nums1.end()); .

2021-10-26 23:30:07 3694

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除