NC_131_GetMedian NC_132_Joseph_circle NC_133_ODDEVENLIST
package org.example.interview.practice; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Objects; /** * @author xianzhe.ma * @date 2021/11/3 */ public class NC_131_GetMedian { private List<Integer> list; public void Insert(Integer num) { if (Objects.isNull(list)) { list = new ArrayList<>(); } list.add(num); Collections.sort(list); } public Double GetMedian() { Integer[] array = new Integer[list.size()]; list.toArray(array); int size = list.size(); if (size % 2 == 1) { return array[size/2]/1.0; } else { int num1 = array[size/2-1]; int num2 = array[size/2]; return (num1 + num2)/2.0; } } }
package org.example.interview.practice; import java.util.ArrayList; /** * @author xianzhe.ma * @date 2021/11/4 */ public class NC_132_Joseph_circle { public int ysf (int n, int m) { // write code here // write code here ArrayList<Integer> list = new ArrayList<Integer>(); // 先构造该数组序列 for(int i = 1; i <= n; i++){ list.add(i); } // 第一次从索引为0处进行报数 int id = 0; // 直到剩最后一个人的时候停止循环 while(n > 1){ // 从开始报数的索引处向后数m-1个人即是被淘汰的人的索引处 id = (id + m -1) % n; // 将被淘汰的人删除掉 list.remove(id); // 将人数减1 n--; } // 最后剩下的一个人即是最终的获胜者 return list.get(0); } }
package org.example.interview.practice; import java.util.Objects; /** * @author xianzhe.ma * @date 2021/8/1 */ public class NC_133_ODDEVENLIST { public static void main (String[] args) { ListNode node1 = new ListNode(1); ListNode node2 = new ListNode(4); ListNode node3 = new ListNode(6); ListNode node4 = new ListNode(3); ListNode node5 = new ListNode(7); node1.next = node2; node2.next = node3; node3.next = node4; node4.next = node5; ListNode res = oddEvenList(node1); while (res != null) { System.out.println(res.val); res = res.next; } } public static ListNode oddEvenList (ListNode head) { // write code here if (Objects.isNull(head)) return null; ListNode oddHead = null; ListNode oddTail = null; ListNode evenHead = null; ListNode evenTail = null; int i = 1; while (head != null) { if (i%2 != 0) { if (oddHead == null) { ListNode cur = new ListNode(head.val); oddHead = cur; oddTail = cur; } else { ListNode cur = new ListNode(head.val); oddTail.next = cur; oddTail = oddTail.next; } } else { if (evenHead == null) { ListNode cur = new ListNode(head.val); evenHead = cur; evenTail = cur; } else { ListNode cur = new ListNode(head.val); evenTail.next = cur; evenTail = evenTail.next; } } i++; head = head.next; } oddTail.next = evenHead; return oddHead; } public static class ListNode { int val; ListNode next = null; public ListNode(int val) { this.val = val; } } }