今天登陆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的更多相关文章

  1. 2017/11/6 Leetcode 日记

    2017/11/6 Leetcode 日记 344. Reverse String Write a function that takes a string as input and returns ...

  2. MyEclipse 2017 CI 10 发布(附下载)

    挑战全年最低价!MyEclipse线上狂欢仅剩最后3天!立即抢购>> 2017 CI 10主要是一个错误修复版本,这个版本为Angular和TypeScript工具提供了重要的修复,并为I ...

  3. 2017/11/22 Leetcode 日记

    2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice ex ...

  4. 2017/11/21 Leetcode 日记

    2017/11/21 Leetcode 日记 496. Next Greater Element I You are given two arrays (without duplicates) num ...

  5. 2017/11/13 Leetcode 日记

    2017/11/13 Leetcode 日记 463. Island Perimeter You are given a map in form of a two-dimensional intege ...

  6. 2017/11/20 Leetcode 日记

    2017/11/14 Leetcode 日记 442. Find All Duplicates in an Array Given an array of integers, 1 ≤ a[i] ≤ n ...

  7. 2017/11/9 Leetcode 日记

    2017/11/9 Leetcode 日记 566. Reshape the Matrix In MATLAB, there is a very useful function called 'res ...

  8. 2017/11/7 Leetcode 日记

    2017/11/7 Leetcode 日记 669. Trim a Binary Search Tree Given a binary search tree and the lowest and h ...

  9. 2017/11/5 Leetcode 日记

    2017/11/5 Leetcode 日记 476. Number Complement Given a positive integer, output its complement number. ...

随机推荐

  1. nprogress进度条和ajax全局事件

    nprogress和ajax全局事件 nprogress 官方网站:http://ricostacruz.com/nprogress/ 下载地址:https://github.com/rstacruz ...

  2. 努比亚(nubia) V18 NX612J 解锁BootLoader 并刷入recovery ROOT

    recovery制作来自绯色玻璃 努比亚(nubia) V18 NX612J 解锁BootLoader 并刷入recovery ROOT 工具下载链接:https://pan.baidu.com/s/ ...

  3. Visual Basic for Application

    Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'The note of Visual Basic for Applicati ...

  4. REST ful

    前后端分离.面向资源.无状态: 请求包含全部信息. 什么是 REST? 下面六条准则定义了一个 REST 系统的特征: 客户-服务器(Client-Server),提供服务的服务器和使用服务的客户需要 ...

  5. vue 上滑加载更多

    移动端网页的上滑加载更多,其实就是滑动+分页的实现. <template> <div> <p class="footer-text">--{{f ...

  6. Java通过接口实现匿名类的实例

    package com.chase.test; /** * 通过接口实现匿名类的实例 * * @author Chase * * @date 2013-10-18 下午04:28:17 * * @ve ...

  7. day37-1 面向对象高阶

    目录 面向对象高阶 isinstance issubclass 反射(自省) 模块的使用 放在类的使用 call 面向对象高阶 isinstance 判断是否为实例化对象,以后可以用来取代type 和 ...

  8. phpMyAdmin使用教程

    ---恢复内容开始--- wamp中自带了管理MySQL的phpMyAdmin,可用来本机测试,服务器维护,虚拟主机用户管理MySQL. 登录需记住servername,username,passwo ...

  9. python爬虫05 | 年轻人,不会正则表达式你睡得着觉?有点出息没有?

    现在 你已经会使用 python 模拟浏览器 进行一些 Http 的请求了 那么请求完之后 服务器返回给我们一堆源代码 我们可不是啥都要的啊 我们是有原则的 我们想要的东西 怎么能一股脑的啥都往自己兜 ...

  10. datawhale爬虫实训4

    DataWhale-Task4(爬取丁香园2) 任务:使用lxml爬虫帖子相关的回复与部分用户信息(用户名,头像地址,回复详情) 难点:需要登录才能看到所有回复 浏览器登录上去,查看cookies信息 ...