笔试更新

This commit is contained in:
markilue 2023-03-26 22:59:47 +08:00
parent 41206526e1
commit a14fd81d5e
8 changed files with 606 additions and 0 deletions

View File

@ -0,0 +1,51 @@
package com.markilue.interview;
import org.junit.Test;
import java.util.HashMap;
import java.util.Scanner;
/**
* @BelongsProject: RedCampus
* @BelongsPackage: com.markilue.interview
* @Author: marklue
* @CreateTime: 2023/3/26 16:23
* @Description: TODO
* @Version: 1.0
*/
public class Question1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
char[] chars1 = sc.nextLine().toCharArray();
new Question1().sovle(chars1);
}
@Test
public void test(){
// char a= (char)('d'-3);
char[] chars={'a','e','f'};
sovle(chars);
}
public void sovle(char[] chars) {
char[] result = new char[chars.length];
HashMap<Character, Character> map = new HashMap<Character, Character>() {{
put('a', 'x');
put('b', 'y');
put('c', 'z');
}};
for (int i = 0; i < chars.length; i++) {
if (map.containsKey(chars[i])) {
result[i] = map.get(chars[i]);
} else {
result[i] = (char) (chars[i] - 3);
}
}
System.out.println(new String(result));
}
}

View File

@ -0,0 +1,52 @@
package com.markilue.interview;
import java.util.Scanner;
/**
* @BelongsProject: RedCampus
* @BelongsPackage: com.markilue.interview
* @Author: marklue
* @CreateTime: 2023/3/26 16:36
* @Description: TODO
* @Version: 1.0
*/
public class Question2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int time = sc.nextInt();
while (time-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
}
}
/**
* 一次最多移动k个数
* 计算有多少个乱序的
*
* @param nums
* @param k
*/
public void sovle(int[] nums, int k) {
int count = 0;
for (int i = 0; i < nums.length; i++) {
if ((i + 1) != nums[i]) {
count++;
}
}
if (count % k == 0) {
System.out.println(count / k);
} else {
System.out.println(count / k + 1);
}
}
}

View File

@ -0,0 +1,133 @@
package com.markilue.interview;
import org.junit.Test;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;
/**
* @BelongsProject: RedCampus
* @BelongsPackage: com.markilue.interview
* @Author: marklue
* @CreateTime: 2023/3/26 16:50
* @Description: TODO
* @Version: 1.0
*/
public class Question3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
int k = sc.nextInt();//操作的次数
int[] left = new int[k];
int[] right = new int[k];
for (int i = 0; i < k; i++) {
left[i] = sc.nextInt() - 1;//-1变成索引
}
for (int i = 0; i < k; i++) {
right[i] = sc.nextInt() - 1;//-1变成索引
}
char[] deal = sc.next().toCharArray();//操作手段
int[] dealNum = new int[k];
for (int i = 0; i < k; i++) {
dealNum[i] = sc.nextInt();
}
}
public void test() {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
String[] s = sc.nextLine().split(" ");
int[] nums = Arrays.stream(s).mapToInt(Integer::parseInt).toArray();
int k = Integer.parseInt(sc.nextLine());//操作的次数
String[] sLeft = sc.nextLine().split(" ");
int[] left = Arrays.stream(sLeft).mapToInt(Integer::parseInt).toArray();
String[] sRight = sc.nextLine().split(" ");
int[] right = Arrays.stream(sRight).mapToInt(Integer::parseInt).toArray();
String sDeal = sc.nextLine();
char[] deal = sDeal.toCharArray();//操作手段
String[] sDealNum = sc.nextLine().split(" ");
int[] dealNum = Arrays.stream(sDealNum).mapToInt(Integer::parseInt).toArray();
new Question3().sovle(nums, left, right, deal, dealNum);
}
@Test
public void test1() {
int[] nums = {5, 4, 7, 4};
int[] left = {1, 2, 3, 2};
int[] right = {4, 3, 4, 2};
char[] deal = {'=', '|', '&', '='};
int[] dealNum = {8, 3, 6, 2};
sovle1(nums, left, right, deal, dealNum);
}
public void sovle(int[] nums, int[] left, int[] right, char[] deal, int[] dealNum) {
//分别对数据进行操作
for (int i = 0; i < left.length; i++) {
int start = left[i] - 1;
int end = right[i] - 1;
for (int j = start; j <= end; j++) {
if (deal[i] == '|') {
nums[j] |= dealNum[i];
} else if (deal[i] == '&') {
nums[j] &= dealNum[i];
} else if (deal[i] == '=') {
nums[j] = dealNum[i];
}
}
}
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}
}
public void sovle1(int[] nums, int[] left, int[] right, char[] deal, int[] dealNum) {
//分别对数据进行操作
for (int i = 0; i < left.length; i++) {
int start = left[i] - 1;
int end = right[i] - 1;
if (deal[i] == '|') {
for (int j = start; j <= end; j++) {
nums[j] |= dealNum[i];
}
} else if (deal[i] == '&') {
for (int j = start; j <= end; j++) {
nums[j] &= dealNum[i];
}
} else if (deal[i] == '=') {
for (int j = start; j <= end; j++) {
nums[j] = dealNum[i];
}
}
}
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}
}
}

