zoukankan      html  css  js  c++  java
  • 205 Isomorphic Strings 同构字符串

    给定两个字符串 s 和 t,判断它们是否是同构的。
    如果 s 中的字符可以被替换最终变成 t ,则两个字符串是同构的。
    所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。
    例如,
    给定 "egg", "add", 返回 true.
    给定 "foo", "bar", 返回 false.
    给定 "paper", "title", 返回 true.

    详见:https://leetcode.com/problems/isomorphic-strings/description/

    Java实现:

    class Solution {
        public boolean isIsomorphic(String s, String t) {
            if(s.length()!=t.length()){
                return false;
            }
            int[] hash1=new int[256];
            int[] hash2=new int[256];
            for(int i=0;i<s.length();++i){
                if(hash1[s.charAt(i)]!=hash2[t.charAt(i)]){
                    return false;
                }
                hash1[s.charAt(i)]=i+1;
                hash2[t.charAt(i)]=i+1;
            }
            return true;
        }
    }
    

    C++实现:

    class Solution {
    public:
        bool isIsomorphic(string s, string t) {
            int m1[256]={0},m2[256]={0};
            for(int i=0;i<s.size();++i)
            {
                if(m1[s[i]]!=m2[t[i]])
                {
                    return false;
                }
                m1[s[i]]=i+1;
                m2[t[i]]=i+1;
            }
            return true;
        }
    };
    

     参考:https://www.cnblogs.com/grandyang/p/4465779.html

  • 相关阅读:
    docker 容器卷及提交
    docker 容器命令及解析
    docker镜像常用命令及解析
    drf 中集成swgger api功能文档
    drf 二次封装Response
    drf 中 自定义 异常处理方法
    drf 中自定义登录 以及token验证
    drf_vue对接极验验证
    django 信号的使用
    element plut tree renderContent
  • 原文地址:https://www.cnblogs.com/xidian2014/p/8746484.html
Copyright © 2011-2022 走看看