# 多數元素
> 原文: [https://www.geeksforgeeks.org/majority-element/](https://www.geeksforgeeks.org/majority-element/)
編寫一個接受數組并打印多數元素(如果存在)的函數,否則打印“無多數元素”。 大小為 n 的數組 A []中的 ***多數元素*** 是出現超過 n / 2 次的元素(因此,最多有一個這樣的元素)。
**示例**:
```
Input : {3, 3, 4, 2, 4, 4, 2, 4, 4}
Output : 4
Explanation: The frequency of 4 is 5 which is greater
than the half of the size of the array size.
Input : {3, 3, 4, 2, 4, 4, 2, 4}
Output : No Majority Element
Explanation: There is no element whose frequency is
greater than the half of the size of the array size.
```
**方法 1**
* **方法**:基本解決方案是具有兩個循環并跟蹤所有不同元素的最大計數。 如果最大計數大于 n / 2,則中斷循環并返回具有最大計數的元素。 如果最大數量不超過 n / 2,則多數元素不存在。
* **算法**:
1. 創建一個變量來存儲最大計數,*計數= 0*
2. 從頭到尾遍歷整個數組。
3. 對于數組中的每個元素,運行另一個循環以查找給定數組中相似元素的數量。
4. 如果計數大于最大計數,則更新最大計數并將索引存儲在另一個變量中。
5. 如果最大計數大于數組大小的一半,則打印該元素。 否則,沒有多數元素。
* **實現**:
## C++
```
// C++ program to find Majority?
// element in an array?
#include <bits/stdc++.h>?
using namespace std;?
// Function to find Majority element?
// in an array?
void findMajority(int arr[], int n)?
{?
????int maxCount = 0;?
????int index = -1; // sentinels?
????for(int i = 0; i < n; i++)?
????{?
????????int count = 0;?
????????for(int j = 0; j < n; j++)?
????????{?
????????????if(arr[i] == arr[j])?
????????????count++;?
????????}?
????????// update maxCount if count of?
????????// current element is greater?
????????if(count > maxCount)?
????????{?
????????????maxCount = count;?
????????????index = i;?
????????}?
????}?
????// if maxCount is greater than n/2?
????// return the corresponding element?
????if (maxCount > n/2)?
????cout << arr[index] << endl;?
????else
????????cout << "No Majority Element" << endl;?
}?
// Driver code?
int main()?
{?
????int arr[] = {1, 1, 2, 1, 3, 5, 1};?
????int n = sizeof(arr) / sizeof(arr[0]);?
????// Function calling?
????findMajority(arr, n);?
????return 0;?
}?
```
## Java
```
// Java? program to find Majority?
// element in an array?
import java.io.*;
class GFG {
// Function to find Majority element?
// in an array?
static void findMajority(int arr[], int n)?
{?
????int maxCount = 0;?
????int index = -1; // sentinels?
????for(int i = 0; i < n; i++)?
????{?
????????int count = 0;?
????????for(int j = 0; j < n; j++)?
????????{?
????????????if(arr[i] == arr[j])?
????????????count++;?
????????}?
????????// update maxCount if count of?
????????// current element is greater?
????????if(count > maxCount)?
????????{?
????????????maxCount = count;?
????????????index = i;?
????????}?
????}?
????// if maxCount is greater than n/2?
????// return the corresponding element?
????if (maxCount > n/2)?
????System.out.println (arr[index]);?
????else
????System.out.println ("No Majority Element");?
}?
// Driver code?
????public static void main (String[] args) {
????????int arr[] = {1, 1, 2, 1, 3, 5, 1};?
????????int n = arr.length;?
????// Function calling?
????findMajority(arr, n);?
????}
//This code is contributed by ajit.????
}
```
## Python 3
```
# Python 3 program to find Majority?
# element in an array
# Function to find Majority?
# element in an array
def findMajority(arr, n):
????maxCount = 0;
????index = -1 # sentinels
????for i in range(n):
????????count = 0
????????for j in range(n):
????????????if(arr[i] == arr[j]):
????????????????count += 1
????????# update maxCount if count of?
????????# current element is greater
????????if(count > maxCount):
????????????maxCount = count
????????????index = i
????# if maxCount is greater than n/2?
????# return the corresponding element?
????if (maxCount > n//2):
????????print(arr[index])
????else:
????????print("No Majority Element")
# Driver code
if __name__ == "__main__":
????arr = [1, 1, 2, 1, 3, 5, 1]
????n = len(arr)
????# Function calling?
????findMajority(arr, n)
# This code is contributed?
# by ChitraNayal
```
## C#
```
// C#? program to find Majority?
// element in an array?
using System;
public class GFG{
// Function to find Majority element?
// in an array?
static void findMajority(int []arr, int n)?
{?
????int maxCount = 0;?
????int index = -1; // sentinels?
????for(int i = 0; i < n; i++)?
????{?
????????int count = 0;?
????????for(int j = 0; j < n; j++)?
????????{?
????????????if(arr[i] == arr[j])?
????????????count++;?
????????}?
????????// update maxCount if count of?
????????// current element is greater?
????????if(count > maxCount)?
????????{?
????????????maxCount = count;?
????????????index = i;?
????????}?
????}?
????// if maxCount is greater than n/2?
????// return the corresponding element?
????if (maxCount > n/2)?
????Console.WriteLine (arr[index]);?
????else
????Console.WriteLine("No Majority Element");?
}?
// Driver code?
????static public void Main (){
????????int []arr = {1, 1, 2, 1, 3, 5, 1};?
????????int n = arr.Length;?
????????// Function calling?
????????findMajority(arr, n);?
????}
//This code is contributed by Tushil..?
}
```
## PHP
```
<?php
// PHP program to find Majority?
// element in an array
// Function to find Majority element
// in an array
function findMajority($arr, $n)
{
????$maxCount = 0;?
????$index = -1; // sentinels
????for($i = 0; $i < $n; $i++)
????{
????????$count = 0;
????????for($j = 0; $j < $n; $j++)
????????{
????????????if($arr[$i] == $arr[$j])
????????????$count++;
????????}
????????// update maxCount if count of?
????????// current element is greater
????????if($count > $maxCount)
????????{
????????????$maxCount = $count;
????????????$index = $i;
????????}
????}
????// if maxCount is greater than n/2?
????// return the corresponding element?
????if ($maxCount > $n/2)
????????echo $arr[$index] . "\n";
????else
????????echo "No Majority Element" . "\n";
}
// Driver code
$arr = array(1, 1, 2, 1, 3, 5, 1);
$n = sizeof($arr);
// Function calling?
findMajority($arr, $n);
// This code is contributed?
// by Akanksha Rai
```
* **輸出**:
```
1
```
* **復雜度分析**:
* **時間復雜度**: O(n * n)。
需要一個嵌套循環,其中兩個循環都從頭到尾遍歷數組,因此時間復雜度為 O(n ^ 2)。
* **輔助空間**:`O(1)`。
由于任何操作都不需要額外的空間,因此空間復雜度是恒定的。
**方法 2(使用[二分搜索樹](https://www.geeksforgeeks.org/binary-search-tree-set-1-search-and-insertion/))**
* **方法**:在 BST 中一個接一個地插入元素,如果已經存在一個元素,則增加節點的計數。 在任何階段,如果節點數超過 n / 2,則返回。
* **算法**:
1. 創建一個二分搜索樹,如果在二分搜索樹中輸入相同的元素,則節點的頻率會增加。
2. 遍歷數組并將元素插入二叉搜索樹。
3. 如果任何節點的最大頻率大于數組大小的一半,則執行有序遍歷并找到頻率大于一半的節點
4. 其他打印無多數元素。
* **Implementation:**
```
// C++ program to demonstrate insert operation in binary search tree.?
#include<bits/stdc++.h>??
using namespace std;
struct node?
{?
????int key;
????int c = 0;
????struct node *left, *right;?
};?
// A utility function to create a new BST node?
struct node *newNode(int item)?
{?
????struct node *temp = (struct node *)malloc(sizeof(struct node));?
????temp->key = item;
????temp->c = 1;
????temp->left = temp->right = NULL;?
????return temp;?
}?
/* A utility function to insert a new node with given key in BST */
struct node* insert(struct node* node, int key,int &ma)?
{?
????/* If the tree is empty, return a new node */
????if (node == NULL)?
????{
????????if(ma==0)
????????????ma=1;
????????return newNode(key);?
????}
????/* Otherwise, recur down the tree */
????if (key < node->key)?
????????node->left = insert(node->left, key, ma);?
????else if (key > node->key)?
????????node->right = insert(node->right, key, ma);?
????else
????????node->c++;
????//find the max count
????ma = max(ma, node->c);
????/* return the (unchanged) node pointer */
????return node;?
}?
// A utility function to do inorder traversal of BST?
void inorder(struct node *root,int s)??
{??
????if (root != NULL)??
????{??
????????inorder(root->left,s);?
????????if(root->c>(s/2))?
????????????printf("%d \n", root->key);??
????????inorder(root->right,s);??
????}??
}
// Driver Program to test above functions?
int main()?
{?
????int a[] = {1, 3, 3, 3, 2};
????int size = (sizeof(a))/sizeof(a[0]);
????struct node *root = NULL;?
????int ma=0;
????for(int i=0;i<size;i++)
????{
????????root = insert(root, a[i],ma);?
????}
????if(ma>(size/2))
????????inorder(root,size);
????else?
????????cout<<"No majority element\n";
????return 0;?
}?
```
**輸出**:
```
3
```
* **復雜度分析**:
* **時間復雜度**:如果使用[二分搜索樹](https://www.geeksforgeeks.org/binary-search-tree-set-1-search-and-insertion/),則時間復雜度將為 O(n ^ 2)。 如果使用[自平衡二分搜索](http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree)樹,則其為 O(nlogn)
* **輔助空間**:`O(n)`。
由于需要額外的空間才能將數組存儲在樹中。
**方法 3(使用摩爾投票算法)**:
* **Approach:** This is a two-step process.
* 第一步給出的元素可能是數組中的多數元素。 如果數組中存在多數元素,則此步驟一定會返回多數元素,否則,它將返回多數元素的候選項。
* 檢查從上述步驟獲得的元素是否為多數元素。 這一步是必要的,因為可能沒有多數元素。
**第 1 步:找到候選人**
在`O(n)`中起作用的第一階段算法稱為摩爾投票算法。 該算法的基本思想是,如果可以用與 *e* 不同的所有其他元素取消元素 *e* 的每次出現,則 *e* 將會存在直到 如果是多數元素,則結束。
**步驟 2:檢查步驟 1 中獲得的元素是否為多數元素。**
遍歷數組并檢查找到的元素的計數是否大于數組大小的一半,然后打印答案,否則打印“無多數元素”。
* **算法**:
1. 遍歷每個元素并維護多數元素的計數以及多數索引 *maj_index*
2. 如果下一個元素相同,則增加計數;如果下一個元素不同,則減少計數。
3. 如果計數達到 0,則將 maj_index 更改為當前元素,然后將計數再次設置為 1。
4. 現在再次遍歷數組,找到找到的多數元素計數。
5. 如果計數大于數組大小的一半,則打印元素
6. 沒有多數元素的其他印刷品* **實現**:
## C++
```
/* C++ Program for finding out?
???majority element in an array */
#include <bits/stdc++.h>
using namespace std;
/* Function to find the candidate for Majority */
int findCandidate(int a[], int size)
{
????int maj_index = 0, count = 1;
????for (int i = 1; i < size; i++)
????{
????????if (a[maj_index] == a[i])
????????????count++;
????????else
????????????count--;
????????if (count == 0)
????????{
????????????maj_index = i;
????????????count = 1;
????????}
????}
????return a[maj_index];
}
/* Function to check if the candidate
???occurs more than n/2 times */
bool isMajority(int a[], int size, int cand)
{
????int count = 0;
????for (int i = 0; i < size; i++)
????if (a[i] == cand)
????count++;
????if (count > size/2)
????return 1;
????else
????return 0;
}
/* Function to print Majority Element */
void printMajority(int a[], int size)
{
???/* Find the candidate for Majority*/
???int cand = findCandidate(a, size);
???/* Print the candidate if it is Majority*/
???if (isMajority(a, size, cand))
???cout << " " << cand << " ";
???else
???cout << "No Majority Element";
}
/* Driver function to test above functions */
int main()
{
????int a[] = {1, 3, 3, 1, 2};
????int size = (sizeof(a))/sizeof(a[0]);
????// Function calling
????printMajority(a, size);
????return 0;
}
```
## C
```
/* Program for finding out majority element in an array */
# include<stdio.h>
# define bool int
int findCandidate(int *, int);
bool isMajority(int *, int, int);
/* Function to print Majority Element */
void printMajority(int a[], int size)
{
??/* Find the candidate for Majority*/
??int cand = findCandidate(a, size);
??/* Print the candidate if it is Majority*/
??if (isMajority(a, size, cand))
????printf(" %d ", cand);
??else
????printf("No Majority Element");
}
/* Function to find the candidate for Majority */
int findCandidate(int a[], int size)
{
????int maj_index = 0, count = 1;
????int i;
????for (i = 1; i < size; i++)
????{
????????if (a[maj_index] == a[i])
????????????count++;
????????else
????????????count--;
????????if (count == 0)
????????{
????????????maj_index = i;
????????????count = 1;
????????}
????}
????return a[maj_index];
}
/* Function to check if the candidate occurs more than n/2 times */
bool isMajority(int a[], int size, int cand)
{
????int i, count = 0;
????for (i = 0; i < size; i++)
??????if (a[i] == cand)
?????????count++;
????if (count > size/2)
???????return 1;
????else
???????return 0;
}
/* Driver function to test above functions */
int main()
{
????int a[] = {1, 3, 3, 1, 2};
????int size = (sizeof(a))/sizeof(a[0]);
????printMajority(a, size);
????getchar();
????return 0;
}
```
## Java
```
/* Program for finding out majority element in an array */
class MajorityElement?
{
????/* Function to print Majority Element */
????void printMajority(int a[], int size)?
????{
????????/* Find the candidate for Majority*/
????????int cand = findCandidate(a, size);
????????/* Print the candidate if it is Majority*/
????????if (isMajority(a, size, cand))
????????????System.out.println(" " + cand + " ");
????????else?
????????????System.out.println("No Majority Element");
????}
????/* Function to find the candidate for Majority */
????int findCandidate(int a[], int size)?
????{
????????int maj_index = 0, count = 1;
????????int i;
????????for (i = 1; i < size; i++)?
????????{
????????????if (a[maj_index] == a[i])
????????????????count++;
????????????else
????????????????count--;
????????????if (count == 0)
????????????{
????????????????maj_index = i;
????????????????count = 1;
????????????}
????????}
????????return a[maj_index];
????}
????/* Function to check if the candidate occurs more
???????than n/2 times */
????boolean isMajority(int a[], int size, int cand)?
????{
????????int i, count = 0;
????????for (i = 0; i < size; i++)?
????????{
????????????if (a[i] == cand)
????????????????count++;
????????}
????????if (count > size / 2)?
????????????return true;
????????else
????????????return false;
????}
????/* Driver program to test the above functions */
????public static void main(String[] args)?
????{
????????MajorityElement majorelement = new MajorityElement();
????????int a[] = new int[]{1, 3, 3, 1, 2};
????????int size = a.length;
????????majorelement.printMajority(a, size);
????}
}
// This code has been contributed by Mayank Jaiswal
```
## Python
```
# Program for finding out majority element in an array
# Function to find the candidate for Majority
def findCandidate(A):
????maj_index = 0
????count = 1
????for i in range(len(A)):
????????if A[maj_index] == A[i]:
????????????count += 1
????????else:
????????????count -= 1
????????if count == 0:
????????????maj_index = i
????????????count = 1
????return A[maj_index]
# Function to check if the candidate occurs more than n/2 times
def isMajority(A, cand):
????count = 0
????for i in range(len(A)):
????????if A[i] == cand:
????????????count += 1
????if count > len(A)/2:
????????return True
????else:
????????return False
# Function to print Majority Element
def printMajority(A):
????# Find the candidate for Majority
????cand = findCandidate(A)
????# Print the candidate if it is Majority
????if isMajority(A, cand) == True:
????????print(cand)
????else:
????????print("No Majority Element")
# Driver program to test above functions
A = [1, 3, 3, 1, 2]
printMajority(A)?
```
## C#
```
// C# Program for finding out majority element in an array
using System;
class GFG
{
????/* Function to print Majority Element */
????static void printMajority(int []a, int size)?
????{
????????/* Find the candidate for Majority*/
????????int cand = findCandidate(a, size);
????????/* Print the candidate if it is Majority*/
????????if (isMajority(a, size, cand))
????????????Console.Write(" " + cand + " ");
????????else
????????????Console.Write("No Majority Element");
????}
????/* Function to find the candidate for Majority */
????static int findCandidate(int []a, int size)?
????{
????????int maj_index = 0, count = 1;
????????int i;
????????for (i = 1; i < size; i++)?
????????{
????????????if (a[maj_index] == a[i])
????????????????count++;
????????????else
????????????????count--;
????????????if (count == 0)
????????????{
????????????????maj_index = i;
????????????????count = 1;
????????????}
????????}
????????return a[maj_index];
????}
????// Function to check if the candidate?
????// occurs more than n/2 times
????static bool isMajority(int []a, int size, int cand)?
????{
????????int i, count = 0;
????????for (i = 0; i < size; i++)?
????????{
????????????if (a[i] == cand)
????????????????count++;
????????}
????????if (count > size / 2)?
????????????return true;
????????else
????????????return false;
????}
????// Driver Code
????public static void Main()?
????{
????????int []a = {1, 3, 3, 1, 2};
????????int size = a.Length;
????????printMajority(a, size);
????}
}
// This code is contributed by Sam007
```
## PHP
```
<?php
// PHP Program for finding out majority?
// element in an array?
// Function to find the candidate?
// for Majority?
function findCandidate($a, $size)
{
????$maj_index = 0;
????$count = 1;
????for ($i = 1; $i < $size; $i++)
????{
????????if ($a[$maj_index] == $a[$i])
????????????$count++;
????????else
????????????$count--;
????????if ($count == 0)
????????{
????????????$maj_index = $i;
????????????$count = 1;
????????}
????}
????return $a[$maj_index];
}
// Function to check if the candidate
// occurs more than n/2 times?
function isMajority($a, $size, $cand)
{
????$count = 0;
????for ($i = 0; $i < $size; $i++)
????if ($a[$i] == $cand)
????$count++;
????if ($count > $size / 2)
????return 1;
????else
????return 0;
}
// Function to print Majority Element?
function printMajority($a, $size)
{
????/* Find the candidate for Majority*/
????$cand = findCandidate($a, $size);
????/* Print the candidate if it is Majority*/
????if (isMajority($a, $size, $cand))
????????echo " ", $cand, " ";
????else
????????echo "No Majority Element";
}
// Driver Code
$a = array(1, 3, 3, 1, 2);
$size = sizeof($a);
// Function calling
printMajority($a, $size);
// This code is contributed by jit_t
?>
```
**輸出**:
```
No Majority Element
```
* **復雜度分析**:
* **時間復雜度**:`O(n)`。
由于需要兩次遍歷數組,因此時間復雜度是線性的。
* **輔助空間**:`O(1)`。
由于不需要額外的空間。
**方法 4(使用哈希圖)**:
* **方法**:就時間復雜度而言,此方法在某種程度上類似于摩爾投票算法,但是在這種情況下,不需要第二步的摩爾投票算法。 但是像往常一樣,這里的空間復雜度變為`O(n)`。
在 Hashmap(鍵-值對)中,在值上,維護每個元素(鍵)的計數,每當該計數大于數組長度的一半時,返回該鍵(多數元素)。
* **算法**:
1. 創建一個哈希圖來存儲鍵值對,即元素頻率對。
2. 從頭到尾遍歷數組。
3. 對于數組中的每個元素,如果該元素不作為鍵存在,則將該元素插入哈希圖中,否則獲取鍵的值(array [i])并將其值增加 1
4. 如果計數大于一半,則打印多數元素并中斷。
5. 如果找不到多數元素,則打印“無多數元素”
* **Implementation:**
## C++
```
/* C++ program for finding out majority?
element in an array */
#include <bits/stdc++.h>
using namespace std;
void findMajority(int arr[], int size)
{
????unordered_map<int, int> m;
????for(int i = 0; i < size; i++)
????????m[arr[i]]++;
????int count = 0;
????for(auto i : m)
????{
????????if(i.second > size / 2)
????????{
????????????count =1;
????????????cout << "Majority found :- " << i.first<<endl;
????????????break;
????????}
????}
????if(count == 0)
????????cout << "No Majority element" << endl;
}
// Driver code?
int main()?
{?
????int arr[] = {2, 2, 2, 2, 5, 5, 2, 3, 3};?
????int n = sizeof(arr) / sizeof(arr[0]);?
????// Function calling?
????findMajority(arr, n);?
????return 0;?
}?
// This code is contributed by codeMan_d
```
## Java
```
import java.util.HashMap;
/* Program for finding out majority element in an array */
class MajorityElement?
{
????private static void findMajority(int[] arr)?
????{
????????HashMap<Integer,Integer> map = new HashMap<Integer, Integer>();
????????for(int i = 0; i < arr.length; i++) {
????????????if (map.containsKey(arr[i])) {
????????????????????int count = map.get(arr[i]) +1;
????????????????????if (count > arr.length /2) {
????????????????????????System.out.println("Majority found :- " + arr[i]);
????????????????????????return;
????????????????????} else
????????????????????????map.put(arr[i], count);
????????????}
????????????else
????????????????map.put(arr[i],1);
????????????}
????????????System.out.println(" No Majority element");
????}
????/* Driver program to test the above functions */
????public static void main(String[] args)?
????{
????????int a[] = new int[]{2,2,2,2,5,5,2,3,3};
????????findMajority(a);
????}
}
// This code is contributed by? karan malhotra
```
## Python3
```
# Python program for finding out majority?
# element in an array?
def findMajority(arr, size):
????m = {}
????for i in range(size):
????????if arr[i] in m:
????????????m[arr[i]] += 1
????????else:
????????????m[arr[i]] = 1
????count = 0
????for key in m:
????????if m[key] > size / 2:
????????????count = 1
????????????print("Majority found :-",key)
????????????break
????if(count == 0):
????????print("No Majority element")
# Driver code?
arr = [2, 2, 2, 2, 5, 5, 2, 3, 3]?
n = len(arr)
# Function calling?
findMajority(arr, n)
# This code is contributed by ankush_953
```
## C#
```
// C# Program for finding out majority
// element in an array?
using System;
using System.Collections.Generic;
class GFG
{
private static void findMajority(int[] arr)
{
????Dictionary<int,?
???????????????int> map = new Dictionary<int,?
?????????????????????????????????????????int>();
????for (int i = 0; i < arr.Length; i++)
????{
????????if (map.ContainsKey(arr[i]))
????????{
????????????????int count = map[arr[i]] + 1;
????????????????if (count > arr.Length / 2)
????????????????{
????????????????????Console.WriteLine("Majority found :- " +?
????????????????????????????????????????????????????arr[i]);
????????????????????return;
????????????????}
????????????????else
????????????????{
????????????????????map[arr[i]] = count;
????????????????}
????????}
????????else
????????{
????????????map[arr[i]] = 1;
????????}
????}
????Console.WriteLine(" No Majority element");
}
// Driver Code
public static void Main(string[] args)
{
????int[] a = new int[]{2, 2, 2, 2,?
????????????????????????5, 5, 2, 3, 3};
????findMajority(a);
}
}
// This code is contributed by Shrikant13
```
**輸出**:
```
Majority found :- 2
```
**感謝 Ashwani Tanwar,Karan Malhotra 提出的建議。**
* **復雜度分析**:
* **時間復雜度**:`O(n)`。
需要對數組進行一次遍歷,因此時間復雜度是線性的。
* **輔助空間**:`O(n)`。
由于哈希圖需要線性空間。
**方法 5**
* **方法**:想法是對數組進行排序。 排序使數組中的相似元素相鄰,因此遍歷數組并更新計數,直到當前元素與上一個元素相似為止。 如果頻率超過數組大小的一半,則打印多數元素。
* **算法**:
1. 對數組進行排序,然后創建一個變量計數和上一個 *prev = INT_MIN* 。
2. 從頭到尾遍歷元素。
3. 如果當前元素等于前一個元素,則增加計數。
4. 否則將計數設置為 1。
5. 如果計數大于數組大小的一半,則將元素打印為多數元素并中斷。
6. 如果找不到多數元素,則打印“無多數元素”
* **Implementation:**
```
// C++ program to find Majority?
// element in an array
#include <bits/stdc++.h>
using namespace std;
// Function to find Majority element
// in an array
// it returns -1 if there is no majority element
int majorityElement(int *arr, int n)
{
????// sort the array in O(nlogn)
????sort(arr, arr+n);
????int count = 1, max_ele = -1, temp = arr[0], ele, f=0;
????for(int i=1;i<n;i++)
????{
????????// increases the count if the same element occurs
????????// otherwise starts counting new element
????????if(temp==arr[i])
????????{
????????????count++;
????????}
????????else
????????{
????????????count = 1;
????????????temp = arr[i];
????????}
????????// sets maximum count
????????// and stores maximum occured element so far
????????// if maximum count becomes greater than n/2
????????// it breaks out setting the flag
????????if(max_ele<count)
????????{
????????????max_ele = count;
????????????ele = arr[i];
????????????if(max_ele>(n/2))
????????????{
????????????????f = 1;
????????????????break;
????????????}
????????}
????}
????// returns maximum occured element
????// if there is no such element, returns -1
????return (f==1 ? ele : -1);
}
// Driver code
int main()
{
????int arr[] = {1, 1, 2, 1, 3, 5, 1};
????int n = sizeof(arr) / sizeof(arr[0]);
????// Function calling?
????cout<<majorityElement(arr, n);
????return 0;
}
```
**輸出**:
```
1
```
* **復雜度分析**:
* **時間復雜度**: O(nlogn)。
排序需要`O(N log N)`時間復雜度。
* **輔助空間**:`O(1)`。
由于不需要額外的空間。
- GeeksForGeeks 數組教程
- 介紹
- 數組介紹
- C/C++ 中的數組
- Java 中的數組
- Python 中的數組| 系列 1(簡介和功能)
- C# | 數組
- 回轉
- 數組旋轉程序
- 數組旋轉的逆向算法
- 數組旋轉的塊交換算法
- 程序循環旋轉一個數組
- 在經過排序和旋轉的數組中搜索元素
- 給定一個經過排序和旋轉的數組,查找是否存在一對具有給定總和的數組
- 在只允許旋轉給定數組的情況下找到Sum(i * arr[i])的最大值
- 給定數組所有旋轉中i * arr [i]的最大和
- 在旋轉排序數組中找到旋轉計數
- 快速找到數組的多個左旋轉| 系列 1
- 在經過排序和旋轉的數組中找到最小元素
- 數組右旋轉的逆向算法
- 查找具有最大漢明距離的旋轉
- 數組左右循環查詢
- 在O(n)時間和O(1)空間中打印數組的左旋轉
- 旋轉幾次后,在給定索引處查找元素
- 拆分數組并將第一部分添加到末尾
- 重排
- 重新排列數組,使arr[i] = i
- 編寫程序以反轉數組或字符串
- 重新排列數組,如果i為偶數則arr[i] >= arr[j],如果i為奇數且j < i則 arr[i] <= arr[j]
- 在O(n)時間和O(1)額外空間中重新排列正數和負數
- 重新排列數組,交替出現&個正數的負數項,多余的空間為O(1) | 系列 1
- 將所有零移動到數組末尾
- 將所有零移動到數組的末尾| 系列 2(使用單遍歷)
- 將所有小于或等于 k 的元素組合在一起所需的最小交換
- 使用內置排序功能重新排列正數和負數
- 重新排列數組,使偶數位置大于奇數
- 按順序重新排列數組-最小,最大,第二個最小,第二個最大..
- 將第一個元素加倍,然后將零移動到結尾
- 根據給定的索引對數組重新排序
- 用恒定的額外空間重新排列正數和負數
- 排列給定數字以形成最大數| 系列 1
- 重新排列數組,如果arr[i]為j,則arr[j]變為i | 系列 1
- 以最大最小形式重新排列數組| 系列 1
- 以最大最小形式重新排列數組| 系列 2(O(1)額外空間)
- 將所有負元素移動到最后,并留出足夠的空間
- 重新排列數組,使偶數索引元素較小而奇數索引元素較大
- 正數元素位于偶數位置,負數元素位于奇數位置(不保持相對順序)
- 用上一個和下一個的乘法替換每個數組元素
- 使用 Fisher-Yates 隨機播放算法隨機播放給定數組
- 分離偶數和奇數| 系列 3
- 將數組中的 0 和 1 分開
- 最長的雙子序列| DP-15
- 在線性時間內找到大小為 3 的排序子序列
- 最大數目等于 0 和 1 的子數組
- 最大產品子數組
- 用右側的最大元素替換每個元素
- 最大循環子數組總和
- 最長遞增子序列的構造(N log N)
- 按頻率對元素排序| 系列 2
- 最大化圓形數組中的連續差之和
- 根據另一個數組定義的順序對數組進行排序
- 查找索引 0 替換為 1,以獲得二進制數組中最長的連續序列 1s
- 在給定范圍內對數組進行三向分區
- 從兩個給定排序數組的備用元素生成所有可能的排序數組
- 安排彼此相鄰的線對所需的最小交換次數
- 將數組轉換為 Zig-Zag 風格
- 從給定序列中形成最小數
- 將兩個連續的相等值替換為一個更大的值
- 重新排列二進制字符串作為 x 和 y 的交替出現
- 數組中不同的相鄰元素
- 不使用多余空間將 2n 個整數隨機排列為 a1-b1-a2-b2-a3-b3-.bn
- 合并 k 個排序的數組| 系列 1
- 訂單統計
- 未排序數組中第 K 個最小/最大元素| 系列 1
- 未排序數組中第 K 個最小/最大元素| 系列 2(預期線性時間)
- 未排序數組中第 K 個最小/最大元素| 組合 3(最壞情況的線性時間)
- 使用 STL 的第 K 個最小/最大元素
- 數組中的 k 個最大(或最小)元素| 添加了最小堆方法
- 按行和按列排序的 2D 數組中的 Kth 個最小元素| 系列 1
- 程序以查找數組中的最大元素
- 查找數組中最大的三個元素
- 查找數組中至少有兩個大元素的所有元素
- 未排序數組的均值和中位數的程序
- 使用 STL 的運行整數流的中位數
- 正整數數組中 k 個整數的最小積
- 第 K 個最大和的連續子數組
- 來自兩個數組的 K 個最大和組合
- 重疊的連續子數組的 K 個最大和
- 非重疊的連續子數組的 K 個最大和
- 使用O(1)額外空間按相同順序排列 k 個最小元素
- 在兩個數組中找到具有最小和的 k 對
- 數組中兩個元素的第 k 個最小絕對差
- 在數組中查找第二大元素
- 查找給定數組中出現次數最多的 k 個數字
- 查找數組中的最小和第二個最小元素
- 尋找最小的遺失號碼
- 使得兩個元素都不相鄰的最大和
- 使用最少數量的比較的數組的最大值和最小值
- 兩個元素之間的最大差異,使得較大的元素出現在較小的數字之后
- 給定數組 arr [],找到最大 j – i,使得 arr [j] > arr [i]
- 最大滑動窗口(大小為 k 的所有子數組的最大值)
- 找到兩個數字之間的最小距離
- 在先增加然后減少的數組中找到最大元素
- 計算右側較小的元素
- 最長遞增子序列大小(N log N)
- 查找未排序數組中缺失的最小正數| 系列 1
- 在O(n)時間和O(1)多余空間中找到最大重復數
- 給定大小為 n 且數字為 k 的數組,找到出現次數超過 n / k 次的所有元素
- 找出長度為 3 且具有最大乘積的遞增子序列
- 兩個數組中的最大求和路徑
- 從兩個排序的數組中找到最接近的對
- 在未排序的數組中找到最大的對和
- 整個數組中最小的較大元素
- 刪除小于 next 或變得更小的數組元素
- 在線檢查回文的在線算法
- 刪除小于 next 或變得更小的數組元素
- 找到要翻轉的零,以使連續的 1 的數目最大化
- 計算嚴格增加的子數組
- 流中的第 K 個最大元素
- 在兩個數組中找到具有最小和的 k 對
- k 元素組與數組其余部分之間的最大差值。
- 要使中位數等于 x 的最小元素數量
- 下一個更大的元素
- 范圍查詢
- MO 的算法(查詢平方根分解)| 系列 1(簡介)
- Sqrt(或平方根)分解技術 系列 1(簡介)
- 稀疏表
- 使用稀疏表進行范圍總和查詢
- 范圍最小查詢(平方根分解和稀疏表)
- 數組元素的頻率范圍查詢
- 數組上的恒定時間范圍添加操作
- 范圍 LCM 查詢
- 數組中給定索引范圍的 GCD
- 查詢給定數組中所有數字的 GCD(給定范圍內的元素除外)
- 給定子數組中小于或等于給定數目的元素數
- 給定子數組中小于或等于給定數字的元素數| 第 2 組(包括更新)
- 查詢值在給定范圍內的數組元素的計數
- 查詢二進制數組的子數組的十進制值
- 計算將 L-R 范圍內的所有數字相除的元素
- 給定數組范圍的 XOR 之和最大的數字
- 在給定范圍內出現偶數次的數字的 XOR
- 范圍查詢中的數組范圍查詢
- 數組范圍查詢以搜索元素
- 數組范圍查詢頻率與值相同的元素
- 給定范圍內的最大出現次數
- 給定范圍內具有相等元素的索引數
- 合并排序樹以獲取范圍順序統計信息
- 范圍內沒有重復數字的總數
- 差異數組|O(1)中的范圍更新查詢
- 對數組的范圍查詢,其每個元素都是索引值與前一個元素的 XOR
- 查找子數組是否為山脈形式
- 范圍總和查詢,無更新
- 子數組中的素數(帶有更新)
- 在二進制數組中檢查子數組表示的數字是奇數還是偶數
- 用于乘法,替換和乘積的數組查詢
- 數組范圍的平均值
- 執行加減命令后打印修改后的數組
- 在給定范圍內對偶數或奇數概率的查詢
- 數組中范圍的乘積
- 計算范圍內的素數
- M 個范圍切換操作后的二進制數組
- 合并重疊間隔
- 檢查給定間隔中是否有兩個間隔重疊
- 間隔之和與除數的更新
- 多次數組范圍遞增操作后打印修改后的數組
- 范圍最大奇數的 XOR 查詢
- 查詢子數組中不同元素的數量
- 計數和切換二進制數組上的查詢
- 數組中的最小-最大范圍查詢
- 優化問題
- 最大總和連續子數組
- 通過最多買賣兩次股份獲得最大利潤
- 查找平均數最少的子數組
- 找到兩個數字之間的最小距離
- 最小化高度之間的最大差異
- 到達終點的最小跳數
- 最大總和增加子序列| DP-14
- 總和大于給定值的最小子數組
- 查找 k 個長度的最大平均子數組
- 計算最小步數以獲得給定的所需數組
- 乘積小于 k 的子集數
- 查找使數組回文的最小合并操作數
- 查找不能表示為給定數組的任何子集之和的最小正整數值
- 具有最大總和的子數組的大小
- 找出任何兩個元素之間的最小差異
- 使用位操作進行空間優化
- 兩個二進制數組中具有相同總和的最長跨度
- 排序
- 替代排序
- 對幾乎排序(或 K 排序)的數組進行排序
- 根據給定值的絕對差對數組進行排序
- 以波形形式對數組進行排序
- 將大小為 n 的數組合并為大小為 m + n 的另一個數組
- 對包含 1 到 n 個值的數組進行排序
- 通過交換相鄰元素將 1 排序為 N
- 對包含兩種類型元素的數組進行排序
- 按頻率對元素排序| 系列 1
- 計算數組中的反轉 系列 1(使用合并排序)
- 兩個元素的和最接近零
- 最短無序子數組
- 排序數組所需的最小交換次數
- 兩個排序數組的并集和交集
- 查找兩個未排序數組的并集和交集
- 對 0、1 和 2 的數組進行排序
- 找到最小長度未排序子數組,進行排序,使整個數組排序
- 中位數為整數流(運行整數)
- 計算可能的三角形數量
- 查找數組中的對數(x,y),使得 x ^ y > y ^ x
- 計算所有等于 k 的不同對
- 打印給定整數數組的所有不同元素
- 從其對和數組構造一個數組
- 合并兩個有O(1)額外空間的排序數組
- 第一個數組中的最大值與第二個數組中的最小值的乘積
- 對數(a [j] > = a [i])的對數,其中 k 個范圍在(a [i],a [j])中,可被 x 整除
- 隨機對為最大加權對的概率
- AP 數組中存在的最小解排列(算術級數)
- 對兩個數組的最小乘積之和進行重新排列
- 將數組劃分為 k 個片段,以最大化片段最小值的最大值
- 最小乘積對為正整數數組
- 計算形成最小產品三胞胎的方法
- 檢查是否反轉子數組使數組排序
- 使用另一個數組最大化元素
- 使兩個數組的元素相同,最小增減
- 檢查是否有任何間隔完全重疊
- 除子數組中的元素外,對數組進行排序
- 對除一個以外的所有數組元素進行排序
- 排序二進制數組所需的最小相鄰交換
- 按數組中出現的元素順序對鏈接列表進行排序
- 打印數組中排序的不同元素
- 可以單獨排序以進行排序的最大分區數
- 使用 STL 根據因素數量進行排序
- 每次取下最小的鋼絲繩后剩下的鋼絲繩
- 數組中所有元素的排名
- 合并 3 個排序的數組
- 使數組遞減的最小減法運算數
- 最大化 arr [i] * i 的總和
- 差異小于 K 的對
- 按排序順序合并兩個未排序的數組
- 從兩個數組最大化唯一對
- 應用給定方程后對數組排序
- 每個數組元素的最小絕對差之和
- 查找是否可以使用一個外部數字使數組元素相同
- 兩個未排序數組之間的最小差值對
- 程序檢查數組是否排序(迭代和遞歸)
- 查找大于數組中一半元素的元素
- 使兩個數組相同的最小交換
- 要添加的元素,以便數組中存在某個范圍的所有元素
- 正在搜尋
- 搜索,插入和刪除未排序的數組
- 在排序的數組中搜索,插入和刪除
- 給定數組 A []和數字 x,請檢查 A []中的對,總和為 x
- 在相鄰項最多相差 k 的數組中搜索
- 在三個排序的數組中查找共同的元素
- 在無數排序數組中查找元素的位置
- 查找 1 到 n-1 之間的唯一重復元素
- 查找在數組中一次出現的元素,其中每個其他元素出現兩次
- 排除某些元素的最大子數組總和
- 數組中的最大平衡和
- 數組的平衡指數
- 領導者數組
- 天花板排列
- 多數元素
- 檢查排序數組中的多數元素
- 檢查數組是否具有多數元素
- 兩指針技術
- 查找峰元素
- 找到給定數組中的兩個重復元素
- 在給定的數組中找到一個固定點(等于索引的值)
- 查找給定總和的子數組| 系列 1(負數)
- 數組中的最大三元組和
- 來自三個數組的最小差異三元組
- 查找一個三元組,將其總和成給定值
- 找到所有零和的三元組
- 所有合計給定值的唯一三元組
- 計算總數小于給定值的三元組
- 打印形成 AP 的排序數組中的所有三元組
- XOR 為零的唯一三元組數
- 找到一個三元組,使得兩個和等于第三元素
- 查找出現次數的奇數
- 查找丟失的號碼
- 計算排序數組中的出現次數(或頻率)
- 給定一個已排序的數組和一個數字 x,在數組中找到總和最接近 x 的對
- 在排序的二進制數組中計數 1
- 在整數數組中找到第一個重復元素
- 從重復的數組中查找丟失的元素
- 找到重復的和丟失的| 添加了 3 種新方法
- 在未排序的數組中找到出現奇數的兩個數字
- 找到具有給定差異的一對
- 找到四個總和為給定值的元素| 集合 1(n ^ 3 解)
- 找到四個總和為給定值的元素| 系列 2
- 查找是否有一個總和為 0 的子數組
- 在相鄰元素之間的差為 1 的數組中搜索元素
- 一系列不同元素中的第三大元素
- 檢查數組中是否存在兩個元素的總和等于數組其余部分的總和
- 檢查給定數組是否包含彼此之間 k 距離內的重復元素
- 使用最少的比較次數搜索未排序數組中的元素
- 連續元素排序數組中僅重復元素的計數
- 在頻率大于或等于 n / 2 的排序數組中查找元素。
- 圓形數組中相鄰元素的最小絕對差
- 在數組中找到第一個,第二個和第三個最小元素
- 程序來查找數組的最小(或最大)元素
- 每個數組元素中另一個數組中最接近的較大元素
- 計算O(1)額外空間和O(n)時間中數組中所有元素的頻率
- 與給定的總和和距末端的最大最短距離配對
- 從數組中刪除一個元素(使用兩次遍歷和一次遍歷)
- 計算給定數組中大小為 3 的反轉
- 計算給定總和的對
- 對排序向量中的二分搜索
- 困雨水
- 替換元素會使數組元素連續
- 排序數組中的第 k 個缺失元素
- O(log(min(n(n,m)))中具有不同大小的兩個排序數組的中位數
- 從兩個排序的數組中打印不常見的元素
- 非重復元素
- 數組中最頻繁的元素
- 數組中最少的元素
- m 個元素的兩個子集之間的最大差
- n 個數組中升序元素的最大和
- 配對使得一個是其他的冪倍
- 查找數組中對的數量,以使它們的 XOR 為 0
- 兩次最大出現之間的最小距離
- 如果我們在數組中每次成功搜索后加倍,則找到最終值
- 排序數組中的最后一個重復元素
- 找到一個數組元素,使所有元素都可被它整除
- 以原始順序查找數組的 k 個最大元素
- 數組中的最大值,至少是其他元素的兩倍
- 連續步驟到屋頂
- 兩個大小的組之間的最大差異
- 兩個大小的組之間的最小差異
- 未排序整數列表中最接近的數字
- 值和索引和的最大絕對差
- 數組中局部極值的數量
- 檢查數組是否具有多數元素
- 查找數組中最接近的數字
- 最大和的對數
- 按原始順序打印給定數組中的 n 個最小元素
- 查找給定數組中缺少的前 k 個自然數
- 數組中的高尚整數(大于等于的元素數等于 value)
- 兩個數組對的絕對差的最小和
- 查找數組中非重復(不同)元素的總和
- 檢查是否可以從給定數組形成算術級數
- 數組的最小乘積子集
- 計算選擇差異最大的對的方法
- 每次成功搜索后通過將元素加倍來重復搜索
- 允許負數的數組中成對乘積的最大和
- 矩陣
- 旋轉矩陣元素
- 將方形矩陣旋轉 90 度| 系列 1
- 將矩陣旋轉 90 度,而無需使用任何額外空間| 系列 2
- 將矩陣旋轉 180 度
- 用 K 元素逆時針旋轉矩陣的每個環
- 將圖像旋轉 90 度
- 檢查矩陣的所有行是否都是彼此旋轉
- 排序給定矩陣
- 查找最大數量為 1 的行
- 在按行排序的矩陣中找到中位數
- 矩陣乘法| 遞歸的
- 程序將兩個矩陣相乘
- 矩陣的標量乘法程序
- 程序打印數組的下三角和上三角矩陣
- 查找矩陣所有行共有的不同元素
- 以螺旋形式打印給定的矩陣
- 查找矩陣中每一行的最大元素
- 在矩陣中查找唯一元素
- 將矩陣元素逐行移動 k
- 矩陣的不同運算
- 以逆時針螺旋形式打印給定矩陣
- 交換方矩陣的主要和次要對角線
- 矩陣中的最大路徑總和
- 矩陣對角元素的正方形
- 沿給定方向移動矩陣元素并添加具有相同值的元素
- 按升序對矩陣行進行排序,然后按降序對列進行排序
- 矩陣中間行和列的總和
- 矩陣的按行遍歷與按列遍歷
- 向右旋轉矩陣 K 次
- 檢查冪等矩陣的程序
- 程序檢查對合矩陣
- 矩陣中第一行和最后一行的交換元素
- zag-zag 方式打印矩陣
- 二維數組中的按行排序
- 馬爾可夫矩陣程序
- 檢查對角矩陣和標量矩陣的程序
- 按行和列對矩陣進行排序
- 查找島嶼數| 系列 1(使用 DFS)
- 魔術廣場| 偶數訂單
- 魔術廣場
- 檢查給定矩陣是否為幻方
- 檢查給定矩陣是否為幻方
- 兩種矩陣的 Kronecker 積
- 計數總和可分為“ k”的子矩陣
- 對角占優矩陣
- 使矩陣的每一行和每一列相等所需的最少操作
- 計算大小為 n 的矩陣中 k 的頻率,其中 matrix(i,j)= i + j
- 給定 1、2、3……k 以之字形打印它們。
- 皇后可以在棋盤上移動的障礙物數量
- 矩陣中 4 個相鄰元素的最大積
- 使二進制矩陣對稱所需的最小翻轉
- 程序檢查矩陣是否為下三角
- 程序檢查矩陣是否為上三角
- 矩陣中偶數和奇數的頻率
- 矩陣的中心元素等于對角線的一半
- 身份矩陣程序
- 程序用矩陣的下對角元素交換上對角元素。
- 稀疏矩陣表示| 系列 3(CSR)
- 填充矩陣以使所有行和所有列的乘積等于 1 的方式
- 矩陣對角線的鏡像
- 查找二進制矩陣中是否有一個角為 1 的矩形
- 查找所有填充有 0 的矩形
- 矩陣或網格中兩個單元之間的最短距離
- 計算二進制矩陣中 1 和 0 的集合
- 搜索按行和按列排序的矩陣
- 創建具有 O 和 X 的交替矩形的矩陣
- 矩陣的鋸齒形(或對角線)遍歷
- 原位(固定空間)M x N 大小的矩陣轉置| 更新
- 排序從 0 到 n ^ 2 – 1 的數字矩陣的最低成本
- 二進制矩陣中的唯一像元
- 計算特殊矩陣中等于 x 的條目
- 檢查給定矩陣是否稀疏
- 方矩陣的兩個對角線中的行式公共元素
- 檢查矩陣中第 i 行和第 i 列的總和是否相同
- 查找最大數為 1 的二進制矩陣的行號
- 程序檢查矩陣是否對稱
- 通過遵循單元格值來查找二維數組是否被完全遍歷
- 程序以 Z 格式打印矩陣
- 在矩陣中從左上到右下打印所有回文路徑
- 騎士的可能舉動
- 有效地計算矩陣的對角線總和
- 矩陣的邊界元素
- 從點開始以螺旋形式打印矩陣
- 以蛇形圖案打印矩陣
- 矩陣對角線互換程序
- 找出兩個對角線之和之間的差
- 從給定的二叉樹構造祖先矩陣
- 從祖先矩陣構造樹
- 圓形矩陣(以螺旋方式構造數字 1 到 m * n 的矩陣)
- Sudoku Generator 程序
- 康威人生游戲計劃
- 矩陣中沙漏的最大和
- 方陣中的最大值和最小值。
- 以防螺旋形式打印矩陣
- 查找矩陣的法線和跡線的程序
- 以各種方式對矩陣進行排序
- 設置二進制矩陣的所有元素所需的最少操作
- 以反向螺旋形式打印給定的矩陣
- C 程序檢查矩陣是否傾斜對稱
- 矩陣元素的總和,其中每個元素是行和列的整數除法
- 稀疏矩陣及其表示| 系列 2(使用列表和鍵字典)
- 查找使兩個矩陣相等的變換數
- 形成矩陣線圈
- 每個元素是其行號和列號的絕對差的矩陣總和
- 檢查二進制矩陣中的水平和垂直對稱性
- 每個值為 0 或 n 的矩陣的最大行列式
- 螺旋奇數階方陣的兩個對角線之和
- 在二進制矩陣中找到具有最大位差的行對
- 查找矩陣中給定行的所有置換行
- 在二進制矩陣中查找以 1s 形成的形狀的周長
- 在矩陣中打印具有相同矩形和的單元格
- 以對角線圖案打印矩陣
- 矩陣中兩行元素之和的最大差
- 查找具有給定總和的對,以便該對的元素位于不同的行中
- 二進制矩陣中所有零的總覆蓋率
- 用行或列的最大 GCD 替換每個矩陣元素
- 計算矩陣中所有排序的行
- 矩陣查詢
- 矩陣中的最大 XOR 值
- 可以從下到右傳輸光線的最大反射鏡
- 最后一個方塊的方向
- 以矩陣的螺旋形式打印第 K 個元素
- 查找給定的矩陣是否為 Toeplitz
- 在按行和按列排序的矩陣中計數零
- 在列明智和行明智排序矩陣中計算負數
- 在二進制矩陣中查找所有位形成的最大“ +”的大小
- 返回擴展矩陣中的前一個元素
- 使用O(1)額外空間打印 n x n 螺旋矩陣
- 二進制迷宮中的最短路徑
- 查找矩陣中圖案的方向
- 在矩陣中查找特定對
- 打印給定大小的最大和平方子矩陣
- 給定矩陣的所有行中的公共元素
- 按特定順序就地轉換矩陣
- 布爾矩陣問題
- 給定布爾矩陣,找到 k,使第 k 行中的所有元素均為 0,第 k 列為 1。
- 在給定的布爾矩陣中打印唯一行
- 找到 1 的最大矩形,并允許交換列
- 給定井字棋盤配置的有效性
- 子矩陣總和查詢
- 矩陣排名程序
- 全為 1 的最大尺寸矩形二進制子矩陣
- 全為 1 的最大尺寸正方形子矩陣
- 查找矩陣中除給定單元格的行和/或列中的元素以外的所有元素的總和?
- 計算每個島按行和列分隔的島數
- 在給定的按行排序的矩陣的所有行中找到一個公共元素
- 給定矩陣“ O”和“ X”,如果被“ X”包圍,則將“ O”替換為“ X”
- 給定矩陣“ O”和“ X”,找到被“ X”包圍的最大子正方形
- 洪水填充算法–如何在 paint 中實現 fill()?
- 從行和列的排序矩陣中按排序順序打印所有元素
- 給定一個 n x n 方陣,求出大小為 k x k 的所有子方和
- 查找矩陣轉置的程序
- 用于添加兩個矩陣的程序
- 矩陣減法程序
- 使用兩次遍歷收集網格中的最大點
- 在死胡同之前收集最多硬幣
- 正好有 k 個硬幣的路徑數
- 查找從給定起始字符開始的最長連續路徑的長度
- 在給定約束條件下找到矩陣中的最長路徑
- 到達目的地的最低初始點
- 分而治之| 第 5 組(Strassen 的矩陣乘法)
- 2D 矩陣中的最大和矩形| DP-27
- 雜項
- 子數組/子字符串與子序列以及生成它們的程序
- 產品數組難題
- 具有給定乘積的子數組數
- 鏈表與數組
- 檢查數組元素是否連續 新增方法 3
- 查找一個數組是否是另一個數組的子集 新增方法 3
- 在一個數組中實現兩個堆棧
- 查找兩個排序數組的相對補碼
- 通過 k 次運算的最小增量以使所有元素相等
- 最小化三個不同排序數組的(max(A [i],B [j],C [k])– min(A [i],B [j],C [k]))