Skip to content

Commit ae3fe15

Browse files
[LEET-535] add 535
1 parent 874bbe3 commit ae3fe15

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

leetcode-algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Algorithms
44
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
55
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
6+
|535|[Encode and Decode TinyURL](https://leetcode.com/problems/encode-and-decode-tinyurl/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/EncodeandDecodeTinyURL.java) | O(1) |O(n) | Medium | Design
67
|530|[Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/MinimumAbsoluteDifferenceinBST.java) | O(n) |O(n) | Easy| DFS
78
|529|[Minesweeper](https://leetcode.com/problems/minesweeper/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/Minesweeper.java) | O(m*n) |O(k) | Medium | BFS
89
|520|[Detect Capital](https://leetcode.com/problems/detect-capital/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/DetectCapital.java) | O(n) |O(1) | Easy|
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.stevesun.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 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.
8+
9+
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.
10+
11+
Note: Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless.
12+
*/
13+
public class EncodeandDecodeTinyURL {
14+
public class Codec {
15+
Map<String, Long> map = new HashMap<>();
16+
Long maxValue = 0L;
17+
Map<Long, String> reverseMap = new HashMap<>();
18+
19+
// Encodes a URL to a shortened URL.
20+
public String encode(String longUrl) {
21+
maxValue++;
22+
map.put(longUrl, maxValue);
23+
reverseMap.put(maxValue, longUrl);
24+
return String.valueOf(maxValue);
25+
}
26+
27+
// Decodes a shortened URL to its original URL.
28+
public String decode(String shortUrl) {
29+
return String.valueOf(reverseMap.get(Long.valueOf(shortUrl)));
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy