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; }}
-------------------------------------
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- leetcode
- IT行业的风投
- Android 获得设备状态信息、Mac地址、IP地址
- ANDROID HTML重定向之后返回原生view处理
- 使用gdal库拆分字符串
- 20. 递归和IO流(字节流和高效字节流)
- leetcode
- Java实现的软键盘并可以向Windows任意窗口发送中/英文信息
- 2017年浙江工业大学大学生程序设计迎新赛热身赛-E:递推(矩阵快速幂)
- 通过固定格式的DataFrame来创建VOC格式XML
- MS8413 光纤同轴解码 soundbar方案
- 最近了解到的知识点
- 搭建并配置splunk
- jquery 操作 checkbox 全选、反选、清除
- 扫盲一: 加密, 认证, 公钥,私钥,证书