[LeetCode]404. Sum of Left Leaves
题目
Find the sum of all left leaves in a given binary tree.
Example:
1  | 3  | 
方法
递归遍历,如果为左节点,则打上标记isLeft,如果该节点没有左右子节点,则将该节点值累加到最后的结果中
python代码
1  | class TreeNode(object):  | 
Find the sum of all left leaves in a given binary tree.
Example:
1  | 3  | 
递归遍历,如果为左节点,则打上标记isLeft,如果该节点没有左右子节点,则将该节点值累加到最后的结果中
1  | class TreeNode(object):  | 
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Note:
You may assume that both strings contain only lowercase letters.
1  | canConstruct("a", "b") -> false  | 
Easy
遍历2个字符串,索引分别为i, j,如果ransomNote[i] == magazine[j],则i++, j++,否则j++。如果i == len(ransomNote),则表示能够用magazine的字符组成ransomNote,否则则不能
1  | class Solution(object):  | 
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.
Note:
You may assume the greed factor is always positive.
You cannot assign more than one cookie to one child.
Example 1:
1  | Input: [1,2,3], [1,1]  | 
Example 2:
1  | Input: [1,2], [1,2,3]  | 
Easy
首先对s和g排序,如果s~j~=gi,则content_count++,j++,i++;否则j++,直到s~j~>=g~i~。注意i和j边界的处理
1  | class Solution(object):  | 
Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.
Example:
1  | Input:  | 
Easy
这是一个数学题,假设数组中最小的数为min,需要移动x次,最后数组中的数都为m,则其实是一个求x的方法
1  | (n - 1) * x + sum = n * m  | 
1  | class Solution(object):  | 
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.
Example 1:
1  | Input:  | 
Note:
Easy
对二叉树进行深度遍历,将深度level作为参数传递,记录每一级的sum和个数n,最后将sum/n保存到结果列表中即可
1  | class TreeNode(object):  | 
X city opened a new cinema, many people would like to go to this cinema. The cinema also gives out a poster indicating the movies’ ratings and descriptions.
Please write a SQL query to output movies with an odd numbered ID and a description that is not ‘boring’. Order the result by rating.
For example, table cinema:
1  | +---------+-----------+--------------+-----------+  | 
For the example above, the output should be:
1  | +---------+-----------+--------------+-----------+  | 
Easy
1  | # Write your MySQL query statement below  | 
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 query and no intermediate temp table.
For example:
1  | | id | name | sex | salary |  | 
After running your query, the above salary table should have the following rows:
1  | | id | name | sex | salary |  | 
Easy
需要用到sql里的if
1  | # Write your MySQL query statement below  | 
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.
You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.
Example 1:
1  | Input:  | 
采用递归的方法。假设有2个根节点t1,t2,用t1保存最后的合并结果。如果t1和t2都不为空,则t1.val = t1.val+t2.val,递归调用赋值t1.left = mergeTrees(t1.left, t2.left)和t1.right = mergeTrees(t1.right, t2.right); 如果t1为空,t2不为空,则t1 = t2,最后返回t1节点
Easy
1  | # Definition for a binary tree node.  | 
There is a table
World
 1
2
3
4
5
6
7
8
9 +-----------------+------------+------------+--------------+---------------+
| name | continent | area | population | gdp |
+-----------------+------------+------------+--------------+---------------+
| Afghanistan | Asia | 652230 | 25500100 | 20343000 |
| Albania | Europe | 28748 | 2831741 | 12960000 |
| Algeria | Africa | 2381741 | 37100000 | 188681000 |
| Andorra | Europe | 468 | 78115 | 3712000 |
| Angola | Africa | 1246700 | 20609294 | 100990000 |
+-----------------+------------+------------+--------------+---------------+A country is big if it has an area of bigger than 3 million square km or a population of more than 25 million.
Write a SQL solution to output big countries’ name, population and area.
For example, according to the above table, we should output:
1  | +--------------+-------------+--------------+  | 
Easy
没想到LeetCode有这么简单的题,直接写sql即可
1  | # Write your MySQL query statement below  | 
Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings.
You need to help them find out their common interest with the least list index sum. If there is a choice tie between answers, output all of them with no order requirement. You could assume there always exists an answer.
Example 1:
1  | Input:  | 
Example 2:
1  | Input:  | 
Note:
Easy
用minSum保存最小的索引和,用一个dict记录list1中每个单词对应的索引序号。遍历list2,将值存入word中,计算word在list1和list2中的索引和,如果小于minSum,则替换minSum,并清空结果list,加入该word,如果==minSum,则直接加入word
1  | import sys  |