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:

  1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
  2. 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的更多相关文章

  1. LeetCode 201. 数字范围按位与(Bitwise AND of Numbers Range)

    201. 数字范围按位与 201. Bitwise AND of Numbers Range 题目描述 给定范围 [m, n],其中 0 <= m <= n <= 214748364 ...

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

  3. 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)

    [LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...

  4. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  5. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  6. 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' ...

  7. [leetcode] Bitwise AND of Numbers Range

    Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...

  8. 【LeetCode】201. Bitwise AND of Numbers Range

    Bitwise AND of Numbers Range  Given a range [m, n] where 0 <= m <= n <= 2147483647, return ...

  9. 【刷题-LeetCode】201 Bitwise AND of Numbers Range

    Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...

随机推荐

  1. CodeForces 185A. Plant (矩阵快速幂)

    CodeForces 185A. Plant (矩阵快速幂) 题意分析 求解N年后,向上的三角形和向下的三角形的个数分别是多少.如图所示: N=0时只有一个向上的三角形,N=1时有3个向上的三角形,1 ...

  2. HTML5 canvas 创意:飞翔的凤凰

    当我看到这件作品的时候,我表示非常喜欢.这个作品的产生不仅仅需要编程和算法,作者肯定是个充满了艺术细胞的人.倘若有什么canvas艺术作品比赛的话,我想它就是获奖的那个. 先观赏下演示吧.注意,要看到 ...

  3. Linux环境下用Weblogic发布项目【二】 -- 配置Domain域

    配置注意事项: 修改密码时密码长度最少8位:在"<下一步>"后面为空即表示敲回车: 具体配置步骤如下: [root@GPS-App ~]# [root@GPS-App ...

  4. contOS镜像快速加载到本地虚拟机软件

    无需任何配置,只要两步: 1.首先打开 虚拟机软件VMware 2.然后打开镜像目录,找到后缀名为 .vmx 的文件,双击,即可. 会自动 挂载好,如下图:

  5. 清北学堂模拟赛d6t6 棋盘迷宫

    3.棋盘迷宫(boardgame.pas/c/cpp)(boardgame.in/out)时间限制:5s/空间限制:256M[题目描述]小 A 和小 Z 是非常要好的朋友, 而且他们都对迷宫游戏非常有 ...

  6. MYSQL性能察看

    http://fengbin2005.iteye.com/blog/1580214 网上有很多的文章教怎么配置MySQL服务器,但考虑到服务器硬件配置的不同,具体应用的差别,那些文章的做法只能作为初步 ...

  7. HDU 3507斜率优化dp

    Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  8. Redrain 通用菜单控件使用方法和说明(附源码和demo)

    转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/42889709 大概半年前我写过博客说明怎么改造duilib的原代Menu ...

  9. SQL Server 代理(已禁用代理 XP)

    sp_configure 'show advanced options', 1; GO RECONFIGURE WITH OVERRIDE; GO sp_configure 'Agent XPs', ...

  10. Android LayoutInflater深度解析

    1. 题外话 相信大家对LayoutInflate都不陌生,特别在ListView的Adapter的getView方法中基本都会出现,使用inflate方法去加载一个布局,用于ListView的每个I ...