Leetcode-Java(十八)

171. Excel Sheet Column Number

Leetcode-Java(十八)_第1张图片
class Solution {
    public int titleToNumber(String s) {
        if(s==null || s.length()==0)
            return 0;
        int res = 0;
        for(int i = 0;i

172. Factorial Trailing Zeroes

Leetcode-Java(十八)_第2张图片

阶乘后面0的个数是有递推公式的:

Leetcode-Java(十八)_第3张图片
class Solution {
    public int trailingZeroes(int n) {
        return n < 5 ? 0 : n / 5 + trailingZeroes(n / 5);
    }
}

173. Binary Search Tree Iterator

Leetcode-Java(十八)_第4张图片

用一个Stack记录从根节点到当前节点的路径。next的时候就返回Stack最上面的元素。不过拿出最上面的元素后,我们还要看一下这个被返回的元素是否有右节点,如果有的话,就把它的右节点及右节点的所有左边节点都压入栈中。另外,初始化栈时,我们要找到最左边的节点,也就是中序遍历的第一个节点,并把根到第一个节点的路径都压入栈。

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

public class BSTIterator {
    Stack stack;
    public BSTIterator(TreeNode root) {
        stack = new Stack();
        if(root!=null){
            TreeNode p = root;
            while(p!=null){
                stack.push(p);
                p = p.left;
            }
        }
    }

    /** @return whether we have a next smallest number */
    public boolean hasNext() {
        return !stack.empty();
    }

    /** @return the next smallest number */
    public int next() {
        if(hasNext()){
            TreeNode top = stack.pop();
            TreeNode p = top.right;
            while(p!=null){
                stack.push(p);
                p = p.left;
            }
            return top.val;
        }
        return 0;
    }
}

/**
 * Your BSTIterator will be called like this:
 * BSTIterator i = new BSTIterator(root);
 * while (i.hasNext()) v[f()] = i.next();
 */

174. Dungeon Game

Leetcode-Java(十八)_第5张图片

这里使用动态规划的思路:
1、如果考虑从左上角到右下角的思路,要考虑路径上有多少药,而且路径上所有的地方的血量都不能小于1,这个需要考虑的因素太多,所以用另一种思路,即从右下角到左上角。
2、要考虑任意位置的最小血量是1.

class Solution {
    public int calculateMinimumHP(int[][] dungeon) {
        if(dungeon==null || dungeon.length==0)
            return 1;
        int m = dungeon.length;
        int n = dungeon[0].length;
        int[][] minHp = new int[m][n];
        for(int i = m-1;i>=0;i--){
            for(int j=n-1;j>=0;j--){
                if(i==m-1 && j==n-1)
                    minHp[i][j] = 1 + (dungeon[i][j] < 0?Math.abs(dungeon[i][j]):0);
                else if(i==m-1)
                    minHp[i][j] = Math.max(minHp[i][j+1] - dungeon[i][j],1);
                else if(j==n-1)
                    minHp[i][j] = Math.max(1,minHp[i+1][j] - dungeon[i][j]);
                else
                    minHp[i][j] = Math.max(1,Math.min(minHp[i][j+1]-dungeon[i][j],minHp[i+1][j]-dungeon[i][j]));  
            }
        }
        return minHp[0][0];
    }
}

175. Combine Two Tables

Leetcode-Java(十八)_第6张图片

偶尔做一道数据库的题陶冶下情操,这题考查的是左链接:

# Write your MySQL query statement below
select 
    FirstName,LastName,City,State
from
    (select * from  Person) t1 
    left join
    (select * from Address) t2
    on t1.PersonId = t2.PersonId

176. Second Highest Salary

Leetcode-Java(十八)_第7张图片
# Write your MySQL query statement below
select 
    max(Salary) as SecondHighestSalary 
from 
    Employee 
where 
    Salary < (select Max(Salary) from Employee)

177. Nth Highest Salary

Leetcode-Java(十八)_第8张图片

这里考察limit的应用,第一个参数是起始位置,后面是取几条

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
        DECLARE M INT;
      Set M=N-1;
  RETURN (
      # Write your MySQL query statement below.
      
      SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT M, 1
      
  );
END

178. Rank Scores

Leetcode-Java(十八)_第9张图片

两个表作笛卡尔积,然后过滤掉左表的Score比右表Score小的记录,此时对于左表的每个ID来说,留下的记录都是比自己大或者相等的记录,然后按照左表的ID进行分组,统计比ID对应的Score大的Score有几个。最后不要忘记排序哟。

# Write your MySQL query statement below
select 
    t1.Score,count(distinct t2.Score)  as Rank
from 
    (select * from Scores) t1
    join
    (select * from Scores) t2
where t1.Score <= t2.Score
group by t1.Id,t1.Score
order by t1.Score desc

179. Largest Number

Leetcode-Java(十八)_第10张图片

这个题是要设计一个排序规则,对于两个字符串来说,如果a+b>b+a,那么a要排在b前面。

这里要介绍一下compareTo函数和Arrays.sort()方法。

compareTo:如果指定的数与参数相等返回0。如果指定的数小于参数返回 -1。如果指定的数大于参数返回 1。
Arrays.sort():默认是升序,compare方法返回负数时代表obj1 < obj2 ,compare方法返回0时代表obj1 = obj2 ,compare方法返回正数时代表obj1 > obj2

那么也就是说,a排在b前面的规则就是方法,compare方法返回一个负数,那么也就是说b+a

这里还要注意全0的情况,比如数组是两个0,那么返回结果只能有一个0

class Solution {
    private class LargerNumberComparator implements Comparator {
        @Override
        public int compare(String a, String b) {
            String order1 = a + b;
            String order2 = b + a;
           return order2.compareTo(order1);
        }
    }
    
    public String largestNumber(int[] nums) {
        if(nums==null || nums.length==0)
            return "";
        String[] strnums = new String[nums.length];
        for(int i = 0;i

180. Consecutive Numbers

Leetcode-Java(十八)_第11张图片

两次内连接,同时要做一次去重。

# Write your MySQL query statement below
select
    t1.Num as ConsecutiveNums
from
    (select * from Logs) t1
    inner join
    (select * from Logs) t2
    on t1.Id = t2.Id - 1
    inner join
    (select * from Logs) t3
    on t1.Id = t3.Id - 2
where 
    t1.Num = t2.Num and t1.Num = t3.Num
group by t1.Num

你可能感兴趣的:(Leetcode-Java(十八))