当前位置:网站首页>535. TinyURL 的加密与解密 / 剑指 Offer II 103. 最少的硬币数目
535. TinyURL 的加密与解密 / 剑指 Offer II 103. 最少的硬币数目
2022-06-29 16:31:00 【彼淇梁】
535. TinyURL 的加密与解密【中等题】【每日一题】
思路:
生成随机短串进行加密,将加密后的 shortUrl 作为 key,longUrl 作为value存入数据库,解密的时候将shortUrl作为key在数据库中取出其对应的值即可。
第一次做没有考虑加密短串重复问题,第二次将其完善,存入数据库前先进行判断,如果重复就重新生成随机短串。
代码1:
public class Codec {
//存储 long - short URL
private Map<String,String> map;
//用于生成随机数
private Random random;
public Codec() {
this.map = new HashMap<>();
this.random = new Random();
}
// Encodes a URL to a shortened URL.
public String encode(String longUrl) {
//对long加密转为short
StringBuilder pwd = new StringBuilder("htttp://");
//加密时保持域名不动
int start = 0,end = 0,n = longUrl.length();
int flag = 0;
for (int i = 0; i < n; i++) {
if (longUrl.charAt(i) == '/'){
if (flag == 0){
flag++;
}else if (flag == 1){
flag++;
start = i+1;
}else if (flag == 2){
flag++;
end = i+1;
}else {
break;
}
}
}
pwd.append(longUrl, start, end);
//对域名的下一级目录生成 6 位 随机短串并存入数据库
pwd.append(random.nextInt(10));
pwd.append((char) ('a' + random.nextInt(26)));
pwd.append(random.nextInt(10));
pwd.append((char) ('a' + random.nextInt(26)));
pwd.append((char) ('A' + random.nextInt(26)));
pwd.append((char) ('a' + random.nextInt(26)));
map.put(pwd.toString(),longUrl);
return pwd.toString();
}
// Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
//解密,从map2中将 shortUrl 对应的 longUrl 取出
return map.getOrDefault(shortUrl,null);
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));
代码2:
public class Codec {
//存储 long - short URL
private Map<String,String> map;
//用于生成随机数
private Random random;
public Codec() {
this.map = new HashMap<>();
this.random = new Random();
}
// Encodes a URL to a shortened URL.
public String encode(String longUrl) {
//对long加密转为short
StringBuilder pwd = new StringBuilder("http://");
//加密时保持域名不动
int start = 0,end = 0,n = longUrl.length();
int flag = 0;
for (int i = 0; i < n; i++) {
if (longUrl.charAt(i) == '/'){
if (flag == 0){
flag++;
}else if (flag == 1){
flag++;
start = i+1;
}else if (flag == 2){
flag++;
end = i+1;
}else {
break;
}
}
}
pwd.append(longUrl, start, end);
//对域名的下一级目录生成 6 位 随机短串并存入数据库
String shortStr = generate6();
while (map.containsKey(shortStr)){
shortStr = generate6();
}
map.put(pwd + shortStr,longUrl);
return pwd + shortStr;
}
// Decodes a shortened URL to its original URL.
public String decode(String shortUrl) {
//解密,从map中将 shortUrl 对应的 longUrl 取出
return map.getOrDefault(shortUrl,null);
}
public String generate6(){
return String.valueOf(random.nextInt(10)) +
(char) ('a' + random.nextInt(26)) +
random.nextInt(10) +
(char) ('a' + random.nextInt(26)) +
(char) ('A' + random.nextInt(26)) +
(char) ('a' + random.nextInt(26));
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(url));
剑指 Offer II 103. 最少的硬币数目【中等题】
思路:【动态规划】
『 一文搞懂完全背包问题 』从0-1背包到完全背包,逐层深入+数学推导
学习评论区的题解思路
划重点记结论:
不超过背包容量的最优解(最大背包容量)
0-1背包:dp[i][j] = max{dp[i-1][j] , dp[i-1][j-vi] + vi}, 0 <= vi <= j
完全背包:dp[i][j] = max{dp[i-1][j] , dp[i][j-vi] + vi},0 <= vi <= j
恰好装满背包的最优解(最小物品方案)
0-1背包:dp[i][j] = max{dp[i-1][j] , dp[i-1][j-vi] + 1}, 0 <= vi <= j
完全背包:dp[i][j] = max{dp[i-1][j] , dp[i][j-vi] + 1},0 <= vi <= j
代码:
class Solution {
public int coinChange(int[] coins, int amount) {
int n = coins.length;
//定义dp数组,dp[i][j]表示前i个硬币组合总价值为j时所需的最小硬币个数
int[][] dp = new int[n+1][amount+1];
//边界条件 j = 0时,不取任何硬币即可满足题意,即 dp[i][0] = 0,由于数组默认赋0,因此这里不用重复操作
//边界条件 i = 0时,只有 j = 0 时,即不取任何硬币,只对要求的组合硬币数为0时符合要求,对于大于0的目标,显然是不可能取到的,这里赋初值为一个永远取不到的数 amount+1 即可
for (int j = 1; j <= amount ; j++) {
dp[0][j] = amount+1;
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= amount; j++) {
if (j >= coins[i-1]){
dp[i][j] = Math.min(dp[i-1][j],dp[i][j-coins[i-1]]+1);
}else {
dp[i][j] = dp[i-1][j];
}
}
}
return dp[n][amount] == amount + 1 ? -1 : dp[n][amount];
}
}
边栏推荐
- Flutter技术与实战(2)
- 贪婪的苹果计划提高iPhone14的价格,这将为中国手机提供机会
- 星环科技数据安全管理平台 Defensor重磅发布
- 自己实现一个ThreadLocal
- 进阶 | webgl性能优化初尝
- C11新特性——auto、decltype类型指示符
- C winfrom chart chart control bar chart and line chart
- Sophon base 3.1 launches mlops function to provide wings for enterprise AI capability operation
- What are the advantages of SaaS services
- 图文带你彻底弄懂MySQL事务原子性之UndoLog
猜你喜欢

