搜尋
首頁後端開發C++鍊錶中出現次數最多的字符

鍊錶中出現次數最多的字符

我們給定了一個字元單鍊錶,我們的任務是列印鍊錶中出現次數最多的字元。如果多個字元出現的次數相同,則列印最後出現的字元。

單鍊錶是一種由節點組成的線性資料結構。每個節點都包含資料和指向下一個節點的指針,該指針包含下一個節點的記憶體位址,因為分配給每個節點的記憶體不是連續的。

範例

假設我們已經給了一個字元連結列表

範例 1

輸入:LL = a -> b -> c -> c -> c

輸出:最多出現的字元是 c。

解釋:在給定的鍊錶 LL 中,a 出現 1 次,b 出現 1 次,c 出現 3 次。因此,輸出為c。

範例 2

輸入:

LL = x -> x -> y -> y -> z -> z

輸出:最大出現的字元是 z。

解釋:在給定的鍊錶LL中,x出現2次,y出現2次,z出現2次。所有的出現次數都相同,因為 z 出現在最後,因此輸出是 z。

在這裡我們將討論兩種方法。讓我們看看下面的部分 -

方法一:迭代計算頻率

這種方法的想法是,我們將遍歷鍊錶併計算每個字符的頻率,然後找出頻率最大的字符,如果多個字符具有相同的頻率,則打印該字符返回最後一個字符。

範例

#include <iostream>
using namespace std;
// creating a class to have a structure for linked list nodes 
class Node{
   public:
   char data; // variable to store the characters
   Node* next = NULL; // variable to store the address of the next node     
   Node(char cur){
      data = cur;
   }
};
// function to print the elements of the linked list 
void printLL(Node* head){   
   // creating a temporary pointer 
   Node* temp = head;    
   while(temp != nullptr){
      cout<<temp->data<<" -> ";
      temp = temp->next;
   }
   cout<<"NULL"<<endl;
}
// function to find the max frequency 
void maxFreq(Node* head){
   // traversing over the linked list for each character 
   // starting from the first character to the last character     
   int ans = 0; // variable to store the maximum frequency 
   char char_ans;
   Node* temp_first = head; // variable to store the current first node     
   while(temp_first != nullptr){
      int cur = 0; // variable to store the frequency of the current character 
      Node* temp = temp_first;        
      while(temp != nullptr){
         if(temp->data == temp_first->data){
            cur++;
         }
         temp = temp->next;
      }
      if(ans < cur){
         ans = cur;
         char_ans = temp_first->data;
      }
      temp_first = temp_first->next;
   }
   cout<<"The last character in the given linked list is '"<<char_ans<<"' with the frequency of "<< ans<<endl;
}
// main function 
int main(){
   // defining the linked list 
   Node* head = new Node('a');
   head->next = new Node('b');
   head->next->next = new Node('b');
   head->next->next->next = new Node('c');
   head->next->next->next->next = new Node('d');
   head->next->next->next->next->next = new Node('d');
   head->next->next->next->next->next->next = new Node('d');  
   cout<<"The given linked list is: "<<endl;
   printLL(head);   
   maxFreq(head);
   return 0;
}

輸出

The given linked list is: 
a -> b -> b -> c -> d -> d -> d -> NULL
The last character in the given linked list is 'd' with the frequency of 3

時間複雜度

:O(N*N),其中N是鍊錶的大小。

空間複雜度:O(1)

方法 2:使用計數數組

這種方法的想法是,我們將維護計數數組,在其中儲存每個字元的頻率,然後遍歷該數組並找到頻率最高的字元。如果多個字符具有相同的頻率,則列印該字符,然後返回最後一個字符。

範例

#include <iostream>
using namespace std;
// creating a class to have a structure for linked list nodes 
class Node{
   public:
   char data; // variable to store the characters
   Node* next = NULL; // variable to store the address of the next node     
   Node(char cur){
      data = cur;
   }
};
// function to print the elements of the linked list 
void printLL(Node* head){    
   // creating a temporary pointer 
   Node* temp = head;    
   while(temp != nullptr){
      cout<<temp->data<<" -> ";
      temp = temp->next;
   }
   cout<<"NULL"<<endl;
}
// function to find the max frequency 
void maxFreq(Node* head){
   int ans = 0; // variable to store the maximum frequency 
   char char_ans;    
   // traversing over the linked list for each lowercase character 
   for(char i = 'a'; i<= 'z'; i++){
      Node* temp = head;
      int cur = 0; // variable to store the frequency of the current character 
      while(temp != nullptr){
         if(temp->data == i){
            cur++;
         }
         temp = temp->next;
      }
      if(ans <= cur){
         ans = cur;
         char_ans = i;
      }
   }     
   cout<<"The last character in the given linked list is '"<<char_ans<<"' with the frequency of "<< ans<<endl;
}
int main(){
   // defining the linked list 
   Node* head = new Node('a');
   head->next = new Node('b');
   head->next->next = new Node('b');
   head->next->next->next = new Node('c');
   head->next->next->next->next = new Node('e');
   head->next->next->next->next->next = new Node('d');
   head->next->next->next->next->next->next = new Node('d');  
   cout<<"The given linked list is: "<<endl;
   printLL(head);   
   maxFreq(head);
   return 0;
}

輸出

The given linked list is: 
a -> b -> b -> c -> e -> d -> d -> NULL
The last character in the given linked list is 'd' with the frequency of 2

時間複雜度

O(N),其中N是鍊錶的大小。

