1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures.
2015-09-16 15:33
603 查看
思路: 假设给定字符串用的是ASCII编码,那么总共就只有256个字符,新建一个256个元素的boolean数组, 遍历字符串,将出现的字符在boolean数组所在位置置 1。如果碰到已经置一,表明出现重复字符,返回false。
优化:如果字符串长度超过256,肯定出现过重复字符了。这是只要直接返回 false 就行
2015-09-16
public class IsUniqueChars_1 { public boolean solu(String s) { boolean table[] = new boolean[256]; for (int i = 0; i < s.length(); i++) { int ascii = (int) s.charAt(i); if (table[ascii]) return false; else table[ascii] = true; } return true; } }
优化:如果字符串长度超过256,肯定出现过重复字符了。这是只要直接返回 false 就行
public class IsUniqueChars_1 { public boolean solu(String s) { if (s.length() > 256) return false; boolean table[] = new boolean[256]; for (int i = 0; i < s.length(); i++) { int ascii = (int) s.charAt(i); if (table[ascii]) return false; else table[ascii] = true; } return true; } }
2015-09-16
相关文章推荐
- UI--普通控件总结1--控件使用
- 可排序的 COMB 类型 GUID
- different/Unique/distinct Binary Search Trees
- iOS实现UIImageView透明区域点击事件穿透
- [ios]自定义UI
- UITableView主要知识框架
- Fuel 7.0介绍
- #import <Cocoa/Cocoa.h> 报错 Lexical or Preprocessor Issue 'Cocoa/Cocoa.h' file not found
- ios中UIActivityIndicatorView使用
- MySql 启动报错:The server quit without updating PID file 如何解决
- Selenium IDE-自动化ui测试插件安装及使用
- php统计数组中各项的个数用函数array_count_values
- uva 11324 The Largest Clique(强连通分量 + DAG)
- systemtap gui
- Windows管理多个java版本--解决'has value '1.8',but'1.7' is required'的方法
- UISearchbar 常用代理方法
- 【转】使用segue页面间传递数据
- Java集合类: Set、List、Map、Queue使用场景梳理
- WPF:如何在工作线程中更新窗体的UI元素(Dispatcher机制)
- 解决PKIX path building failed的问题