Jerry's Blog

Recording what I learned everyday

View on GitHub


1 September 2019

Vim(1)

by Jerry Zhang

LeetCode Day 34: P205. Isomorphic Strings (Easy)

题目:

两个字符串,s和t,判断它们是否同态,一一映射。

Input: s = "egg", t = "add"
Output: true
Input: s = "foo", t = "bar"
Output: false

我的思路:

我原以为只有小写字母,所以就想,不用Hashmap, 通过ASCII码天然自带的映射关系,判定它们是否出现过,如果出现过,断定 它们是否是正确的对应字母。

遇到的一个问题就是需要判定两遍,分别用s和t作为数组的索引,另一个作为判定的字符。

class Solution {
    public boolean isIsomorphic(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }
        char[] arr_s = new char[127];
        for (int i = 0; i < s.length(); i++) {
            char current_t = arr_s[s.charAt(i)];
            if ( current_t == 0) {
                arr_s[s.charAt(i)] = t.charAt(i);
            } else if (current_t != t.charAt(i)){
                return false;
            }
        }

        char[] arr_t = new char[127];
        for (int i = 0; i < t.length(); i++) {
            char current_s = arr_t[t.charAt(i)];
            if ( current_s == 0) {
                arr_t[t.charAt(i)] = s.charAt(i);
            } else if (current_s != s.charAt(i)){
                return false;
            }
        }
        return true;
    }
}

结果还算可以,87.5%

最优解

class Solution {
    public boolean isIsomorphic(String s, String t) {

        if (s == null || s.isEmpty())
            return (t == null || t.isEmpty());

        if (s.length() != t.length())
            return false;

        char[] s1 = s.toCharArray();
        char[] t1 = t.toCharArray();

        int length = s.length();

        char[] sm = new char[256];
        char[] tm = new char[256];

        for (int i = 0; i < length; i++) {
            char sc = s1[i];
            char tc = t1[i];

            /**
             * Have u seen sc and tc before?
             */
            if (sm[sc] == 0 && tm[tc] == 0) {
                /**
                 * If not, create a mapping from sc -> tc
                 */
                sm[sc] = tc;
                tm[tc] = sc;
            } else {
                /**
                 * If seen, then they must be same mapping
                 */
                if (sm[sc] != tc || tm[tc] != sc) {
                    return false;
                }
            }
        }
        return true;
    }
}

看完代码,感觉完全像我的升级版,思路比我清楚多了,而且一趟跑完。

Vim

Edit Mode:

Normal Mode:

Split Screen

:vs vertical split

:sp horizontal split

:q quit screen

Replace

:% s/java/python/g Replace all “java” with “python”

/java search “java”

Visual Mode

In normal mode, press “v” to change to Visual Mode

V: select current line

ctrl + v: block selection

After selection:

Insert Mode:

These commands can be used in any terminal as well.

Useful terminal commands:

Alternative for ESC

Moving

Here, Capital letter means only considering space as spliter.

Searching

f{char} move to the character. ; next , previous F before t{char} move the the character before. F{char} find the character backwards.

Moving to beginning of a line or end of a line

0 move to the first character of a line. ^ move to the first non-empty character of a line. $ move to the end of a line. g_ move to the last non-empty character of a line.

tags: Vim