LeetCode Longest Palindrome(计算最长的对称串)
题意:给出一个字符串,问用这个字符串的字符可以组成的最长的对称串的长度
思路:用map来做,如果字符的个数为偶数个,直接相加,如果字符个数为奇数个,取小于这个奇数的最在偶数。如果奇数个字符的个数存在多个,则只能再加1
代码如下:
public class Solution
{public int longestPalindrome(String s){Map<Character, Integer> map = new HashMap<>();for (int i = 0; i < s.length(); i++){if (map.containsKey(s.charAt(i))){map.put(s.charAt(i), map.get(s.charAt(i)) + 1);}else{map.put(s.charAt(i), 1);}}int ans = 0;boolean flag = false;for(Map.Entry<Character, Integer> entity: map.entrySet()){if (entity.getValue() % 2 == 0){ans += entity.getValue();}else{ans += entity.getValue() & ~1;if (!flag){ans += 1;flag = true;}}}return ans;}
}
LeetCode Longest Palindrome(计算最长的对称串)相关推荐
- C#LeetCode刷题之#409-最长回文串(Longest Palindrome)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3788 访问. 给定一个包含大写字母和小写字母的字符串,找到通过这 ...
- 计算最长公共数字串个数
一.华为OJ题目(计算最长公共数字个数) 样例输出 输出123058789,函数返回值9 输出54761,函数返回值5 接口说明 函数原型: unsignedint Continumax(char** ...
- LeetCode解题记录(409)——最长回文串
LeetCode解题记录--最长回文串 题目描述 示例 题目理解 解题思路 题目描述 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比 ...
- LeetCode:Longest Palindromic Substring 最长回文子串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- LeetCode简单题之最长回文串
题目 给定一个包含大写字母和小写字母的字符串 s ,返回 通过这些字母构造成的 最长的回文串 . 在构造过程中,请注意 区分大小写 .比如 "Aa" 不能当做一个回文字符串. 示例 ...
- leetcode 131. Palindrome Partitioning | 131. 分割回文串(递归解法)
题目 https://leetcode.com/problems/palindrome-partitioning/ 题解 中规中矩的递归,因为每一层都需要在上一层保存好的 list 的基础上继续,而且 ...
- leetcode题库:5.最长回文子串Longest Palindrome string
题目描述: /** 题目地址:https://leetcode-cn.com/problems/longest-palindromic-substring/description/ * 题目:最长回 ...
- [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串...
Given a string S, find the length of the longest substring T that contains at most two distinct char ...
- 【回文串9】LeetCode 409. Longest Palindrome
LeetCode 409. Longest Palindrome Solution1:我的答案 有时候真笨的可以... class Solution { public:int longestPalin ...
最新文章
- little w and Soda(思维题)
- 工程勘察设计收费标准2002修订版_全过程工程咨询收费模式超全解析
- MySQL对一行多列求和
- 根据list中某个字段的值排序_MySql中常用函数
- 【英语学习】【English L06】U08 News L1 It's really a magical book!
- 阿里云解决方案架构师,讲述分布式架构云平台解决方案(附图文)
- android shape 使用小结
- 小芭比linux怎么装win7_超迷你的Linux系统--Puppy Linux(小芭比)
- 基于python的交通_基于Python的交通流数据清洗
- 怎么制作GIF微信表情
- “一个优秀程序员可抵五个普通程序员!”
- 操作系统实验 连续内存分配 首次适应(FF)算法
- 域名检测工具-免费老域名扫描软件下载
- [FastDFS] 文件路径M00是什么意思
- 三种安卓模拟器的安装和比较
- phase-portrait相轨迹
- NodeMCU-ESP8266烧录手把手教程
- 软件测试面试题 | 提BUG相关
- 35岁前成功的黄金法则(12)-十二分努力
- python人工智能方向面试准备_人工智能实战就业(面试)学习路线图
热门文章
- limit是mysql的语法
- [原创]浅谈持续集成在测试中的应用
- 关于页面加载的方法收集
- python urllib.request 爬虫 数据处理-运维学python之爬虫基础篇(二)urllib模块使用...
- 财务学python有意义吗-8年老财务:财务分析学python就能提高效率?一半人是骗子...
- python excel 自动化-Python控制Excel实现自动化办公
- pythonis啥意思-Python基础:is和==的区别
- python就业方向哪个好-学习python就业方向都有哪些?
- python基础知识资料-Python基础知识(一)—简介
- python人工智能-Python在人工智能中的作用