`
chaostan
  • 浏览: 28303 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

CareerTop Question 1

 
阅读更多

Data Structures - Arrays and Strings:

Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?

My Original Solution:

        private static boolean uniqueStrByHushTable(String str) {
		boolean flag = false;
		char[] chars = str.toCharArray();
		HashSet<Character> hashSet = new HashSet<Character>();
		for(int i = 0; i < chars.length; i ++) {
			hashSet.add(chars[i]);
		}
		if(hashSet.size()==chars.length) {
			flag = true;
		}
		return flag;
	}

 

Book original solution:

 

	private static boolean uniqueStrByASCII(String str) {
		boolean[] char_val = new boolean[256];
		for (int i = 0; i < str.length(); i++) {
			int val = str.charAt(i);
			if (char_val[val]) {
				return false;
			}
			char_val[val] = true;
		}
		return true;
	}

 

Book original solution 2:

 

private static boolean isUniqueStr(String str) {
		int checker = 0;
		for (int i = 0; i < str.length(); i++) {
			int val = str.charAt(i) - 'a';
			if ((checker & (i << val)) > 0) {
				return false;
			}
			checker |= (1 << val);
		}
		return true;
	}

 

Have no idea about how to check unique string without any data structure.

分享到:
评论

相关推荐

    Java练习题Question1.txt

    Java练习题Question1.txtJava练习题Question1.txtJava练习题Question1.txtJava练习题Question1.txtJava练习题Question1.txtJava练习题Question1.txtJava练习题Question1.txtJava练习题Question1.txtJava练习题...

    python练习题Question1.txt

    python练习题Question1.txtpython练习题Question1.txtpython练习题Question1.txtpython练习题Question1.txtpython练习题Question1.txtpython练习题Question1.txtpython练习题Question1.txtpython练习题Question1.txt...

    dw-question-answer wordpres插件

    1. **PHP编程**: DW Question & Answer 是用PHP编写的,与WordPress的PHP环境兼容。 2. **MySQL数据库**: 数据存储在MySQL数据库中,用于存储用户信息、问题、答案等数据。 3. **AJAX技术**: 使用AJAX实现页面无刷新...

    QuestionOne.java

    QuestionOne.java

    网申和open question

    1. **摸清题目套路**:了解常见的Open Question类型,例如申请原因、事例描述、个人兴趣等,针对性地准备答案。 2. **STAR法则**:在回答问题时,运用情境(Situation)、任务(Task)、行动(Action)和结果...

    Question Behind Question

    【QBQ】,全称"The Question Behind The Question",是一个由John G. Miller提出的概念,旨在促进个人责任感和自我改进的思维方式。这个理念的核心在于,我们应该关注并提出那些能够激发积极行动的问题,而不是那些...

    Java练习题Question3.txt

    Java练习题Question3.txtJava练习题Question3.txtJava练习题Question3.txtJava练习题Question3.txtJava练习题Question3.txtJava练习题Question3.txtJava练习题Question3.txtJava练习题Question3.txtJava练习题...

    Java练习题Question8.txt

    Java练习题Question8.txtJava练习题Question8.txtJava练习题Question8.txtJava练习题Question8.txtJava练习题Question8.txtJava练习题Question8.txtJava练习题Question8.txtJava练习题Question8.txtJava练习题...

    Java练习题Question6.txt

    Java练习题Question6.txtJava练习题Question6.txtJava练习题Question6.txtJava练习题Question6.txtJava练习题Question6.txtJava练习题Question6.txtJava练习题Question6.txtJava练习题Question6.txtJava练习题...

    Java练习题Question9.txt

    Java练习题Question9.txtJava练习题Question9.txtJava练习题Question9.txtJava练习题Question9.txtJava练习题Question9.txtJava练习题Question9.txtJava练习题Question9.txtJava练习题Question9.txtJava练习题...

    Java练习题Question10.txt

    Java练习题Question10.txtJava练习题Question10.txtJava练习题Question10.txtJava练习题Question10.txtJava练习题Question10.txtJava练习题Question10.txtJava练习题Question10.txtJava练习题Question10.txtJava练习...

    Java练习题Question2.txt

    Java练习题Question2.txtJava练习题Question2.txtJava练习题Question2.txtJava练习题Question2.txtJava练习题Question2.txtJava练习题Question2.txtJava练习题Question2.txtJava练习题Question2.txtJava练习题...

    Java练习题Question7.txt

    Java练习题Question7.txtJava练习题Question7.txtJava练习题Question7.txtJava练习题Question7.txtJava练习题Question7.txtJava练习题Question7.txtJava练习题Question7.txtJava练习题Question7.txtJava练习题...

    Java练习题Question5.txt

    Java练习题Question5.txtJava练习题Question5.txtJava练习题Question5.txtJava练习题Question5.txtJava练习题Question5.txtJava练习题Question5.txtJava练习题Question5.txtJava练习题Question5.txtJava练习题...

    Java练习题Question4.txt

    Java练习题Question4.txtJava练习题Question4.txtJava练习题Question4.txtJava练习题Question4.txtJava练习题Question4.txtJava练习题Question4.txtJava练习题Question4.txtJava练习题Question4.txtJava练习题...

    标准的quora question pairs数据集

    如果该标签为1,则意味着“question1”和“question2”表达的是相同或近乎相同的意思;如果为0,则表示它们是不同的问题。这个问题设置对于构建能有效检测重复内容的算法至关重要,特别是在大型问答社区如Quora中,...

    python练习题Question2.txt

    python练习题Question2.txtpython练习题Question2.txtpython练习题Question2.txtpython练习题Question2.txtpython练习题Question2.txtpython练习题Question2.txtpython练习题Question2.txtpython练习题Question2.txt...

    python练习题Question8.txt

    python练习题Question8.txtpython练习题Question8.txtpython练习题Question8.txtpython练习题Question8.txtpython练习题Question8.txtpython练习题Question8.txtpython练习题Question8.txtpython练习题Question8.txt...

    python练习题Question5.txt

    python练习题Question5.txtpython练习题Question5.txtpython练习题Question5.txtpython练习题Question5.txtpython练习题Question5.txtpython练习题Question5.txtpython练习题Question5.txtpython练习题Question5.txt...

    python练习题Question11.txt

    python练习题Question11.txtpython练习题Question11.txtpython练习题Question11.txtpython练习题Question11.txtpython练习题Question11.txtpython练习题Question11.txtpython练习题Question11.txtpython练习题...

Global site tag (gtag.js) - Google Analytics