20250217力扣每日一题

知识点

解题思路

  • 定义一个cnt变量,记录与当前元素在数组中共有多少个
  • 当遇到不一样的变量时,cnt归零,重新开始记录
  • 每次cnt更新之后,判断cnt * 4arr.size()的大小。
  • 由题意可知,数组内只存在一个符合条件的元素,所以当cnt * 4 > arr.size()时,直接返回当前元素

实现代码

class Solution {
public:
    int findSpecialInteger(vector<int>& arr) {
        int n  = arr.size(), target = n / 4, cnt = 0;
        for(int i = 0; i < n; i ++){
            if(i > 0 && arr[i] != arr[i - 1])
                cnt = 0;
            cnt ++;
            cout << cnt << endl;
            if(cnt > target)
                return arr[i];
        }

        return -1;

    }
};



Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Google Gemini updates: Flash 1.5, Gemma 2 and Project Astra
  • Displaying External Posts on Your al-folio Blog
  • 强化学习导论
  • 企业项目实训
  • 面试总结