leetcode Ch8-Others
1. Rotate Image 旋转图像
顺时针旋转90度:先沿水平线翻转,再沿主对角线翻转。
逆时针旋转90度:先沿竖直线翻转,再沿主对角线翻转。
顺时针旋转180度:水平翻转和竖直翻转各一次。 逆时针旋转180度效果同顺时针180度。
2.Set Matrix Zeroes
空间O(1)方法:利用第一行和第一列。
1.先确定第一行和第一列是否需要清零
2.扫描剩下的矩阵元素,如果遇到了0,就将对应的第一行和第一列上的元素赋值为0 (反正早晚都要对它赋0,现在赋0能起到标记作用)
3.根据第一行和第一列的信息,已经可以讲剩下的矩阵元素赋值为结果所需的值了
4.根据1中确定的状态,处理第一行和第一列。
ref: http://fisherlei.blogspot.com/2013/01/leetcode-set-matrix-zeroes.html
3. gas station
consider the case that, if started at station i, and when goes to the station j, there is not enough gas to go the j+1 station. What happened now? For the brutal force method, we go back to the station i+1 and do the same thing. But, actually, if the accumutive gas cannot make it from j to j+1, then the stations from i to j are all not the start station.
That is because, (1)the tank is unlimited, every time arrive to the station, the tank will fuel the max gas here, and comsume the cost to go to the next. (2)There can not be negative tank when arriving a station, at least the tank is empty. So, if i to j cannot go to j+1, then i+1 to j still cannot go to j+1... In this way, the next starting station we will try is not i+1, but the j+1. And after a single loop from i to j, we can find the result!
原因很简单:tank里的油量是不可能是负数的。如果你从起点i处到j都到不了,那从i+1就更不可能到j了,因为从i到i+1后tank里可能会有些剩余的油,最差情况就是从i 到i+1后tank刚好空。如果带着剩余的油从i+1都到不了j,那以白手起家的状态从i+1出发更不可能到j了。
所以,如果从i到j的累积和为负,那i不用从i+1开始继续循环,直接跳到 j 处开始循环就行了。
explanation ref: http://yucoding.blogspot.com/2013/12/leetcode-question-gas-station.html
code refer to soulmachine.
4. candy
为什么从左向右扫一次,又从右向左扫一次?
举个最简单的例子,如果ratings是5,4,3,2,1, 从左向右扫能到的candy数1,1,1,1,1,但从右向左扫能到的candy数是1,2,3,4,5。
对每个位置都取一个max。
ref: http://yucoding.blogspot.com/2014/02/leetcode-question-candy.html
5. single number II
还是位操作。设一个32位的count数组,用来统计每一位出现的次数。如果哪一位出现的次数不是3的倍数,那就在最后算result的时候按照其权重加进去。
class Solution {
public:
int singleNumber(vector<int>& nums) {
int n = nums.size();
vector<int> count(sizeof(int) * , );
for (int i = ; i < sizeof(int) * ; i++) {
for (int j = ; j < n; j++) {
count[i] += (nums[j] >> i) & ;
}
}
int result = ;
for (int i = ; i < count.size(); i++) {
result += (count[i] % ) << i;
}
return result;
}
};
leetcode Ch8-Others的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- CentOS7安装virtualbox
1.进入virtualbox官网 https://www.virtualbox.org/ 2.点击download 3.点击Linux distributions 4.向下翻至如图,并且进入同种框选页 ...
- 对于SQL Server 2008删除或压缩数据库日志的方法
由于数据库日志增长被设置为“无限制”,所以时间一长日志文件必然会很大,一个400M的数据库居然有4G的LOG文件,严重占用了磁盘空间.由于主要是做OLAP,所以数据库本身不会有大变动,所以日志也就没有 ...
- Can't find the 'libpq-fe.h header when trying to install pg gem
https://stackoverflow.com/questions/6040583/cant-find-the-libpq-fe-h-header-when-trying-to-install-p ...
- 设计模式应用场景之Model设计中可以用到的设计模式
在开发中,不同的场景适用不同的设计模式.对于开发建模方面,可以根据模型的抽象类型设计成简单属性模式和复杂组合模式. 因为组合模式中的属性为多种类型对象,可操作性也比较灵活. 而针对这种复杂数据结构,有 ...
- Linux笔记-vim 配置
本文是转载的,我用的ubuntu12.04在vim设置方面就是参考了本文,拿来分享给大家! ubuntu10.10中的设置方法: $cd /etc/vim $sudo cp vimrc vimrc.b ...
- Esper简介
1. CEP(Complex Event Processing, 复杂事件处理) 事件(Event)一般情况下指的是一个系统中正在发生的事,事件可能发生在系统的各个层面上,它可以是某个动作,例如客户下 ...
- Nginx 基于客户端 IP 来开启/关闭认证
前些日子帮助公司在搭建了一个内部资源的导航页面,方便公司员工访问各种常用的系统.因为这个页面包含一些敏感信息,我们希望对其做认证,但仅当从外网访问的时候才开启,当从公司内网访问的时候,则无需输入账号密 ...
- layer关闭弹出层返回值到父页面
1.首先在父页面定义一个空间,Id=layerResult 然后 layer.open({ type: , title: '选择看课件', shadeClose: true, shade: 0.8, ...
- C# Lambda表达式详细总结
(一)输入参数 在Lambda表达式中,输入参数是Lambda运算符的左边部分.它包含参数的数量可以为0.1或者多个.只有当输入参数为1时,Lambda表达式左边的一对小括弧才可以省略.输入参数的数量 ...
- C# 之多线程(二)
一.确定多线程的结束时间,thread的IsAlive属性 在多个线程运行的背景下,了解线程什么时候结束,什么时候停止是很有必要的. 案例:老和尚念经计时,2本经书,2个和尚念,一人一本,不能撕破,最 ...