2017-3-6 leetcode 118 169 189
今天什么都没发生
=================================================
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的更多相关文章
- 2017/11/22 Leetcode 日记
2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice ex ...
- 2017/11/21 Leetcode 日记
2017/11/21 Leetcode 日记 496. Next Greater Element I You are given two arrays (without duplicates) num ...
- 2017/11/13 Leetcode 日记
2017/11/13 Leetcode 日记 463. Island Perimeter You are given a map in form of a two-dimensional intege ...
- 2017/11/20 Leetcode 日记
2017/11/14 Leetcode 日记 442. Find All Duplicates in an Array Given an array of integers, 1 ≤ a[i] ≤ n ...
- 2017/11/9 Leetcode 日记
2017/11/9 Leetcode 日记 566. Reshape the Matrix In MATLAB, there is a very useful function called 'res ...
- 2017/11/7 Leetcode 日记
2017/11/7 Leetcode 日记 669. Trim a Binary Search Tree Given a binary search tree and the lowest and h ...
- 2017/11/6 Leetcode 日记
2017/11/6 Leetcode 日记 344. Reverse String Write a function that takes a string as input and returns ...
- 2017/11/5 Leetcode 日记
2017/11/5 Leetcode 日记 476. Number Complement Given a positive integer, output its complement number. ...
- 2017/11/3 Leetcode 日记
2017/11/3 Leetcode 日记 654. Maximum Binary Tree Given an integer array with no duplicates. A maximum ...
随机推荐
- guice基本使用,guice整合guice-servlet,web开发(五)
介绍 Guice Servlet 为使用web应用程序和Servlet容器提供了一个完整的模式.. Guice's servlet 扩展允许从你的servlet应用中完全淘汰web.xml,并且具有类 ...
- C-C语言概述
1.数据+算法=程序. 2.C语言程序是由一个或多个函数组成的,函数是由语句组成的,语句是由关键字,标识符,运算符,数据组成的:语句可分为:声明语句,赋值语句,控制语句,函数语句,空语句. 3.#in ...
- 30秒就能理解的JavaScript优秀代码
数组 arrayMax 返回数组中的最大值. 将Math.max()与扩展运算符 (...) 结合使用以获取数组中的最大值. const arrayMax = arr => Math.max(. ...
- VueJS 开发常见问题集锦
由于公司的前端开始转向 VueJS,最近开始使用这个框架进行开发,遇到一些问题记录下来,以备后用. 主要写一些 官方手册 上没有写,但是实际开发中会遇到的问题,需要一定知识基础. 涉及技术栈 CLI: ...
- ProgressDialog的关键几个函数
进度条对话框在开发是常见的一种工具,只要注意以下几点,就可以轻松使用. ProgressDialog.setMax(MAX_PROGRESS); //设置最大值,可以如下定义一个常值 //priva ...
- Windows上部署Python
以Python 3.5为例 1.到https://www.python.org/downloads/windows/下载embeddable zip file类型的压缩包python-3.5.1-em ...
- tomcat 使用常见问题
tomcat 下目录的介绍: 参考地址: http://blog.csdn.net/u013132035/article/details/54949593 参考书籍:tomcat权威指南 (1) 非w ...
- 一文带您了解5G的价值与应用
一文带您了解5G的价值与应用 5G最有趣的一点是:大多数产品都是先有明确应用场景而后千呼万唤始出来.而5G则不同,即将到来的5G不仅再一次印证了科学技术是第一生产力还给不少用户带来了迷茫——我们为什么 ...
- K8s的工作原理
title: Kubernetes之初探 subtitle: K8s的工作原理 date: 2018-09-18 18:26:37 --- K8s概述 我清晰地记得曾经读到过的一篇博文,上面是这样写的 ...
- JS 100内与7相关的数
var s =""; for(var i=0;i;i++) { if(i%7 == 0 ){ s += i+","; } else if((i-7)%10 == ...