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;

	    }
}

  

原文地址:https://www.cnblogs.com/dongqiSilent/p/10909312.html

时间: 2024-08-30 13:59:38

ARTS打卡计划第6周-ALGORITHM的相关文章

ARTS打卡计划第三周-Algorithm

1. 两数之和 提供了2种揭发 public class Solution1 { public int[] twoSum(int[] nums, int target) { int len=nums.length; int[] result = new int[2]; for(int i=0;i<len;i++) { for(int j=i+1;j<len;j++) { if(nums[i]==target-nums[j]) { result[0]=i; result[1]=j; } } }

ARTS打卡计划第三周-Tips

本周分享一个好用的小工具,java-faker:https://github.com/DiUS/java-faker,相应的python:https://github.com/joke2k/faker. 使用faker库,可以轻松的造假数据,再也不用担心演示的时候,假数据不好看了. java版本使用很简单,引入依赖: <dependency> <groupId>com.github.javafaker</groupId> <artifactId>javafa

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

https://medium.com/@viral_shah/beyond-coding-soft-skills-to-avoid-project-failures-4ed7821fa93a 做项目中经常听到如下的抱怨不: 1.客户在后面更改了需求了,导致整个项目延期,这不是我们的锅. 2.系统设计时候,不是这样使用的,是他们不会用 3.我们的系统太专业太复杂了,他们不会明白如何使用的 4.我们提前都说了,系统还没有准备好,但是他们还是发布上线了 5.我们开发了一套牛逼的系统,但是销售却不知道如

ARTS打卡计划第6周-TIPS-多台服务器免密码登录

本周分享,一个命令rsync,可以在多台服务器上进行ssh免秘钥登录,反正我使用后是觉得相当好用. for i in `cat /root/hosts`;do rsync -e "ssh -p22" -avpgolr /root/.ssh [email protected]$i:/root/;done 脚本的含义是,取出 /root/hosts 下每一行的ip,同步他们的/root/.ssh 目录. 参考了:https://www.cnblogs.com/kevingrace/p/90

ARTS打卡计划第六周

Algorithms: https://leetcode-cn.com/problems/longest-palindromic-substring/ 中心扩展法首先考虑,当然看到有个动态规划,一直很难理解.动态规划要好好研究一番. Review: https://link.medium.com/rstYA3alzX Goodbye, Object Oriented Programming” by Charles Scalfani Tips: c++11 : 1.正则表达式 RE库. Share

ARTS打卡计划第八周

Algorithms: https://leetcode-cn.com/problems/repeated-substring-pattern/ 重复子字符串 Review: “I’m Leaving Google?—?and Here’s the Real Deal Behind Google Cloud” by Amir Hermelin https://link.medium.com/mPXXS8aBWX Tips: android : init.rc Android初始化语言包含了四种类

ARTS打卡计划第十周

Algorithms: https://leetcode-cn.com/problems/next-greater-node-in-linked-list/ 链表中下一个更大的值,双层循环及优化,后面看可以栈处理,学习了 Review: “Can You Avoid Functional Programming as a Policy?” by Eric Elliott https://link.medium.com/oWci9jdLjY Tips: android : Andorid  lin

ARTS打卡计划第二周-Algorithm

665. 非递减数列  https://leetcode-cn.com/problems/non-decreasing-array/ 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i < n),满足 array[i] <= array[i + 1]. class Solution { public boolean checkPossibility(int[]

ARTS打卡计划第二周-Review

本周review的文章是:https://medium.com/@hakibenita/optimizing-django-admin-paginator-53c4eb6bfca3 改篇文章的题目是:Optimizing Django Admin Paginator,How we finally made Django admin fast for large tables. django分页的时候,大部分时间都会消耗在求count上,本篇文章提到了几点用于提升大表分页的方法: 1.重写默认的分