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 ...
随机推荐
- eclipse中文汉字看不清或过小的问题解决方法!!
把字体修改为 中欧字体就可以看清汉字
- window下svn开机自动启动
- idiom的学习笔记(一)、三栏布局
三栏布局左右固定,中间自适应是网页中常用到的,实现这种布局的方式有很多种,这里我主要写五种.他们分别是浮动.定位.表格.flexBox.网格. 在这里也感谢一些老师在网上发的免费教程,使我们学习起来更 ...
- c++ 编译期与运行期
分享到 一键分享 QQ空间 新浪微博 百度云收藏 人人网 腾讯微博 百度相册 开心网 腾讯朋友 百度贴吧 豆瓣网 搜狐微博 百度新首页 QQ好友 和讯微博 更多... 百度分享 转自:http://h ...
- VC++抛出自定义编译期异常的指令
#error 即可, 抛出消息是 #pragma message 最新的还有static_assert有一些用 一下子忘了网上居然搜不到...尝试了 关键字vc++.vc.vs.msvc + 抛出编 ...
- (转)shiro权限框架详解05-shiro授权
http://blog.csdn.net/facekbook/article/details/54910606 本文介绍 授权流程 授权方式 授权测试 自定义授权realm 授权流程 开始构造Secu ...
- DNS解析过程详解(转载)
DNS解析过程详解(转载) DNS Domain Name System 域名系统,它就是根据域名查出IP地址. 先说一下DNS的几个基本概念: 一. 根域 就是所谓的“.”,其实我们的网址ww ...
- mac 安装卸载python
第 1 步,删除框架: sudo rm -rf /Library/Frameworks/Python.framework/Versions/x.x第 2步,删除应用目录: sudo rm -rf &q ...
- js中标签字符串的拼接
//1.用单双引号拼接 var valueDemo = "111"; var htmlStrs1 = '<option selected="selected&quo ...
- 内核如何检测SOFT LOCKUP与HARD LOCKUP?
内核如何检测SOFT LOCKUP与HARD LOCKUP? From article 所谓lockup,是指某段内核代码占着CPU不放.Lockup严重的情况下会导致整个系统失去响应.Lockup有 ...