【LeetCode】Isomorphic Strings 解题报告
来源:互联网 发布:c#图形编程 书籍 编辑:程序博客网 时间:2024/06/05 21:55
【LeetCode】Isomorphic Strings 解题报告
标签(空格分隔): LeetCode
题目地址:https://leetcode.com/problems/isomorphic-strings/#/description
题目描述:
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.
Note:
You may assume both s and t have the same length.
Ways
这个题可以看出来使用hashmap,但是,注意的一点是不要用位置++的方式,这样的话只统计了这个字符出现的次数,没有统计对应的位置,导致出错。比如aba
和baa
就会导致结果错误。因此,用到的是字符出现的位置+1的方式,保证能在hash位置处保存字符出现的次数。
public class Solution { public boolean isIsomorphic(String s, String t) { int[] m1 = new int[256]; int[] m2 = new int[256]; int len = s.length(); for(int i = 0; i < len; i++){ if(m1[s.charAt(i)] != m2[t.charAt(i)]){ return false; } m1[s.charAt(i)] = i + 1; m2[t.charAt(i)] = i + 1; } return true; }}
Date
2017 年 5 月 15 日
0 0
- LeetCode-Isomorphic Strings-解题报告
- 【LeetCode】Isomorphic Strings 解题报告
- [leetcode] 205. Isomorphic Strings 解题报告
- [Leetcode] 205. Isomorphic Strings 解题报告
- LeetCode---Isomorphic Strings、Word Pattern解题分析
- 【LeetCode】Multiply Strings 解题报告
- [LeetCode] Multiply Strings 解题报告
- 【LeetCode】Add Strings 解题报告
- LeetCode 205 - Isomorphic Strings
- [LeetCode] Isomorphic Strings
- Isomorphic Strings Leetcode JAVA
- [LeetCode] Isomorphic Strings
- [LeetCode 205]Isomorphic Strings
- LeetCode 205-Isomorphic Strings
- leetcode Isomorphic Strings
- leetcode(c++) Isomorphic Strings
- leetcode--Isomorphic Strings
- [LeetCode]Isomorphic Strings
- 关于0.3-0.2 != 0.1 的问题
- VNC远程连接服务器出现 A problem has occurred and the system can't recover. 错误
- 微信模板消息换行
- Android开发之RecyclerView添加头部和底部
- [HDU](2045)LELE的RPG难题
- 【LeetCode】Isomorphic Strings 解题报告
- Java从入门到精通_5 修饰符
- 招聘工资给高些
- Linux 连接脚本文件ld详解
- 单例模式
- 查看端口号的占用情况,如8080
- Android:背景选择器selector及shape详解
- [java-算法-数据结构] 判断栈的弹出顺序是否正确
- Matlab emd工具箱、时频分析工具箱下载以及安装方法