2017-3-10 leetcode 229 238 268
今天登陆leetcode突然发现531被锁了,有种占了便宜的感觉哈哈哈!
================================================
leetcode229 Majority Element II
leetcode238 Product of Array Except Self
leetcode268 Missing Number
================================================
229讲的是
给你n个数字,找出所有出现次数超过n/3的数字,要求O(n)time O(1)space
这道题简化版是 leetcode169 解法见2017-3-6
我的思路
类似169,只不过这次因为总数大于[n/3],要维护两个候选数字(最多有两个元素符合要求),每次有新的数字出现的时候,两个计数器都要减。扫一遍得到两个候选数字后,再扫一遍确认数目是否符合要求。
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
int n=nums.size();
int num1=-,num2=-,cnt1=,cnt2=;
for(int i=;i<n;i++){
if(num1==nums[i]){
cnt1++;
}else if(num2==nums[i]){
cnt2++;
}else if(!cnt1){
cnt1++;num1=nums[i];
}else if(!cnt2){
cnt2++;num2=nums[i];
}else{
cnt1--;cnt2--;
}
}
cnt1=cnt2=;
for(int i=;i<n;i++){
if(nums[i]==num1)cnt1++;
if(nums[i]==num2)cnt2++;
}
vector<int> aim;
if(cnt1>n/)aim.push_back(num1);
if(cnt2>n/)aim.push_back(num2);
return aim;
}
};
229
=================================================
238讲的是
给你一个n个数的数组nums[i],输出一个n个数的数组output[i]=nums的累乘/nums[i]。要求不能使用除法,O(n)time
我的思路
求个前缀乘,求个后缀乘,然后乘在一起?????
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int n=nums.size();
vector<int> pre_pro(n,),suf_pro(n,),output(n);
for(int i=n-;i>-;i--){
suf_pro[i]=suf_pro[i+]*nums[i+];
}
output[]=suf_pro[];
for(int i=;i<n;i++){
pre_pro[i]=pre_pro[i-]*nums[i-];
output[i]=suf_pro[i]*pre_pro[i];
}
return output;
}
};
prefix*suffix
击败了7%的用户,恩,意料之中
看一下别人写的
思维被局限了,如果只要求前缀和,我们可以O(1)space来解决,现在多了后缀,也可以,只需要顺序扫的同时把逆序的也处理一下。
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int n=nums.size();
int pre_pro=,suf_pro=;
vector<int> output(n,);
for(int i=;i<n;i++){
output[i]*=pre_pro;
pre_pro*=nums[i];
output[n--i]*=suf_pro;
suf_pro*=nums[n--i];
}
return output;
}
};
O(1)space
击败了26%。。。哪里有问题呢。。。。我看了其他的代码,貌似思路都一样,可能是评测机抽风吧
=================================================
268讲的是
给你n个不同的数字,a[i]属于[0,n],问你缺少了哪个数字
思路
水题。。。n*(n+1)/2减去累加和
class Solution {
public:
int missingNumber(vector<int>& nums) {
int n=nums.size(),tot=;
for(int i=;i<n;i++){
tot+=nums[i];
}
return n*(n+)/-tot;
}
};
268
2017-3-10 leetcode 229 238 268的更多相关文章
- 2017/11/6 Leetcode 日记
2017/11/6 Leetcode 日记 344. Reverse String Write a function that takes a string as input and returns ...
- MyEclipse 2017 CI 10 发布(附下载)
挑战全年最低价!MyEclipse线上狂欢仅剩最后3天!立即抢购>> 2017 CI 10主要是一个错误修复版本,这个版本为Angular和TypeScript工具提供了重要的修复,并为I ...
- 2017/11/22 Leetcode 日记
2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice ex ...
- 2017/11/21 Leetcode 日记
2017/11/21 Leetcode 日记 496. Next Greater Element I You are given two arrays (without duplicates) num ...
- 2017/11/13 Leetcode 日记
2017/11/13 Leetcode 日记 463. Island Perimeter You are given a map in form of a two-dimensional intege ...
- 2017/11/20 Leetcode 日记
2017/11/14 Leetcode 日记 442. Find All Duplicates in an Array Given an array of integers, 1 ≤ a[i] ≤ n ...
- 2017/11/9 Leetcode 日记
2017/11/9 Leetcode 日记 566. Reshape the Matrix In MATLAB, there is a very useful function called 'res ...
- 2017/11/7 Leetcode 日记
2017/11/7 Leetcode 日记 669. Trim a Binary Search Tree Given a binary search tree and the lowest and h ...
- 2017/11/5 Leetcode 日记
2017/11/5 Leetcode 日记 476. Number Complement Given a positive integer, output its complement number. ...
随机推荐
- 域名转让(bcd.cn,tjcpda.com,jscpda.com,fjcpda.com)
因本人工作有所调整,现对所拥有的域名全部转让,有需要者请与我联系,谢谢. bcd.cn tjcpda.com jscpda.com fjcpda.com 联系方式: QQ:1181608198 Ema ...
- 【Oracle】闪回表
语法: FLASHBACK TABLE [ schema. ] table [, [ schema. ] table ]... TO { { { SCN | TIMESTAMP } expr| RES ...
- 【Oracle】数据迁移工具(1):SQL Loader
SQL Loader是一种数据加载工具,可以把外部数据加载到Oracle数据库中.SQL Loader中的参数有很多,本文只在第一部分中列出常用参数.要想运用SQL Loader工具 ,需要我们编辑一 ...
- Deutsch lernen (01)
Was macht Martin? - Um 8.00 Uhr steht martin auf. aufstehen - aufstand - ist aufgestanden 起床 Um 6 Uh ...
- luoguP4719 【模板】动态 DP 线段树+树链剖分+矩阵乘法+动态DP
题目描述 给定一棵n个点的树,点带点权. 有m次操作,每次操作给定x,y,表示修改点x的权值为y. 你需要在每次操作之后求出这棵树的最大权独立集的权值大小. 输入输出格式 输入格式: 第一行,n,m分 ...
- 在vue中写一个跟着鼠标跑的div,div里面动态显示数据
1.div应该放在body里面,这是我放在body中的一个div里面的div <!-- 信息查看层 --> <div class="floatDiv" :styl ...
- vue 路由跳转传参
<li v-for="article in articles" @click="getDescribe(article.id)"> getDescr ...
- appium的等待
在自动化过程中,元素出现受网络环境,设备性能等多种因素影响.因此元素加载的时间可能不一致,从而会导致元素无法定位超时报错,但是实际上元素是正常加载了的,只是出现时间晚一点而已.那么如何解决这个问题呢? ...
- eas之设置编辑界面分录的某一列不可编辑
KDTEntrys.getColumn(“xx”).getStayAttributes().setlokced(true);
- BZOJ 3930 Luogu P3172 选数 (莫比乌斯反演)
手动博客搬家:本文发表于20180310 11:46:11, 原地址https://blog.csdn.net/suncongbo/article/details/79506484 题目链接: (Lu ...