leetcode 409. Longest Palindrome
来源:互联网 发布:幽灵虎淘宝快10万了 编辑:程序博客网 时间:2024/04/30 10:12
/*leetcode 409. Longest PalindromeGiven a string which consists of lowercase or uppercase letters,find the length of the longest palindromes that can be built with those letters.This is case sensitive, for example "Aa" is not considered a palindrome here.Note:Assume the length of given string will not exceed 1,010.Example:Input:"abccccdd"Output:7Explanation:One longest palindrome that can be built is "dccaccd", whose length is 7.题目大意:对于给定的字符串,用这些字符能组成的最长回文是多长。解题思路:最长回文长度计算方法:如果出现偶数次,则直接加上;如果出现奇数次,第一个基数加上奇数次,后面的基数只能取奇数次-1.*/#include <iostream>#include <vector>#include <string>#include <algorithm>using namespace std;class Solution {public: int longestPalindrome(string s) { int len = s.size(); if (len == 0 || len == 1) return len; vector<int> v(128, 0); for (int i = 0; i < len; ++i) ++v[s[i]]; int ret = 0; int oddNum = 0; for (int i = 0; i < 128; ++i) { if (v[i] & 0x01) { ++oddNum; } ret += v[i]; } if (oddNum) ret -= (--oddNum); return ret; }};void test(){ Solution sol; cout << sol.longestPalindrome("civilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth") << endl;}int main(){ test(); return 0;}
0 0
- 【Leetcode】409. Longest Palindrome
- [LeetCode]409. Longest Palindrome
- LeetCode 409. Longest Palindrome
- [LeetCode]--409. Longest Palindrome
- leetcode 409. Longest Palindrome
- LeetCode 409. Longest Palindrome
- LeetCode 409. Longest Palindrome
- leetcode 409. Longest Palindrome
- leetcode 409. Longest Palindrome
- 409. [LeetCode]Longest Palindrome
- 【LeetCode】 409. Longest Palindrome
- Leetcode 409. Longest Palindrome
- LeetCode 409. Longest Palindrome
- LeetCode 409. Longest Palindrome
- Leetcode-409. Longest Palindrome
- [LeetCode]409. Longest Palindrome
- leetcode 409. Longest Palindrome
- 【Leetcode】409. Longest Palindrome
- 获取jqGrid中选中行的数据以及 jqGrid获得所有行数据的方法
- Java并发编程实战笔记(3)- 多线程并发编程模型
- 悲观锁乐观锁
- 机器学习实战---书中谬误讨论
- 查看 MySQL 数据库中每个表占用的空间大小
- leetcode 409. Longest Palindrome
- 【ES6学习】— (2)异步编程Generator函数和Promise对象简介
- Mahout K-Means输出结果解析
- Poj 3208 Apocalypse Someday
- 主键与外键篇
- Retrofit 2.0 轻松实现多文件/图片上传
- 网站架构(页面静态化,图片服务器分离,负载均衡)方案全解析
- Action与Func委托
- eclipse启动tomcat时servlet-api.jar jar not loaded