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. STL Vector使用

    http://blog.163.com/zhoumhan_0351/blog/static/399542272010225104536463 Vector 像一个快速的数组,其具有数组的快速索引方式. ...

  2. ubuntu server遇到的问题

    1.在呢用is把隐藏的文件显示出来: ls -a 就可以啦 2.vim退出: 在命令模式中,连按两次大写字母Z,若当前编辑的文档曾被修改过,则Vi保存该文档后退出,返回到shell:若当前编辑的文档没 ...

  3. oracle 12如何解锁账户锁定状态及修改忘记的密码

    有两种方法,大同小异吧,感觉命令真是个好东西,哈哈哈哈,挽救了我安了4次才安好的oracle!!! 方法一: 1.如果忘记密码,找到忘记密码的是哪个用户身份,如果用户被锁定,可以使用下面说的方法解除锁 ...

  4. CSS实现图片等比例缩小不变形

    <img src="../images/bg1.jpg" alt="" /> img { /*等宽缩小不变形*/ /*width: 100%;*/ ...

  5. linux ping命令实践

          ping 解析       Linux系统的ping命令是常用的网络命令,它通常用来检测与目标主机的连通性,经常说"ping以下机器,看是否开着,不能打开网页时候,可以ping ...

  6. 【mongodb用户和身份认证管理】

    admin系统库用户管理 #移除 #查看 特定的数据库用户管理 #添加 #修改密码

  7. web开发问题汇总

    Meta基础知识: H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 //一.HTML页面结构 <meta name="viewport" content="wi ...

  8. 支付宝支付示例-python

    项目演示: 1.输入金额 ​ 2.扫码支付: ​ 3.支付完成: ​ ​ 具体操作步骤: 第一步:注册账号 https://openhome.alipay.com/platform/appDaily. ...

  9. STM32 硬件UART接收超时检测设置

    STM32 硬件UART接收超时检测设置 -----------------本文作者"智御电子",期待与电子爱好者交流学习.---------------- 应用场景 在uart应 ...

  10. Chip-seq peak annontation

    Chip-seq peak annontation Chip-seq peak annontation PeRl narrowPeak/boardPeak narrowPeak/boardPeak 是 ...