空間複雜度:O(N),其中 N 是鍊錶的大小。

結論

這裡我們討論如何找到鍊錶中出現最多的字元。為了找到最大出現的字符,我們討論了兩種方法。第一種方法對給定鍊錶的每個字元使用 while 循環,第二種方法對每個小寫字元使用 for 循環並維護計數。

以上是鍊錶中出現次數最多的字符的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:tutorialspoint。如有侵權,請聯絡admin@php.cn刪除
如何在 Word 中键入箭头如何在 Word 中键入箭头Apr 16, 2023 pm 11:37 PM

如何使用自动更正在 Word 中键入箭头在 Word 中键入箭头的最快方法之一是使用预定义的自动更正快捷方式。如果您键入特定的字符序列,Word 会自动将这些字符转换为箭头符号。您可以使用此方法绘制多种不同的箭头样式。要使用自动更正在 Word 中键入箭头:将光标移动到文档中要显示箭头的位置。键入以下字符组合之一:如果您不希望将您键入的内容更正为箭头符号,请按键盘上的退格键会将

如何在 Microsoft Excel 中应用上标和下标格式选项如何在 Microsoft Excel 中应用上标和下标格式选项Apr 14, 2023 pm 12:07 PM

上标是一个字符或多个字符,可以是字母或数字,您需要将其设置为略高于正常文本行。例如,如果您需要写1st,则字母st需要略高于字符1。同样,下标是一组字符或单个字符,需要设置为略低于正常文本级别。例如,当你写化学式时,你需要把数字放在正常字符行的下方。以下屏幕截图显示了上标和下标格式的一些示例。尽管这似乎是一项艰巨的任务,但实际上将上标和下标格式应用于您的文本非常简单。在本文中,我们将通过一些简单的步骤说明如何轻松地使用上标或下标格式设置文本。希望你喜欢阅读这篇文章。如何在 Excel 中应用上标

使用java的Character.isDigit()函数判断字符是否为数字使用java的Character.isDigit()函数判断字符是否为数字Jul 27, 2023 am 09:32 AM

使用Java的Character.isDigit()函数判断字符是否为数字字符在计算机内部以ASCII码的形式表示,每个字符都有一个对应的ASCII码。其中,数字字符0到9分别对应的ASCII码值为48到57。要判断一个字符是否为数字,可以使用Java中的Character类提供的isDigit()方法进行判断。isDigit()方法是Character类的

如何在 iPhone 和 Mac 上输入扩展字符,例如度数符号?如何在 iPhone 和 Mac 上输入扩展字符,例如度数符号?Apr 22, 2023 pm 02:01 PM

您的物理或数字键盘在表面上提供有限数量的字符选项。但是,有几种方法可以在iPhone、iPad和Mac上访问重音字母、特殊字符等。标准iOS键盘可让您快速访问大写和小写字母、标准数字、标点符号和字符。当然,还有很多其他角色。您可以从带有变音符号的字母到倒置的问号中进行选择。您可能无意中发现了隐藏的特殊字符。如果没有,以下是在iPhone、iPad和Mac上访问它们的方法。如何在iPhone和iPad上访问扩展字符在iPhone或iPad上获取扩展字符非常简单。在“信息”、“

正确在matplotlib中显示中文字符的方法正确在matplotlib中显示中文字符的方法Jan 13, 2024 am 11:03 AM

在matplotlib中正确地显示中文字符,是很多中文用户常常遇到的问题。默认情况下,matplotlib使用的是英文字体,无法正确显示中文字符。为了解决这个问题,我们需要设置正确的中文字体,并将其应用到matplotlib中。下面是一些具体的代码示例,帮助你正确地在matplotlib中显示中文字符。首先,我们需要导入需要的库:importmatplot

使用递归方法在C++中找到链表倒数第n个节点使用递归方法在C++中找到链表倒数第n个节点Sep 15, 2023 pm 05:53 PM

给定一个单链表和正整数N作为输入。目标是使用递归找到给定列表中从末尾算起的第N个节点。如果输入列表有节点a→b→c→d→e→f并且N为4,那么倒数第4个节点将是c。我们将首先遍历直到列表中的最后一个节点以及从递归(回溯)增量计数返回时。当count等于N时,则返回指向当前节点的指针作为结果。让我们看看此的各种输入输出场景-输入-List:-1→5→7→12→2→96→33N=3输出−倒数第N个节点为:2解释−第三个节点是2。输入−列表:-12→53→8→19→20→96→33N=8输出-节点不存

如何使用Golang判断一个字符是否为字母如何使用Golang判断一个字符是否为字母Dec 23, 2023 am 11:57 AM

如何使用Golang判断一个字符是否为字母在Golang中,判断一个字符是否为字母可以通过使用Unicode包中的IsLetter函数来实现。IsLetter函数会检查给定的字符是否是一个字母。接下来,我们将详细介绍如何使用Golang编写代码来判断一个字符是否为字母。首先,你需要创建一个新的Go文件,用于编写代码。你可以将文件命名为"main.go"。代码

有关Java中回车键的字符表示,请问是哪一个?有关Java中回车键的字符表示,请问是哪一个?Mar 29, 2024 am 11:48 AM

Java中回车键的字符表示是`。在Java中,`表示换行符,当遇到这个字符时,文本输出会换行。下面是一个简单的代码示例,演示如何使用``来表示回车键:publicclassMain{publicstaticvoidmain(String[]args){System.out.println("这是第一行这

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。