首页 > Web开发 > 详细

535. Encode and Decode TinyURL

时间:2020-09-24 09:59:33      阅读:51      评论:0      收藏:0      [点我收藏+]
package LeetCode_535

/**
 * 535. Encode and Decode TinyURL
 * https://leetcode.com/problems/encode-and-decode-tinyurl/description/
 *
 * TinyURL is a URL shortening service where you enter a URL such as
 * https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk.
Design the encode and decode methods for the TinyURL service.
There is no restriction on how your encode/decode algorithm should work.
You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL.
 * */
class Codec() {
    /*
    * solution, Two HaspMap
    * */
    private val BASE_URL = "http://tinyurl.com/"
    //total 62 characters for generate short_url
    val chatSet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    //key is long url,value short url
    val mapShortUrl = HashMap<String, String>()
    //key is short url,value long url
    val mapLongUrl = HashMap<String, String>()

    // Encodes a URL to a shortened URL.
    fun encode(longUrl: String): String {
        if (mapShortUrl.containsKey(longUrl)) {
            return mapShortUrl.get(longUrl) ?: ""
        }
        var key = ""
        do {
            val sb = StringBuilder()
            for (i in 0..6) {
                val r = (Math.random() * chatSet.length).toInt()
                sb.append(chatSet[r])
            }
            key = sb.toString()
            //if contains key, keep doing generate
        } while (mapShortUrl.containsKey(longUrl))
        val url = BASE_URL + key
        mapShortUrl.put(longUrl, url)
        mapLongUrl.put(url, longUrl)
        return url
    }

    // Decodes a shortened URL to its original URL.
    fun decode(shortUrl: String): String {
        return mapLongUrl.getOrDefault(shortUrl, "")
    }
}
/**
 * Your Codec object will be instantiated and called as such:
 * var obj = Codec()
 * var url = obj.encode(longUrl)
 * var ans = obj.decode(url)
 */

 

535. Encode and Decode TinyURL

原文:https://www.cnblogs.com/johnnyzhao/p/13721893.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!