当前位置:网站首页>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;
}
边栏推荐
- Vs2019 initial use
- CS zero foundation introductory learning record
- Pyside6 signal, slot
- Path problem before dynamic planning
- Web based photo digital printing website
- Truck History
- [exercise -11] 4 values why sum is 0 (and 4 values of 0)
- Shell脚本编程
- Borg maze (bfs+ minimum spanning tree) (problem solving report)
- 信息安全-威胁检测引擎-常见规则引擎底座性能比较
猜你喜欢
树莓派4B安装opencv3.4.0
mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志
b站 實時彈幕和曆史彈幕 Protobuf 格式解析
C language is the watershed between low-level and high-level
渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
Ball Dropping
Borg Maze (BFS+最小生成树)(解题报告)
Write web games in C language
Luogu P1102 A-B number pair (dichotomy, map, double pointer)
随机推荐
Alice and Bob (2021 Niuke summer multi school training camp 1)
Interesting drink
JS call camera
HDU - 6024 Building Shops(女生赛)
[exercise-4] (UVA 11988) broken keyboard = = (linked list)
[exercise 4-1] cake distribution
Penetration test (3) -- Metasploit framework (MSF)
Opencv learning log 33 Gaussian mean filtering
Determine the Photo Position
HDU-6025-Coprime Sequence(女生赛)
【练习-9】Zombie’s Treasure Chest
Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B
Opencv learning log 15 count the number of solder joints and output
Opencv learning log 26 -- detect circular holes and mark them
Opencv learning log 16 paperclip count
【练习-7】(Uva 10976)Fractions Again?!(分数拆分)
[exercise -10] unread messages
Essai de pénétration (1) - - outils nécessaires, navigation
C language is the watershed between low-level and high-level
C language learning notes