In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
The knowledge of this article "how to use Moore Voting in C++" is not quite understood by most people, so the editor summarizes the following, with detailed contents and clear steps, which can be used for reference. I hope you can get something after reading this article. Let's take a look at this "how to use Moore Voting in C++" article.
Algorithm:
Typical use scenario of Moore voting method
The Moore voting method is divided into two stages: the offset stage and the counting stage.
1. Offset stage: two different votes are matched and one vote is offset at the same time, and if the two votes are the same, the number of times that can be offset; 2. Counting stage: as long as the final offset count in the offset stage is not 0, then the candidate is likely to have more than half of the votes. In order to verify, it is necessary to traverse and count the votes before it can be determined. Note: for 1gamma 3meme 4.1Gap, the practice is to set up a voting candidate for nMub, and use the Moore voting method to operate.
Topic 1: more than half of the most elements
Code implementation:
Func majorityElement (nums [] int) int {tmp,count: = 0num 0 for _, num:=range nums {if count = = 0 {tmp = num} if num = = tmp {count++} else {count--}} return tmp} / / algorithm: there is a number of more than half in the array, / / then you can use this number as an indication This number is + 1, not this number is-1, and the last remaining number is what is needed.
Topic 2: counting the number
Code implementation:
Func majorityElement (nums [] int) [] int {/ / create the returned value var res = make ([] int, 0) if nums = = nil | | len (nums) = = 0 {return res}
/ / initialize the two candidates candidate and their counting votes cand1: = nums [0] count1: = 0 cand2: = nums [0] count2: = 0
/ / Molar voting method / / pairing stage for _, num: = range nums {/ / Voting if cand1 = = num {count1++ continue} if cand2 = = num {count2++ continue}
If count1 = = 0 {cand1 = num count1++ continue} if count2 = = 0 {cand2 = num count2++ continue}
Count1-- count2--} / / count stage count1 = 0 count2 = 0 for _, num: = range nums {if cand1 = = num {count1++} else if cand2 = = num {count2++}}
If count1 > len (nums) / 3 {res = append (res, cand1)} if count2 > len (nums) / 3 {res = append (res, cand2)} return res} / / algorithm: the application of the Moore Voting method is / / because it is 1Comp3, so two candidates are used to make the choice. The above is about the content of this article on "how to use Moore Voting in C++". I believe you all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.