Leetcode - Longest Palindrome

My code:

public class Solution {
    public int longestPalindrome(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        
        int counter = 0;
        HashSet<Character> set = new HashSet<Character>();
        for (int i = 0; i < s.length(); i++) {
            char curr = s.charAt(i);
            if (set.contains(curr)) {
                counter++;
                set.remove(curr);
            }
            else {
                set.add(curr);
            }
        }
        
        if (!set.isEmpty()) {
            return 2 * counter + 1;
        }
        else {
            return 2 * counter;
        }
    }
}

reference:
https://discuss.leetcode.com/topic/61300/simple-hashset-solution-java

这道题目一开始没理解题意。看了答案,才知道什么意思。
刷题有些浮躁,不思考,就直接看答案。

Anyway, Good luck, Richardo! — 10/12/2016

    原文作者:Richardo92
    原文地址: https://www.jianshu.com/p/7eab90be1adb
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