Cracking the coding interview--Q1.1
原文:
Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?
译文:
实现一个算法来判断一个字符串中的字符是否唯一(即没有重复).不能使用额外的数据结构。 (即只使用基本的数据结构)
解答:
假设题目中提到的字符是包含在ASCII中的字符的话,那么最多是256个。所以可以用一个256长的数组来标记,或者用一个8个32位的int值来标记。
如果题目中没有规定只使用基本的数据结构的话,用BitSet来做也很方便的。
public class Main {
public static boolean isUnique1(String str) {
boolean [] flag = new boolean[256];
for(int i = 0; i < 256; i++) {
flag[i] = false;
}
int len = str.length();
for(int i = 0; i < len; i++) {
int index = (int)str.charAt(i);
if(flag[index])
return false;
flag[index] = true;
}
return true;
} public static boolean isUnique2(String str) {
int [] flag = new int[8];
int len = str.length();
for(int i = 0; i < len; i++) {
int v = (int)str.charAt(i);
int index= v/32;
int offset = v%32;
if((flag[index] & (1 << offset)) == 1)
return false;
flag[index] |= (1 << offset);
}
return true;
} public static void main(String args[]) {
String s1 = "i am hawstein.";
String s2 = "abcdefghijklmnopqrstuvwxyzABCD1234567890";
System.out.println(isUnique1(s1) + " " + isUnique1(s2));
System.out.println(isUnique2(s1) + " " + isUnique2(s2));
}
}
Cracking the coding interview--Q1.1的更多相关文章
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)
#include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...
随机推荐
- BZOJ3301: [USACO2011 Feb] Cow Line
3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 67 Solved: 39[Submit ...
- WinForm ListControl MouseWheel Envent
最近在使用Listbox开发程序的时候, 遇到MouseWheel event 异常. 设置如下: 1. DrawModel:ownerDrawVariable. 2. InternalHeight: ...
- LeetCode-Word LadderII
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 形形色色的软件生命周期模型(4)——MSF、实用型
摘要: 读大学时,我们曾经学习过不少软件生命周期模型,当时还不是很懂软件开发,你可能会觉得这些东西很新奇.在实际工作中,你会发现这些模型其实很难应用,与此同时你会接触到RUP.MSF等权威软件公司的生 ...
- Oracle Spool详解
转自:http://blog.sina.com.cn/s/blog_6bccf0360101hzsh.html 1.spool的作用是什么? spool的作用可以用一句话来描述:在sqlplus中用来 ...
- iOS 时区问题总结 NSTimeZone
基本概念 GMT 0:00 格林威治标准时间; UTC +00:00 校准的全球时间; CCD +08:00 中国标准时间 [来自百度百科] 夏时制,英文"DaylightSavingTim ...
- 【Android】还原“微信”apk中的“发现”和“我”两个模块
先下载一个微信apk,以压缩包的形式打开,对比微信界面,找出我们需要的素材. 以下两个模块的还原完全采用RelativeLayout相对布局. 按钮效果的实现 点击对应版块,将有点击效果.这可以通过修 ...
- JSTL配合正则表达式在JSP中的应用
<%@ page language="java" import="java.util.*,cn.com.Person" pageEncoding=&quo ...
- Nuget找不到服务器
Nuget的新地址 http://nuget-prod-v2gallery.trafficmanager.net/api/v2/
- Android SDK Manager 无法更新SDK
Android SDK Manager 被墙后无法更新SDK 下载sdk时抛出错误:Failed to fetch URL http://dl-ssl.google.com/ 參考例如以下博客: ht ...