Java基础二

链表的基础操作

链表的基础操作I

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.*;

class ListNode{
int val;
ListNode next;

ListNode(){}
ListNode(int val) {
this.val = val;
}
}


class LinkList{

int size;
ListNode head;

public LinkList(){
this.head = new ListNode(0);
}

public void insert(int val){
ListNode newNode = new ListNode(val);

ListNode pre = this.head;
int index = size;
while (index-- > 0){
pre = pre.next;
}
pre.next = newNode;
this.size++;
}

public void printList(){
ListNode cur = head.next;

while (cur != null){
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}

}

public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()){
int n = sc.nextInt();
LinkList result = new LinkList();
while (n-- > 0){
int m = sc.nextInt();
result.insert(m);
}
result.printList();
}
sc.close();
}

}

链表的基础操作II

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77


import java.util.*;

class ListNode{
int val;
ListNode next;
ListNode(){}
ListNode(int val){this.val = val;}
}

class LinkedList {
int size;
ListNode head;

LinkedList(){
this.size = 0;
this.head = new ListNode(0);
}

public int get(int index){
if (index < 1 || index > size){
System.out.println("Output position out of bounds.");
return -1;
}

ListNode cur = head.next;

for(int i = 1; i < index; i++){
cur = cur.next;
}

System.out.println(cur.val);
return cur.val;
}
public void insert(int val){
ListNode pre = head;
ListNode newNode = new ListNode(val);
for (int i = 0; i < size; i++){
pre = pre.next;
}

pre.next = newNode;
this.size++;
}

public void printList(){
ListNode cur = head.next;
while (cur != null){
System.out.print(cur + " ");
cur = cur.next;
}

System.out.println();
}
}

class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);

while(sc.hasNextInt()){
int n = sc.nextInt();
int k = sc.nextInt();
LinkedList result = new LinkedList();
while (n-- > 0){
result.insert(sc.nextInt());
}
while (k-- > 0){
result.get(sc.nextInt());
}
}

sc.close();
}
}

链表的基础操作III

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.util.*;


class ListNode{

int val;
ListNode next;
ListNode(){}
ListNode(int val){
this.val = val;
}
}

class LinkedList{
ListNode head;
int size;

LinkedList(){
this.size = 0;
this.head = new ListNode(0);
}

public boolean insert(int index, int val){
if (index < 1 || index > size + 1){

return false;
}
this.size++;
ListNode newNode = new ListNode(val);

ListNode pre = head;

for(int i = 0; i < index - 1; i++){
pre = pre.next;
}

newNode.next = pre.next;
pre.next = newNode;

return true;
}

public boolean delete(int index){
if (index < 1 || index > size){
return false;
}
this.size--;
ListNode pre = head;

for(int i = 0; i < index - 1; i++){
pre = pre.next;
}

pre.next = pre.next.next;
return true;
}

public void printList(){
ListNode cur = head.next;
while (cur != null){
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
}

class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt()){
int k = sc.nextInt();
LinkedList result = new LinkedList();
while (k-- > 0){
result.insert(result.size + 1, sc.nextInt());
}

int S = sc.nextInt();
while (S-- > 0){
int n = sc.nextInt();
int x = sc.nextInt();

if (result.insert(n,x)){
result.printList();
} else {
System.out.println("Insertion position is invalid.");
}
}

int L = sc.nextInt();
while (L-- > 0){
int m = sc.nextInt();
if (result.delete(m)){
result.printList();
} else {
System.out.println("Deletion position is invalid.");
}
}
}

sc.close();
}
}

出现频率最高的字母

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.*;

class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);

while (sc.hasNextInt()){
int n = sc.nextInt();
sc.nextLine();

while (n-- > 0){
int[] record = new int[26];
String s = sc.nextLine();
char[] str = s.toCharArray();
for (char i : str){
record[i - 'a']++;
}
int result = -1;
int getIndex = 0;
for (int i = 0; i < record.length; i++){
if (record[i] > result){
result = record[i];
getIndex = i;
}
}

System.out.println((char)(getIndex + 'a'));
}
}
}
}

判断集合成员

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.*;

class Main{
public static void main(String[] args){

Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()){
int k = sc.nextInt();

while (k-- > 0){
int m = sc.nextInt();
Set<Integer> record = new HashSet<>();
while (m-- > 0){
record.add(sc.nextInt());
}

int n = sc.nextInt();
if (record.contains(n)){
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
}
}

开房门

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.*;

class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);

while (sc.hasNextInt()){
int s = sc.nextInt();
while (s-- > 0){
int n = sc.nextInt();

Map<Integer, Integer> record = new HashMap<>();

while (n-- > 0){
int k = sc.nextInt();
int d = sc.nextInt();

record.put(d,k);
}

int x = sc.nextInt();
if (record.containsKey(x)){
System.out.println(record.get(x));
} else {
System.out.println("Can't open the door.");
}
}
}
}
}