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 ...
随机推荐
- (转)Oracle与DB2在数据库高可用技术上的相同与差异探讨
原文:http://www.talkwithtrend.com/Article/178339 数据库建设过程中,高可用是每一个企业数据中心数据库建设过程中至关重要的一个关注点,直接关系到业务连续性和稳 ...
- Linux下SSH工具 PAC Manager的安装
PAC Manager, Linux下类似SecureCRT Xshell的SSH工具,该工具功能上相当的不错,完全可以代替SecureCRT Xshell的功能. PAC (Perl Auto Co ...
- mysql8.0关闭log-bin功能
一.查看log-bin是否开启: mysql> show variables like 'log-bin'; 二.关闭log-bin: 在配置文件中的 [mysqld] 配置节中增加 skip- ...
- 弗格森&红魔
本周日曼联对阵斯旺西时,一位名叫皮特-莫利纽克斯的58岁老人,将会举起一张老旧的床单,就如他曾做过的那样.那一次“臭名昭着”的行为要追溯到1989年12月,弗格森当时已经来到了悬崖边上,他的背后是一堵 ...
- 498_Diagonal-Traverse
目录 498_Diagonal-Traverse Description Solution Java solution Python solution 1 Python solution 2 Pyth ...
- Eclipse常用快捷键之代码编辑篇
Eclipse是Java开发常用的IDE工具,熟练使用快捷键可以提高开发效率,使得编码工作事半功倍,下面介绍几种常用的代码编辑和补全工具 重命名快捷键:Alt+Shift+R 可用于类名,方法名,属性 ...
- docker部署ELK日志处理
docker环境下部署的ELK系统,日志处理,保留最近五个小时的日志 dockerlog.sh #!/bin/bash logs=`find /var/lib/docker/containers/ - ...
- Java开发相关官方存档下载地址
前言 集中收藏Java开发中需要用到的常用下载地址 jdk Java SE 最新下载 | Oracle 技术网 : http://www.oracle.com/technetwork/cn/java/ ...
- Cookie写入之path的坑
问题 我在/page/index/index.html中向浏览器添加了一个useid的cookie(这里没有指定path), 然后试着从/page/demo/demo.html中取值,发现无法取到, ...
- JAVA基础之——使用idea创建maven项目 以及使用tomcat本地调试springmvc
前言:关于这个话题网上有很多,本文旨在引导实战纠偏,理论偏少,如果按照步骤还不能达到本文目的,请留言. 1 环境准备 1.1 软件准备 idea:官方下载社区版,下载后安装 maven:Apache- ...