首页 > 其他分享 >LeetCode 169. 多数元素

LeetCode 169. 多数元素

时间:2023-07-07 12:13:20浏览次数:31  
标签:cnt nums int 元素 169 LeetCode

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        int cnt=1;
        int res=nums[0];
        for(int i=1;i<nums.size();i++)
        {
            if(nums[i]==res)    cnt++;
            else    cnt--;
            if(!cnt)
            {
                res=nums[i];
                cnt=1;
            }
        }
        return res;
    }
};

标签:cnt,nums,int,元素,169,LeetCode
From: https://www.cnblogs.com/tangxibomb/p/17534591.html

相关文章

  • [LeetCode] 2024. Maximize the Confusion of an Exam
    Ateacheriswritingatestwith n true/falsequestions,with 'T' denotingtrueand 'F' denotingfalse.Hewantstoconfusethestudentsby maximizing thenumberof consecutive questionswiththe same answer(multipletruesormultiple......
  • CSS学习笔记3-CSS元素定位
    1标准流布局1.1认识定位属性......
  • 如何让父元素的最小宽度为某一个子元素的内容宽度
    具体做法是让除了那个子元素以外,所有子元素都使用flex布局,让后再叠加一层flex-grow:1;width:0的inner布局上代码:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edg......
  • [LeetCode] 2178. Maximum Split of Positive Even Integers
    Youaregivenaninteger finalSum.Splititintoasumofa maximum numberof unique positiveevenintegers.Forexample,given finalSum=12,thefollowingsplitsare valid (uniquepositiveevenintegerssummingupto finalSum): (12), (2+10), ......
  • leetcode-1629-easy
    SlowestKeyYouhaveabombtodefuse,andyourtimeisrunningout!Yourinformerwillprovideyouwithacirculararraycodeoflengthofnandakeyk.Todecryptthecode,youmustreplaceeverynumber.Allthenumbersarereplacedsimultaneously.I......
  • leetcode-1652-easy
    DefusetheBombYouhaveabombtodefuse,andyourtimeisrunningout!Yourinformerwillprovideyouwithacirculararraycodeoflengthofnandakeyk.Todecryptthecode,youmustreplaceeverynumber.Allthenumbersarereplacedsimultaneously......
  • LeetCode 160. 相交链表
    /***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode(intx):val(x),next(NULL){}*};*/classSolution{public:ListNode*getIntersectionNode(ListNode*headA,ListNode*headB){......
  • Jquery操作元素的属性和css
    <buttonid="problem_chart_search"type="confirm"style="margin:03em;color:#fff;font-size:.75em;padding:2px10px;">搜索</button>//1、改属性$('#problem_chart_search').attr("disabled","disa......
  • 【笔试实战】LeetCode题单刷题-编程基础 0 到 1【三】
    682. 棒球比赛题目链接682. 棒球比赛题目描述你现在是一场采用特殊赛制棒球比赛的记录员。这场比赛由若干回合组成,过去几回合的得分可能会影响以后几回合的得分。比赛开始时,记录是空白的。你会得到一个记录操作的字符串列表 ops,其中 ops[i] 是你需要记录的第 i 项操作......
  • Leetcode155. 最小栈
    classMinStack{public:stack<int>st;multiset<int>s;MinStack(){}voidpush(intval){st.push(val);s.insert(val);}voidpop(){intval=st.top();st.pop();......