415. Add Strings

LeetCode easy original: C# #array #csharp #easy #leetcode #string
선택한 UI 언어에 맞게 문제 텍스트를 러시아어에서 번역합니다. 코드는 변경하지 않습니다.

Если задан 정수 배열 nums, return третье максимальное number в этом 배열е. Если третьего максимального числа не существует, return максимальное number.

예제:

Input: num1 = "11", num2 = "123"

Output: "134"

C# 해법

매칭됨/원본
public class Solution {
    public int ThirdMax(int[] nums) {
        int? first = null;
        int? second = null;
        int? third = null;
        
        foreach (int num in nums) {
            if (num == first || num == second || num == third) {
                continue;
            }
            if (first == null || num > first) {
                third = second;
                second = first;
                first = num;
            } else if (second == null || num > second) {
                third = second;
                second = num;
            } else if (third == null || num > third) {
                third = num;
            }
        }
        
        return third ?? first.Value;
    }
}

C++ 해법

자동 초안, 제출 전 검토
#include <bits/stdc++.h>
using namespace std;

// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
    public int ThirdMax(vector<int>& nums) {
        int? first = null;
        int? second = null;
        int? third = null;
        
        foreach (int num in nums) {
            if (num == first || num == second || num == third) {
                continue;
            }
            if (first == null || num > first) {
                third = second;
                second = first;
                first = num;
            } else if (second == null || num > second) {
                third = second;
                second = num;
            } else if (third == null || num > third) {
                third = num;
            }
        }
        
        return third ?? first.Value;
    }
}

Java 해법

매칭됨/원본
import java.util.HashSet;
import java.util.Set;

public class Solution {
    public int thirdMax(int[] nums) {
        Integer first = null;
        Integer second = null;
        Integer third = null;
        
        for (Integer num : nums) {
            if (num.equals(first) || num.equals(second) || num.equals(third)) {
                continue;
            }
            if (first == null || num > first) {
                third = second;
                second = first;
                first = num;
            } else if (second == null || num > second) {
                third = second;
                second = num;
            } else if (third == null || num > third) {
                third = num;
            }
        }
        
        return third != null ? third : first;
    }
}

JavaScript 해법

매칭됨/원본
function thirdMax(nums) {
    let first = null;
    let second = null;
    let third = null;
    
    for (const num of nums) {
        if (num === first || num === second || num === third) {
            continue;
        }
        if (first === null || num > first) {
            third = second;
            second = first;
            first = num;
        } else if (second === null || num > second) {
            third = second;
            second = num;
        } else if (third === null || num > third) {
            third = num;
        }
    }
    
    return third !== null ? third : first;
}

Go 해법

매칭됨/원본
package main

import (
    "math"
)

func thirdMax(nums []int) int {
    first, second, third := math.MinInt64, math.MinInt64, math.MinInt64
    
    for _, num := range nums {
        if num == first || num == second || num == third {
            continue
        }
        if num > first {
            third = second
            second = first
            first = num
        } else if num > second {
            third = second
            second = num
        } else if num > third {
            third = num
        }
    }
    
    if third == math.MinInt64 {
        return first
    }
    return third
}

Algorithm

Инициализируйте три переменные для хранения первого, второго и третьего максимальных чисел.

Пройдите по 배열у nums, обновляя переменные первого, второго и третьего максимальных чисел, избегая дубликатов.

return третье максимальное number, если оно существует, иначе return первое максимальное number.

😎

Vacancies for this task

활성 채용 with overlapping task tags are 표시됨.

전체 채용
아직 활성 채용이 없습니다.