zoukankan      html  css  js  c++  java
  • lintcode211- String Permutation- easy

    Given two strings, write a method to decide if one is a permutation of the other.

    Example

    abcd is a permutation of bcad, but abbe is not a permutation of abe

    函数头:public boolean Permutation(String A, String B)

    算法:开一个int[] cnt数组,A里读到一个char就计数+1,B里读到一个char就计数-1,最后如果cnt都是0的话就说明两者计数一致,是正确的。数组不要用map取代,大材小用。

    细节:1.因为是char,256个就可以枚举完ascii码的部分(虽然java里char是用两个字节存储的,有65536种可能),所以就只要开一个数组记录就好了,没必要用map。以后如果key本身就是数字型的(integer || char),就可以作为数组的idx来当key了,没必要用map数据类型。2.Map的一些用法。Set<Key> map.keySet(); Set<Entry<K,V>> map.entrySet(); Collection<V> map.values(); 三者取出来的可以遍历,记好函数名。然后Entry<K,V> entry的用法是K entry.getKey();  entry.getValue(); 

    1.数组版

    public class Solution {
        /*
         * @param A: a string
         * @param B: a string
         * @return: a boolean
         */
        public boolean Permutation(String A, String B) {
            // write your code here
            if (A == null || B == null || A.length() != B.length()) {
                return false;
            }
            
            int[] cnt = new int[256];
            for (int i = 0; i < A.length(); i++) {
                cnt[(int)A.charAt(i)]++;
                cnt[(int)B.charAt(i)]--;
            }
            for (int i = 0; i < cnt.length; i++) {
                if (cnt[i] != 0) {
                    return false;
                }
            }
            return true;
        }
    }

    2.用map的杀鸡用牛刀版

    public class Solution {
        /*
         * @param A: a string
         * @param B: a string
         * @return: a boolean
         */
        public boolean Permutation(String A, String B) {
            // write your code here
            if (A == null || B == null || A.length() != B.length()) {
                return false;
            }
            
            char[] charsA = A.toCharArray();
            char[] charsB = B.toCharArray();
            Map<Character, Integer> mapA = new HashMap<Character, Integer>();
            Map<Character, Integer> mapB = new HashMap<Character, Integer>();
            
            for (int i = 0; i < charsA.length; i++) {
                if (!mapA.containsKey(charsA[i])) {
                    mapA.put(charsA[i], 1);
                }
                mapA.put(charsA[i], mapA.get(charsA[i]) + 1);
            }
            
            for (int i = 0; i < charsB.length; i++) {
                if (!mapB.containsKey(charsB[i])) {
                    mapB.put(charsB[i], 1);
                }
                mapB.put(charsB[i], mapB.get(charsB[i]) + 1);
            }
            
            for (Character c : mapA.keySet()) {
                if (mapA.get(c) != mapB.get(c)) {
                    return false;
                }
            }
            
            return true;
        }
    }
  • 相关阅读:
    Solr相似度算法二:BM25Similarity
    Solr相似度算法一:Lucene TF-IDF 相关性算分公式
    Solr相似度算法一:DefaultSimilarity(基于TF-IDF的默认相似度算法)
    Solr特性:Schemaless Mode(自动往Schema中添加field)
    Mahout的taste里的几种相似度计算方法
    Solr之functionQuery(函数查询)
    Solr中的一些查询参数
    solr特点三: 基于Solr实现排序定制化参考
    solr特点三: 排序样例汇总
    Solr 使用自定义 Query Parser(短语查询,精准查询)
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/7782166.html
Copyright © 2011-2022 走看看