Leetcode 127周赛有感

转眼之间,研一生活快结束了,实验室的师兄也开始找工作了,想参加华为的软挑又什么也不会,挺伤感的,生活真的很艰难,不过再艰难也要努力,加油。

上次参加周赛还是第116次,中间过了快3个月,然后感觉基本不会做题了,规定时间内只做出一道5分的题,超时10分钟做出另一道,写出的代码也简直不能看,贴出来警示自己吧。
leetcode1005题

class Solution {
   public  int largestSumAfterKNegations(int[] A, int K) {
		    
		    if(A.length == 1 && (K % 2 == 0))
		    	return A[0];
		    if(A.length == 1 && (K % 2 != 0))
		    	return -A[0];
	        Arrays.sort(A);
	        int cnt = 0;
	        int res = 0;
	      
	        for(int a:A)
	        	if(a < 0)
	        		cnt++;
       
          if(cnt == 0){
              if(K % 2 == 0){
                  for(int a:A)
                      res += a;
                  
                  return res;
              }
              else{
                  for(int i = 1;i<A.length;i++)
                      res += A[i];
                  return res - A[0];
              }
          }
	        
	        
	   
	        if(cnt > K) {
	        	for(int i = 0;i < K;i++)
	        		res += Math.abs(A[i]);
	        	for(int i = K;i < cnt;i++)
	        		res += A[i];
	        	
                for(int i = cnt;i < A.length;i++)
                	res += A[i];
	        }
	        else {
	        	for(int i = 0;i < cnt;i++)
	        		res += Math.abs(A[i]);
	        	
	        	if((K - cnt) % 2 == 0) {
	        		for(int i = cnt;i < A.length;i++)
	        		 res += A[i];
	         
	        	}
	        	else {
	        		for(int i = cnt + 1;i < A.length;i++)
		        		 res += A[i];
	        	      
	        		res = Math.max(res - A[cnt], res + A[cnt] + 2*A[cnt-1]);
	        		}
	        		
	        	
	        }
	        return res;
	        
	 }       

}

leetcode1007题

class Solution {
   	 public  int minDominoRotations(int[] A, int[] B) {
		    int n = A.length;
            if(n == 1)
                return 0;
		    int res = 0;
	        HashMap<Integer, Integer> hashMap = new HashMap<>();
	        HashSet<Integer> hashSet = new HashSet<>();
	        boolean flag = false;
	        for(int a:A) {
	        	if(hashMap.containsKey(a)) {
	        		if(hashMap.get(a) == n)
	        		{
	        			res = a;
	        			flag = true;
	        		}
	        			
	        		else {
	        		  hashMap.put(a, hashMap.get(a)+1);
	        		}
	        	}
	        	else {
	        		hashMap.put(a, 1);
	        	}
	        	if(flag)
	        		break;
	        }
	        if(!flag) {
	        	  for(int b:B) {
	  	        	if(hashMap.containsKey(b)) {
	  	        		if(hashMap.get(b) == n)
	  	        		{
	  	        			res = b;
	  	        			flag = true;
	  	        		}
	  	        		
	  	        		else {
	  	        		  hashMap.put(b, hashMap.get(b)+1);
	  	        		}
	  	        	}
	  	        	else {
	  	        		hashMap.put(b, 1);
	  	        	}	
	  	        	if(flag)
		        		break;
	  	        }
	        }
	       
	        	  if(!flag)
	        		  return -1;
	        	  
	        	  for(int i = 0;i < n;i++) {
	        		  if(A[i] == res || B[i] == res)
	        			  hashSet.add(i);
	        	  }
	        	  if(hashSet.size() < n)
	        		  return -1;
	        	 
	        	  int cnt1 = 0;
	        	  int cnt2 = 0;
	        	  for(int i = 0;i < n;i++)
	        		  if(A[i] == res)
	        			  cnt1++;
	        	  
	        	  for(int i = 0;i < n;i++)
	        		  if(B[i] == res)
	        			  cnt2++;
	   
	        	  
	        	  return Math.min(Math.min(cnt1, n-cnt1), Math.min(cnt2, n-cnt2));
	        	  
     }      
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值