博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 535: Encode and Decode TinyURL
阅读量:7239 次
发布时间:2019-06-29

本文共 1507 字,大约阅读时间需要 5 分钟。

public class Codec {    private long id;    private Map
urlMap; private final String elements = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; public Codec() { id = 0L; urlMap = new HashMap<>(); } // Encodes a URL to a shortened URL. public String encode(String longUrl) { urlMap.put(id, longUrl); return encode(id++); } private String encode(long id) { StringBuilder result = new StringBuilder(); while (id > 0) { result.append(elements.charAt((int)(id % 62))); id /= 62; } return result.toString(); } // Decodes a shortened URL to its original URL. public String decode(String shortUrl) { return urlMap.get(mapTo(shortUrl)); } private long mapTo(String url) { long result = 0; for (int i = url.length() - 1; i >= 0; i--) { result += maps(url.charAt(i)) * Math.pow(62, (url.length() - i - 1)); } return result; } private int maps(char c) { if (c >= '0' && c <= '9') { return Integer.valueOf(c); } else if (c >= 'a' && c <= 'z') { return 10 + (int)(c - 'a'); } else if ( c >= 'A' && c <= 'Z') { return 36 + (int)(c - 'A'); } return 0; }}// Your Codec object will be instantiated and called as such:// Codec codec = new Codec();// codec.decode(codec.encode(url));

 

转载于:https://www.cnblogs.com/shuashuashua/p/7469345.html

你可能感兴趣的文章
linux命令---unzip
查看>>
(转)做自己网站的访问记录系统,用于推广统计
查看>>
Chisel语言
查看>>
浅谈Java的学习
查看>>
Mac使用技巧
查看>>
[十二省联考2019]春节十二响
查看>>
【iOS】苹果IAP(内购)中沙盒账号使用注意事项
查看>>
跨域名上传图片
查看>>
XML解析——Java中XML的四种解析方式
查看>>
玩转SpringCloud(F版本) 一.服务的注册与发现(Eureka)
查看>>
Quartz 2D基础知识
查看>>
Servlet常用类
查看>>
leetcode 47全排列II
查看>>
线性/简单DP | 问题集合
查看>>
转 Xenserver HVM is required for this operation的解决办法
查看>>
数据库触发器,禁止DDL操作
查看>>
获取身份证号码信息
查看>>
Python之条件判断
查看>>
Ring0句柄表遍历
查看>>
AC日记——[SDOI2009]HH去散步 洛谷 P2151
查看>>