今天什么都没发生

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

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. [
  2. [1],
  3. [1,1],
  4. [1,2,1],
  5. [1,3,3,1],
  6. [1,4,6,4,1]
  7. ]

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

  1. class Solution {
  2. public:
  3. vector<vector<int> > generate(int numRows) {
  4. int &n=numRows;
  5. vector<vector<int> > aim(n);
  6. if(n==)return aim;
  7. aim[].push_back();
  8. for(int i=;i<n;i++){
  9. aim[i].push_back();
  10. for(int j=;j<i;j++){
  11. aim[i].push_back(aim[i-][j]+aim[i-][j-]);
  12. }
  13. aim[i].push_back();
  14. }
  15. return aim;
  16. }
  17. };

118

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

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

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

  1. class Solution {
  2. public:
  3. int majorityElement(vector<int>& nums) {
  4. int n=nums.size();
  5. vector<int> mynums(nums);
  6. sort(mynums.begin(),mynums.end(),less<int>());
  7. int cnt=;
  8. for(int i=;i<n;i++){
  9. if(mynums[i]!=mynums[i-]){
  10. if(cnt>(n/))
  11. return mynums[i-];
  12. cnt=;
  13. }else cnt++;
  14. }
  15. return mynums[n-];
  16. }
  17. };

169 nlogn

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

  1. public class Solution {
  2. public int majorityElement(int[] num) {
  3.  
  4. int major=num[], count = ;
  5. for(int i=; i<num.length;i++){
  6. if(count==){
  7. count++;
  8. major=num[i];
  9. }else if(major==num[i]){
  10. count++;
  11. }else count--;
  12.  
  13. }
  14. return major;
  15. }
  16. }

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

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

  1. class Solution {
  2. public:
  3. void rotate(vector<int>& nums, int k) {
  4. int n=nums.size();
  5. if(!n)return;
  6. k=k%n;
  7. int temp=__gcd(n,k);
  8. for(int i=;i<temp;i++){
  9. int ptr=i+k;
  10. while(ptr!=i){
  11. nums[i]^=nums[ptr];
  12. nums[ptr]^=nums[i];
  13. nums[i]^=nums[ptr];
  14. ptr=ptr+k;
  15. ptr-=ptr>=n?n:;
  16. }
  17. }
  18. }
  19. };

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. hdu3416 Marriage Match IV 最短路+ 最大流

    此题的大意:给定一幅有向图,求起点到终点(都是固定的)的不同的最短路有多少条.不同的最短路是说不能有相同的边,顶点可以重复.并且图含有平行边. 看了题以后,就想到暴力,但是暴力往往是不可取的.(暴力的 ...

  2. JQuery学习笔记系列(一)----选择器详解

    笔者好长时间没有更新过博客园的笔记了,一部分原因是去年刚刚开始工作一段时间忙碌的加班,体会了一种每天加班到凌晨的充实感,之后闲暇时间了也因为自己懒惰没有坚持记笔记的习惯,现在重新拾起来. 借用古人的一 ...

  3. (转)基于MVC4+EasyUI的Web开发框架经验总结(7)--实现省份、城市、行政区三者联动

    http://www.cnblogs.com/wuhuacong/p/3841338.html 为了提高客户体验和进行一些技术探索,现在正准备把我自己的客户关系管理系统CRM在做一个Web的版本,因此 ...

  4. sqlserver 时间测试

    select * from GropBy where [date] BETWEEN '2010-10' and '2015-10' --从字符串转换日期和/或时间时,转换失败. select * fr ...

  5. 为什么java io流必须得关闭

    当我们new一个java流对象之后,不仅在计算机内存中创建了一个相应类的实例对象.而且,还占用了相应的系统资源,比如:文件句柄.端口.数据库连接等.在内存中的实例对象,当没有引用指向的时候,java垃 ...

  6. C语言基础 (9) 数组指针

    复习 只要把地址拿到就能这么操作.. (这里是合法的地址,不是野指针) 只有定义变量后,此变量的地址才是合法的地址 野指针就是保存没有意义地址的指针变量 操作野指针变量本身不会有任何问题 操作野指针所 ...

  7. jQuery.extend()的合并对象功能

    jQuery.extend( [ deep ], target, object1, [ objectN ] )合并对象到第一个对象 //deep为boolean类型,其它参数为object类型 var ...

  8. HDU2147 - kiki's game 【巴什博弈】

    Recently kiki has nothing to do. While she is bored, an idea appears in his mind, she just playes th ...

  9. nyoj303-序号交换

    序号互换 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 Dr.Kong设计了一个聪明的机器人卡多,卡多会对电子表格中的单元格坐标快速计算出来.单元格的行坐标是由数字编号 ...

  10. nginx 301 重定向设置不含www到www域名

    如果需要将不含www主机的域名301重定向到含www的域名,如将 shwww.net 重定向至 www.shwww.net,nginx 配置指令如下: server { server_name shw ...