205. Isomorphic Strings*
来源:互联网 发布:网络暴力 英语 编辑:程序博客网 时间:2024/05/18 09:12
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.
public boolean isIsomorphic(String s, String t) { if(s.length()!=t.length()) return false; HashMap<Character, Character> map = new HashMap<Character, Character>(); for(int i = 0;i<s.length();i++){ char a = s.charAt(i); char b = t.charAt(i); if(map.containsKey(a)){ if(map.get(a).equals(b)) continue; else return false; } else{ if(!map.containsValue(b)) map.put(a,b); else return false; } } return true; }总结:用了hashmap
public boolean isIsomorphic(String s, String t) { int [] m = new int[512]; for(int i=0;i<s.length();i++){ if(m[s.charAt(i)] != m[t.charAt(i)+256]) return false; m[s.charAt(i)] = m[t.charAt(i)+256] =i+1; } return true; }直接数组索引,归根结底看上一组出现的是不是同一个数字。还是不要用put
0 0
- 205. Isomorphic Strings
- 【LeetCode】205.Isomorphic Strings
- [leetcode] 205.Isomorphic Strings
- LeetCode 205. Isomorphic Strings
- 205. Isomorphic Strings
- 205. Isomorphic Strings LeetCode
- 205. Isomorphic Strings
- 205. Isomorphic Strings
- 205. Isomorphic Strings
- 205. Isomorphic Strings
- 205. Isomorphic Strings
- 205. Isomorphic Strings
- 205. Isomorphic Strings
- 【LeetCode】205. Isomorphic Strings
- LeetCode *** 205. Isomorphic Strings
- leetcode 205. Isomorphic Strings
- LeetCode 205. Isomorphic Strings
- 205. Isomorphic Strings
- 8张图理解Java
- Hibernate相关配置文件内容详解
- unity 绘制Bezier曲线
- 《learning laravel》翻译第一章-----关于本书
- 解决报错NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
- 205. Isomorphic Strings*
- 关键字及变量的理解
- x265入门学习
- 疯狂JAVA讲义学习笔记5——面向对象2
- windows10下python机器学习开发环境搭建
- 工厂方法模式
- Java对象初始化顺序
- mexw32文件删除异常处理
- Grunt的安装与使用 (以Windows 64位为例)