搜索
首页后端开发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.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
2 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具