今天什么都没发生

=================================================

leetcode118 https://leetcode.com/problems/pascals-triangle/?tab=Description

leetcode169 https://leetcode.com/problems/majority-element/?tab=Description

leetcode189 https://leetcode.com/problems/rotate-array/?tab=Description

===================================================

118说的是
给你n,输出n行的杨辉三角

比如输入5,输出下面这个

[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]

我的思路
没什么思路,模拟题,看代码

 class Solution {
public:
vector<vector<int> > generate(int numRows) {
int &n=numRows;
vector<vector<int> > aim(n);
if(n==)return aim;
aim[].push_back();
for(int i=;i<n;i++){
aim[i].push_back();
for(int j=;j<i;j++){
aim[i].push_back(aim[i-][j]+aim[i-][j-]);
}
aim[i].push_back();
}
return aim;
}
};

118

==============================================

169说的是
求众数,所谓众数,是有一个数字,他出现在n个数中超过2/n次,题目保证n>0且这个数字一定存在

我的思路
讲道理我没什么好的思路,那就nlogn排个序,然后线性的扫一遍吧。。。。

 class Solution {
public:
int majorityElement(vector<int>& nums) {
int n=nums.size();
vector<int> mynums(nums);
sort(mynums.begin(),mynums.end(),less<int>());
int cnt=;
for(int i=;i<n;i++){
if(mynums[i]!=mynums[i-]){
if(cnt>(n/))
return mynums[i-];
cnt=;
}else cnt++;
}
return mynums[n-];
}
};

169 nlogn

天,这题神他妈真的有O(n)的算法。。。。秘诀在于该题特殊的“众数定义”,不仅仅是最多的,而且他的“数量超过一半”。。。。代码如下

 public class Solution {
public int majorityElement(int[] num) { int major=num[], count = ;
for(int i=; i<num.length;i++){
if(count==){
count++;
major=num[i];
}else if(major==num[i]){
count++;
}else count--; }
return major;
}
}

O(n)

厉害厉害。。。。

好吧,这不是分析的结果,这道题有专门的算法,叫  Boyer-Moore Majority Vote algorithm 学习了。。。

=================================================

189说的是
给你n个数字,向循环右移k次,输出操作后的数组。(要求使用三种以上的方法)
For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].

我的思路
额。。。。一开始就会想不用多余的空间,就是从一个点开始循环交换,这算是一道思路题吧。。。直接给个我认为比较优的解好了。。。

 class Solution {
public:
void rotate(vector<int>& nums, int k) {
int n=nums.size();
if(!n)return;
k=k%n;
int temp=__gcd(n,k);
for(int i=;i<temp;i++){
int ptr=i+k;
while(ptr!=i){
nums[i]^=nums[ptr];
nums[ptr]^=nums[i];
nums[i]^=nums[ptr];
ptr=ptr+k;
ptr-=ptr>=n?n:;
}
}
}
};

189

好吧这是一道思路题,考查的是逻辑思维,不是代码能力。。。。我看到了几种都挺不错的解法
第一种就是重新开个数组,直接暴力O(n)扫着移过去

。后面的解法比较巧妙

2,reverse前(n-k)个和后k个,然后再整体reverse

3,交换前k个和后k个,这样,前k个位置定下来了,对于后n-k个我们用同样的操作循环来弄

厉害了。。。

2017-3-6 leetcode 118 169 189的更多相关文章

  1. 2017/11/22 Leetcode 日记

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

  2. 2017/11/21 Leetcode 日记

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

  3. 2017/11/13 Leetcode 日记

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

  4. 2017/11/20 Leetcode 日记

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

  5. 2017/11/9 Leetcode 日记

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

  6. 2017/11/7 Leetcode 日记

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

  7. 2017/11/6 Leetcode 日记

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

  8. 2017/11/5 Leetcode 日记

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

  9. 2017/11/3 Leetcode 日记

    2017/11/3 Leetcode 日记 654. Maximum Binary Tree Given an integer array with no duplicates. A maximum ...

随机推荐

  1. C - Twins(贪心)

    Problem description Imagine that you have a twin brother or sister. Having another person that looks ...

  2. ts中类的继承

    定义类 class Person { name: string; //属性 constructor(_name: string) { this.name = _name; } //构造函数 sayHe ...

  3. Redis之Ubuntu下Redis集群搭建

    安装redis 首先下载redis $ wget http://download.redis.io/releases/redis-4.0.10.tar.gz $ .tar.gz $ cd redis- ...

  4. Python 之 风格规范(Google )

    开头先mark一下网址:goole官网 任何语言的程序员,编写出符合规范的代码,是开始程序生涯的第一步. 一.分号 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 二.行长度 每行不超过80个 ...

  5. 使用Flask和Bootstrap构建博客系统(1) - 准备篇

    技术栈 macOS10.12.5 Python2.7.13 Bootstrap4.0.0-beta.2 virtualenv virtualenvwrapper 安装Python2.7.13 下载Bo ...

  6. PythonGIS可视化—Matplot basemap工具箱

    原文链接:http://www.douban.com/group/topic/32821988/ 原文链接:http://www.cnblogs.com/vamei/archive/2012/09/1 ...

  7. DDoS攻击与防范策略

    DDoS(Distributed Denial of Service,分布式拒绝服务)攻击的主要目的是让指定目标无法提供正常服务,甚至从互联网上消失,是目前最强大.最难防御的攻击之一. 按照发起的方式 ...

  8. sql_2

    编辑表结构ALTER TABLE `sp_account_trans`     MODIFY COLUMN `TRANS_DESC` varchar(81) CHARACTER SET utf8 CO ...

  9. vc++如何创建程序-构造函数02

    1.若忘记了赋值,出现运行结果是很大的负值(因为我们定义的x与y这两个成员变量存储在内存中是一个随机的值) 当我们调用时,随机输出. //包含输入输出的头文件#include<iostream. ...

  10. Ubuntu 16.04 安装python3.6 环境并设置为默认

    1.添加python3.6安装包,并且安装 sudo apt-get install software-properties-common 2.下载python3.6 sudo add-apt-rep ...