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 ...
随机推荐
- java 并发编程——Thread 源码重新学习
Java 并发编程系列文章 Java 并发基础——线程安全性 Java 并发编程——Callable+Future+FutureTask java 并发编程——Thread 源码重新学习 java并发 ...
- SSH使用密钥免密码登录
使用ssh远程连接服务器,有两种身份校验方式:账号密码和秘钥.使用秘钥的方式理论上更加安全,而且免去了输入密码的步骤,使用起来更方便(尤其对于sftp,scp等). 设置 SSH,打开密钥登录功能 编 ...
- hibernate的反向生成改懒加载的地方
改变懒加载只需要把生成的文件中的获取类型改为eager fetch = FetchType.EAGER @ManyToOne(fetch = FetchType.EAGER)//把懒加载换成饿加载模式 ...
- h2数据库的简单使用
1.登录H2数据库的WebConsole控制台 2.设置数据库连接 3.连接测试通过之后,点击[连接]按钮,登录到test数据库的webConsole 4.创建表 复制H2数据库提供的样例SQL脚本, ...
- JavaScript设计模式-15.适配器模式
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- java的IO流,字节流和字符流
java操作文件都是通过流来处理的,(其实其他很多语言也是这样) 第一:java的IO流,分为:输入流 和 输出流(这真是废话,这是从流向的角度来说的) 第二:java的所有IO流,只分为:字节流 和 ...
- maven备忘
maven常见命令 mvn clean mvn compile mvn package mvn clean compile package mvn -Djetty.port= jetty:run mv ...
- maven在pom文件中添加你想要的jar包
概述:POM 文件里面的依赖jar包经常需要添加, 仅需要在google中代码查找 :maven 你需的jar包名称 repository 用了Maven,所需的JAR包就不能再像往常一样,自己找到并 ...
- 【c++】字符串流输出恢复状态问题
缘起 #include <iostream> #include <sstream> using namespace std; int main() { istringstrea ...
- 解决angular-deckgrid高度不均衡和重加载的问题
在项目中使用angular-deckgrid+ng-infinite-scroll实现瀑布流的无限加载.但是实际测试中发现deckgrid有2个比较严重影响体验的BUG: 每次添加新的card,整个d ...