java 正则表达式

(.)\1+ 表示 表示任意一个字符重复两次或两次以上(括号里的点表示任意字符,后面的 \1表示取第一个括号匹配的内容 ,后面的加号表示匹配1次或1次以上。二者加在一起就是某个字符重复两次或两次以上)
**$1是第一个小括号里的内容,$2是第二个小括号里面的内容**,

1
2
3
4
5
6
7
8
9
10
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int line = scanner.nextInt();
scanner.nextLine();
for (int i = 0; i < line; i++) {
System.out.println(scanner.nextLine().replaceAll("(.)\\1+","$1$1").replaceAll("(.)\\1(.)\\2","$1$1$2"));
}
}
}

万万没想到之抓捕孔连顺

  1. 我们在字节跳动大街的N个建筑中选定3个埋伏地点。

  2. 为了相互照应,我们决定相距最远的两名特工间的距离不超过D。

  3. 题目描述

    解题描述

    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
    """
    固定最左边的人i 让最右边的人移动 直到范围超出规定的distance
    该段的埋伏方案=(j-i-1)*(long)(j-i-2)/2; 即组合公式
    """
    import java.util.Scanner;
    public class Main{
    public static void main(String[] args){
    Scanner input=new Scanner(System.in);
    int numBuilding=input.nextInt();
    int maxLength=input.nextInt();
    input.nextLine();
    int[] distance=new int[numBuilding];
    for(int k=0;k<numBuilding;k++){
    distance[k]=input.nextInt();
    }

    int i=0,j=2;
    long res=0L;
    long mod=99997867;
    while(i<numBuilding-2){
    while(j>=i+2&&j<numBuilding&&(distance[j]-distance[i])<=maxLength){
    j+=1;
    }
    res+=(long)(j-i-1)*(long)(j-i-2)/2;
    i+=1;
    }
    System.out.println(res%mod);
    }
    }

雀魂启动

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
import java.util.*;

"""
回溯遍历
假如摸到某张牌 然后去判断是否能胡
判断是否能胡:
遍历 若该牌为雀头 是否能胡
若该牌为刻子 是否能胡
若该牌为顺子 是否能胡
最后若待匹配的牌为0 则能胡
"""
public class Main {

private void sln() {
Scanner sc = new Scanner(System.in);
int[] state = new int[9]; //手上拥有的牌
int[] helpArr = new int[9];
ArrayList<Integer> res = new ArrayList<>();
for (int i = 0; i < 13; i++) {
int num = sc.nextInt();
state[num - 1]++;
}
for (int i = 0; i < 9; i++) {
if (state[i] < 4) {
int num = i + 1;
System.arraycopy(state, 0, helpArr, 0, 9);
helpArr[i]++; //假如是摸到牌num
if (canHu(helpArr, 14, false)) res.add(num);
}
}
if (res.isEmpty()) System.out.println(0);
else {
StringBuffer sbf = new StringBuffer();
sbf.append(res.get(0));
for (int i = 1; i < res.size(); i++) {
sbf.append(" ");
sbf.append(res.get(i));
}
System.out.println(sbf.toString());
}
}
"""
arr 手上待匹配的牌
total 待匹配的牌数
hasHead 雀头已有
"""
private boolean canHu(int[] arr, int total, boolean hasHead) {
if (total == 0) return true;
if (!hasHead) {
for (int i = 0; i < 9; i++) {
if (arr[i] >= 2) {
arr[i] -= 2; //i+1为雀头
if (canHu(arr, total - 2, true)) return true;
arr[i] += 2; //撤销选择
}
}
return false;
} else {
for (int i = 0; i < 9; i++) {
if (arr[i] > 0) {
if (arr[i] >= 3) {
arr[i] -= 3; //作为刻子
if (canHu(arr, total - 3, true)) return true;
arr[i] += 3;
}
if (i + 2 < 9 && arr[i + 1] > 0 && arr[i + 2] > 0) {
arr[i]--;
arr[i + 1]--;
arr[i + 2]--;//作为顺子
if (canHu(arr, total - 3, true)) return true;
arr[i]++;
arr[i + 1]++;
arr[i + 2]++;
}
}
}
}
return false;
}

public static void main(String[] args) {
new Main().sln();
}
}

链表k反转

  1. 题目描述
    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
    """
    头插法
    按块反转
    pre是前一块最后的节点
    cur指向本块最后一个节点
    cur.next==temp
    temp是待头插节点

    """
    import java.util.*;

    /*
    * public class ListNode {
    * int val;
    * ListNode next = null;
    * }
    */

    public class Solution {
    /**
    *
    * @param head ListNode类
    * @param k int整型
    * @return ListNode类
    */
    public ListNode reverseKGroup (ListNode head, int k) {
    // write code here
    if(head==null||k==1||head.next==null) return head;
    ListNode pc=head;
    int len=0;
    while(pc!=null){
    len+=1;
    pc=pc.next;
    }
    ListNode p=new ListNode(-1);
    p.next=head;
    ListNode pre=p;
    ListNode cur=head;
    ListNode temp;
    for(int i=0;i<len/k;i++){
    for(int j=1;j<k;j++){
    temp=cur.next;
    cur.next=temp.next;
    temp.next=pre.next;
    pre.next=temp; //头插法

    }
    pre=cur;
    cur=cur.next;
    }
    return p.next;

    }
    }

判断链表是否有环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
if(head==null || head.next==null) return false;
ListNode slow = head;
ListNode fast = head.next.next;
while(slow!=fast){
if(fast==null||fast.next==null) return false; //只需要判断快指针是否还能继续走下去即可 快指针走过的地方慢指针肯定能走过
slow = slow.next;
fast = fast.next.next;
}
return true;
}
}

去重+排序 —TreeSet

题目描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner in =new Scanner(System.in);
while(in.hasNext()){
int len=in.nextInt();
Set<Integer> set=new TreeSet<>();
for(int i=0;i<len;i++){
int num=in.nextInt();
set.add(num);
}
set.forEach((T)->{System.out.println(T);});

}
}
}

十六进制转十进制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner in =new Scanner(System.in);
while(in.hasNext()){
String s=in.nextLine();
int len=s.length();
int res=0;
int t;
for(int i=2;i<len;i++){
char temp=s.charAt(i);
if(temp>='A') t=temp-'A'+10; //A--F
else t=temp-'0'; //0123456789
res=res*16+t;
}
System.out.println(res);
}
}
}