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的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [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 ...

  4. 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 ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. Windows Server 2008 R2 Enterprise 上用 SqlServer 2008 R2 创建发布出现异常

    标题: 新建发布向导------------------------------ SQL Server 无法将“AC”配置为分发服务器. ------------------------------其 ...

  2. Couchbase进阶-集群与版本升级

    最近在负责公司Couchbase版本升级工作,之前只有一台Cache服务器,使用Couchbase Enterprise Edition 1.8,为避免单点故障现在需要新增加一台Cache服务器做负载 ...

  3. 大数据搭建各个子项目时配置文件技巧(适合CentOS和Ubuntu系统)(博主推荐)

    不多说,直接上干货! 很多同行,也许都知道,对于我们大数据搭建而言,目前主流,分为Apache 和 Cloudera 和 Ambari. 后两者我不多说,是公司必备和大多数高校科研环境所必须的! 分别 ...

  4. 关于function构造函数特别注意的

    function在javascript中是对象,所以function持有构造函数例子:var a = new Function("x","y","re ...

  5. 运维监控之zabbix(yum安装)

    简介 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系统管理员快速定位 ...

  6. poj 1222EXTENDED LIGHTS OUT

    高斯消元的题本质思想一样. 学习网址:http://www.cnblogs.com/rainydays/archive/2011/08/31/2160748.html #include <ios ...

  7. R语言变量赋值

    变量可以使用向左,向右且等于操作符来分配值.可以使用 print() 或 cat() 函数打印变量的值.cat() 函数将多个项目并成连续并打印输出. # Assignment using equal ...

  8. 【转】winform程序textbox滚动条保持在最下面 内容不闪烁

    在开发winform程序时,会用到textbox控件来显示信息,当把textbox的Multiline属性改为Ture时(即多行显示状态),ScrollBars属性改为Vertical(内容过多时,显 ...

  9. Fanvas是一个把swf转为html5 canvas动画的系统

      https://github.com/Tencent/Fanvas   使用方法:     代码: <!DOCTYPE html> <html> <head> ...

  10. 关于vue.js中slot的理解

    slot这块看官网文档,起初有点不懂,仔细研究还是最终理解了,slot是用来干嘛的呢,先看下一个例子: <script src="https://unpkg.com/vue/dist/ ...