摘要:用做,十分簡單。如果不使用之外的數據結構的話,可以用來做。不過只能過純字母字符的。
Problem
Implement an algorithm to determine if a string has all unique characters.
ExampleGiven "abc", return true.
Given "aab", return false.
ChallengeWhat if you can not use additional data structures?
Note用HashSet做,十分簡單。
如果不使用String之外的數據結構的話,可以用bit manipulation來做。不過只能過純字母字符的testcase。
public class Solution { public boolean isUnique(String str) { HashSetset = new HashSet (); for (int i = 0; i < str.length(); i++) { if (!set.contains(str.charAt(i))) { set.add(str.charAt(i)); } else return false; } return true; } }
public class Solution { public boolean isUnique(String s) { int checker = 0; for (int i = 0; i < s.length(); i++) { int cur = s.charAt(i) - "a"; if ((checker & (1 << cur)) > 0) { return false; } checker |= 1 << cur; } return true; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/65694.html
Problem Given a string source and a string target, find the minimum window in source which will contain all the characters in target. Notice If there is no such window in source that covers all charac...
Problem Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with an...
摘要:建立一個長度為的數組,每一位對應那個字母出現的個數,先遍歷,對數組做增操作,再遍歷,對數組做減操作。 Problem Compare two strings A and B, determine whether A contains all of the characters in B. The characters in string A and B are all Upper Ca...
摘要:和完全一樣的做法,只要在初始化首行和首列遇到時置零且即可。對了,數組其它元素遇到也要置零喏,不過就不要啦。 Problem Follow up for Unique Paths: Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle...
Problem Given a string, find the first non-repeating character in it and return its index. If it doesnt exist, return -1. Example Given s = lintcode, return 0. Given s = lovelintcode, return 2. Tags A...
閱讀 1626·2021-10-14 09:43
閱讀 5503·2021-09-07 10:21
閱讀 1275·2019-08-30 15:56
閱讀 2123·2019-08-30 15:53
閱讀 1231·2019-08-30 15:44
閱讀 2010·2019-08-30 15:44
閱讀 1320·2019-08-29 17:24
閱讀 752·2019-08-29 15:19