中信信用卡中信算法投票4-14
ac了3道,附加题dfs写错了ac0
/**
* Created by lwf on 2021/4/14 16:08
*/
public class Main1 {
public static void main(String[] args) {
System.out.println(new Main1().primeSum(2, 5));
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* primeSum函数返回素数和
* @param n int整型 n为正整数,且n>1
* @param m int整型 m为正整数,且m>n
* @return int整型
*/
public int primeSum (int n, int m) {
// write code here
int sum=0;
for(int i=n;i<=m;i++){
if(isSu(i)){
sum+=i;
}
}
return sum;
}
private boolean isSu(int a){
if(a==2){
return true;
}
for(int i=2;i<=Math.sqrt(a);i++){
if(a%i==0){
return false;
}
}
return true;
}
} public class Main2 {
public static void main(String[] args) {
Main2 main2=new Main2();
String[] days=new String[]{"111101110111011101110111011010", "111101110111011101110111011010", "111101110111010101010111011010", "111101110101011101110111111010", "111101110111011101110111011110", "111101010111011101110111010111", "101101110111011101110111011110", "010101110111011101110101011110"};
System.out.println("main2.countDays(days) = " + main2.countDays(days));
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 获取全体员工全员上班的天数
* @param workDays string字符串一维数组 每个员工的上班日期,0代表休息1代表上班,数组大小大于0小于10000
* @return int整型
*/
public int countDays (String[] workDays) {
// write code here
int days=workDays[0].length();
byte[] bytes=new byte[days];
for(int i=0;i<days;i++){
byte flag=1;
for(String str:workDays){
flag&=str.charAt(i);
}
bytes[i]=flag;
}
int sum=0;
for(byte b:bytes){
sum+=b;
}
return sum;
}
} import java.util.*;
/**
* Created by lwf on 2021/4/14 16:29
*/
public class Main3 {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param strs string字符串一维数组
* @return string字符串ArrayList<ArrayList<>>
*/
public ArrayList<ArrayList<String>> groupAnagrams (String[] strs) {
// write code here
ArrayList<ArrayList<String>> list=new ArrayList<>();
HashMap<String,ArrayList<String>> map=new HashMap<>();
for(int i=0;i<strs.length;i++){
String hash=hash(strs[i]);
if(!map.containsKey(hash)){
ArrayList<String> list1=new ArrayList<>();
list1.add(strs[i]);
list.add(list1);
map.put(hash,list1);
}else {
ArrayList<String> strings = map.get(hash);
strings.add(strs[i]);
}
}
return list;
}
/**
* 字符串编程位图
* @param str
* @return */
private String hash(String str){
byte[] bytes=new byte[26];
//小写
//str.toLowerCase();
for(char c:str.toCharArray()){
bytes[c-'a']=1;
}
return new String(bytes);
}
} 附加题写错了,代码也贴出来。 /**
* Created by lwf on 2021/4/14 16:49
* "{{-5,-1,-1},{-4,-2,0},{1,1,-1},{-3,0,-8}}"
*/
public class Main4 {
public static void main(String[] args) {
Main4 main4=new Main4();
System.out.println(main4.calculateAdventureMinHp("{{-2,-1,0},{-1,0,-3},{-2,2,-1}}"));
}
public int calculateAdventureMinHp (String adventurePlayer) {
// write code here
String[] handler=adventurePlayer.substring(2,adventurePlayer.length()-2).split("},\\{");
int x=handler.length;
int y=len(handler[0]);
int[][] res=new int[x][y];
for(int i=0;i<handler.length;i++){
String[] tmp=handler[i].split(",");
for(int j=0;j<tmp.length;j++){
res[i][j]=Integer.parseInt(tmp[j]);
}
}
return dps(res,0,y-1);
}
private int len(String str){
return str.split(",").length;
}
private int dps(int[][] arr,int x,int y){
int min=0;
System.out.println("arr[x][y] = " + arr[x][y]);
if(x>=arr.length||y<0){
return 0;
}
if(x==(arr.length-1)&&y==0){
System.out.println("end");
return arr[x][y];
}
if(y-1<0){
min=dps(arr,x+1,y);
}else if(x+1>=arr.length){
min=dps(arr,x,y-1);
}else {
min=Math.max(dps(arr,x+1,y),dps(arr,x,y-1));
}
System.out.println("min = " + min);
return arr[x][y]+min;
}
} 7点20收到预约面试邮件了。
