LeetCode解题报告—— Number of Islands & Bitwise AND of Numbers Range
1. Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
Input:
11110
11010
11000
00000 Output: 1
Example 2:
Input:
11000
11000
00100
00011 Output: 3
思路:基本思路是利用DFS,遍历矩阵,遇到1的话就利用DFS将所有与其邻接的1全置为0,将计数器加1即可。
public class Solution {
private int n;
private int m;
public int numIslands(char[][] grid) {
int count = 0;
n = grid.length;
if (n == 0) return 0;
m = grid[0].length;
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++)
if (grid[i][j] == '1') {
DFSMarking(grid, i, j);
++count;
}
}
return count;
}
private void DFSMarking(char[][] grid, int i, int j) {
if (i < 0 || j < 0 || i >= n || j >= m || grid[i][j] != '1') return;
grid[i][j] = '0';
DFSMarking(grid, i + 1, j);
DFSMarking(grid, i - 1, j);
DFSMarking(grid, i, j + 1);
DFSMarking(grid, i, j - 1);
}
}
2. Bitwise AND of Numbers Range
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.
Example 1:
Input: [5,7]
Output: 4
Example 2:
Input: [0,1]
Output: 0
思路:奇数与偶数And,最后一位结果肯定是0。如果m和n不想等,那么这个range之间一定有奇数和偶数,它们and之后最后一位肯定是0,再和其它数字and最后一位还是0。这样确定了最后一位数字是0还是1之后将m和n右移一位再执行相同步骤,直到m和n相等为止,这个相等的部分and之后是保持原值的。
public class Solution {
public int rangeBitwiseAnd(int m, int n) {
if(m == 0){
return 0;
}
int moveFactor = 1;
while(m != n){ // 这个循环能确定最后1位,2位...是0(只要此时的m和n不想等,这时的最后位结果一定是0),直至到二进制高位相同的部分
m >>= 1;
n >>= 1;
moveFactor <<= 1; // 记录往右移了多少次,即确定了末尾处的几个0,moveFactor应该是10....(1后面跟多个0) 这样的形式
}
return m * moveFactor; // 此时的m剩下的是高位相同(不变的)部分,后面应该都是0,所以要乘以moveFactor来还原结果
}
}
3. Course Schedule
There are a total of n courses you have to take, labeled from 0 to n-1.
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
Example 1:
Input: 2, [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.
Example 2:
Input: 2, [[1,0],[0,1]]
Output: false
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0, and to take course 0 you should
also have finished course 1. So it is impossible.
Note:
- The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
- You may assume that there are no duplicate edges in the input prerequisites.
思路:和第一题一样,都是属于和图相关的题目,说到图的话,首先想到的是DFS和BFS。对于这题来说,如何把它建模成是一个图问题是个比较难的地方,根据输入得到的是一系列的边,现在一个想法是将输入转化成一个二维矩阵来考虑。也就是说现在一个对于图相关问题的通用思路是能不能将输入变成二维矩阵的形式。对于此题,可将每节课看成是矩阵的行索引和列索引,即行从0到n,列也从0到n,matrix[i][j] 值如果是1的话则表示从 i 到 j 是有边的,放在这题中就是说课程i 是课程j 的先决条件。这样的一个二维矩阵便可以用来表示一个有向图(这里让我想起了数据结构的那个弗洛伊德算法)。
第一个的图转化为矩阵完成了,对于此题还不够,因为一个课程可能有多个先决课程,对应到图中来说就是一个节点的入度会大于1,只有当先决课程都满足的情况下才能take这个课程。那么还要记录每个节点的入度数目,如果某个节点的入度数是0则表示这是个开始节点,也就是说如果我们需要遍历整个图的话,是从这些节点开始的。那么现在思路很明显了,从这些开始节点开始遍历整个图,如果所有节点都能遍历得到那么则说明全部课程是可以完成的,这个过程中只有一点要注意的是对于入度数大于等于1的节点的处理情况。简而言之在BFS或DFS遍历图的节点时,要加一个判断条件看看这个节点是否符合不同题目所给出的实际要求。
public boolean canFinish(int numCourses, int[][] prerequisites) {
int[][] matrix = new int[numCourses][numCourses]; // 矩阵来表示整个有向图
int[] indegree = new int[numCourses]; // 记录每个图节点的入度数,放在这里就是每个课程需要的前提课程是多少个
for (int i=0; i<prerequisites.length; i++) { // 将从i到j的有向边转化为矩阵形式
int ready = prerequisites[i][0];
int pre = prerequisites[i][1];
if (matrix[pre][ready] == 0) // avoid duplicate case
indegree[ready]++;
matrix[pre][ready] = 1;
}
int count = 0;
Queue<Integer> queue = new LinkedList();
for (int i=0; i<indegree.length; i++) {
if (indegree[i] == 0) queue.offer(i); // 将入度为0的节点作为遍历的开始节点推入队列中
}
while (!queue.isEmpty()) { // BFS,直到队列全部出完为止
int course = queue.poll();
count++; // 记录总共遍历了多少course
for (int i=0; i<numCourses; i++) {
if (matrix[course][i] != 0) { // 如果目前出队列的course是课程i的前提课程,那么课程i的入度数减1
if (--indegree[i] == 0) // 如果课程i的前提课程都满足了,那么可以take这个课程,将其推入队列中
queue.offer(i);
}
}
}
return count == numCourses; // 如果都遍历到了则表示可以完成所有cousre
}
LeetCode解题报告—— Number of Islands & Bitwise AND of Numbers Range的更多相关文章
- LeetCode 201. 数字范围按位与(Bitwise AND of Numbers Range)
201. 数字范围按位与 201. Bitwise AND of Numbers Range 题目描述 给定范围 [m, n],其中 0 <= m <= n <= 214748364 ...
- leetcode解题报告(33): Find All Numbers Disappeared in an Array
描述 Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ...
- 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)
[LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number
一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' ...
- [leetcode] Bitwise AND of Numbers Range
Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...
- 【LeetCode】201. Bitwise AND of Numbers Range
Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return ...
- 【刷题-LeetCode】201 Bitwise AND of Numbers Range
Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...
随机推荐
- bzoj1014: [JSOI2008]火星人prefix(splay+hash+二分)
题目大意:一个字符串三个操作:①求两个后缀的LCP②插入一个字符③修改一个字符. 前几天刚学了hash+二分求lcp,就看到这题. 原来splay还能这么用?!原来splay模板这么好写?我以前写的s ...
- [NOI2017]蔬菜——时光倒流+贪心
题目链接 题解: 貌似一眼看过去是一个贪心. 其他的算法要记录的东西就太多了. 部分分其实很高.但是没有什么提示. 想一些套路:二分?不行还要贪心判断. 分治?前后取法是有影响的. 时光倒流? 也许可 ...
- c++模板类编译错误
最近想写一个c++模板类,实现一个有向图.依据惯例,类在头文件中声明,类的实现写在源文件中.可是编译的时候出现了如下错误: undefined reference to 通过谷歌发现,这是一个很常见的 ...
- 手脱ACProtect V1.4X(有Stolen Code)之补区段
首先需要说的是,这个壳是ximo大神视频教程里的 0041F000 > pushad ; //程序入口点 0041F001 E8 call NgaMy.0041F007 0041F006 E8 ...
- std::string在多字节字符集环境下substr的实现方法
昨天写到<使用多字节字符集的跨平台(PC.Android.IOS.WP)编码/解码方法>中提到服务端使用std::string处理字符串,std::string对多字节字符集支持并不是很完 ...
- Hive、Pig、HBase的关系与区别
欢迎关注大数据和人工智能技术文章发布的微信公众号:清研学堂,在这里你可以学到夜白(作者笔名)精心整理的笔记,让我们每天进步一点点,让优秀成为一种习惯! Pig 一种操作hadoop的轻量级脚本语言,最 ...
- 洛谷2115 [USACO14MAR]破坏Sabotage
https://www.luogu.org/problem/show?pid=2115 题目描述 Farmer John's arch-nemesis, Farmer Paul, has decide ...
- 【转载】VS2013安装需要IE10
因为需要移动办公,需要给笔记本搭建编程环境.安装VS2013时遇到了小麻烦,提示我,需要安装IE10. 然后我很听话的按照提供的超链接,到了官网,下载了最新的IE11,然后安装,结果告诉我下载的IE版 ...
- 取(m堆)石子游戏 HDU2176(Nim博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2176 题目: Problem Description m堆石子,两人轮流取.只能在1堆中取.取完者胜. ...
- 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛) F.猴子排序的期望
题目链接:https://www.nowcoder.com/acm/contest/116/F 题目描述 我们知道有一种神奇的排序方法叫做猴子排序,就是把待排序的数字写在卡片上,然后让猴子把卡片扔在空 ...