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:
-
i(insert) I(insert at the beginning of the line)
-
a(append) A(append after line)
-
o(open a line blow) O(Append a line above)
-
Esc: back to normal mode.
-
:wq Save and quit
-
Linux cat: see the content in the file
-
:set nu Set line number
Normal Mode:
Split Screen
:vs vertical split
:sp horizontal split
:q quit screen
Replace
:% s/java/python/g Replace all “java” with “python”
Search
/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:
- d: to delete
- y: copy
- p: paste
Insert Mode:
- ctrl + h delete one character before the cursor.
- ctrl + w delete one word before.
- ctrl + u delete the line before.
These commands can be used in any terminal as well.
Useful terminal commands:
- Ctrl + a move to the beginning of the line.
- Ctrl + e move to the end of the line.
- Ctrl + b move backwards.
- Ctrl + f move forward.
Alternative for ESC
- From insert to normal: Ctrl + c or Ctrl + [
- From normal to the last place to insert: gi
Moving
- hjkl: left, down, up, right
- w/W: move to the next word beginning.
- e/E: move to the next word ending.
- b/B: move to the last word beginning.
- ( move to the last sentence.
- ) move to the next sentence.
- { move to the last paragraph.
- } move to the next paragraph.
- gg move to the beginning of the file.
- G move to the end of the file.
- Ctrl + o go back.
- H go to the head
- M go to the middle of the page.
- L go to the lower of this page.
- Ctrl + u upward page.
- Ctrl + f forward page.
- zz set the current cursor in the middle of the screen.
Here, Capital letter means only considering space as spliter.
- { move to the last empty line.
- } move to the next empty line.
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