leetcode
刷题打卡
「已注销」
这个作者很懒,什么都没留下…
展开
-
1.1. 删除排序数组中的重复项(数组)
class Solution { /*利用双指针思路*/public: int removeDuplicates(vector<int>& nums) { if (nums.size() == 0) return 0; int slow = 0; for (int fast = 1; fast < nums.size(); fast++) { if (nums[slow原创 2021-04-06 22:20:17 · 71 阅读 · 0 评论 -
二、玩转双指针
二、玩转双指针双指针主要用于遍历数组,两个指针指向不同的元素,从而协同完成任务。也可以延伸到多个数组的多个指针。若两个指针指向同一数组,遍历方向相同且不会相交,则也称为滑动窗口(两个指针包围的区域称为窗口),经常用于区间搜索。若两个指针指向同一数组,但是遍历方向相反,则可用来进行搜索,待搜索的数组往往是排好序的。1 Two sum(167)https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/submissions/数组原创 2021-02-03 19:21:23 · 152 阅读 · 0 评论 -
一、贪心算法
一、贪心算法保证每次操作都是局部最优的,从而使最后得到的结果是全局最优的。1 分配问题1.1 Assign Cookies(Easy) 455.分发饼干https://leetcode-cn.com/problems/assign-cookies/题解:饥饿度最小的孩子最容易吃饱,所以先考虑这个孩子,把大于等于这个孩子饥饿度的、且大小最小的饼干给这个孩子。所以先对孩子和饼干按照从小到大排序然后循环筛选。class Solution {public: int findContentCh原创 2021-01-30 19:52:37 · 135 阅读 · 1 评论 -
9. 回文数
9. 回文数比较暴力的解法,把反转后的数字求出来(有可能超过int的边界值,所以用long定义)。然后和原始的数进行判断。class Solution {public: bool isPalindrome(int x) { if (x < 0) return false; if (x < 10) return true; long num = 0; int old = x; while (x){原创 2021-01-10 13:56:10 · 70 阅读 · 0 评论 -
8. 字符串转换整数 (atoi)
8. 字符串转换整数 (atoi)主要边界,然后进行循环。class Solution {public: int myAtoi(string s) { int length_s = s.size(), i = 0; // i代表索引 if (length_s == 0) return 0; int f = 1, upperboundary = INT_MAX / 10; // f:1代表+,-1代表- int res = 0;原创 2021-01-10 13:42:03 · 80 阅读 · 0 评论 -
7. 整数反转
7. 整数反转https://leetcode-cn.com/problems/reverse-integer/这里注意一下第6行和第10行,因为10行的结果为加之后的结果所以是和31次方的边界值对比,6行为最后结果×10之前的结果,所以要和31次方的边界值去掉个位(除以10)的值进行比较。class Solution {public: int reverse(int x) { int res = 0; while (x){ if (re原创 2021-01-10 12:55:55 · 82 阅读 · 0 评论 -
6.Z 字形变换
6.Z 字形变换采用了一个hash的思想,比如要求三行,可以理解为最后的结果为三个字符串连接起来。![image.png](https://img-blog.csdnimg.cn/img_convert/303aaf50bad9b49673eb9af6aa6a666e.png#align=left&display=inline&height=87&margin=[object Object]&name=image.png&originHeight=87&or原创 2021-01-09 20:34:35 · 92 阅读 · 0 评论 -
5. 最长回文子串
5. 最长回文子串class Solution {public: string longestPalindrome(string s) { int length_s = s.size(); if (length_s == 0){ return s; } int dp[length_s][length_s], maxlength = 1, begin = 0; for (int i = 0; i原创 2021-01-09 20:33:03 · 76 阅读 · 0 评论 -
4. 寻找两个正序数组的中位数
4. 寻找两个正序数组的中位数https://leetcode-cn.com/problems/median-of-two-sorted-arrays/参考了合并两个有序链表的思想。class Solution {public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { int length1 = nums1.size();原创 2021-01-09 20:32:21 · 76 阅读 · 0 评论 -
3.无重复字符的最长子串
3.无重复字符的最长子串https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/submissions/参考连接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/solution/longest-substring-without-repeating-characters-b-2/个人理解:相原创 2021-01-09 20:31:39 · 62 阅读 · 0 评论 -
2.两数相加(中等)
2.两数相加(中等)https://leetcode-cn.com/problems/add-two-numbers/个人理解:(1)两个链表有先结束和后结束。(2)相加是有进位的如果两个链表最后两数相加有进位,则需要把这个进位的值考虑进去,需要在循环判断的时候考虑这一点。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNo原创 2021-01-09 20:30:19 · 88 阅读 · 0 评论