第十天代码进阶,完成以下四个题目,提高编程能力。
package cn.msf.baseJava.d_14;
import java.util.*;
public class Test1 {
public static void main(String[] args) {
Random r = new Random();
ArrayList<Integer> p = new ArrayList<>();
Map<Integer,Integer> memo = new HashMap<>();
for (int i = 0; i < 100;) {
// 1. 随机生成编号
int num = r.nextInt(200) + 1;
if (!memo.containsKey(num)) {
p.add(num);
memo.put(num,i);
i++;
}
}
// 2.循环删除奇数位置
boolean flag = true;
while (flag) {
ArrayList<Integer> temp = new ArrayList<>();
for (int i = 0; i < p.size(); i++) {
if ((i + 1) % 2 == 0) {
temp.add(p.get(i));
}
}
p = temp;
if (p.size() == 1) {
flag = false;
}
}
// 幸运者编号
System.out.println(p.get(0));
// 第一次所站的位置
System.out.println(memo.get(p.get(0)));
}
}
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test2 {
public static void main(String[] args) {
String userStrs = "" +
"10001:张三:男:1990-01-01#" +
"10002:李四:女:1989-01-09#" +
"10003:王五:男:1999-09-09#" +
"10004:刘备:男:1899-01-01#" +
"10005:孙惜空:男:1900-01-01#" +
"10006:张三:女:1999-01-01#" +
"10007:刘备:女:1999-01-01#" +
"10008:张三:女:2003-07-01#" +
"10009:猪八戒:男:1900-01-01";
// 业务一:解析字符串。
List<User> users = new ArrayList<>();
String[] split = userStrs.split("#");
for (int i = 0; i < split.length; i++) {
String str = split[i];
String[] userStr = str.split(":");
User user = new User();
user.setId(Long.parseLong(userStr[0]));
user.setName(userStr[1]);
user.setGender(userStr[2]);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date = LocalDate.parse(userStr[3], fmt);
user.setBirthday(date);
users.add(user);
}
// System.out.println(users);
// 业务2:统计名字出现的次数,封装到Map中
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < users.size(); i++) {
User user = users.get(i);
map.put(user.getName(), map.getOrDefault(user.getName(), 0) + 1);
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
import java.util.Arrays;
public class Test3 {
public static void main(String[] args) {
int[] nums = {};
int target = 10;
int[] res = binarySearch(nums, target);
System.out.println(Arrays.toString(res));
}
private static int[] binarySearch(int[] nums, int target) {
int[] ans = {-1, -1};
int left = 0;
int right = nums.length;
boolean flag = true;
// 找到最左边的target,
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) {
right = mid;
flag = false;
} else if (nums[mid] < target) {
left = mid + 1;
} else if (nums[mid] > target) {
right = mid;
}
}
if (flag) {
return ans;
}
ans[0] = left;
left = 0;
right = nums.length;
// 找到最右边的target
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) {
left = mid + 1;
} else if (nums[mid] < target) {
left = mid + 1;
} else if (nums[mid] > target) {
right = mid;
}
}
ans[1] = left - 1;
return ans;
}
}
package cn.msf.baseJava.d_14;
public class Test4 {
public static void main(String[] args) {
MyLinkedList<Integer> linkedList = new MyLinkedList<Integer>();
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);
linkedList.add(4);
linkedList.add(5);
linkedList.reverse(2,5);
linkedList.forEach();
}
}
class MyLinkedList<E> {
int size;
Node<E> head = null;
private static class Node<E> {
public Node() {
}
public E data;
public Node<E> next;
public Node(E data, Node<E> next) {
this.data = data;
this.next = next;
}
}
public void add(E e) {
if (head == null) {
head = new Node<>(e, null);
} else {
Node<E> temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = new Node<>(e, null);
}
size++;
}
public Node<E> reverse(int left, int right) {
if (head == null || left >= right || right > size || left < 1) {
return head;
}
int gap = right - left;
Node<E> dummy = new Node<>();
dummy.next = head;
Node<E> pre = dummy;
for (int i = 0; i < left - 1; i++) {
pre = pre.next;
}
Node<E> leftHead = pre;
Node<E> start = leftHead.next;
pre = start;
Node<E> cur = pre.next;
while (--gap >= 0) {
Node<E> next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
leftHead.next = pre;
start.next = cur;
head = dummy.next;
return head;
}
public void forEach() {
if (head == null) {
System.out.println("{}");
return;
}
Node<E> temp = head;
while (temp.next != null) {
System.out.print(temp.data + "->");
temp = temp.next;
}
System.out.print(temp.data);
}
}