今天什么都没发生

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

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. DropDownListFor

  2. 第八周读书笔记(人月神话X月亮与六便士)——到底什么才是一个程序员的自我修养?

    写了这么久的读书笔记,涉及到问题大多是一些如何把软件工程做好,如何把自己的职业生涯做好.但总感觉逻辑链上缺了一环,亦即:我们为什么要把软件工程做好,我们成为一名优秀的职业生涯的意义到底在于什么?我觉得 ...

  3. Eclipse中添加对Python的中文支持

    原文链接:http://down.51cto.com/data/751371 首先要确保eclipse编辑器环境的编码为utf8,这个是大前提:其次如果py文件中含有中文字符的话,需要在py文件中对编 ...

  4. 编写可维护的javascript阅读笔记

    格式 变量 变量命名, 采取小驼峰大小写 变量使用名词, 函数前缀为动词 局部变量应统一定义在函数的最上面, 而不是散落在函数的任意角落. 赋初始值的定义在未赋初始值的变量的上面. 我个人建议不使用单 ...

  5. C#的split函数分割

    C#的split函数分割 string str = textBox1.Text; string[] strlist = str.Split("\r\n".ToCharArray() ...

  6. type="radio"样式修改

    input[type=radio],input[type=checkbox] { display: inline-block; vertical-align: middle; width: 20px; ...

  7. vc++如何创建程序-构造和继承

    #include<iostream.h>//定义一个动物类class Animal{public: void eat();//添加方法 { cout<<"animal ...

  8. Oracle语句执行顺序

  9. KEEPALIVED+LVS+MYCAT实现MYSQL高可用环境搭建

    一.安装keepalived和ipvsadm 注意:ipvsadm并不是lvs,它只是lvs的配置工具. 为了方便起见,在这里我们使用yum的安装方式 分别在10.18.1.140和10.18.1.1 ...

  10. SQL中group by的理解

    1.group by A,B,C的分组顺序与汇总: group by A,B,C的分组顺序与order by A,B,C的排序一样.即先按A,如果A一样,则再按B,以此类推. 而数据将在最后指定的分组 ...