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 path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
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 path 4->9->5 represents the number 495.
The root-to-leaf path 4->9->1 represents the number 491.
The root-to-leaf path 4->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的更多相关文章

  1. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

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

  3. 【LeetCode OJ】Sum Root to Leaf Numbers

    # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...

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

  5. 【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 ...

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

  7. LeetCode题解之Sum Root to Leaf Numbers

    1.题目描述 2.问题分析 记录所有路径上的值,然后转换为int求和. 3.代码 vector<string> s; int sumNumbers(TreeNode* root) { tr ...

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

  9. Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)

    Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...

随机推荐

  1. HDOJ.2187 悼念512汶川大地震遇难同胞——老人是真饿了(贪心)

    悼念512汶川大地震遇难同胞--老人是真饿了 点我挑战题目 题目分析 每组数据给出所拥有的钱数,和大米的种类.每种大米给出单价(每单位重量)和大米的重量.求能买到的大米最大重量是多少? 采用贪心算法. ...

  2. React注释

    React中注释有以下三种 var content = ( <Nav> {/* 一般注释, 用 {} 包围 */} <Person /* 多 行 注释 */ name={window ...

  3. eclipse ide for java ee developers 开发环境搭建(J2EE) 【转载】

    使用eclipse真的有年头了,相信java程序员没有不知道它的,最近在给团队中新来的应届生做指导,专门讲解了一下Eclipse开发环境的搭建过程, 一是帮助他们尽快的熟悉IDE的使用,二也是保证团队 ...

  4. MongoDB插入数据的3种方法

    insert()方法: 下面是在inventory集合中插入一个三个字段的文档: db.inventory.insert( { _id: 10, type: "misc", ite ...

  5. Codeforces Round #345 (Div. 2) B

    B. Beautiful Paintings time limit per test 1 second memory limit per test 256 megabytes input standa ...

  6. hdu 5625

    Clarke and chemistry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  7. noip模拟赛 helloworld

    分析:对于第一个点,答案为26^n - 25^n,这个很好想.另外30%的点因为n <= 5,所以可以直接暴力搜索. 数学方法不是很好处理,考虑dp,设f[i][j]为前i位匹配到危险串第j位的 ...

  8. thrift源码阅读笔记

    http://note.youdao.com/noteshare?id=3f3cf77bf70656ac626f7bf2099063c7

  9. HDU3579 线性同余方程(模板 余数不一定互质)

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  10. spring boot 在IDEA控制台中打印彩色日志

    只需要在application.properties中加入 spring.output.ansi.enabled=ALWAYS 即可