Description

Implement an algorithm to determine if a string has all unique characters.

Example

Given "abc", return true.

Given "aab", return false.

Challenge

What if you can not use additional data structures?

解题:题目要求判断字符串是否有重复的元素,并且要求最好不用其他的数据结构,比如集合什么的。这个题目如果用HashMap来做还是挺容易的,假如没有最后那个要求,我们可以怎么做。可以申请一个布尔型的HashMap,再将字符串中的字符当做key值一个一个放进哈希表里,放之前先判断有没有这个元素,如果有的话,说明有重复的元素,返回false。循环结束,则返回true。代码如下:

public class Solution {
/*
* @param str: A string
* @return: a boolean
*/
public boolean isUnique(String str) {
// write your code here
Map map=new HashMap();
for (int i = 0; i < str.length() ; i++){
char temp=str.charAt(i);
if(map.containsKey(temp))
return false;
else
map.put(temp,i);
}
return true;
}
}

如果不用hash表,只用数组也是可以实现的。不过只是针对在ASCII表范围内的字符串,也就是说输入中文字符是无效的。申请大小为256的数组,下标和ASCII表中的字符一一对应,思路与上面的差不多。代码如下:

public class Solution {
/*
* @param str: A string
* @return: a boolean
*/
public boolean isUnique(String str) {
// write your code here
boolean[]char_set=new boolean[256];
for(int i = 0; i < str.length(); i++){
int index = (int)str.charAt(i);
if(char_set[index])
return false;
char_set[index] = true;
}
return true;
}
}

157. Unique Characters 【LintCode by java】的更多相关文章

  1. 156. Merge Intervals【LintCode by java】

    Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...

  2. 212. Space Replacement【LintCode by java】

    Description Write a method to replace all spaces in a string with %20. The string is given in a char ...

  3. 158. Valid Anagram【LintCode by java】

    Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...

  4. 165. Merge Two Sorted Lists【LintCode by java】

    Description Merge two sorted (ascending) linked lists and return it as a new sorted list. The new so ...

  5. 177. Convert Sorted Array to Binary Search Tree With Minimal Height【LintCode by java】

    Description Given a sorted (increasing order) array, Convert it to create a binary tree with minimal ...

  6. 173. Insertion Sort List【LintCode by java】

    Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...

  7. 172. Remove Element【LintCode by java】

    Description Given an array and a value, remove all occurrences of that value in place and return the ...

  8. 30. Insert Interval【LintCode by java】

    Description Given a non-overlapping interval list which is sorted by start point. Insert a new inter ...

  9. 155. Minimum Depth of Binary Tree【LintCode by java】

    Description Given a binary tree, find its minimum depth. The minimum depth is the number of nodes al ...

随机推荐

  1. Jmeter--JDBC请求(sqlserver)

    做JDBC请求,首先要了解这个JDBC对象是什么,然后寻找响应的数据库连接URL和数据库驱动. 数据库URL:jdbc:sqlserver://200.99.197.190:1433;database ...

  2. 第28章 LTDC—液晶显示中英文

    本章参考资料:<STM32F76xxx参考手册>.<STM32F7xx规格书>.库帮助文档<STM32F779xx_User_Manual.chm>. 关于开发板配 ...

  3. C# 5.0中使用CallerMemberName、CallerFilePath和CallerLineNumber获取代码的调用方信息(转载)

    很多时候,我们需要在运行过程中记录一些调测的日志信息,如下所示: public void DoProcessing() { TraceMessage("DoProcessing()被XXX调 ...

  4. Winodws SNMP服务安装和配置(Windows 2003 & 2008 R2)

    简单网络管理协议SNMP服务起着代理的作用,它会收集可以向SNMP管理站或控制台报告的信息.您可以使用SNMP服务来收集数据,并且在整个公司网络范围内管理基于Windows Server 2003.M ...

  5. 给requests模块添加请求头列表和代理ip列表

    Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装,符合了Python语言的思想,通俗的说去繁存 ...

  6. Oracle 体系结构四 逻辑和物理存储结构之间的关系

    Oracle数据库从物理存储中完全抽象出逻辑存储.逻辑数据存储采用“段”的形式.段的类型有很多种:典型的段是“表”.这些段以物理形式存储在数据文件中.通过表空间将逻辑存储从物理存储中抽象出来.逻辑结构 ...

  7. 利用MyFlash闪回丢失数据(续)

          last night,i've tested flashback by MyFlash tool,but failed,now let's do some other test with ...

  8. vue vue-router 完美实现前进刷新,后退不刷新。附scrollBehavior源码解析

    需求:在一个vue的项目中,我们需要从一个列表页面点击列表中的某一个详情页面,从详情页面返回不刷新列表,而从列表的上一个页面重新进入列表页面则需要刷新列表. 而浏览器的机制则是每一次的页面打开都会重新 ...

  9. xp sp3安装.Net 4.0提示严重错误,0x80070643,解决办法2017版

    客户电脑上要装金税开票软件,需要.net 4.0.30319.1,电脑环境是xp sp3,已经安装了.net 2, .net 3.5sp1,安装.net 4.0的时候提示错误0x80070643 因为 ...

  10. 使用 win10 的库来组织自己的同类文件

    库相当于虚拟目录,可以把不同的文件夹包含起来. 找起东西来不用东奔西跑了...