【南京大学】考研初试复试资料分享

「科普大佬说」AI与创造力

AI and creativity

如何利用OpenMesh实现不同格式的3D文件间的转换

【 OpenGL 】 Random Talk 1. The camera rotates around a point in the space by dragging the mouse

Stable currency risk profile: are usdt and usdc safe?

Cv5200 ad hoc network remote WiFi module, UAV wireless image transmission application, HD low delay scheme

使用kalibr標定工具進行單目相機和雙目相機的標定

After eight years of testing and opening experience and interview with 28K company, hematemesis sorted out high-frequency interview questions and answers

都3年测试经验了,用例设计还不知道状态迁移法?
随机推荐
2022年有哪些适合穷人的理财产品?
穩定幣風險狀况:USDT 和 USDC 安全嗎?
The understanding of industrial Internet is directly related to how we view it
使用kalibr标定工具进行单目相机和双目相机的标定
kotlin基础语法
代码大全读后感
UWB precise positioning scheme, centimeter level high-precision technology application, intelligent pairing induction technology
可转债策略之---(摊饼玩法,溢价玩法,强赎玩法,下修玩法,双低玩法)
实践 | 移动端图片上传旋转、压缩的解决方案
DAP large screen theme development description
一个简单但是能上分的特征标准化方法
Simulink仿真模式
[proteus simulation] 8-bit nixie tube dynamic scanning display change data
Calibration of binocular camera based on OpenCV
元代理模型可迁移对抗攻击
Tool chain empowers hundreds of companies, horizon opens the "Matthew effect" of mass production of intelligent driving
八年测开经验面试28K公司后,吐血整理出高频面试题和答案
How to distinguish between instructions and data in the "group counting" CPU
Picture and text show you how to thoroughly understand the atomicity of MySQL transaction undolog
What's the difference between isempty and isblank? Half of the people can't answer it?