diff --git a/5.leetcode/src/com/fanxb/common/Q135.java b/5.leetcode/src/com/fanxb/common/Q135.java new file mode 100644 index 0000000..387e2ac --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q135.java @@ -0,0 +1,46 @@ +package com.fanxb.common; + +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.stream.Stream; + +/** + * 旋转链表 + * 题目地址:https://leetcode-cn.com/problems/assign-cookies/submissions/ + * 解题思路: + * 贪心算法,想不明白 + * + * @author fanxb + * Date: 2020/6/9 15:10 + */ +public class Q135 { + + public static int candy(int[] ratings) { + int[] candys = new int[ratings.length]; + Arrays.fill(candys, 1); + //从左到右 + for (int i = 1; i < candys.length; i++) { + if (ratings[i] > ratings[i - 1]) { + candys[i] = candys[i - 1] + 1; + } + } + System.out.println(Arrays.toString(candys)); + for (int i = candys.length - 1; i > 0; i--) { + if (ratings[i - 1] > ratings[i] && candys[i - 1] <= candys[i]) { + candys[i - 1] = candys[i] + 1; + } + } + int count = 0; + for (int item : candys) { + count += item; + } + System.out.println(Arrays.toString(candys)); + return count; + } + + public static void main(String[] args) { + int[] s = {1, 2, 87, 87, 87, 2, 1}; + System.out.println(Arrays.toString(s)); + System.out.println(candy(s)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q455.java b/5.leetcode/src/com/fanxb/common/Q455.java new file mode 100644 index 0000000..751dd77 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q455.java @@ -0,0 +1,31 @@ +package com.fanxb.common; + +import java.util.Arrays; + +/** + * 旋转链表 + * 题目地址:https://leetcode-cn.com/problems/assign-cookies/submissions/ + * 解题思路: + * 贪心算法,优先让饥饿度最小的孩子吃能吃饱的最小的饼干 + * + * @author fanxb + * Date: 2020/6/9 15:10 + */ +public class Q455 { + + public static int findContentChildren(int[] g, int[] s) { + Arrays.sort(g); + Arrays.sort(s); + int i=0,j=0; + while (i=链表长度时会有很多无效的移动),真实k=k%(链表长度),然后发现没每向右移动k位,相当于让倒数第k个节点变成头节点 + * + * @author fanxb + * Date: 2020/6/9 15:10 + */ +public class Q61 { + + public static class ListNode { + int val; + ListNode next; + + ListNode() { + } + + ListNode(int val) { + this.val = val; + } + + ListNode(int val, ListNode next) { + this.val = val; + this.next = next; + } + } + + public static ListNode rotateRight(ListNode head, int k) { + if (head == null) { + return null; + } + if (k == 0) { + return head; + } + ListNode[] lists = new ListNode[500]; + int count = 0; + do { + lists[count++] = head; + head = head.next; + } while (head != null); + k = k % count; + if (k == 0) { + return lists[0]; + } else { + lists[count - k - 1].next = null; + lists[count - 1].next = lists[0]; + return lists[count - k]; + } + + } + + + public static void main(String[] args) { + ListNode node = new ListNode(0); + node.next = new ListNode(1); + node.next.next = new ListNode(2); + rotateRight(node, 2); + } +}