03 2022 档案

摘要:题目 分析 本题思想不难,就是仿照合并两个升序链表的思路,假设 k 个 链表,最长的链表长度为 n,那么只用将 k 个链表表头元素相比找到最小的 插到新的链表表尾即可。这样的话时间复杂度 为 O(n * k)。我们可以进一步优化,k个链表表头元素比较取最小, 我们可以采用堆,这样 取最小的时间为O( 阅读全文
posted @ 2022-03-28 23:00 Uitachi 阅读(23) 评论(0) 推荐(0) 编辑
摘要:题目 分析 关于合法的括号序列的重要推论 一个合法的括号序列的充要条件 :1. 任意前缀中 '(' 的数量 大于等于 ')' 的数量 2. 左右'(' ')'括号的数量相等 n对括号组成的合法括号序列的个数为 卡特兰数 !!! 以上结论非常重要!!! 代码 yxc 老师版本 1 class Solu 阅读全文
posted @ 2022-03-28 21:18 Uitachi 阅读(30) 评论(0) 推荐(0) 编辑
摘要:题目 分析 这里使用了一个技巧,括号之间的ASCII码相差不超过2就匹配 代码 1 class Solution { 2 public: 3 bool isValid(string s) { 4 stack<char>stk; 5 6 for(auto c : s){ 7 if(c == '(' | 阅读全文
posted @ 2022-03-27 22:15 Uitachi 阅读(18) 评论(0) 推荐(0) 编辑
摘要:题目 分析 我的做法:快慢指针,找到倒数第k+1个节点,然后删除当前节点的下一个节点 y总:先求链表长长度,然后正着遍历找到倒数k+1个节点 代码 自己 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * in 阅读全文
posted @ 2022-03-27 21:42 Uitachi 阅读(16) 评论(0) 推荐(0) 编辑
摘要:题目 分析 和LeetCode15题差不多的思路,可以暴力搜索,但是时间复杂度太高,考虑用双指针降低时间复杂度 也和LeetCode 11题差不多思路 https://leetcode-cn.com/problems/3sum-closest/solution/zui-jie-jin-de-san- 阅读全文
posted @ 2022-03-17 14:47 Uitachi 阅读(21) 评论(0) 推荐(0) 编辑
摘要:题目 分析 直接暴力搜索 代码 1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string>& strs) { 4 string res ; 5 // i 控制列,j 控制l行 6 for(int i = 0;i < 阅读全文
posted @ 2022-03-15 17:07 Uitachi 阅读(18) 评论(0) 推荐(0) 编辑
摘要:题目 分析 受LeetCode 12题目影响,一时没反应过来用哈希存储,虽说难度是简单,还没上一道做的舒服。。。。 这道题目关键在于特判下4的特殊情况,就是后一个字母比当前字母要大时情况要特殊判断下 代码 1 class Solution { 2 public: 3 int romanToInt(s 阅读全文
posted @ 2022-03-15 16:03 Uitachi 阅读(20) 评论(0) 推荐(0) 编辑
摘要:题目 分析 模拟 + 找规律 代码 暴力模拟 1 class Solution { 2 public: 3 string intToRoman(int num) { 4 // string roman[] ={"M","D","C","L","X","V","I"}; 5 int a[] = {30 阅读全文
posted @ 2022-03-08 22:50 Uitachi 阅读(24) 评论(0) 推荐(0) 编辑
摘要:题目 分析 这题出的太精巧了,本人的话只会无脑暴力搜索,但实际上用双指针。其严格证明见: https://www.acwing.com/solution/content/100/ 这题应该是贪心算法,但没看出要贪心什么,没有直觉。。。 代码 1 class Solution { 2 public: 阅读全文
posted @ 2022-03-07 22:50 Uitachi 阅读(14) 评论(0) 推荐(0) 编辑
摘要:题目 分析 代码 LeetCode7.整数反转 的变形 1 class Solution { 2 public: 3 bool isPalindrome(int x) { 4 if(x < 0) return false; 5 int n = 0,o = x; 6 while(x){ 7 if(n 阅读全文
posted @ 2022-03-06 21:12 Uitachi 阅读(20) 评论(0) 推荐(0) 编辑
摘要:题目 分析 简单模拟 代码 1 class Solution { 2 public: 3 int myAtoi(string s) { 4 //首先去除开头空格 5 int k = 0; 6 while(k < s.size() && s[k] == ' '){k++;} 7 8 int flag 阅读全文
posted @ 2022-03-06 11:32 Uitachi 阅读(24) 评论(0) 推荐(0) 编辑
摘要:题目 分析 取出每一位(取余除以十) 代码 1 class Solution { 2 public: 3 int reverse(int x) { 4 long long int r = 0; 5 while(x){ 6 r = r * 10 + x % 10; 7 x /= 10; 8 } 9 i 阅读全文
posted @ 2022-03-05 22:09 Uitachi 阅读(18) 评论(0) 推荐(0) 编辑
摘要:题目 分析 对于这类问题,我们采用dx, dy 方向数组来存储,并且将顺时针方向定义为1,2,3,0 撞墙的情况有两种,一个是出去,一个是走重复的格子 代码 1 #include<iostream> 2 using namespace std; 3 4 const int N = 110; 5 in 阅读全文
posted @ 2022-03-04 15:12 Uitachi 阅读(108) 评论(0) 推荐(0) 编辑
摘要:题目 分析 Manacher 算法算法比较复杂,不考虑,我们只学习中心扩散算法和DP 法一、采用中心扩散法 也就是枚举每个点,找到最长的回文串。回文串分为两种:奇数和偶数长度的回文串。其中奇数的串是关于中心对称的,偶数的串是左右相同。 因为要求最长的回文子串,但是我们并不清楚最长的回文子串的长度是奇 阅读全文
posted @ 2022-03-03 22:19 Uitachi 阅读(24) 评论(0) 推荐(0) 编辑
摘要:题目 分析 此题目转换为求两个有序数组的第 K 小的数 参考 https://www.acwing.com/solution/content/50/ 代码 1 class Solution { 2 public: 3 double findMedianSortedArrays(vector<int> 阅读全文
posted @ 2022-03-01 17:34 Uitachi 阅读(23) 评论(0) 推荐(0) 编辑
摘要:题目 分析 用滑动窗口,就是双指针来做。设 i 为串的尾指针,符合题目要求的串的左侧指针为 j ,且 i 指针向后移动时,j 指针可能不动或者向右移动。 用哈希表来存 从 j 到 i 部分的每个字符的次数,如果 i + 1 重复的话,那么冲突的一定是 位置 i 和 i + 1 部分冲突,这时 j 需 阅读全文
posted @ 2022-03-01 15:05 Uitachi 阅读(16) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示