diff --git a/5.leetcode/src/com/fanxb/common/Q149.java b/5.leetcode/src/com/fanxb/common/Q149.java new file mode 100644 index 0000000..9069daa --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q149.java @@ -0,0 +1,37 @@ +package com.fanxb.common; + +/** + * Created with IntelliJ IDEA + * 寻找旋转排序数组中的最小值 + * 地址: https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/ + * 思路: 需要找到分界点 + * + * @author fanxb + * Date: 2020/6/11 9:56 + */ +public class Q149 { + public int maxPoints(int[][] points) { + int max = 1; + for (int i = 0; i < points.length; i++) { + int[] a1 = points[i]; + for (int j = i + 1; j < points.length; j++) { + int[] a2 = points[j]; + int n = 2; + for (int k = j + 1; k < points.length; k++) { + int[] a3 = points[k]; + if ((a2[1] - a1[1]) * (a3[0] - a1[0]) == (a3[1] - a1[1]) * (a2[0] - a1[0])) { + n++; + } + } + if (n > max) { + max = n; + } + } + } + return max; + } + + public static void main(String[] args) { + System.out.println(new Q149().maxPoints(new int[][]{{1, 1}, {3, 2}, {5, 3}, {4, 1}, {2, 3}, {1, 4}})); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q1833.java b/5.leetcode/src/com/fanxb/common/Q1833.java new file mode 100644 index 0000000..8991c6a --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q1833.java @@ -0,0 +1,29 @@ +package com.fanxb.common; + +import java.util.*; + +/** + * 两数相加 + * + * @author fanxb + * @date 2021/6/1 + **/ +public class Q1833 { + public int maxIceCream(int[] costs, int coins) { + Arrays.sort(costs); + int count = 0; + for (int i = 0; i < costs.length; i++) { + coins -= costs[i]; + if (coins >= 0) { + count++; + } else { + break; + } + } + return count; + } + + public static void main(String[] args) { + System.out.println(new Q1833().maxIceCream(new int[]{1, 3, 2, 4, 1}, 7)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q401.java b/5.leetcode/src/com/fanxb/common/Q401.java index b2a2173..5b78a0b 100644 --- a/5.leetcode/src/com/fanxb/common/Q401.java +++ b/5.leetcode/src/com/fanxb/common/Q401.java @@ -11,26 +11,28 @@ import java.util.*; * @date 2021/6/1 **/ public class Q401 { - private static Map> map=new HashMap<>(10); + private static Map> map = new HashMap<>(10); + static { - for(int i=0;i<=11;i++){ - for(int j=0;j<=59;j++){ - LinkedList linkedList = map.computeIfAbsent(count(i)+count(j),k->new LinkedList<>()); - linkedList.add(i+":"+(j<10?"0"+j:j)); + for (int i = 0; i <= 11; i++) { + for (int j = 0; j <= 59; j++) { + LinkedList linkedList = map.computeIfAbsent(count(i) + count(j), k -> new LinkedList<>()); + linkedList.add(i + ":" + (j < 10 ? "0" + j : j)); } } } - private static int count(int x){ - int count=0; - while (x>0){ - x-=x&(-x); - count++; + + private static int count(int x) { + int count = 0; + while (x > 0) { + x -= x & (-x); + count++; } return count; } public List readBinaryWatch(int turnedOn) { - return map.getOrDefault(turnedOn,new LinkedList()); + return map.getOrDefault(turnedOn, new LinkedList()); } public static void main(String[] args) {