zoukankan      html  css  js  c++  java
  • 266. Palindrome Permutation

    原题链接:https://leetcode.com/articles/palindrome-permutation/
    这道题目很简单:

    /**
     * Created by clearbug on 2018/2/26.
     */
    public class Solution {
    
        public static void main(String[] args) {
            Solution s = new Solution();
            System.out.println(s.canPermutePalindrome("code"));
            System.out.println(s.canPermutePalindrome("aab"));
            System.out.println(s.canPermutePalindrome("carerac"));
        }
    
        /**
         * 我的方法就是使用哈希表啦,不过我这里只是把字符串中的字符范围当成了 [a-z] 了,好像不太符合题意呢!更加符合题意的见官方方法一;
         *
         * @param s
         * @return
         */
        public boolean canPermutePalindrome(String s) {
            if (s == null) {
                return false;
            }
            if (s.length() == 0 || s.length() == 1) {
                return true;
            }
    
            int[] arr = new int[26];
            for (int i = 0; i < s.length(); i++) {
                arr[s.charAt(i) - 'a']++;
            }
    
            int singleNum = 0;
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] % 2 != 0) {
                    if (singleNum == 0) {
                        singleNum++;
                    } else {
                        return false;
                    }
                }
            }
            return true;
        }
    
        // 官方方法一:比如的更完善一点啦
        // 官方方法二:使用了一个 JDK 自带的 HashMap,代码比我的要简洁啊
        // 官方方法三:使用一个数组作为哈希表,这他么就是把我的方法完善简化版本啦
        // 官方方法四:跟前面的方法差不多思路了
        // 官方方法五:虽然还是思路差不多,但是使用 HashSet 简洁易懂多了
    }
    
  • 相关阅读:
    Pull Request
    选择器
    常见HTTP状态码
    286. Walls and Gates
    200. Number of Islands
    1. Two Sum
    名片管理系统(python实现)
    k近邻算法(简单版)
    基数排序
    递归算法的调试
  • 原文地址:https://www.cnblogs.com/optor/p/8728493.html
Copyright © 2011-2022 走看看