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. TextView selector 在LinearLayout中获取焦点问题

    通常需要修改textview被选中时文字颜色,总是没效果,有以下几种方式可以实现: <?xml version="1.0" encoding="utf-8" ...

  2. 关于sharepoint如何做SSO,如何做OOS监视编辑

    应客户需求,需要做sharepoint SSO,以前都是默认的AD验证,如果客户已经有一套SSO系统,验证过SSO之后就能自动登录,而不是浏览器上设置保存用户名密码的AD登陆. 怎么做呢? 首先sha ...

  3. Enable directory listing on Nginx Web Server

    1:Test environment [root@linux-node1 ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core ...

  4. tornado用户指引(四)------tornado协程使用和原理(三)

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/happyAnger6/article/details/51291221几种常用的协程方式: 1.回调 ...

  5. Flask之Flask实例有哪些参数

    常用的参数应用实例 from flask import Flask, render_template, url_for, session, request, redirect app = Flask( ...

  6. Centos7 安装ipython 和 ipython3

    [root@localhost ~]# wget https://pypi.python.org/packages/79/63/b671fc2bf0051739e87a7478a207bbeb45cf ...

  7. Mac进度条卡在100%

  8. python神坑系列之深浅拷贝

    深浅拷贝 1.注意在拷贝中只有使用了.deepcopy方法才能进行深度拷贝!其余的一律是浅拷贝 #深拷贝import copy lst = copy.deepcopy(lst1)  浅拷贝: 拷贝的是 ...

  9. 用kubeadm构建k8s集群部署

    一.环境 三台centos机器 二.软件及容器准备 1.安装docker环境 本例安装 docker-ce版本,repo源为docker-ce.repo文件,拷贝到 /etc/yum.repos.d下 ...

  10. [POJ1014]Dividing(二进制优化多重背包)

    #include <cstdio> #include <algorithm> #include <cstring> using namespace std; int ...