当前位置:网站首页>409. Longest palindrome
409. Longest palindrome
2022-07-06 16:08:00 【mrbone9】
Address :
Power button https://leetcode-cn.com/problems/longest-palindrome/
subject :
Given a string of uppercase and lowercase letters s , return Constructed from these letters The longest palindrome string .
In the process of construction , Please note that Case sensitive . such as "Aa" Can't be treated as a palindrome string .
Example 1:
Input :s = "abccccdd" Output :7 explain : The longest palindrome string we can construct is "dccaccd", Its length is 7. |
Example 2:
Input :s = "a" Input :1 |
Example 3:
Input :s = "bb" Input : 2 |
Tips :
1 <= s.length <= 2000 s Only lowercase and / Or capital letters |
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/longest-palindrome
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
Palindromes mean pairs , Even number , As for whether there is an odd number in the middle , All palindromes
So we use the idea of counting arrays to complete
Count array can refer to :
Counting sorting of sorting algorithm
Method 1 、 Count array
int longestPalindrome(char * s){
int count[58]={0};
int cnt = 0;
int odd = 0;
int i = 0;
while(s[i])
{
int idx = s[i] - 'A';
//printf("s[%d]=%d, idx=%d\n", i, s[i], idx);
count[idx]++;
i++;
}
for(i=0; i<58; i++)
{
if(count[i] != 0)
{
if(count[i] & 1)
odd = 1;
cnt += count[i] / 2;
}
}
cnt *= 2;
if(odd == 1)
cnt++;
return cnt;
}
边栏推荐
- JS调用摄像头
- The concept of C language array
- Opencv learning log 26 -- detect circular holes and mark them
- Nodejs+vue online fresh flower shop sales information system express+mysql
- 想应聘程序员,您的简历就该这样写【精华总结】
- Research Report on surgical fluid treatment industry - market status analysis and development prospect prediction
- [exercise-6] (PTA) divide and conquer
- SSM框架常用配置文件
- Penetration test (3) -- Metasploit framework (MSF)
- HDU - 6024 Building Shops(女生赛)
猜你喜欢
渗透测试 ( 1 ) --- 必备 工具、导航
Essai de pénétration (1) - - outils nécessaires, navigation
Penetration test (3) -- Metasploit framework (MSF)
信息安全-威胁检测-NAT日志接入威胁检测平台详细设计
[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
1010 things that college students majoring in it must do before graduation
[teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
Ball Dropping
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
STM32 how to use stlink download program: light LED running light (Library version)
随机推荐
[exercise-7] (UVA 10976) fractions again?! (fraction split)
Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
Research Report of peripheral venous catheter (pivc) industry - market status analysis and development prospect prediction
Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
【练习-10】 Unread Messages(未读消息)
Opencv learning log 24 -- Hough transform 2 (maximum interval and minimum length can be limited)
Raspberry pie csi/usb camera uses mjpg to realize web camera monitoring
HDU - 6024 Building Shops(女生赛)
HDU-6025-Coprime Sequence(女生赛)
b站 實時彈幕和曆史彈幕 Protobuf 格式解析
基于web的照片数码冲印网站
Ball Dropping
Hdu-6025-prime sequence (girls' competition)
Research Report on shell heater industry - market status analysis and development prospect forecast
mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
E. Breaking the Wall
Find 3-friendly Integers
Basic Q & A of introductory C language
最全编程语言在线 API 文档
渗透测试 ( 3 ) --- Metasploit Framework ( MSF )