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-9only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path
1->2->3which 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 [code]1->2represents the number
12. The root-to-leaf path
1->3represents the number
13. Therefore, sum = 12 + 13 =
25.[/code]
Example 2:
Input: [4,9,0,5,1] 4 / \ 9 0 / \ 5 1 Output: 1026 Explanation: The root-to-leaf path [code]4->9->5represents the number 495. The root-to-leaf path
4->9->1represents the number 491. The root-to-leaf path
4->0represents the number 40. Therefore, sum = 495 + 491 + 40 =
1026.[/code]
思路:本来想用递归解决这个问题,结果在传参时出现了一个很典型的错误,结果还是点来了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 解题报告
- leetcode解题报告:129. Sum Root to Leaf Numbers
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
- [leetcode] 129. Sum Root to Leaf Numbers 解题报告
- LeetCode 129. Sum Root to Leaf Numbers 解题报告
- Leetcode 129. Sum Root to Leaf Numbers 计算路径和 解题报告
- [Leetcode] 129. Sum Root to Leaf Numbers 解题报告
- leetcode --day12 Surrounded Regions & Sum Root to Leaf Numbers & Longest Consecutive Sequence
- 【leetcode刷题笔记】Sum Root to Leaf Numbers
- LeetCode_Sum Root to Leaf Numbers
- [Leetcode 129, Medium] Sum Root to Leaf Number
- leetcode Sum Root to Leaf Numbers
- 【leetcode】Sum Root to Leaf Numbers
- leetcode-Sum Root to Leaf Numbers
- Leetcode: Sum Root to Leaf Number
- leetcode129---Sum Root to Leaf Numbers
- Leetcode Sum Root to Leaf Numbers
- leecode 解题总结:129. Sum Root to Leaf Numbers
- 《leetCode》:Sum Root to Leaf Numbers
- leetcode笔记:Sum Root to Leaf Numbers