zoukankan      html  css  js  c++  java
  • LeetCode 面试题 01.01. 判定字符是否唯一

    我的LeetCode:https://leetcode-cn.com/u/ituring/

    我的LeetCode刷题源码[GitHub]:https://github.com/izhoujie/Algorithmcii

    LeetCode 面试题 01.01. 判定字符是否唯一

    题目

    实现一个算法,确定一个字符串 s 的所有字符是否全都不同。

    示例 1:

    输入: s = "leetcode"
    输出: false 
    

    示例 2:

    输入: s = "abc"
    输出: true
    

    限制:

    • 0 <= len(s) <= 100
    • 如果你不使用额外的数据结构,会很加分。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/is-unique-lcci
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    解题思路

    思路1-indexOf和lastIndexOf配合使用,效率慢;

    前者找第一次出现位置,后者找最后一次出现位置,相等则必唯一;

    算法复杂度:

    • 时间复杂度: $ {color{Magenta}{Omicronleft(n^{2} ight)}} $
    • 空间复杂度: $ {color{Magenta}{Omicronleft(1 ight)}} $

    思路2-使用HashSet

    HashSet特性;

    算法复杂度:

    • 时间复杂度: $ {color{Magenta}{Omicronleft(n ight)}} $
    • 空间复杂度: $ {color{Magenta}{Omicronleft(n ight)}} $

    思路3-使用位运算

    思路解析:字符的数量在128或者加上不可见字符等特殊字符256个;
    考虑每个字符用一个bit位统计,那么需要256个bit,即4个long的长度;
    对于一个字符,若其转int值为val,那么:

    • val/4表示用第几个long来存储;
    • val%64表示将其记录在第val/4个long的bit位上;
    • bit位只记录一次,第一次从0到1,若再记录时说明出现了重复字符;

    算法复杂度:

    • 时间复杂度: $ {color{Magenta}{Omicronleft(n ight)}} $
    • 空间复杂度: $ {color{Magenta}{Omicronleft(1 ight)}} $

    算法源码示例

    package leetcode;
    
    import java.util.HashSet;
    
    /**
     * @author ZhouJie
     * @date 2020年5月11日 下午11:48:09 
     * @Description: 面试题 01.01. 判定字符是否唯一
     *
     */
    public class LeetCode_Satine_01_01 {
    
    }
    
    class Solution_Satine_01_01 {
    	/**
    	 * @author: ZhouJie
    	 * @date: 2020年5月11日 下午11:48:34 
    	 * @param: @param astr
    	 * @param: @return
    	 * @return: boolean
    	 * @Description: 1-indexOf和lastIndexOf配合使用,效率慢;
    	 *
    	 */
    	public boolean isUnique_1(String astr) {
    		if (astr == null || astr.length() == 0) {
    			return true;
    		}
    		for (int i = 0; i < astr.length(); i++) {
    			char c = astr.charAt(i);
    			if (astr.indexOf(c) != astr.lastIndexOf(c)) {
    				return false;
    			}
    		}
    		return true;
    	}
    
    	/**
    	 * @author: ZhouJie
    	 * @date: 2020年5月12日 上午12:02:01 
    	 * @param: @param astr
    	 * @param: @return
    	 * @return: boolean
    	 * @Description: 2-使用HashSet;
    	 *
    	 */
    	public boolean isUnique_2(String astr) {
    		if (astr == null || astr.length() == 0) {
    			return true;
    		}
    		HashSet<Character> set = new HashSet<Character>();
    		for (Character c : astr.toCharArray()) {
    			if (set.contains(c)) {
    				return false;
    			}
    			set.add(c);
    		}
    		return true;
    	}
    
    	/**
    	 * @author: ZhouJie
    	 * @date: 2020年5月12日 上午12:12:24 
    	 * @param: @param astr
    	 * @param: @return
    	 * @return: boolean
    	 * @Description: 3-使用位运算;
    	 *
    	 */
    	public boolean isUnique_3(String astr) {
    		long[] bucket = new long[4];
    		int k, index;
    		long bits;
    		for (int i = 0; i < astr.length(); i++) {
    			k = (int) astr.charAt(i);
    			index = k / 64;
    			bits = 1 << (k % 64);
    			if ((bucket[index] & bits) != 0) {
    				return false;
    			}
    			bucket[index] |= bits;
    		}
    		return true;
    	}
    
    }
    
    
  • 相关阅读:
    二分+RMQ/双端队列/尺取法 HDOJ 5289 Assignment
    思维题 HDOJ 5288 OO’s Sequence
    树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
    最大流增广路(KM算法) HDOJ 1853 Cyclic Tour
    最大流增广路(KM算法) HDOJ 1533 Going Home
    最大流增广路(KM算法) HDOJ 2255 奔小康赚大钱
    Complete the Word CodeForces
    Gadgets for dollars and pounds CodeForces
    Vasya and Basketball CodeForces
    Carries SCU
  • 原文地址:https://www.cnblogs.com/izhoujie/p/12877074.html
Copyright © 2011-2022 走看看