-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path535.go
More file actions
36 lines (30 loc) · 654 Bytes
/
Copy path535.go
File metadata and controls
36 lines (30 loc) · 654 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import (
"fmt"
"strconv"
)
type Codec struct {
count int
m map[int]string
}
func Constructor() Codec {
c := Codec{0, map[int]string{}}
return c
}
// Encodes a URL to a shortened URL.
func (this *Codec) encode(longUrl string) string {
this.count++
this.m[this.count] = longUrl
res := strconv.Itoa(this.count)
return res
}
// Decodes a shortened URL to its original URL.
func (this *Codec) decode(shortUrl string) string {
index, _ := strconv.Atoi(shortUrl)
return this.m[index]
}
/**
* Your Codec object will be instantiated and called as such:
* obj := Constructor();
* url := obj.encode(longUrl);
* ans := obj.decode(url);
*/