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. HDU 1715 大数java

    大菲波数 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. Android Studio中进行单元测试

    写单元测试类 1.创建单元测试文件夹,即新建一个用于单元测试的包,存放单元测试的类. 2.创建一个类如 ExampleTest,注意要继承自InstrumentationTestCase类. 3.创建 ...

  3. 允许Traceroute探测

    允许Traceroute探测 漏洞描述: 允许Traceroute探测 漏洞描述 本插件使用Traceroute探测来获取扫描器与远程主机之间的路由信息.攻击者也可以利用这些信息来了解目标网络的网络拓 ...

  4. javascript实现购物车思路

    /* 思路: 第一步:获取所要操作的节点对象 第二步:当页面加载完后,需要计算本地cookie存了多少[个]商品,把个数赋值给count 第三步:为每一个商品对应的添加购物车按钮绑定一个点击事件onc ...

  5. 前后端分离中,gulp实现头尾等公共页面的复用 前言

    前言 通常我们所做的一些页面,我们可以从设计图里面看出有一些地方是相同的.例如:头部,底部,侧边栏等等.如果前后端分离时,制作静态页面的同学,对于这些重复的部分只能够通过复制粘贴到新的页面来,如果页面 ...

  6. [技巧篇]14.据说SSH框架需要的监听器,IntrospectorCleanupListener

    开发这么久,我也没有使用过IntrospectorCleanupListener监听器,今天偶尔看到一篇文章,虽然没有怎么读懂,也不太理解,但是好像给官方提供一些解释!给自己留一个备注,多点东西因为没 ...

  7. js和jq实现全选反选

    在前端中用到全选反选的案例并不少,在这里呢我就实现这个功能给大家参考参考. 这里呢就先贴上我的html和css代码 <div class="wrap"> <tab ...

  8. [USACO Mar08] 牛跑步

    http://www.cogs.pro/cogs/problem/problem.php?pid=133 ★★★   输入文件:cowjog.in   输出文件:cowjog.out   简单对比时间 ...

  9. 数据结构:Rope-区间翻转

    BZOJ1269 上一篇文章介绍了Rope的简单应用,这里多了一个操作,区间翻转 同时维护一正一反两个rope……反转即交换两个子串 下面给出代码: #include<cstdio> #i ...

  10. PHP网页架站

    目前,Windows下已经有集成的PHP网页架站工具,例如:AppServ.WampServer.这些软件将Apache.PHP.MySQL.phpMyAdmin集成到一起,极大地方便了开发者架站.但 ...