zoukankan      html  css  js  c++  java
  • ARTS打卡计划第6周-ALGORITHM

    535. TinyURL 的加密与解密

    这题其实是很常见的一个开发场景,短地址的开发。我这里只是分享一种md5的方式,还有其他的生成字符串比较短的hash方式。

    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.util.HashMap;
    
    public class Codec {
    	private HashMap<String, String> map = new HashMap<>();
    
    	// Encodes a URL to a shortened URL.
    	public String encode(String longUrl) {
    		String md5 = crypt(longUrl);
    		String url = "http://tinyurl.com/" + md5;
    		map.put(url, longUrl);
    		return url;
    
    	}
    
    	// Decodes a shortened URL to its original URL.
    	public String decode(String shortUrl) {
    		return map.get(shortUrl);
    
    	}
    
    	public String crypt(String str) {
    		if (str == null || str.length() == 0) {
    			throw new IllegalArgumentException("String to encript cannot be null or zero length");
    		}
    		StringBuffer hexString = new StringBuffer();
    		try {
    			MessageDigest md = MessageDigest.getInstance("MD5");
    			md.update(str.getBytes());
    			byte[] hash = md.digest();
    			for (int i = 0; i < hash.length; i++) {
    				if ((0xff & hash[i]) < 0x10) {
    					hexString.append("0" + Integer.toHexString((0xFF & hash[i])));
    				} else {
    					hexString.append(Integer.toHexString(0xFF & hash[i]));
    				}
    			}
    		} catch (NoSuchAlgorithmException e) {
    			e.printStackTrace();
    		}
    		return hexString.toString();
    	}
    	
    	public static void main(String[] args) {
    		String url = "https://leetcode.com/problems/design-tinyurl";
    		Codec codec = new Codec();
    		System.out.println(codec.decode(codec.encode(url)));
    	}
    }
    

      

    728. 自除数

    import java.util.LinkedList;
    import java.util.List;
    class Solution {
        public List<Integer> selfDividingNumbers(int left, int right) {
    		  List<Integer> ret = new LinkedList<>();
    		  for(int i=left;i<=right;i++) {
    			  if(isSelfDividing(i)) {
    				  ret.add(i);
    			  }
    		  }
    		  return ret;
    	    }
    	  
    	   public boolean isSelfDividing(int n) {
    		  char[] chars = (n+"").toCharArray();
    		  for(char c :chars) {
    			  int cInt = c-'0';
    			  if(cInt ==0 ) {
    				  return false;
    			  }
    			  if(n%cInt!=0) {
    				  return false;
    			  }
    		  }
    		  return true;
    		  
    	  }
    }
    

      

    507. 完美数

    public class Solution507 {
    
    	   public boolean checkPerfectNumber(int num) {
    		  
    		   int sum = 1;
    		   for(int i=2;i<=Math.sqrt(num);i++) {
    			   int num1,num2=0;
    			   if(num%i==0) {
    				   num1=i;
    				   num2=num/num1;
    				   if(num1!=num2) {
    					   sum+=num1+num2;
    				   }else {
    					   sum = num1;
    				   }
    			   }
    			   
    		   }
    		   if (num ==sum && num!=1) {
    			   return true;
    		   }else {
    			   return false;
    		   }
    	        
    	    }
    	
    	   
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Solution507 s = new Solution507();
    		System.err.println(s.checkPerfectNumber(1));
    		System.err.println(s.checkPerfectNumber(2));
    		System.err.println(s.checkPerfectNumber(4));
    		System.err.println(s.checkPerfectNumber(28));
    	}
    
    }
    

      

    633. 平方数之和

    class Solution {
     	  public boolean judgeSquareSum(int c) {
    		 for(int i=0;i<=Math.sqrt(c);i++) {
    			 int lastNums = c -i*i;
    			 int last = (int) Math.sqrt(lastNums) ;
    			 if(last*last == lastNums) {
    				 return true;
    			 }
    			
    		 }
    		 return false;
    	        
    	    }
    }
    

      

  • 相关阅读:
    java(一) 基础部分
    Spring使用Autowiring自动装配 解决提示报错小技巧
    idea 中dao层自动生成接口
    从git远程仓库Checkout项目到本地
    idea 新建项目上传至git(coding)
    使用Travis CI给hexo部署做持续集成
    LeetCode395-至少有 K 个重复字符的最长子串
    Java注解
    Java反射
    Java反射应用实例
  • 原文地址:https://www.cnblogs.com/dongqiSilent/p/10909312.html
Copyright © 2011-2022 走看看