update 169
This commit is contained in:
		
							
								
								
									
										16
									
								
								leetcode/169.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								leetcode/169.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
				
			|||||||
 | 
					# Majority Elements
 | 
				
			||||||
 | 
					# the majority elements will appears more than half times
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Use Boyer-Moore Voting Alogrithm
 | 
				
			||||||
 | 
					class Solution:
 | 
				
			||||||
 | 
					    def majorityElements(self, nums: list[int]) -> int:
 | 
				
			||||||
 | 
					        count = 0
 | 
				
			||||||
 | 
					        candidate = None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for num in nums:
 | 
				
			||||||
 | 
					            if count == 0:
 | 
				
			||||||
 | 
					                candidate = num
 | 
				
			||||||
 | 
					            count += 1 if num == candidate else -1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return candidate
 | 
				
			||||||
		Reference in New Issue
	
	Block a user