leetcode

来源:互联网 发布:测量编程视频教学 编辑:程序博客网 时间:2024/06/09 16:18

----------------------------------------------------------------------------



------------------------------------------------------------------------------

210 Say you have an array for which the i th 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.

package leetcode;/*created by fanqunsong    Date : 2017/12/22    Time : 16:32    Tips:先买才能卖    */public class Solution210 {    public static void main(String[] args) {        int[] num = new int[]{1,2};        System.out.println(maxProfit(num));    }    public static int maxProfit(int[] prices) {        if(prices.length == 0 ||prices.length == 1 || prices == null){            return 0;        }        int min = prices[0];        int res = 0;        for (int i = 1; i < prices.length; i++) {            if(prices[i]>min){                res = res > prices[i] - min?res : prices[i] - min;            }            else {                min = prices[i];            }        }        return res;    }}

-------------------------------------------------------------------------------------

209 Say you have an array for which the i th 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).

package leetcode;/*created by fanqunsong    Date : 2017/12/22    Time : 17:09    */public class Solution209 {    public static void main(String[] args) {        int[] num = new int[]{1,2};        System.out.println(maxProfit(num));    }    public static int maxProfit(int[] prices) {        if(prices.length == 0 ||prices.length == 1 || prices == null){            return 0;        }        int[] profit = new int[prices.length-1];        for (int i = 1; i < prices.length; i++) {            profit[i-1] = prices[i] - prices[i-1];        }        int sum = 0;        for (int i = 0; i < profit.length; i++) {            if (profit[i]>0){                sum += profit[i];            }        }        return sum;    }}

------------------------------------------------------------------------------------

401 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space to hold additional elements from B.

The number of elements initialized in A and B are m and n respectively.

package leetcode;/*created by fanqunsong    Date : 2017/12/21    Time : 16:59    *//* Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space to hold additional elements from B.The number of elements initialized in A and B are m and n respectively. *//*tips:从后往前记录 */public class Solution401 {    public void merge(int A[], int m, int B[], int n) {        int j = m+n-1;        m--;        n--;        while(m>=0 && n>=0){            A[j--] = A[m] > B[n]? A[m--] : B[n--];        }        if(n>=0){            while (n>=0){                A[j--] = B[n--];            }        }    }}


-------------------------------------

502 Given a number represented as an array of digits, plus one to the number

public class Solution {    public int[] plusOne(int[] digits) {        int sum = digits.length;        digits[sum-1] ++;        for (int i = sum-1; i >0 ; i--) {            if(digits[i]>9){                digits[i] = 0;                digits[i-1]++;            }        }        if(digits[0]<10){            return digits;        }        int[] res = new int[sum+1];        res[0] = 1;        res[1] = 0;        for (int i = 2; i <= sum ; i++) {            res[i] = digits[i-1];        }        return res;    }}

-------------------------------------

原创粉丝点击