LeetCode Weekly Contest 32
581. Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.
You need to find the shortest such subarray and output its length.
Input: [2, 6, 4, 8, 10, 9, 15]
Output: 5
Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.
排好序,然后对比一下就行了
class Solution {
public:
int findUnsortedSubarray(vector<int>& nums) {
int n=nums.size();
vector<int> nums2;
nums2.insert(nums2.end(),nums.begin(),nums.end());
sort(nums2.begin(),nums2.end());
int st=,ed=n-;
while(st!=n&&nums2[st]==nums[st]) st++;
while(ed!=-&&nums2[ed]==nums[ed]) ed--;
if(st==n) return ;
return ed-st+;
} };
582. Kill Process
删除一个树上的节点,求出所有子节点编号
真的很生疏了,树都不知道怎么遍历了
由于可能编号开的很大,因此用map作下映射,不说数据范围真坑,一直TLE(leetcode有TLE?),最后改了下数组大小AC了
Input:
pid = [1, 3, 10, 5]
ppid = [3, 0, 5, 3]
kill = 5
Output: [5,10]
Explanation:
3
/ \
1 5
/
10
Kill 5 will also kill 10.
class Solution {
public:
vector<int> ans;
map<int,int> mp;
map<int,int> fmp;
int tot;
vector<int> g[];
void dfs(int i){
ans.push_back(fmp[i]);
for(int j=;j<g[i].size();j++){
dfs(g[i][j]);
}
}
vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
int n=pid.size();
int i,st;
tot=;
for(i=;i<n;i++){
if(!mp[ppid[i]]){
mp[ppid[i]]=tot;
fmp[tot]=ppid[i];
tot++;
}
if(!mp[pid[i]]){
mp[pid[i]]=tot;
fmp[tot]=pid[i];
tot++;
}
int u=mp[ppid[i]],v=mp[pid[i]];
g[u].push_back(v);
}
dfs(mp[kill]);
return ans; }
};
583. Delete Operation for Two Strings
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string.
Input: "sea", "eat"
Output: 2
Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
求下最长公共序列,注意是序列不是子串,因为对于abcab,abab这样的情况,公共子串就无法解决,求完之后减一下就可以了
class Solution {
public:
int Max(int a,int b)
{
return (a>b)?a:b;
}
int creatDp(string s1,string s2,int *dp)
{
int len1 = s1.length();
int len2 = s2.length();
//int *dp = new int[len1*len2];
//先求出第一行
for(int j = ;j<len2;j++)
{
if(s1[] == s2[j]){
*(dp+*len2+j) = ;
for(;j<len2;j++)
*(dp+*len2+j) = ;
break;
}
else
*(dp+*len2+j) = ;
}
//然后求第一列
for(int i = ;i<len1;i++)
{
if(s1[i] == s2[]){
*(dp+i*len2+) = ;
for(;i<len1;i++)
*(dp+i*len2+) = ;
break;
}
else
*(dp+i*len2+) = ;
}
//求其他的数据
for(int i =;i<len1;i++)
{
for(int j =;j<len2;j++)
{
if(s1[i] == s2[j])
*(dp+i*len2+j) = *(dp+(i-)*len2+(j-))+;
else
*(dp+i*len2+j) = Max(*(dp+(i-)*len2+j),*(dp+i*len2+j-));
}
}
return dp[len1*len2-];
}
int minDistance(string word1, string word2) {
int len1 = word1.length();
int len2 = word2.length();
int *dp = new int[len1*len2];
int w=creatDp(word1, word2,dp);
return len1-w+len2-w;
}
};
587. Erect the Fence
裸凸包问题,找了几个模板都没套进去,重载操作符没法用,23333,leetcode真坑
LeetCode Weekly Contest 32的更多相关文章
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- 【LeetCode Weekly Contest 26 Q3】Friend Circles
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...
- 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
- 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
随机推荐
- gitlab的完全卸载
一:先停止gitlab gitlab-ctl stop 二:卸载gitlab部分(之前我是rpm安装的,这里rpm卸载) rpm -e gitlab-ce 三:发现系统进程还有一个gitlab的进 ...
- 没有-jackson相关依赖会抛出如下异常--------在spring官方文档有解释
<!--jackson相关依赖--><!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackso ...
- 微信JSAPI分享朋友圈调试经验:invalid signature签名错误排查
.invalid signature签名错误.建议按如下顺序检查: 1.确认签名算法正确,可用http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapi ...
- 关于The specified Android SDK Build Tools version (26.0.2) is ignored, as it is below the minimum...
今天将项目迁移到另一台笔记本,进行build出现以下问题,导致build失败 The specified Android SDK Build Tools version (26.0.2) is ign ...
- k8s 1.12.6版的kubeadm默认配置文件
这个对于提高安装配置的便捷性,相当有帮助. 命令如下: kubeadm config print-default 输出如下: apiEndpoint: advertiseAddress: 1.2.3. ...
- webpack学习笔记--区分环境
为什么需要区分环境 在开发网页的时候,一般都会有多套运行环境,例如: 在开发过程中方便开发调试的环境. 发布到线上给用户使用的运行环境. 这两套不同的环境虽然都是由同一套源代码编译而来,但是代码内容却 ...
- C# Enum,Int,String的互相转换 [转]
C# Enum,Int,String的互相转换 Enum为枚举提供基类,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用 Int32.编程语言通常提供语法来声明由一组已命名 ...
- 计算1至n中数字X出现的次数【math】
转自: nailperry 一.1的数目 编程之美上给出的规律: 1. 如果第i位(自右至左,从1开始标号)上的数字为0,则第i位可能出现1的次数由更高位决定(若没有高位,视高位为0),等于更高位数字 ...
- 关于文件I/o的原子操作
[摘自<Linux/Unix系统编程手册>] 所有系统调用都是以原子操作方式执行的.这里是指内核保证了某系统调用中的所有步骤会作为独立操作而一次性执行,其间不会为其它进程或线程所中断. 原 ...
- java运算符-逻辑、三元运算符
1.逻辑运算符 逻辑运算符,它是用于布尔值进行运算的,运算的最终结果为布尔值true或false. 运算符 运算规则 范例 结果 & 与 false&true False | 或 fa ...