今天登陆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. 【HTTP】长连接和短连接

    1. HTTP协议与TCP/IP协议的关系 HTTP的长连接和短连接本质上是TCP长连接和短连接.HTTP属于应用层协议,在传输层使用TCP协议,在网络层使用IP协议.IP协议主要解决网络路由和寻址问 ...

  2. C语言笔记(一)

    笑话一枚:程序员 A:“哥们儿,最近手头紧,借点钱?”程序员 B:“成啊,要多少?”程序员 A:“一千行不?”程序员 B:“咱俩谁跟谁!给你凑个整,1024,拿去吧.” =============== ...

  3. Swift Pointer 使用指南

    Overview C Syntax Swift Syntax Note const Type * UnsafePointer<Type> 指针可变,指针指向的内存值不可变. Type * ...

  4. Memcached 之取模与哈希算法命中率实验

    当5台memcache服务器中有一台宕机时的命中率实验. 一.php实现代码 1. config.php $server = array( "A" => array(&quo ...

  5. GFS分布式文件系统环境部署与管理

    Gluster分布式文件系统 准备环境五台虚拟机 创建/gfs目录,把软件包全部拷贝目录 把yum仓库的源放进bak下才能执行以下脚本 并指定主机名这四台机器都要执行脚本 [root@localhos ...

  6. 【剑指Offer】58、对称的二叉树

      题目描述:   请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的.   解题思路:   本题判断一棵树是不是对称的,和第18题可以对比分 ...

  7. 基于分布式框架 Jepsen 的 X-Cluster 正确性测试

    转自:https://mp.weixin.qq.com/s/iOe1VjG1CrHalr_I1PKdKw 原创 2017-08-27 严祥光(祥光) 阿里巴巴数据库技术 1 概述 AliSQL X-C ...

  8. 自定义UEditor右键菜单

    //打开右键菜单功能 ,enableContextMenu: true //右键菜单的内容,label留空支持国际化,否则以此配置为准 //,contextMenu:[ // { // label:' ...

  9. Q&A to prepare interview of HSBC

    1.How do you keep updating lastest IT knowledge? 1).keep an eye on current project technology evetho ...

  10. 彻底禁用chrome请停用以开发者模式运行的扩展程序弹框

    首先上图 怎么解决呢? 进入安装目录-->下图目录(一串数字的目录) 2. 找到chrome.dll 3.下载patch.exe   下载网址 https://itdocs.pipipan.co ...