今天什么都没发生

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

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. A - Antipalindrome

    Problem description A string is a palindrome if it reads the same from the left to the right and fro ...

  2. Dalvik虚拟机和JVM的对比

    Dalvik虚拟机与Java虚拟机有着很多相似的特性,都支持GC,JIT,JNI等等.其主要区别在于文件格式以及指令集不同,下面对两者的特性进行比较与讨论. Difference1:文件格式 Dalv ...

  3. ItemArray DataRow对象的RowState和DataRowVersion属性特点

    DataTable.Rows[i].ItemArray DataTable.Rows表示所有行的集合DataTable.Rows[i]加上下标表示其中某一行DataTable.Rows[i].Item ...

  4. 洛谷P1208 [USACO1.3]混合牛奶 Mixing Milk(贪心)

    题目描述 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格是 ...

  5. JavaScript与jquery的对比

      javascript jQuery 入口函数 只能有一个,如果有多个,后面的会覆盖前面 可以有多个,并且不会发生覆盖的情况 代码容错性 代码容错性差,代码出现错误,会影响到后面代码的运行. 代码容 ...

  6. C#多线程(Thread)开发基础

    除非另有说明,否则所有的例子都假定以下命名空间被引用: using System; using System.Threading; 1      基本概念 在描述多线程之前,首先需要明确一些基本概念. ...

  7. PCL:Ubuntu下安装配置PCL

    一:安装PCL 依据官网介绍:http://www.pointclouds.org/downloads/linux.html Ubuntu We currently support all Ubunt ...

  8. ZBrush细说3D海盗角色的创建艺术

    一提到海盗,就不由自主想到了<加勒比海盗>,那个帅得一塌糊涂的杰克船长更是让人夜不能寐寝难安,但在艺术的世界里,角色无美丑,今天我们要讲的这位海盗,就与“帅气”八竿子打不着了,它甚至有点古 ...

  9. 在Linux Centos 7.2 上安装指定版本Docker 17.03

    相关资料链接: https://docs.docker.com/install/linux/docker-ce/centos/#install-docker-ce 先清空下“历史” yum insta ...

  10. js脚本捕获页面 GET 方式请求的参数?其实直接使用 window.location.search 获得

    js脚本捕获页面 GET 方式请求的参数?其实直接使用 window.location.search 获得