157. Unique Characters 【LintCode by java】
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】的更多相关文章
- 156. Merge Intervals【LintCode by java】
Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...
- 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 ...
- 158. Valid Anagram【LintCode by java】
Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...
- 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 ...
- 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 ...
- 173. Insertion Sort List【LintCode by java】
Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...
- 172. Remove Element【LintCode by java】
Description Given an array and a value, remove all occurrences of that value in place and return the ...
- 30. Insert Interval【LintCode by java】
Description Given a non-overlapping interval list which is sorted by start point. Insert a new inter ...
- 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 ...
随机推荐
- 友盟分享——Android App接入微信开放平台注意事项
一.Android第三方应用接入微信开放平台的注意事项: 1. 到微信开放平台官网申请正式的AppID(需通过审核),要填写包名.app签名的md5值.至于如何获取app签名信息,官方提供签名包apk ...
- Google Performance工具,你还不会用?Git走起。
2018俄罗斯世界杯如火如荼的进行中,第一轮各种冷门,让大家的确大跌眼界,尤其是那些买球的同志们,慌得一笔,还敢继续买吗?话说来,看球归看球,学习还是不能落下,我们来学习Chrome Devtool ...
- one or more listeners failed to start问题解决思路
今日搭建一个web应用的时候总是遇到tomcat报错:one or more listeners failed to start. Full detail balabale....而且还没有其他提示, ...
- OpenID Connect Core 1.0(三)验证
OpenID Connect执行终端用户登录或确定终端用户已经登录的验证工作.OpenID Connect 使服务器以一种安全的方式返回验证结果.所以客户可以依靠它.出于这个原因,在这种情况下客户被称 ...
- 轻量ORM-SqlRepoEx (六) JOIN
示例使用的是最新 SqlRepoEx 2.0.2 可在:https://github.com/AzThinker/SqlRepoEx2.0Demo 或:https://gitee.com/azthin ...
- DQL-排序查询
三:排序查询 语法: select 列名 from 表名 where 筛选条件 order by 需要排序的列名 asc/desc 特点:不写升序还是降序,默认升序 排序列表 可以是 ...
- JavaScript面向对象(封装)
阮一峰的网络日志 1.生成实例对象的原始模式 var People = { name : '', age : '' } 根据这个原型对象生成两个实例对象 var people1 = {}; peopl ...
- Pycharm常用的快捷键
常用快捷键: Ctrl + D 复制选定的区域或行 Ctrl + Y 删除选定的行 Ctrl + Alt + L 代码格式化 Ct ...
- Sass变量及嵌套
1. 变量:SASS允许使用变量,所有变量以$开头. 变量声明:$highlight-color: #000; 注意:变量可以在css规则块定义之外存在.如下例子: $nav-color: #F90; ...
- 使用Letsencrypt做SSL certificate
为什么要使用Letsencrypt做SSL certificate? 最简单直接的原因是免费.但是免费存在是否靠谱的问题,尤其是对安全要求比较高的网站,需要考虑使用letsencrypt的安全性是否符 ...