代码面试题:Delete duplicated char in string
来源:互联网 发布:淘宝联名卡怎么申请 编辑:程序博客网 时间:2024/06/06 02:51
Input: "nanananaa! baaaaatmaan! batman!"
Output: "na! btm"
使用set的做法:时间复杂度O(n)
public String deleteDuplicate(String A) { if (A == null || A.length() <= 1) { return A; } char str[] = A.toCharArray(); LinkedHashSet<Character> hs = new LinkedHashSet<Character>(); for (char a : str) { hs.add(a); } StringBuilder sb = new StringBuilder(); for (Character c : hs) { sb.append(c); } return sb.toString(); }
不使用另外的数据结构的做法:时间复杂度O(n^2)
public String deleteDuplicate(String A) { if (A == null || A.length() <= 1) { return A; } String result = ""; for (int i = 0; i < A.length(); i++) { if (!result.contains("" + A.charAt(i))) { result += A.charAt(i); } } return result; }
0 0
- 代码面试题:Delete duplicated char in string
- remove duplicated chars in a string
- 代码面试题:Find the first element in a given string A that also appears in another given string B
- How to remove the duplicated value in a String array
- 关于int,String,char的一道面试题
- 代码面试题:Count ones in binary
- 删除链表中重复的结点 Delete duplicated nodes in linked list
- leetcode面试题1:Reverse Words in a String
- java面试题:string,stringbuffer详解(通过代码说明)
- 代码面试题:Rotate string by k element
- string 管理 char 数组, 摆脱new delete
- Delete numbers in a string
- String类面试题
- String面试题
- c string 面试题
- String相关面试题
- Java String面试题
- String 的面试题
- libcurl教程
- Android开发之旅一重启app程序
- eclipse 使用PMD预先检测错误
- 插入排序
- iOS 之适配iOS 8备忘录1
- 代码面试题:Delete duplicated char in string
- 【Multitasking】Executing a Finite-Length Task in the Background
- NFC模组,开发NFC功能 只要几条指令的事情
- 详细剖析Struts工作流程
- 控件安装和卸载(vc)
- 多节点的ntp 时间同步方法
- yarn
- 为什么要使用SLF4J而不是Log4J
- 程序员一周花多少时间编程?