leetcode [#118] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (6)
目录1. 题目2. 解决方案3. 注意事项
题目Given numRows, generate the first numRows of Pascal’s triangle.
Example:given numRows = 5,Return
[     [1],    [1,1],   [1,2,1],  [1,3,3,1], [1,4,6,4,1]]
解决方案1234567891011121314151617181920public class Solution {    public List<List<Integer>> generate(int numRows) {        if(numRows == 0) return new ArrayList<List<Integer>>();                List<List<Integer>> result = new ArrayList<List<Integer>>();        for(int i = 1; i <= numRows; i++){            List<Integer>...阅读全文

leetcode [#122] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (7)
目录1. 题目2. 解决方案3. 注意事项
题目Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
解决方案1234567891011121314public class Solution {    public int maxProfit(int[] prices) {        int N = p...阅读全文

leetcode [#136] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (6)
目录1. 题目2. 解决方案3. 注意事项
题目Given an array of integers, every element appears twice except for one. Find that single one.
解决方案123456789101112131415161718import java.util.Hashtable;public class Solution {    public int singleNumber(int[] nums) {        int result = 0;        Hashtable<Integer, Integer> table = new Hashtable<>();        for(int i = 0; i < nums.length; i++){            if(!table.containsKey(nums[i])){                table.put(nums[i], 1);            } else {                table.put(...阅读全文

leetcode [#14] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (5)
目录1. 题目2. 解决方案3. 注意事项
题目Write a function to find the longest common prefix string amongst an array of strings.
解决方案1234567891011121314151617181920212223242526272829303132public class Solution {    public String longestCommonPrefix(String[] strs) {        int N = strs.length;        if(N == 0) return "";        if(N == 1) return strs[0];        StringBuilder builder = new StringBuilder();        for(int i = 0; i < N - 1; i++){            int len1 = strs[i].length();            int len2 = strs[i...阅读全文

leetcode [#147] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (4)
目录1. 题目2. 解决方案3. 注意事项
题目Sort a linked list using insertion sort.
解决方案12345678910111213141516171819202122232425262728293031323334353637383940/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Solution {    public ListNode insertionSortList(ListNode head) {        if(head == null) return head;        //新建一个节点,指向头结点        ListNode root = new ListNode(0);        root.next = head;        ListN...阅读全文