学习目标:
学习Spring MVC的数据绑定
运用二分查找法的思想求解leetcode上的两道问题
学习内容:
学习了URL映射,学习了用方法参数以及实体对象接收请求参数,还学习了接收表单的复合数据。
leetcode875:
import java.util.Arrays; public class Leetcode875 { public int minEatingSpeed(int[] piles, int H) { int l=1,r= Arrays.stream(piles).max().getAsInt(); while (l<r){ int mid = l + (r - l) / 2; if (eatingTime(piles, mid) <= H) r = mid; else l=mid+1; } return l; } private int eatingTime(int[] piles,int k){ int res=0; for(int pile:piles){ res += pile / k + (pile % k > 0 ? 1 : 0); } return res; } }
leetcode1011:
package lc206; import java.util.Arrays; public class Leetcode1011 { public int shipWithinDays(int[] weights, int days) { int l= Arrays.stream(weights).max().getAsInt(); int r=Arrays.stream(weights).sum(); while (l<r){ int mid=l+(r-l)/2; if(days(weights,mid)<=days){ r=mid; }else { l=mid+1; } } return l; } private int days(int[] weights,int k){ //cur为载重,res为返回结果 int cur=0,res=0; for(int weight:weights){ if(cur+weight<=k) cur+=weight; else { res++; cur=weight; } } res++; return res; } }
学习时间:
07:00-10:00 15:30-16:00
学习产出:
掌握了一些基本的SpringMVC数据绑定的方法。