[LeetCode] 第158场周赛题解

第一题:

public class Solution {
    public int balancedStringSplit(String s) {
        int ans=0;
        int[] buf=new int[s.length()];
        buf[0]=s.charAt(0)=='R'?-1:1;
        for(int i=1;i<s.length();i++) {
        	if(s.charAt(i)=='L')
        		buf[i]=buf[i-1]+1;
        	else
        		buf[i]=buf[i-1]-1;
        	if(buf[i]==0)
        		ans++;
        }
        return ans;
    } 
}

第二题:

这题是真的无聊。。。

public class Solution {

	
    public List<List<Integer>> queensAttacktheKing(int[][] queen, int[] king) {
        List<List<Integer>> ans=new ArrayList<List<Integer>>();
        int[][] map=new int[64][64];
        for(int[] index:queen)
        	map[index[0]][index[1]]=1;
        map[king[0]][king[1]]=2;
        for(int[] index:queen) {
        	for(int i=index[0]-1;i>=0;i--) {
        		if(map[i][index[1]]==1)
        			break;
        		if(map[i][index[1]]==2) {
        			ArrayList<Integer> temp=new ArrayList<Integer>();
        			temp.add(index[0]);
        			temp.add(index[1]);
        			ans.add(temp);
        		}
        	}
        	for(int i=index[0]+1;i<64;i++) {
        		if(map[i][index[1]]==1)
        			break;
        		if(map[i][index[1]]==2) {
        			ArrayList<Integer> temp=new ArrayList<Integer>();
        			temp.add(index[0]); temp.add(index[1]);
        			ans.add(temp);
        		}
        	}
        	for(int j=index[1]-1;j>=0;j--) {
        		if(map[index[0]][j]==1)
        			break;
        		if(map[index[0]][j]==2) {
        			ArrayList<Integer> temp=new ArrayList<Integer>();
        			temp.add(index[0]); temp.add(index[1]);
        			ans.add(temp);
        		}
        	}
        	for(int j=index[1]+1;j<64;j++) {
        		if(map[index[0]][j]==1)
        			break;
        		if(map[index[0]][j]==2) {
        			ArrayList<Integer> temp=new ArrayList<Integer>();
        			temp.add(index[0]); temp.add(index[1]);
        			ans.add(temp);
        		}
        	}
        	for(int i=index[0]-1,j=index[1]-1;i>=0&&j>=0;i--,j--) {
        		if(map[i][j]==1)
        			break;
        		if(map[i][j]==2) {
        			ArrayList<Integer> temp=new ArrayList<Integer>();
        			temp.add(index[0]); temp.add(index[1]);
        			ans.add(temp);
        		}
        	}
        	for(int i=index[0]+1,j=index[1]+1;i<64&&j<64;i++,j++) {
        		if(map[i][j]==1)
        			break;
        		if(map[i][j]==2) {
        			ArrayList<Integer> temp=new ArrayList<Integer>();
        			temp.add(index[0]); temp.add(index[1]);
        			ans.add(temp);
        		}
        	}
        	for(int i=index[0]-1,j=index[1]+1;i>=0&&j<64;i--,j++) {
        		if(map[i][j]==1)
        			break;
        		if(map[i][j]==2) {
        			ArrayList<Integer> temp=new ArrayList<Integer>();
        			temp.add(index[0]); temp.add(index[1]);
        			ans.add(temp);
        		}
        	}
        	for(int i=index[0]+1,j=index[1]-1;i<64&&j>=0;i++,j--) {
        		if(map[i][j]==1)
        			break;
        		if(map[i][j]==2) {
        			ArrayList<Integer> temp=new ArrayList<Integer>();
        			temp.add(index[0]); temp.add(index[1]);
        			ans.add(temp);
        		}
        	}
        }
        return ans;
    }
}

第三题:

dp[i][j][k]表示第k次扔到i且已经出现j次的方案数。

public class Solution {
	public static final int Mod=(int) (1e9+7);
	
    public int dieSimulator(int n, int[] rollMax) {
        int[][][] dp=new int[6][16][n+1];
        for(int i=0;i<6;i++) 
        	dp[i][1][1]=1;
        for(int k=2;k<=n;k++) {
        	for(int i=0;i<6;i++) {
            	for(int j=2;j<=rollMax[i];j++) {
            		dp[i][j][k]+=dp[i][j-1][k-1];
            		dp[i][j][k]%=Mod;
            	}
            	for(int x=0;x<6;x++) {
            		for(int y=1;y<=rollMax[x];y++) {
            			if(i==x)
            				continue;
            			dp[i][1][k]+=dp[x][y][k-1];
            			dp[i][1][k]%=Mod;
            		}
            	}
            }
        }
        int ans=0;
        for(int i=0;i<6;i++) {
        	for(int j=1;j<=rollMax[i];j++) {
        		ans=(ans+dp[i][j][n])%Mod;
        	}
        }
        return ans;
    }
}

第四题:

一定要理清楚,理清楚就没啥问题了。

public class Solution {	
	public HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
	public TreeMap<Integer,Integer> times=new TreeMap<Integer,Integer>();
	
	public void add(int x) {
		int val=map.getOrDefault(x, 0);
		if(val!=0) {
			int cnt=times.get(val);
			if(cnt==1) 
				times.remove(val);
			else
				times.put(val,cnt-1);
		}
		map.put(x, val+1);
		times.put(val+1, times.getOrDefault(val+1, 0)+1);
	}
	
	public boolean check() {
		if(times.size()==1&&map.size()==1)
			return true;
		if(times.size()==1&&times.containsKey(1))
			return true;
		if(times.size()!=2)
			return false;
		ArrayList<int[]> buf=new ArrayList<int[]>();
		for(Map.Entry<Integer, Integer> temp:times.entrySet()) {
			int[] p=new int[2];
			p[0]=temp.getKey();
			p[1]=temp.getValue();
			buf.add(p);
		}
		if(buf.get(0)[0]==1&&buf.get(0)[1]==1)
			return true;
		if(buf.get(1)[0]-buf.get(0)[0]==1&&buf.get(1)[1]==1)
			return true;
		return false;
	}
	
    public int maxEqualFreq(int[] nums) {
    	int ans=2;
    	for(int i=0;i<nums.length;i++) {
    		add(nums[i]);
    		if(check()) {
    			ans=Math.max(ans, i+1);
    		}
    	}
    	return ans;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值