返回介绍

solution / 0200-0299 / 0229.Majority Element II / README_EN

发布于 2024-06-17 01:04:03 字数 4926 浏览 0 评论 0 收藏 0

229. Majority Element II

中文文档

Description

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.

 

Example 1:

Input: nums = [3,2,3]
Output: [3]

Example 2:

Input: nums = [1]
Output: [1]

Example 3:

Input: nums = [1,2]
Output: [1,2]

 

Constraints:

  • 1 <= nums.length <= 5 * 104
  • -109 <= nums[i] <= 109

 

Follow up: Could you solve the problem in linear time and in O(1) space?

Solutions

Solution 1

class Solution:
  def majorityElement(self, nums: List[int]) -> List[int]:
    n1 = n2 = 0
    m1, m2 = 0, 1
    for m in nums:
      if m == m1:
        n1 += 1
      elif m == m2:
        n2 += 1
      elif n1 == 0:
        m1, n1 = m, 1
      elif n2 == 0:
        m2, n2 = m, 1
      else:
        n1, n2 = n1 - 1, n2 - 1
    return [m for m in [m1, m2] if nums.count(m) > len(nums) // 3]
class Solution {
  public List<Integer> majorityElement(int[] nums) {
    int n1 = 0, n2 = 0;
    int m1 = 0, m2 = 1;
    for (int m : nums) {
      if (m == m1) {
        ++n1;
      } else if (m == m2) {
        ++n2;
      } else if (n1 == 0) {
        m1 = m;
        ++n1;
      } else if (n2 == 0) {
        m2 = m;
        ++n2;
      } else {
        --n1;
        --n2;
      }
    }
    List<Integer> ans = new ArrayList<>();
    n1 = 0;
    n2 = 0;
    for (int m : nums) {
      if (m == m1) {
        ++n1;
      } else if (m == m2) {
        ++n2;
      }
    }
    if (n1 > nums.length / 3) {
      ans.add(m1);
    }
    if (n2 > nums.length / 3) {
      ans.add(m2);
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> majorityElement(vector<int>& nums) {
    int n1 = 0, n2 = 0;
    int m1 = 0, m2 = 1;
    for (int m : nums) {
      if (m == m1)
        ++n1;
      else if (m == m2)
        ++n2;
      else if (n1 == 0) {
        m1 = m;
        ++n1;
      } else if (n2 == 0) {
        m2 = m;
        ++n2;
      } else {
        --n1;
        --n2;
      }
    }
    vector<int> ans;
    if (count(nums.begin(), nums.end(), m1) > nums.size() / 3) ans.push_back(m1);
    if (count(nums.begin(), nums.end(), m2) > nums.size() / 3) ans.push_back(m2);
    return ans;
  }
};
func majorityElement(nums []int) []int {
  var n1, n2 int
  m1, m2 := 0, 1
  for _, m := range nums {
    if m == m1 {
      n1++
    } else if m == m2 {
      n2++
    } else if n1 == 0 {
      m1, n1 = m, 1
    } else if n2 == 0 {
      m2, n2 = m, 1
    } else {
      n1, n2 = n1-1, n2-1
    }
  }
  n1, n2 = 0, 0
  for _, m := range nums {
    if m == m1 {
      n1++
    } else if m == m2 {
      n2++
    }
  }
  var ans []int
  if n1 > len(nums)/3 {
    ans = append(ans, m1)
  }
  if n2 > len(nums)/3 {
    ans = append(ans, m2)
  }
  return ans
}
public class Solution {
  public IList<int> MajorityElement(int[] nums) {
    int n1 = 0, n2 = 0;
    int m1 = 0, m2 = 1;
    foreach (int m in nums)
    {
      if (m == m1)
      {
        ++n1;
      }
      else if (m == m2)
      {
        ++n2;
      }
      else if (n1 == 0)
      {
        m1 = m;
        ++n1;
      }
      else if (n2 == 0)
      {
        m2 = m;
        ++n2;
      }
      else
      {
        --n1;
        --n2;
      }
    }
    var ans = new List<int>();
    ans.Add(m1);
    ans.Add(m2);
    return ans.Where(m => nums.Count(n => n == m) > nums.Length / 3).ToList();
  }
}
class Solution {
  /**
   * @param Integer[] $nums
   * @return Integer[]
   */
  function majorityElement($nums) {
    $rs = [];
    $n = count($nums);
    for ($i = 0; $i < $n; $i++) {
      $hashmap[$nums[$i]] += 1;
      if ($hashmap[$nums[$i]] > $n / 3) {
        array_push($rs, $nums[$i]);
      }
    }
    return array_unique($rs);
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文