2024-01-18
1.单链表循环输出、查找
for (int i = head; i != -1; i = ne[i]){
System.out.print(e[i] + " ");
}
2.单链表添加
//头节点head = -1
public static void addToHead(int x){
e[idx] = x;
ne[idx] = head;
head = idx ++;
}
//任意点
public static void add(int k, int x){
e[idx] = x;
ne[idx] = ne[k];
ne[k] = idx ++;
}
3.Arrays.fill(h, -1)
h是一个数组,单链表的空指针一般用-1来表示,所以清空一个数组一般这样写
4.k = (x % N + N) % N
哈希表-先模再加载模
5.维护一个集合,支持如下几种操作:
I x,插入一个整数 x
Q x,询问整数 x是否在集合中出现过;
拉链法
import java.util.Scanner;
import java.util.Arrays;
public class Main{
static int N = 100003, idx;
static int[] h = new int[N], e = new int[N], ne = new int[N];
public static void insert(int x){
int k = (x % N + N) % N;
e[idx] = x;
ne[idx] = h[k];
h[k] = idx ++;
}
public static boolean find(int x){
int k = (x % N + N) % N;
for(int i = h[k]; i != -1; i = ne[i]){
if(e[i] == x) return true;
}
return false;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
idx = 0;
//在开始之前,要先把数组先赋一个值
Arrays.fill(h, -1);
while(n -- > 0){
String str = sc.next();
if("I".equals(str)){
int x = sc.nextInt();
insert(x);
}else{
int x = sc.nextInt();
if(find(x)) System.out.println("Yes");
else System.out.println("No");
}
}
}
}
开放寻址法
import java.util.*;
public class Main{
static int N = 200003, nu = 0x3f3f3f3f;//可以把nu的值换成Integer.MAX_VALUE
static int[] h = new int[N];
public static int find(int x){
int k = (x % N + N) % N;
while(h[k] != nu && h[k] != x)//这个位置有人,并且不等于x
{
k ++;
if(k == N) k = 0;
}
return k;//有两种含义,如果x在哈希表中,那k就是x的下标;如果x不在哈希表中,那k就是x应该所处的位置
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
//在开始之前,要先把数组先赋一个值
Arrays.fill(h, 0x3f3f3f3f);
while(n -- > 0){
String str = sc.next();
int x = sc.nextInt();
int k = find(x);
if("I".equals(str)){
h[k] = x;
}else{
if(h[k] == nu) System.out.println("No");
else System.out.println("Yes");
}
}
}
}
6.字符串哈希
import java.util.*;
public class Main{
static int N = 100010, P = 13331;//经验值
static long[] h = new long[N], p = new long[N];
public static long get(int l, int r){
return h[r] - h[l - 1] * p[r - l + 1];
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt();
String str = sc.next();//这里写成nextLine是不行的?!!!!
p[0] = 1;
for(int i = 1; i <= n; i ++){
p[i] = p[i - 1] * P;//每个下标对应p的多少次方
h[i] = h[i - 1] * P + str.charAt(i - 1);
}
while(m -- > 0){
int l1 = sc.nextInt(), r1 = sc.nextInt();
int l2 = sc.nextInt(), r2 = sc.nextInt();
if(get(l1, r1) == get(l2, r2)) System.out.println("Yes");
else System.out.println("No");
}
}
}
7.leetcode 76最小覆盖字串
核心:用两个数组分别记录字母在两个字符串中的个数
滑动窗口:指针j不断++,直到满足条件(记录此时i,j指针的位置,以便用于后面的比较判断),此时指针i开始++,直到不满足条件,然后j又开始移动巴拉巴拉巴拉……
class Solution {
public static int get(char c){
return c >= 'A' && c <= 'Z' ? c - 'A' + 26 : c - 'a';
}
public String minWindow(String s, String t) {
int[] a1 = new int[60], a2 = new int[60];
int type = 0;//这里用来记录字符串中字母的种类
for(char c : t.toCharArray()){
if(++ a1[get(c)] == 1) type ++;
}
String ans = "";
for(int i = 0, j = 0; j < s.length(); j ++){
int idx1 = get(s.charAt(j));
if(++ a2[idx1] == a1[idx1]){
type --;
}
while(i < j){
int idx2 = get(s.charAt(i));
if(a2[idx2] > a1[idx2] && -- a2[idx2] >= 0) i ++;
else break;
}
if(type == 0 && (ans.length() == 0 || ans.length() > j - i + 1)) ans = s.substring(i, j + 1);
}
return ans;
}
}
8.螺旋矩阵:偏移量法
class Solution {
public int[][] generateMatrix(int n) {
int[][] res = new int[n][n];//为n行n列的矩阵
int[] idx = {-1, 0, 1, 0}, idy = {0, 1, 0, -1};
int x = 0, y = 0, d = 1;
for(int i = 1; i <= n * n; i ++){
res[x][y] = i;
int a = x + idx[d], b = y + idy[d];
if(a < 0 || a >= n || b < 0 || b >= n || res[a][b] > 0){
d = (d + 1) % 4;
a = x + idx[d];
b = y + idy[d];
}
x = a;
y = b;
}
return res;
}
}
9.获取二维数组的行数
int[][] array = {{1, 2}, {3, 4}}; // 示例二维数组
int rows = array.length; // 获取行数
System.out.println("行数为:" + rows); // 输出结果为:行数为:2
10.获取二维数组的列数
int[][] array = {{1, 2}, {3, 4}}; // 示例二维数组
int columns = array[0].length; // 获取列数
System.out.println("列数为:" + columns); // 输出结果为:列数为:2
11.力扣-54螺旋矩阵plus版
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int INF = 101;//要给它定义一个是否访问过的标志
int m = matrix.length;
int n = matrix[0].length;
List<Integer> ans = new ArrayList<>();//首先这里要用一个list返回
int[] idx = {-1, 0, 1, 0}, idy = {0, 1, 0, -1};
int x = 0, y = 0, d = 1;
for(int i = 0; i < n * m; i ++){
ans.add(matrix[x][y]);
matrix[x][y] = INF;
int a = x + idx[d];
int b = y + idy[d];
if(a >= m || a < 0 || b < 0 || b >= n || matrix[a][b] == INF)//这里要写成==INF用来判断
{
d = (d + 1) % 4;
a = x + idx[d];
b = y + idy[d];
}
x = a;
y = b;
}
return ans;
}
}