Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdda0e03

Browse files
authored
Merge pull requestneetcode-gh#2007 from AkifhanIlgaz/0535
Create: 0535-encode-and-decode-tinyURL.rs
2 parents6b15c8f +99b6928 commitdda0e03

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::collections::HashMap;
2+
3+
constBASE:&str ="http://tinyurl.com";
4+
5+
structCodec{
6+
url_map:HashMap<String,String>,
7+
}
8+
9+
implCodec{
10+
fnnew() ->Self{
11+
Self{
12+
url_map:HashMap::new(),
13+
}
14+
}
15+
16+
// Encodes a URL to a shortened URL.
17+
fnencode(&mutself,longURL:String) ->String{
18+
let short_url =format!("{}{}",BASE,Self::generate_hash(&longURL));
19+
self.url_map.insert(short_url.clone(), longURL.clone());
20+
short_url
21+
}
22+
23+
// Decodes a shortened URL to its original URL.
24+
fndecode(&self,shortURL:String) ->String{
25+
self.url_map.get(&shortURL).unwrap().clone()
26+
}
27+
28+
fngenerate_hash(url:&String) ->i32{
29+
letmut hash =5831;
30+
31+
for chin url.chars(){
32+
hash =((hash <<5) + hash) +(chasi32);
33+
}
34+
35+
hash
36+
}
37+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp