LC628. 三个数的最大乘积
//将数组进行排序过后,对情况进行分类讨论即可,均为正,均为负,有正有负。
class Solution {
public int maximumProduct(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
int res = Math.max(nums[0] * nums[1] * nums[n - 1],nums[n - 1] * nums[n - 2] * nums[n - 3]);
return res;
}
}
LC107. 二叉树的层序遍历 II
//老经典二叉树的层次遍历了
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> res = new ArrayList();
if(root == null)
return res;
Queue<TreeNode> queue = new LinkedList();
queue.offer(root);
while(!queue.isEmpty()){
int len = queue.size();
List<Integer> item = new ArrayList();
while(len != 0){
TreeNode node = queue.poll();
item.add(node.val);
if(node.left != null) queue.offer(node.left);
if(node.right != null) queue.offer(node.right);
len--;
}
res.add(item);
}
Collections.reverse(res);
return res;
}
}
LC172. 阶乘后的零
//递归去计算,起始就是看是否有5阶乘因子,因为有5的话肯定会有余数0.
class Solution {
public int trailingZeroes(int n) {
return n < 5 ? 0 : (n/5 + trailingZeroes(n / 5));
}
}