中级篇
13. 字符串旋转 编写一个Java程序来判断一个字符串是否是另一个字符串的旋转。
1public class StringRotation {
2 public static boolean isRotation(String s1, String s2) {
3 if (s1.length() != s2.length()) {
4 return false;
5 }
6 String s1s1 = s1 + s1;
7 return s1s1.contains(s2);
8 }
9
10 public static void main(String[] args) {
11 String s1 = "waterbottle";
12 String s2 = "erbottlewat";
13 System.out.println(isRotation(s1, s2)); // 输出: true
14 }
15}
14. 回文数检测 编写一个Java程序来检查一个整数是否是回文数。
1public class PalindromeNumber {
2 public static boolean isPalindrome(int number) {
3 int reversed = 0;
4 int original = number;
5 while (number != 0) {
6 int digit = number % 10;
7 reversed = reversed * 10 + digit;
8 number /= 10;
9 }
10 return original == reversed;
11 }
12
13 public static void main(String[] args) {
14 System.out.println(isPalindrome(12321)); // 输出: true
15 System.out.println(isPalindrome(12345)); // 输出: false
16 }
17}
高级篇
15. 图的深度优先搜索 实现一个图类,并使用深度优先搜索算法遍历它。
1import java.util.*;
2
3public class GraphDFS {
4 private final Map<Integer, List<Integer>> adjacencyList;
5
6 public GraphDFS() {
7 adjacencyList = new HashMap<>();
8 }
9
10 public void addVertex(int vertex) {
11 adjacencyList.putIfAbsent(vertex, new ArrayList<>());
12 }
13
14 public void addEdge(int source, int destination) {
15 adjacencyList.computeIfAbsent(source, k -> new ArrayList<>()).add(destination);
16 }
17
18 public void dfs(int startVertex) {
19 Set<Integer> visited = new HashSet<>();
20 dfsUtil(startVertex, visited);
21 }
22
23 private void dfsUtil(int vertex, Set<Integer> visited) {
24 visited.add(vertex);
25 System.out.print(vertex + " ");
26
27 for (Integer neighbor : adjacencyList.getOrDefault(vertex, Collections.emptyList())) {
28 if (!visited.contains(neighbor)) {
29 dfsUtil(neighbor, visited);
30 }
31 }
32 }
33
34 public static void main(String[] args) {
35 GraphDFS graph = new GraphDFS();
36 graph.addVertex(1);
37 graph.addVertex(2);
38 graph.addVertex(3);
39 graph.addVertex(4);
40
41 graph.addEdge(1, 2);
42 graph.addEdge(1, 3);
43 graph.addEdge(2, 4);
44 graph.addEdge(3, 4);
45
46 graph.dfs(1); // 输出: 1 2 4 3
47 }
48}
16. 线程同步 编写一个Java程序,演示如何使用wait()
和notify()
方法来同步线程。
1public class ThreadSynchronization {
2 public static void main(String[] args) {
3 SharedResource resource = new SharedResource();
4
5 Thread producer = new Thread(() -> {
6 resource.produce();
7 });
8
9 Thread consumer = new Thread(() -> {
10 resource.consume();
11 });
12
13 producer.start();
14 consumer.start();
15 }
16}
17
18class SharedResource {
19 private boolean isProduced = false;
20
21 public synchronized void produce() {
22 while (isProduced) {
23 try {
24 wait();
25 } catch (InterruptedException e) {
26 e.printStackTrace();
27 }
28 }
29 System.out.println("Produced");
30 isProduced = true;
31 notify();
32 }
33
34 public synchronized void consume() {
35 while (!isProduced) {
36 try {
37 wait();
38 } catch (InterruptedException e) {
39 e.printStackTrace();
40 }
41 }
42 System.out.println("Consumed");
43 isProduced = false;
44 notify();
45 }
46}
17. 使用装饰器模式 实现一个简单的装饰器模式来增强功能。
1public interface Component {
2 void operation();
3}
4
5public class ConcreteComponent implements Component {
6 @Override
7 public void operation() {
8 System.out.println("Concrete Component operation.");
9 }
10}
11
12public abstract class Decorator implements Component {
13 protected Component component;
14
15 public Decorator(Component component) {
16 this.component = component;
17 }
18
19 @Override
20 public void operation() {
21 component.operation();
22 }
23}
24
25public class ConcreteDecoratorA extends Decorator {
26 public ConcreteDecoratorA(Component component) {
27 super(component);
28 }
29
30 @Override
31 public void operation() {
32 super.operation();
33 addedBehaviorA();
34 }
35
36 private void addedBehaviorA() {
37 System.out.println("Added behavior A.");
38 }
39}
40
41public class ClientCode {
42 public static void clientCode(Component component) {
43 component.operation();
44 }
45
46 public static void main(String[] args) {
47 Component simple = new ConcreteComponent();
48 Component decorated = new ConcreteDecoratorA(simple);
49 System.out.println("Client: I've got a simple component:");
50 clientCode(simple);
51 System.out.println("Client: Now it's decorated:");
52 clientCode(decorated);
53 }
54}