View File

@ -0,0 +1,30 @@
package com.markilue.interview;
import java.util.List;
/**
* @BelongsProject: Tencent
* @BelongsPackage: com.markilue.interview
* @Author: marklue
* @CreateTime: 2023/3/26 20:01
* @Description: TODO
* @Version: 1.0
*/
public class ListNode {
int val;
ListNode next = null;
public ListNode() {
}
public ListNode(int val) {
this.val = val;
}
public ListNode(int val,ListNode next) {
this.val = val;
this.next=next;
}
}

View File

@ -0,0 +1,59 @@
package com.markilue.interview;
import java.util.ArrayList;
import java.util.List;
/**
* @BelongsProject: Tencent
* @BelongsPackage: com.markilue.interview
* @Author: marklue
* @CreateTime: 2023/3/26 20:01
* @Description: TODO
* @Version: 1.0
*/
public class Question1 {
/**
* 代码中的类名方法名参数名已经指定请勿修改直接返回方法规定的值即可
*
* @param head ListNode类
* @return ListNode类
*/
public ListNode reorderList(ListNode head) {
// write code here
if (head.next == null || head.next.next == null) return head;
ListNode dummy = new ListNode(0, head);
ListNode fake = dummy;
ListNode cur = head.next;
ListNode temp;
while (cur != null && cur.next != null && cur.next.next != null) {
temp = cur.next.next.next;
cur.next.next.next = dummy.next;
dummy.next = cur.next;
cur.next = temp;
dummy = cur;
if (temp != null) {
cur = temp.next;
}
}
if (cur != null && cur.next != null && cur.next.next == null) {
temp=cur.next;
temp.next=dummy.next;
cur.next=null;
dummy.next=temp;
}
return fake.next;
}
public void swap(List<Integer> list, int left, int right) {
int temp = list.get(left);
list.set(left, list.get(right));
list.set(right, temp);
}
}

View File

@ -0,0 +1,69 @@
package com.markilue.interview;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
/**
* @BelongsProject: Tencent
* @BelongsPackage: com.markilue.interview
* @Author: marklue
* @CreateTime: 2023/3/26 20:23
* @Description: TODO
* @Version: 1.0
*/
public class Question2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int k = Integer.parseInt(sc.nextLine());
String[] s = new String[k];
for (int i = 0; i < k; i++) {
s[i] = sc.nextLine();
}
}
@Test
public void test(){
String[] s={"ab","ca","ccb"};
sovle(s);
}
public void sovle(String[] s) {
backtracking(s,0,new boolean[24]);
System.out.println(result1);
}
StringBuilder sb = new StringBuilder();
List<String> result = new ArrayList<String>();
int result1=0;
public void backtracking(String[] s, int i, boolean[] used) {
if (i == s.length) {
result1++;
result.add(new String(sb));
return;
}
HashSet<Character> set = new HashSet<>();
for (int j = 0; j < s[i].length(); j++) {
char c = s[i].charAt(j);
if (!set.contains(c)&&!used[c - 'a']) {
used[c - 'a'] = true;
set.add(c);
sb.append(c);
backtracking(s, i + 1, used);
sb.deleteCharAt(sb.length() - 1);
used[c-'a']=false;
}
}
}
}

