From ccf3e34c1e64cdb0abb374d4baf33fb5a0392d04 Mon Sep 17 00:00:00 2001 From: YDZ Date: Fri, 5 Jul 2019 20:20:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20problem=20414?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../414. Third Maximum Number.go | 25 ++++++++ .../414. Third Maximum Number_test.go | 57 +++++++++++++++++++ .../0414. Third Maximum Number/README.md | 40 +++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 Algorithms/0414. Third Maximum Number/414. Third Maximum Number.go create mode 100644 Algorithms/0414. Third Maximum Number/414. Third Maximum Number_test.go create mode 100644 Algorithms/0414. Third Maximum Number/README.md diff --git a/Algorithms/0414. Third Maximum Number/414. Third Maximum Number.go b/Algorithms/0414. Third Maximum Number/414. Third Maximum Number.go new file mode 100644 index 00000000..bb741e6a --- /dev/null +++ b/Algorithms/0414. Third Maximum Number/414. Third Maximum Number.go @@ -0,0 +1,25 @@ +package leetcode + +import ( + "math" +) + +func thirdMax(nums []int) int { + a, b, c := math.MinInt64, math.MinInt64, math.MinInt64 + for _, v := range nums { + if v > a { + c = b + b = a + a = v + } else if v < a && v > b { + c = b + b = v + } else if v < b && v > c { + c = v + } + } + if c == math.MinInt64 { + return a + } + return c +} diff --git a/Algorithms/0414. Third Maximum Number/414. Third Maximum Number_test.go b/Algorithms/0414. Third Maximum Number/414. Third Maximum Number_test.go new file mode 100644 index 00000000..062cc829 --- /dev/null +++ b/Algorithms/0414. Third Maximum Number/414. Third Maximum Number_test.go @@ -0,0 +1,57 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question414 struct { + para414 + ans414 +} + +// para 是参数 +// one 代表第一个参数 +type para414 struct { + one []int +} + +// ans 是答案 +// one 代表第一个答案 +type ans414 struct { + one int +} + +func Test_Problem414(t *testing.T) { + + qs := []question414{ + + question414{ + para414{[]int{1, 1, 2}}, + ans414{2}, + }, + + question414{ + para414{[]int{3, 2, 1}}, + ans414{1}, + }, + + question414{ + para414{[]int{1, 2}}, + ans414{2}, + }, + + question414{ + para414{[]int{2, 2, 3, 1}}, + ans414{1}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 414------------------------\n") + + for _, q := range qs { + _, p := q.ans414, q.para414 + fmt.Printf("【input】:%v 【output】:%v\n", p, thirdMax(p.one)) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/0414. Third Maximum Number/README.md b/Algorithms/0414. Third Maximum Number/README.md new file mode 100644 index 00000000..9cb9b38b --- /dev/null +++ b/Algorithms/0414. Third Maximum Number/README.md @@ -0,0 +1,40 @@ +# [414. Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) + +## 题目 + +Given a **non-empty** array of integers, return the **third** maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n). + +**Example 1:** + + Input: [3, 2, 1] + + Output: 1 + + Explanation: The third maximum is 1. + +**Example 2:** + + Input: [1, 2] + + Output: 2 + + Explanation: The third maximum does not exist, so the maximum (2) is returned instead. + +**Example 3:** + + Input: [2, 2, 3, 1] + + Output: 1 + + Explanation: Note that the third maximum here means the third maximum distinct number. + Both numbers with value 2 are both considered as second maximum. + + +## 题目大意 + +给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是 O(n)。 + + +## 解题思路 + +- 水题,动态维护 3 个最大值即可。注意数组中有重复数据的情况。如果只有 2 个数或者 1 个数,则返回 2 个数中的最大值即可。 \ No newline at end of file