DFS
261. 树状结构查询
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
static Map<String, Set<String>> map = new HashMap<>(); // 使用 set 避免录入重复信息
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int row = Integer.parseInt(in.nextLine());
for (int i = 0; i < row; i++) {
String[] split = in.nextLine().split(" ");
String son = split[0];
String father = split[1];
Set<String> orDefault = map.getOrDefault(father, new HashSet<>());
orDefault.add(son);
map.put(father, orDefault);
}
String target = in.nextLine();
select(target);
String[] split = sb.toString().trim().split(" ");
String[] res = Arrays.copyOfRange(split, 1, split.length); // 截断首个
Arrays.sort(res);
for (String re : res) {
System.out.println(re);
}
}
public static void select(String str){
sb.append(str + " "); // 递归查找!!!
if (!map.containsKey(str)){
return;
}
Set<String> set = map.get(str);
for (String s : set) {
select(s);
}
}
}
239. 目录删除
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
static Map<Integer, Set<Integer>> map = new HashMap<>();
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = Integer.parseInt(in.nextLine());
for (int i = 0; i < count; i++) {
String[] split = in.nextLine().split(" ");
int son = Integer.parseInt(split[0]);
int father = Integer.parseInt(split[1]);
Set<Integer> orDefault = map.getOrDefault(father, new HashSet<>());
orDefault.add(son);
map.put(father, orDefault);
}
int target = Integer.parseInt(in.nextLine());
del(target);
TreeSet<Integer> res = new TreeSet<>();
for (Set<Integer> value : map.values()) {
for (Integer integer : value) {
if (target != integer){
res.add(integer);
}
}
}
StringBuilder sb = new StringBuilder();
for (Integer re : res) {
sb.append(re + " ");
}
System.out.println(sb.toString().trim());
}
public static void del(Integer target){
if (!map.containsKey(target)){ // 如果是叶子节点,没资格做 father(即作为 key),直接返回
return;
}
Set<Integer> set = map.get(target);
map.remove(target); // map.remove() 方法返回的是被删除的 value
for (Integer integer : set) {
del(integer);
}
}
}
166. 分糖果
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long count = in.nextLong();
System.out.println(getMin(count));
}
public static int getMin(long count){
if (count == 1){
return 0;
}
if (count % 2 == 0){
return getMin(count / 2) + 1; // 平均分配:1 次数
}
return Math.min(getMin((count + 1) / 2), getMin((count - 1) / 2) ) + 2; 平均分配 1 次 + 【取出、或者放回的 1 次】
}
}
解密犯罪时间
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
static StringBuilder sb = new StringBuilder();
static List<String> list = new ArrayList<>();
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine().replace(":", "");
backtracking(s);
Collections.sort(list, (o1, o2) -> (o1 + o2).compareTo(o2 + o1));
int index = list.indexOf(s);
if (index != list.size() - 1){
String s1 = list.get(index + 1);
System.out.println(s1.substring(0, 2) + ":" + s1.substring(2, 4));
return;
}
String s1 = list.get(0);
System.out.println(s1.substring(0, 2) + ":" + s1.substring(2, 4));
}
public static void backtracking(String s){
if (sb.length() == 4 && (sb.charAt(0) >= '3' || sb.charAt(2) >= '6')){
return;
}
if (sb.length() == 4 && sb.charAt(0) < '3' && sb.charAt(2) < '6'){
if (sb.charAt(0) == '2'){
if (sb.charAt(1) <= '3'){
if (!list.contains(sb.toString())){
list.add(sb.toString());
}
}
}else {
if (!list.contains(sb.toString())){
list.add(sb.toString());
}
}
return;
}
for (int i = 0; i < s.length(); i++) {
sb.append(s.charAt(i));
backtracking(s);
sb.delete(sb.length() - 1, sb.length());
}
}
}