View File

@ -0,0 +1,85 @@
package com.markilue.interview;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
/**
* @BelongsProject: Tencent
* @BelongsPackage: com.markilue.interview
* @Author: marklue
* @CreateTime: 2023/3/26 20:51
* @Description: TODO
* @Version: 1.0
*/
public class Question3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] A = new int[n];
int[] B = new int[n];
for (int i = 0; i < n; i++) {
A[i]=sc.nextInt();
}
for (int i = 0; i < n; i++) {
B[i]=sc.nextInt();
}
new Question3().sovle(A,B);
}
@Test
public void test(){
int[] A={1,10};
int[] B={1,0};
sovle(A,B);
}
public void sovle(int[] A,int[] B){
ArrayList<Integer> list0 = new ArrayList<>();
ArrayList<Integer> list1 = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
for (int i = 0; i < B.length; i++) {
if(B[i]==0){
list0.add(i);
}else if(B[i]==1){
list1.add(i);
}else {
list2.add(i);
}
}
Collections.sort(list0, (a,b) ->A[a]-A[b]);
Collections.sort(list1, (a,b) ->A[a]-A[b]);
Collections.sort(list2, (a,b) ->A[a]-A[b]);
int[] c = new int[A.length];
int k=0;
for (Integer i : list0) {
c[i]=++k;
}
for (Integer i : list1) {
c[i]=++k;
}
for (Integer i : list2) {
c[i]=++k;
}
long result=0;
for (int i = 0; i < A.length; i++) {
result+=Math.abs(c[i]-A[i]);
}
System.out.println(result);
}
}

View File

@ -0,0 +1,127 @@
package com.markilue.interview;
import org.junit.Test;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/**
* @BelongsProject: Tencent
* @BelongsPackage: com.markilue.interview
* @Author: marklue
* @CreateTime: 2023/3/26 21:09
* @Description: TODO
* @Version: 1.0
*/
public class Question4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
}
@Test
public void test() {
int[] nums = {1, 2, 1};
sovle3(nums);
}
public void sovle(int[] nums) {
Arrays.sort(nums);
backtracking(nums, 0, 1, 0, 0);
System.out.println(result);
}
@Test
public void test3() {
System.out.println(0 ^ 2);
}
List<Integer> cur = new ArrayList<>();
List<List<Integer>> result = new ArrayList<>();
public void backtracking(int[] nums, int start, int cheng, int yi, int level) {
if (level != 0 && cheng == yi) {
result.add(new ArrayList<>(cur));
}
for (int i = start; i < nums.length; i++) {
// if (i != start && nums[i] == nums[i - 1]) {
// continue;
// }
cur.add(nums[i]);
backtracking(nums, i + 1, cheng * nums[i], yi ^ nums[i], level + 1);
cur.remove(cur.size() - 1);
}
}
public void solve1(int[] nums) {
int result = 0;
for (int i = 0; i < nums.length; i++) {
int cheng = 1;
int yihou = 0;
for (int j = i; j < nums.length; j++) {
cheng *= nums[j];
yihou ^= nums[j];
if (cheng == yihou) result++;
}
}
System.out.println(result);
}
//统计每个词出现的个数
public void sovle3(int[] nums) {
int max = 0;
int min = Integer.MAX_VALUE;
//统计每个词出现的次数
for (int i = 0; i < nums.length; i++) {
if (max < nums[i]) max = nums[i];
if (min > nums[i]) min = nums[i];
}
int[] count = new int[max - min + 1];
int result = 0;
for (int i = 0; i < nums.length; i++) {
count[nums[i] - min]++;
result++;//统计单个的值
}
ArrayList<Integer> count1 = new ArrayList<>();
int num = 0;
for (int i = 0; i < count.length; i++) {
//统计个数超过2的值
if (count[i] != 0) num++;
if (count[i] >= 2) {
int temp = count[i] / 2;
count1.add(temp);
}
}
int total = 1;
for (int i = 0; i < count1.size(); i++) {
total *= count1.get(i);
}
result += total * (num - 1);
System.out.println(result);
}
}