今天登陆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. 【PostgreSQL-9.6.3】函数(3)--日期和时间函数

    在PostgreSQL中,DATE.TIME.TIMESTAMP是三种不同的数据类型.DATE表示日期类型,格式为YYYY-MM-DD或YYYYMMDD:TIME表示时间类型,格式为hh:mi:ss: ...

  2. oracle从入门到精通复习笔记续集之PL/SQL(轻量版)

    复习内容: PL/SQL的基本语法.记录类型.流程控制.游标的使用. 异常处理机制.存储函数/存储过程.触发器. 为方便大家跟着我的笔记练习,为此提供数据库表文件给大家下载:点我下载 为了要有输出的结 ...

  3. Embedded之Makefile

    1 Header files The header files are empty, so you can create them with touch: $ touch a.h $ touch b. ...

  4. ANN:DNN结构演进History—RNN

    前言: CNN在图像处理领域的极大成功源于CNN的二维递进映射结构,通过训练多层卷积核来进行特征提取函数训练,在二维图像的稀疏表达和语义关联分析方面有天生的结构优势.而涉及时序问题的逻辑序列分析-边长 ...

  5. DataGridView 单击赋值

    void dataGridView1_Click(object sender, EventArgs e) { M_int_judge = ; btnSave.Enabled = true; btnSa ...

  6. c# md5加密封装

    /// <summary> /// md5加密字符串 /// </summary> /// <param name="str">需要加密的字符串 ...

  7. S-HR薪酬项目与核算表的关系

  8. eas之获取单据编码规则

    //获取单据编码规则  /*** @Title: getNumber* @Description: TODO(获取单据编码规则)*               <p>* @date 201 ...

  9. 微信小程序-蓝牙连接

    最近的项目需要使用小程序的蓝牙功能与硬件设备进行连接相互传送数据指令,联调过程中发现一些问题,于是想着记录下来,方便以后查看! 1.0一般使用蓝牙功能肯定是想连接某一个蓝牙设备,所以需要知道这个蓝牙设 ...

  10. 七夕心形demo

    from turtle import * pensize(1) pencolor('red') fillcolor('pink') speed(5) up() goto(-30, 100) down( ...