ARTS打卡计划第6周-ALGORITHM

Posted dongqisilent

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
	        
	    }
}

  

以上是关于ARTS打卡计划第6周-ALGORITHM的主要内容,如果未能解决你的问题,请参考以下文章

ARTS打卡计划第6周-REVIEW-超越编码的避免项目失败的软技能

ARTS打卡计划第三周-Algorithm

ARTS打卡计划第四周-ALGORITHM

ARTS打卡计划第二周-Algorithm

ARTS打卡第6周

Arts打卡第10周