leetcode [#104] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (7)
目录1. 题目2. 解决方案3. 注意事项
题目Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
解决方案12345678910111213141516171819202122232425262728/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public int maxDepth(TreeNode root) {        int depth = 0;        if(...阅读全文

leetcode [#119] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (6)
目录1. 题目2. 解决方案3. 注意事项
题目Given an index k, return the kth row of the Pascal’s triangle.
Example:given k = 3,Return [1,3,3,1].
解决方案1234567891011121314151617181920212223public class Solution {    public List<Integer> getRow(int rowIndex) {        List<Integer> zero = new ArrayList<>();        zero.add(1);        if(rowIndex == 0) return zero;        List<Integer> row = new ArrayList<>();        List<List<Integer>> result = new ArrayList<List<Integer>>();        for(int i = 1; i <= rowIndex + 1; ...阅读全文

leetcode [#121] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (9)
目录1. 题目2. 解决方案3. 注意事项
题目Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Example1:Input: [7, 1, 5, 3, 6, 4]Output: 5
max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
Example1:Input: [7, 6, 4, 3, 1]Output: 0
In this case, no transaction is done, i.e. m...阅读全文

leetcode [#1] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (5)
目录1. 题目2. 解决方案3. 注意事项
题目Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution.
Example:Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1].
Note:The return format had been changed to zero-based indices. Please read the above updated description carefully.
解决方案12345678910111213141516171819public class Solution {    public int[] twoSum(in...阅读全文