Leetcode - Height Checker
Leetcode - Angle Between Hands of a Clock

Leetcode - Third Maximum Number

violet posted @ Jul 14, 2020 08:43:09 AM in 算法 with tags Algorithm Golang array , 405 阅读

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.

 

func thirdMax(nums []int) int {
	if len(nums) == 1 {
		return nums[0]
	}
	if len(nums) == 2 {
		if nums[0] > nums[1] {
			return nums[0]
		}
		return nums[1]
	}

	first := math.MinInt64
	second := math.MinInt64
	third := math.MinInt64

	for i := 0; i < len(nums); i++ {
		if nums[i] == first || nums[i] == second || nums[i] == third {
			continue
		}
		if first == math.MinInt32 || nums[i] > first {
			third = second
			second = first
			first = nums[i]
		} else if second == math.MinInt32 || nums[i] > second {
			third = second
			second = nums[i]
		} else if third == math.MinInt32 || nums[i] > third {
			third = nums[i]
		}
	}

	fmt.Println("first: ", first)
	fmt.Println("second: ", second)
	fmt.Println("third: ", third)

	if third == math.MinInt64 {
		return first
	}
	return third
}
JKBOSE Question Pape 说:
Sep 08, 2022 07:07:20 PM

JKBOSE Model Paper 2023 Class 4 Pdf Download with Answers for English Medium, Hindi Medium, Urdu Medium & Students for Small Answers, Long Answer, Very Long Answer Questions, and Essay Type Questions to Term1 & Term2 Exams at official website. JKBOSE Question Paper Class 4 New Exam Scheme or Question Pattern for Sammittive Assignment Exams (SA1 & SA2): Very Long Answer (VLA), Long Answer (LA), Small Answer (SA), Very Small Answer (VSA), Single Answer, Multiple Choice and etc.

BSNL FTTH 说:
Feb 08, 2023 04:09:41 PM

BSNL is installing Bharat Net a country-wide fiber optic cable for internet connectivity in many of the panchayats, and on the other hand, ISP brought the same fibernet technology to your doorstep directly and through TIPs. BSNL FTTH Telecom Infrastructure Providers (TIPs) with new BSNL Fiber plans covering many isolated pockets in all BSNL circles of the country for 50Mbps to 300 Mbps internet speed on providing with BSNL Fiber Plans along with FREE ONT as per the possibility.

bravotv.com link act 说:
Jul 11, 2023 11:24:43 PM

The activation code will appear on the TV screen when the streaming device has been connected. To enter the activation code, go to official website. First, you must log in to your TV provider and then select the Activate Now option to begin the activation procedure. bravotv.com link activation code Users may log in to the Bravo TV channel on their Smart TV or streaming device with this code and the service is now available on Android, Roku, Amazon Fire TV, Apple TV, iOS devices, computers, tablets, and Smart TVs.

WB 6th Class Syllab 说:
Jul 13, 2023 03:38:43 PM

West Bengal Board of Secondary Education is the West Bengal State Government Administered Autonomous Examining Authority for the 6th Class Standard Examination of West Bengal, WBBSE is an agency of Government of West Bengal Entrusted with the Responsibilities of Prescribing Courses of instructions and Books and Syllabus, Conducting Examinations for High School Level Students in West Bengal.Board High SchoolWB 6th Class Syllabus 2024 Parents can use the Syllabus to Understand the Concepts and Prepare their Children for the Exam, Accordingly, The West Bengal Class Syllabus 2024 All Subject Chapter Wise Students Prepare for the Upcoming 6th Class Exam


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter