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. 51nod 1172 Partial Sums V2 卡精度的任意模数FFT

    卡精度的任意模数fft模板题……这道题随便写个表就能看出规律来(或者说考虑一下实际意义),反正拿到这题之后,很快就会发现他是任意模数fft模板题.然后我就去网上抄了一下板子……我打的是最土的任意模数f ...

  2. 【HASH】【UVA 10125】 Sumset

    传送门 Description 给定一个整数集合S,求一个最大的d,满足a+b+c=d,其中a,b,c,d∈S Input 多组数据,每组数据包括: 第一行一个整数n,代表元素个数 下面n行每行一个整 ...

  3. 剑桥offer(31~40)

    31.题目描述 统计一个数字在排序数组中出现的次数. 思路:找到最低和最高,相减 class Solution { public: int GetNumberOfK(vector<int> ...

  4. Linux之poll与select20160619

    使用非阻塞I/O的应用程序通常会使用select()和poll()系统调用查询是否可对设备进行无阻塞的访问,这两个系统调用最终又会引发设备驱动中的poll()函数被执行 如果当前不可读(先调用驱动.p ...

  5. JavaScript引擎是单线程的

    从基础的层面来讲,理解JavaScript的定时器是如何工作的是非常重要的.计时器的执行常常和我们的直观想象不同,那是因为JavaScript引擎是单线程的.我们先来认识一下下面三个函数是如何控制计时 ...

  6. JavaScript正则表达式的浏览器的差异

    JavaScript中的正则表达式在不同的浏览器中得到的结果可能会有差异,下面把正则表达式在五大主流浏览器(IE.Firefox.Chrome.Safari.Opera,以当前版本为准)之间的差异整理 ...

  7. unix awk手册读书笔记

    http://note.youdao.com/noteshare?id=9ac76eb63a53ac000f7814454642d2b0

  8. Struts整合ExtJS

    1准备工作: 除了平时引入的struts2的jar包以外,还需要引入struts2-json-plugin-2.1.8.1.jar:json-lib-2.1.jar这两个包. 2.建立我们的model ...

  9. 深度解析Java多线程的内存模型

    内部java内存模型 硬件层面的内存模型 Java内存模型和硬件内存模型的联系 共享对象的可见性 资源竞速 Java内存模型很好的说明了JVM是如何在内存里工作的,JVM可以理解为java执行的一个操 ...

  10. 搜索:N皇后

    N皇后问题是DFS的代表性问题,其最难的地方就是在判重这里,想明白了怎么判重的话问题就很显然了. 这里给出两份代码,其中第一份代码的效率更好,就是在判重上下了功夫.当然,我记得还有使用位运算进行判重的 ...