Tree 1 Tree 2
1 2
/ \ / \
3 2 1 3
/ \ \
5 4 7
Output:
Merged tree:
3
/
4 5
/ \ \
5 4 7
Note: The merging process must start from the root nodes of both trees.
[Answer]
Runtime: 0 ms, faster than 100.00% of Java online submissions for Merge Two Binary Trees.
Memory Usage: 39.9 MB, less than 99.92% of Java online submissions for Merge Two Binary Trees.
/**
-
Definition for a binary tree node.
-
public class TreeNode {
-
int val;
-
TreeNode left;
-
TreeNode right;
-
TreeNode(int x) { val = x; }
-
}
*/
class Solution {
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if (t1 == null && t2 == null)
return null;
if (t1 == null)
return t2;
if (t2 == null)
return t1;
t1.val = t1.val + t2.val;
t1.left = mergeTrees(t1.left, t2.left);
t1.right = mergeTrees(t1.right, t2.right);
return t1;
}
}
620. Not Boring Movies
Runtime: 124 ms, faster than 74.64% of MySQL online submissions for Not Boring Movies.
Memory Usage: N/A
Write your MySQL query statement below
select * from cinema where id%2=1 and description != ‘boring’ order by rating desc;
627. Swap Salary
[Description]
SQL Schema
Given a table salary, such as the one below, that has m=male and f=female values. Swap all f and m values (i.e., change all f values to m and vice versa) with a single update statement and no intermediate temp table.
Note that you must write a single update statement, DO NOT write any select statement for this problem.
Example:
| id | name | sex | salary |
| — | — | — | — |
| 1 | A | m | 2500 |
| 2 | B | f | 1500 |
| 3 | C | m | 5500 |
| 4 | D | f | 500 |
After running your update statement, the above salary table should have the following rows:
| id | name | sex | salary |
| — | — | — | — |
| 1 | A | f | 2500 |
| 2 | B | m | 1500 |
| 3 | C | f | 5500 |
| 4 | D | m | 500 |
[Answer]
Runtime: 151 ms, faster than 61.29% of MySQL online submissions for Swap Salary.
Memory Usage: N/A
Write your MySQL query statement below
UPDATE salary SET sex = IF(sex = ‘f’, ‘m’, ‘f’)
657. Robot Return to Origin
[Description]
There is a robot starting at po
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
sition (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.
The move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.
Note: The way that the robot is “facing” is irrelevant. “R” will always make the robot move to the right once, “L” will always make it move left, etc. Also, assume that the magnitude of the robot’s movement is the same for each move.
Example 1:
Input: “UD”
Output: true
Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.
Example 2:
Input: “LL”
Output: false
Explanation: The robot moves left twice. It ends up two “moves” to the left of the origin. We return false because it is not at the origin at the end of its moves.
[Answer]
Runtime: 5 ms, faster than 94.72% of Java online submissions for Robot Return to Origin.
Memory Usage: 36.8 MB, less than 99.80% of Java online submissions for Robot Return to Origin.
class Solution {
public boolean judgeCircle(String moves) {
if (moves == null || moves.trim().length() == 0)
return true;
int x = 0, y = 0;
for (char c : moves.toCharArray()) {
switch © {
case ‘L’:
x–;
break;
case ‘R’:
x++;
break;
case ‘U’: