LeetCode解题报告—— Sum Root to Leaf Numbers & Surrounded Regions & Single Number II
1. Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9
only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path 1->2->3
which represents the number 123
.
Find the total sum of all root-to-leaf numbers.
Note: A leaf is a node with no children.
Example:
Input: [1,2,3]
1
/ \
2 3
Output: 25
Explanation:
The root-to-leaf path1->2
represents the number12
.
The root-to-leaf path1->3
represents the number13
.
Therefore, sum = 12 + 13 =25
.
Example 2:
Input: [4,9,0,5,1]
4
/ \
9 0
/ \
5 1
Output: 1026
Explanation:
The root-to-leaf path4->9->5
represents the number 495.
The root-to-leaf path4->9->1
represents the number 491.
The root-to-leaf path4->0
represents the number 40.
Therefore, sum = 495 + 491 + 40 =1026
.
思路:本来想用递归解决这个问题,结果在传参时出现了一个很典型的错误,结果还是点来了discussion,瞬间清朗很多,果然大佬就是大佬。
public int sumNumbers(TreeNode root) {
return sum(root, 0);
} public int sum(TreeNode n, int s){
if (n == null) return 0;
if (n.right == null && n.left == null) return s*10 + n.val; // 叶节点的情形
return sum(n.left, s*10 + n.val) + sum(n.right, s*10 + n.val); // 不是叶节点的情形
}
感觉对于树有关的题目,很多都可以用递归来解决,因为在处理完了根节点后,对于它的左子节点和右子节点的处理流程和根节点差不多,关键是着其中参数的变化。
2. Surrounded Regions
Given a 2D board containing 'X'
and 'O'
(the letter O), capture all regions surrounded by 'X'
.
A region is captured by flipping all 'O'
s into 'X'
s in that surrounded region.
Example:
X X X X
X O O X
X X O X
X O X X
After running your function, the board should be:
X X X X
X X X X
X X X X
X O X X
思路:类似于围棋一样,基本思路是先将整个矩阵中没有围住的 O (也就是临接边界的) 置为 * ,再将被围住的(其余的) O 置为 X ,再将 * 置回 O 即可,需要注意的是在第一步寻找没有被围住的 O 时,需要用到DFS或者BFS来搜索。
public void solve(char[][] board) {
if (board.length == 0 || board[0].length == 0)
return;
if (board.length < 2 || board[0].length < 2)
return;
int m = board.length, n = board[0].length;
//Any 'O' connected to a boundary can't be turned to 'X', so ...
//Start from first and last column, turn 'O' to '*'.
for (int i = 0; i < m; i++) {
if (board[i][0] == 'O')
boundaryDFS(board, i, 0);
if (board[i][n-1] == 'O')
boundaryDFS(board, i, n-1);
}
//Start from first and last row, turn '0' to '*'
for (int j = 0; j < n; j++) {
if (board[0][j] == 'O')
boundaryDFS(board, 0, j);
if (board[m-1][j] == 'O')
boundaryDFS(board, m-1, j);
}
//post-prcessing, turn 'O' to 'X', '*' back to 'O', keep 'X' intact.
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == 'O')
board[i][j] = 'X';
else if (board[i][j] == '*')
board[i][j] = 'O';
}
}
}
//Use DFS algo to turn internal however boundary-connected 'O' to '*';
private void boundaryDFS(char[][] board, int i, int j) {
if (i < 0 || i > board.length - 1 || j <0 || j > board[0].length - 1)
return;
if (board[i][j] == 'O') // 感觉这个判断条件可以省略
board[i][j] = '*';
if (i > 1 && board[i-1][j] == 'O') // 向四个相邻的方向DFS,DFS是递归,BFS是队列。上
boundaryDFS(board, i-1, j);
if (i < board.length - 2 && board[i+1][j] == 'O') // 下
boundaryDFS(board, i+1, j);
if (j > 1 && board[i][j-1] == 'O') // 左
boundaryDFS(board, i, j-1);
if (j < board[i].length - 2 && board[i][j+1] == 'O' ) // 右
boundaryDFS(board, i, j+1);
}
注意的是上面往四个方向DFS时的if判断条件,这里用 length-2 而不是 length-1 是为了避免过深的DFS而导致的stackOverflow。
3. Single Number II
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,3,2]
Output: 3
Example 2:
Input: [0,1,0,1,0,1,99]
Output: 99
思路:本来为是比较简单的一题,结果还是各种恪手,算法这玩意儿,真的是好难学(┬_┬)。看了一下discuss,主要是利用位运算符,既然题目要求在线性时间内完成,即扫描一遍整个数组就能得出结果。
public int singleNumber(int[] nums) {
int ans = 0;
for(int i = 0; i < 32; i++) { // 从低位到高位处理
int sum = 0;
for(int j = 0; j < nums.length; j++) {
if(((nums[j] >> i) & 1) == 1) {
sum++;
sum %= 3; // 这里可以扩展
}
}
if(sum != 0) {
ans |= sum << i; // 确定每个位置最终是0还是1后,还原值
}
}
return ans;
}
选择了discuss中最容易理解的一个,主要思想就是将整数转化成32位的二进制来考虑,因为数组中的元素要么出现了3次要么出现了一次,所以任何一位上的数字(0或者1)出现的总次数是3的倍数或者3的倍数加1,所以统计总共每个位置上出现的1的个数即可,如果到了次数3就置为0,这样统计下来,那些出现次数为3的数每个二进制位置上都是0,只要出现一次的数字的二进制位留在了最后处理结果上。
LeetCode解题报告—— Sum Root to Leaf Numbers & Surrounded Regions & Single Number II的更多相关文章
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【LeetCode】129. Sum Root to Leaf Numbers (2 solutions)
Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...
- 【LeetCode OJ】Sum Root to Leaf Numbers
# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...
- LeetCode OJ 129. Sum Root to Leaf Numbers
题目 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a num ...
- 【LeetCode】129. Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- LeetCode OJ:Sum Root to Leaf Numbers(根到叶节点数字之和)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- LeetCode题解之Sum Root to Leaf Numbers
1.题目描述 2.问题分析 记录所有路径上的值,然后转换为int求和. 3.代码 vector<string> s; int sumNumbers(TreeNode* root) { tr ...
- LeetCode: Sum Root to Leaf Numbers 解题报告
Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...
- Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)
Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...
随机推荐
- 洛谷 P4735 最大异或和 解题报告
P4735 最大异或和 题目描述 给定一个非负整数序列\(\{a\}\),初始长度为\(N\). 有\(M\)个操作,有以下两种操作类型: A x:添加操作,表示在序列末尾添加一个数\(x\),序列的 ...
- HDOJ.2187 悼念512汶川大地震遇难同胞——老人是真饿了(贪心)
悼念512汶川大地震遇难同胞--老人是真饿了 点我挑战题目 题目分析 每组数据给出所拥有的钱数,和大米的种类.每种大米给出单价(每单位重量)和大米的重量.求能买到的大米最大重量是多少? 采用贪心算法. ...
- 项目管理---git----快速使用git笔记(四)------远程项目代码的首次获取
使用git最常见的场景是 你需要参与到一个项目中,而这个项目的代码,同事已经上传到github或者https://coding.net了. 这时候他会给你一个项目代码的远程仓库链接. 例如: http ...
- poj:1850 Code(组合数学?数位dp!)
题目大意:字符的字典序依次递增才是合法的字符串,将字符串依次标号如:a-1 b-2 ... z-26 ab-27 bc-52. 为什么题解都是组合数学的...我觉得数位dp很好写啊(逃 f[pos][ ...
- django项目初探
一:创建django项目 设置数据库(默认sqlit3) mysql: setting:中设置 DATABASES = { 'default': { 'ENGINE': 'django.db.back ...
- 关于Mybatis的@Param注解 及 mybatis Mapper中各种传递参数的方法
原文:https://blog.csdn.net/mrqiang9001/article/details/79520436 关于Mybatis的@Param注解 Mybatis 作为一个轻量级的数 ...
- CMDB资产管理系统开发【day26】:CMDB上节回顾
一.上节知识点回顾 服务器设计了一个表结构 开发了一个客户端 二.后台创建缓存区表 客户端连接服务器,在服务器的下面看报错信息 因为URL都没有写,所以我找不到呀 1.在MadKing\url.py ...
- 南阳ACM 题目517:最小公倍数 Java版
最小公倍数 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 为什么1小时有60分钟,而不是100分钟呢?这是历史上的习惯导致. 但也并非纯粹的偶然:60是个优秀的数字,它 ...
- 精通BIRT:Eclipse商务智能报表工具开发实践指南
http://blog.csdn.net/birtbird/article/details/8935520 [置顶] 精通BIRT:Eclipse商务智能报表工具开发实践指南 分类: BIRT 201 ...
- jsp04状态管理
1.http 协议的无状态性 无状态是指,当浏览器发送请求给服务器的时候,服务器会响应.但当同一个浏览器再次发送请求时,服务器不会知道是刚才那个浏览器. 简单说,服务器[不会保存用户状态],不会记得客 ...