137. Single Number II
選択した UI 言語に合わせて問題文をロシア語から翻訳します。コードは変更しません。
Дан 配列 целых чисел nums, в котором каждый element встречается три раза, кроме одного, который встречается ровно один раз. find этот единственный element и return его.
C# 解法
照合済み/オリジナルpublic class Solution {
public int SingleNumber(int[] nums) {
Array.Sort(nums);
for (int i = 0; i < nums.Length - 1; i += 3) {
if (nums[i] == nums[i + 1]) {
continue;
} else {
return nums[i];
}
}
return nums[nums.Length - 1];
}
}
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 SingleNumber(vector<int>& nums) {
sort(nums.begin(), nums.end());
for (int i = 0; i < nums.size() - 1; i += 3) {
if (nums[i] == nums[i + 1]) {
continue;
} else {
return nums[i];
}
}
return nums[nums.size() - 1];
}
}
Java 解法
照合済み/オリジナルclass Solution {
public int singleNumber(int[] nums) {
Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; i += 3) {
if (nums[i] == nums[i + 1]) {
continue;
} else {
return nums[i];
}
}
return nums[nums.length - 1];
}
}
JavaScript 解法
照合済み/オリジナルvar singleNumber = function (nums) {
nums.sort();
for (let i = 0; i < nums.length - 1; i += 3) {
if (nums[i] == nums[i + 1]) {
continue;
} else {
return nums[i];
}
}
return nums[nums.length - 1];
};
Python 解法
照合済み/オリジナルclass Solution:
def singleNumber(self, nums: List[int]) -> int:
nums.sort()
for i in range(0, len(nums) - 1, 3):
if nums[i] == nums[i + 1]:
continue
else:
return nums[i]
return nums[len(nums) - 1]
Go 解法
照合済み/オリジナルfunc singleNumber(nums []int) int {
sort.Ints(nums)
for i := 0; i < len(nums)-1; i += 3 {
if nums[i] == nums[i+1] {
continue
} else {
return nums[i]
}
}
return nums[len(nums)-1]
}
Vacancies for this task
有効な求人 with overlapping task tags are 表示.
有効な求人はまだありません。