米哈游笔试题更新
This commit is contained in:
parent
a4334c4a16
commit
a606e20bb2
|
|
@ -0,0 +1,116 @@
|
||||||
|
package com.markilue.interview;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: MiHayo
|
||||||
|
*@BelongsPackage: com.markilue.interview
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-03-19 20:29
|
||||||
|
*@Description: TODO
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class Question1 {
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
String[] s = sc.nextLine().split(" ");
|
||||||
|
|
||||||
|
int row = Integer.parseInt(s[0]);
|
||||||
|
int col = Integer.parseInt(s[1]);
|
||||||
|
|
||||||
|
char[][] nums = new char[row][col];
|
||||||
|
String[] sn = new String[row];
|
||||||
|
|
||||||
|
for (int i = 0; i < row; i++) {
|
||||||
|
sn[i] = sc.nextLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < row; i++) {
|
||||||
|
for (int j = 0; j < col; j++) {
|
||||||
|
nums[i][j] = sn[i].charAt(j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char[][] fake = new char[row][col];
|
||||||
|
|
||||||
|
for (int i = 0; i < row; i++) {
|
||||||
|
for (int j = 0; j < col; j++) {
|
||||||
|
if (nums[i][j] == 'B') {
|
||||||
|
fake[i][j] = 'G';
|
||||||
|
} else {
|
||||||
|
fake[i][j] = nums[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
char[][] chars = {
|
||||||
|
{'R', 'R', 'G', 'G', 'B', 'B'},
|
||||||
|
{'R', 'G', 'B', 'G', 'R', 'R'}
|
||||||
|
};
|
||||||
|
|
||||||
|
char[][] fake={
|
||||||
|
{'R', 'R', 'G', 'G', 'G', 'G'},
|
||||||
|
{'R', 'G', 'G', 'G', 'R', 'R'}
|
||||||
|
};
|
||||||
|
|
||||||
|
System.out.println(sovle(chars));
|
||||||
|
System.out.println(sovle(fake));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
boolean[][] used;
|
||||||
|
|
||||||
|
//计算连通块的数量
|
||||||
|
public int sovle(char[][] chars) {
|
||||||
|
|
||||||
|
int result = 0;
|
||||||
|
used = new boolean[chars.length][chars[0].length];
|
||||||
|
|
||||||
|
for (int i = 0; i < chars.length; i++) {
|
||||||
|
for (int j = 0; j < chars[0].length; j++) {
|
||||||
|
if (chars[i][j] != 'z') {
|
||||||
|
result++;
|
||||||
|
dfs(chars, i, j, chars[i][j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void dfs(char[][] chars, int i, int j, char last) {
|
||||||
|
if (i < 0 || j < 0 || i >= chars.length || j >= chars[0].length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!used[i][j] && chars[i][j] == last) {
|
||||||
|
used[i][j] = true;
|
||||||
|
//是一样的,消除
|
||||||
|
chars[i][j] = 'z';
|
||||||
|
dfs(chars, i, j + 1, last);
|
||||||
|
dfs(chars, i + 1, j, last);
|
||||||
|
dfs(chars, i - 1, j, last);
|
||||||
|
dfs(chars, i, j - 1, last);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,87 @@
|
||||||
|
package com.markilue.interview;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: MiHayo
|
||||||
|
*@BelongsPackage: com.markilue.interview
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-03-19 20:29
|
||||||
|
*@Description: TODO
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class Question2 {
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
int nums = Integer.parseInt(sc.nextLine());
|
||||||
|
|
||||||
|
while (nums-- > 0) {
|
||||||
|
String s = sc.nextLine();
|
||||||
|
String t = sc.nextLine();
|
||||||
|
new Question2().sovle(s, t);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sovle(String s, String t) {
|
||||||
|
|
||||||
|
HashMap<Character, Integer> tmap = new HashMap<>();//统计s的单词数量
|
||||||
|
|
||||||
|
|
||||||
|
for (int i = 0; i < t.length(); i++) {
|
||||||
|
char c = t.charAt(i);
|
||||||
|
tmap.put(c, tmap.getOrDefault(c, 0) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < s.length(); i++) {
|
||||||
|
char c = s.charAt(i);
|
||||||
|
tmap.put(c, tmap.getOrDefault(c, 0) - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//判断两个单词的个数是不是一样
|
||||||
|
int[] countMHY=new int[3];
|
||||||
|
for (Map.Entry<Character, Integer> entry : tmap.entrySet()) {
|
||||||
|
Character key = entry.getKey();
|
||||||
|
int count = entry.getValue();
|
||||||
|
if(key=='m'){
|
||||||
|
countMHY[0]=count;
|
||||||
|
}else if(key=='h'){
|
||||||
|
countMHY[1]=count;
|
||||||
|
}else if(key=='y'){
|
||||||
|
countMHY[2]=count;
|
||||||
|
}else {
|
||||||
|
if(count!=0){
|
||||||
|
System.out.println("No");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i < countMHY.length; i++) {
|
||||||
|
if(countMHY[i]!=countMHY[i-1]){
|
||||||
|
System.out.println("No");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Yes");
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,137 @@
|
||||||
|
package com.markilue.interview;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: MiHayo
|
||||||
|
*@BelongsPackage: com.markilue.interview
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-03-19 20:29
|
||||||
|
*@Description: TODO
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class Question3 {
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
int count = sc.nextInt();
|
||||||
|
int[] nums = new int[count];
|
||||||
|
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
|
nums[i] = sc.nextInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
new Question3().sovle(nums);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
List<Integer> cur = new ArrayList<Integer>();
|
||||||
|
List<List<Integer>> total = new ArrayList<>();
|
||||||
|
|
||||||
|
public void sovle(int[] nums) {
|
||||||
|
|
||||||
|
Arrays.sort(nums);
|
||||||
|
|
||||||
|
dfs(nums, 0, nums[0]);
|
||||||
|
System.out.println(total.size());
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test1() {
|
||||||
|
int[] nums = {1, 1, 2, 2, 3, 4, 5};
|
||||||
|
dfs(nums, 0, nums[0]);
|
||||||
|
System.out.println(total);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void dfs(int[] nums, int start, int last) {
|
||||||
|
|
||||||
|
// if (start == nums.length) {
|
||||||
|
// total.add(new ArrayList<>(cur));
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (cur.size() >= 2) {
|
||||||
|
total.add(new ArrayList<>(cur));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for (int i = start; i < nums.length; i++) {
|
||||||
|
if (i != start && nums[i] == nums[i - 1]) continue;//树枝去重
|
||||||
|
if (nums[i] % last == 0) {
|
||||||
|
cur.add(nums[i]);
|
||||||
|
dfs(nums, i + 1, nums[i]);
|
||||||
|
cur.remove(cur.size() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test4(){
|
||||||
|
int[] nums={5,4,6,1,2,1,2};
|
||||||
|
|
||||||
|
quickSort(nums,0,nums.length-1);
|
||||||
|
System.out.println(Arrays.toString(nums));
|
||||||
|
}
|
||||||
|
public void quickSort(int[] nums,int start,int end) {
|
||||||
|
if(start>=end)return;
|
||||||
|
|
||||||
|
int index = partition(nums, start, end);
|
||||||
|
|
||||||
|
quickSort(nums, start, index-1);
|
||||||
|
quickSort(nums, index+1, end);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public int partition(int[] nums, int left, int right) {
|
||||||
|
|
||||||
|
int start = left;
|
||||||
|
int end = right + 1;
|
||||||
|
int target = nums[left];
|
||||||
|
|
||||||
|
while (start < end) {
|
||||||
|
|
||||||
|
|
||||||
|
while (++start < nums.length && nums[start] < target) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (--end >=0 && nums[end] > target) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(start<end){
|
||||||
|
swap(nums,start,end);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
swap(nums,left,end);
|
||||||
|
|
||||||
|
return end;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void swap(int[] nums,int left,int right){
|
||||||
|
int temp=nums[left];
|
||||||
|
nums[left]=nums[right];
|
||||||
|
nums[right]=temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue