分享小红书算法三个题
小红书是第一次笔试全部AC的,也来不自量力跟大家分享一下。写得比较啰嗦,大家看看就好了。
第一题
import java.util.*; class ListNode { int val; ListNode next = null; ListNode(int val){ this.val = val; } } public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); int k = sc.nextInt(); sc.close(); String[] strs = str.split(" "); int[] node = new int[strs.length]; for (int i = 0; i < node.length; i++) { node[i] = Integer.parseInt(strs[i]); } ListNode head = new ListNode(node[0]); ListNode p = head; for (int i = 1; i < node.length; i++) { ListNode tmp = new ListNode(node[i]); p.next = tmp; p = p.next; } ListNode res = reverseKGroup(head, k); ListNode index = res; System.out.print(index.val); while(index.next != null) { index = index.next; System.out.print(" " + index.val); } } public static ListNode reverseKGroup(ListNode head, int k) { if (k <= 1) { return head; } ListNode root = new ListNode(0); ListNode groupHead = root; ListNode curr = head; ListNode groupTail = head; ListNode next; int count = 0; while (curr != null) { if (count == 0) { groupTail = curr; } count++; next = curr.next; curr.next = groupHead.next; groupHead.next = curr; curr = next; if (count == k) { groupHead = groupTail; count = 0; } } if (count != 0) { curr = groupHead.next; groupHead.next = null; while (curr != null) { next = curr.next; curr.next = groupHead.next; groupHead.next = curr; curr = next; } } return root.next; } }
第二题
import java.util.*; public class jiecheng0 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.close(); int num = 0; for (int i = 1; i <= n; i++) { num += zeroNum(i); } System.out.println(num); } public static int zeroNum(int n) { int count = 0; while (n != 0) { count += n / 5; n = n / 5; } return count; } }
第三题
import java.util.*; public class fenban { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int personNum = sc.nextInt(); int requestNum = sc.nextInt(); int[][] reqs = new int[requestNum][2]; for (int i = 0; i < requestNum; i++) { reqs[i][0] = sc.nextInt(); reqs[i][1] = sc.nextInt(); } sc.close(); sort(reqs, new int[] {0,1}); HashSet<Integer> hs1 = new HashSet<>(); HashSet<Integer> hs2 = new HashSet<>(); boolean flag = true; hs1.add(reqs[0][0]); hs2.add(reqs[0][1]); for (int i = 1; i < requestNum; i++) { if ((hs1.contains(reqs[i][0]) && hs1.contains(reqs[i][1])) || (hs2.contains(reqs[i][0]) && hs2.contains(reqs[i][1]))) { flag = false; continue; } if (hs1.contains(reqs[i][0]) && !hs2.contains(reqs[i][1])) { hs2.add(reqs[i][1]); continue; } else if (hs2.contains(reqs[i][0]) && !hs1.contains(reqs[i][1])) { hs1.add(reqs[i][1]); continue; } } if (flag == true) System.out.println(1); else System.out.println(0); } public static void sort(int[][] ob, final int[] order) { Arrays.sort(ob, new Comparator<Object>() { public int compare(Object o1, Object o2) { int[] one = (int[]) o1; int[] two = (int[]) o2; for (int i = 0; i < order.length; i++) { int k = order[i]; if (one[k] > two[k]) { return 1; } else if (one[k] < two[k]) { return -1; } else { continue; } } return 0; } }); } }#小红书#