美文网首页
409. Longest Palindrome

409. Longest Palindrome

作者: becauseyou_90cd | 来源:发表于2018-07-31 05:33 被阅读0次

https://leetcode.com/problems/longest-palindrome/description/
解题思路:

  1. 把字符转化为相应的数字
    class Solution {
    public int longestPalindrome(String s) {
    if(s == null) return 0;
    int[] nums = new int[58];
    int res = 0;
    int flag = 0;
    for(int i = 0; i < s.length(); i++){
    nums[s.charAt(i) - 'A']++;
    }
    for(int i = 0; i < nums.length; i++){
    res += nums[i] / 2 * 2;
    if(flag == 0 && nums[i] % 2 != 0){
    res += 1;
    flag = 1;
    }
    }
    return res;
    }
    }

相关文章

网友评论

      本文标题:409. Longest Palindrome

      本文链接:https://www.haomeiwen.com/subject/agfcvftx.html