LeetCode--No.205--Isomorphic Strings
来源:互联网 发布:centos服务器测速 编辑:程序博客网 时间:2024/03/29 13:55
Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
For example,
Given "egg"
, "add"
, return true.
Given "foo"
, "bar"
, return false.
Given "paper"
, "title"
, return true.
最开始的想法是不用map,一个一个比较下去,看看s相等的地方,t是不是都等,t相等的地方,s是不是都等。
果然超时了。
原来map的contains的时间,并没有我想象中的那么多。关于map的各项操作的时间复杂度,还需要更详细的了解。
这个算法是抄的555555。自己没能写粗来,桑心了。
public class Solution { public boolean isIsomorphic(String s, String t) { HashMap<Character, Character> map = new HashMap<>(); HashSet<Character> set = new HashSet<>(); for(int i = 0; i < s.length(); i++){ if (!map.containsKey(s.charAt(i))){ if(set.contains(t.charAt(i))) return false; else{ map.put(s.charAt(i), t.charAt(i)); set.add(t.charAt(i)); } } else{ if(map.get(s.charAt(i)) != t.charAt(i)) return false; } } return true; }}
昨天和今天都没能完成刷题任务T.T内心很是崩溃,滚去继续截图了T.T神马时候才能脱离这个怪圈,求射手座转运嘤嘤
0 0
- Leetcode NO.205 Isomorphic Strings
- LeetCode--No.205--Isomorphic Strings
- LeetCode 205 - Isomorphic Strings
- [LeetCode 205]Isomorphic Strings
- LeetCode 205-Isomorphic Strings
- leetcode 205: Isomorphic Strings
- leetcode 205 Isomorphic Strings
- LeetCode 205:Isomorphic Strings
- leetcode-205 Isomorphic Strings
- Isomorphic Strings - LeetCode 205
- Isomorphic Strings leetcode 205
- leetcode 205:Isomorphic Strings
- leetcode-205 Isomorphic Strings
- Isomorphic Strings(leetcode 205)
- leetcode[205]:Isomorphic Strings
- leetcode-205-Isomorphic Strings
- leetcode 205: Isomorphic Strings
- leetcode: (205) Isomorphic Strings
- ubuntu16.04 sogoupinyin No such key ‘Gtk/IMMOdule’
- [leetcode] 162. Find Peak Element
- oracle 中文乱码 和 sqlplus 格式输出
- Fill Countries in Python Basemap
- Maven Oracle jdbc驱动
- LeetCode--No.205--Isomorphic Strings
- iOS 友盟微博分享文字+图片+链接实现
- [leetcode] 279. Perfect Squares
- POJ3984 BFS 输出路径
- PhpStorm 注册码 PhpStorm10 PhpStorm9 PhpStorm8 JetBrains key
- android中view的两种更新机制
- poj1562 DFS/BFS连通块
- 基于NX的研发产品设计管理平台实现(十二)-图纸及文档管理
- 替换字符串