今天登陆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. android黑科技系列——防自动抢红包外挂原理解析

    一.前言 春节过年发个红包本来就是为了讨个喜庆,朋友亲戚之间的关系交流,但是现在随着技术变革,抢红包插件越来越多,导致现在不太愿意发红包了,特别是在一个多人群里,潜水的非常多,但是丢个红包瞬间就没了, ...

  2. ★Java语法(一)——————————标识符

    1.定义:用来标识类名.变量名.方法名.数组名.文件名的有效字符序列: 2.命名规则:a 由字母.数字._(下划线)和$(美元符号)组成 b 数字不能作为第一个字符 c 不能是Java中的关键字和保留 ...

  3. AMQP及RabbitMQ

    AMQPAMQP协议是一个高级抽象层消息通信协议,RabbitMQ是AMQP协议的实现.它主要包括以下组件: 1.Server(broker): 接受客户端连接,实现AMQP消息队列和路由功能的进程. ...

  4. Windows Phone 应用程序的生命周期(二)

    一.App.xaml.cs /// <summary> /// Application 对象的构造函数. /// </summary> public App() { // 未捕 ...

  5. [iOS Reverse]logify日志追踪,锁定注入口-控制台查看

    前言 logify是theos的一个组件,路径是: /opt/theos/bin/logify.pl 我们还是以微信红包为例子,根据[iOS Hacking]运行时分析cycript得到的入口文件: ...

  6. spring IOC bean中注入bean

    俩个实体 package com.java.test4; /** * @author nidegui * @create 2019-06-22 14:45 */ public class People ...

  7. Html-如何正确给table加边框

    一般来说,给表格加边框都会出现不同的问题,以下是给表格加边框后展现比较好的方式 <style> table,table tr th, table tr td { border:1px so ...

  8. Cache占用过多内存导致Linux系统内存不足问题排查

    问题描述 Linux服务器内存使用量超过阈值,触发报警. 问题排查 首先,通过free命令观察系统的内存使用情况,显示如下: total used free shared buffers cached ...

  9. 56.doc values

    主要知识点 doc values     搜索的时候,要依靠倒排索引:在54小节中写到在聚合排序的时候如果仅仅依靠倒排索引的话是不能得出准确的结果的,需要依靠正排索引,所谓的正排索引,其实就是doc ...

  10. PAT 1080. Graduate Admission

    It is said that in 2013, there were about 100 graduate schools ready to proceed over 40,000 applicat ...