文章目录
  1. 1. 文档更新说明
  2. 2. 前言
  3. 3. 535. Encode and Decode TinyURL
    1. 3.1. Example1
    2. 3.2. Note
    3. 3.3. Infer
    4. 3.4. Solution
  4. 4. 537. Complex Number Multiplication
    1. 4.1. Example1
    2. 4.2. Example1
    3. 4.3. Note
    4. 4.4. Solution

文档更新说明

  • 最后更新 2018年03月23日
  • 首次更新 2017年05月18日

前言

  使用Swift语言完成LetCode题目,难度Medium,就不多介绍了.

535. Encode and Decode TinyURL

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.

Subscribe to see which companies asked this question.

Example1

Note

Infer

Solution

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
37
38
39
40
41
42
43
44
////
//// main.swift
//// TestSwiftCommandLine
////
//// Created by Cocos on 2016/10/27.
//// Copyright © 2016年 Cocos. All rights reserved.
////

class Solution {
let dictStr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var seq = 0
var urlMapL2S: [String: String] = [:]
var urlMapS2L: [String: String] = [:]
//短地址长度
let shortLength = 6

//原理就是对longUrl进行映射
func encode(_ longUrl: String) -> String {
if let s = urlMapL2S[longUrl] {
return s
}

var res = ""
var count = seq
seq += 1
while count != 0 {
res = String(dictStr[dictStr.index(dictStr.startIndex, offsetBy: count % dictStr.count)]) + res
count /= dictStr.characters.count
}

for _ in res.characters.count ..< shortLength {
res = "0" + res
}

urlMapL2S[longUrl] = res
urlMapS2L[res] = longUrl

return res
}

func decode(_ shortUrl: String) -> String {
return urlMapS2L[shortUrl]!
}
}

537. Complex Number Multiplication

Given two strings representing two complex numbers.

You need to return a string representing their multiplication. Note i2 = -1 according to the definition.

Example1

Input: "1+1i", "1+1i"
Output: "0+2i"
Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.

Example1

Input: "1+-1i", "1+-1i"
Output: "0+-2i"
Explanation: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i.

Note

  1. The input strings will not have extra blank.
  2. The input strings will be given in the form of a+bi, where the integer a and b will both belong to the range of [-100, 100]. And the output should be also in this form.

Solution

  这是一道复数相乘的题目,直接套公式上去就好了.算是考察Swift语法吧😂.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import Foundation

class Solution {
func complexNumberMultiply(_ a: String, _ b: String) -> String {
//去掉最后一个字符...
let aArr = a[..<a.index(before: a.endIndex)].split(separator: "+")
let bArr = b[..<b.index(before: b.endIndex)].split(separator: "+")

let a = aArr[0], b = aArr[1]
let c = bArr[0], d = bArr[1]
//按照复数乘法公式套进去就好了,公式我百度的.类似多项式相乘,注意 i^2=-1,这个是规定的
return "\(Int(a)! * Int(c)! - Int(b)! * Int(d)!)+\(Int(a)! * Int(d)! + Int(c)! * Int(b)!)i"
}
}

let s = Solution()
print(s.complexNumberMultiply("1+-1i","0+0i"))
文章目录
  1. 1. 文档更新说明
  2. 2. 前言
  3. 3. 535. Encode and Decode TinyURL
    1. 3.1. Example1
    2. 3.2. Note
    3. 3.3. Infer
    4. 3.4. Solution
  4. 4. 537. Complex Number Multiplication
    1. 4.1. Example1
    2. 4.2. Example1
    3. 4.3. Note
    4. 4.4. Solution