VIM

一、打开文件、保存、关闭文件(vi命令模式下使用)
:w //保存文件
:w vpser.net //保存至vpser.net文件
:q //退出编辑器,如果文件已修改请使用下面的命令
:q! //退出编辑器,且不保存
:wq //退出编辑器,且保存文件

二、插入文本或行(vi命令模式下使用,执行下面命令后将进入插入模式,按ESC键可退出插入模式)
一般使用i
a //在当前光标位置的右边添加文本
i //在当前光标位置的左边添加文本
A //在当前行的末尾位置添加文本
I //在当前行的开始处添加文本(非空字符的行首)
O //在当前行的上面新建一行
o //在当前行的下面新建一行
R //替换(覆盖)当前光标位置及后面的若干文本
J //合并光标所在行及下一行为一行(依然在命令模式)

三、移动光标(vi命令模式下使用)
1、使用上下左右方向键

2、命令模式下:h 向左、j 向下 、k 向上、l 向右。
空格键 向右、Backspace 向左、Enter 移动到下一行首、- 移动到上一行首。

四、删除、恢复字符或行(vi命令模式下使用)
x //删除当前字符
nx //删除从光标开始的n个字符
dd //删除当前行
ndd //向下删除当前行在内的n行
u //撤销上一步操作
U //撤销对当前行的所有操作

五、搜索(vi命令模式下使用)
/vpser //向光标下搜索vpser字符串,vpser可以是正则表达式
?vpser //向光标上搜索vpser字符串
n //向下搜索前一个搜素动作
N //向上搜索前一个搜索动作

*(#) //当光标停留在某个单词上时, 输入这条命令表示查找与该单词匹配的下(上)一个单词. 同样, 再输入 n 查找下一个匹配处, 输入 N 反方向查找.

g*(g#) //此命令与上条命令相似, 只不过它不完全匹配光标所在处的单词, 而是匹配包含该单词的所有字符串.

六、跳至指定行(vi命令模式下使用)
n+ //向下跳n行
n- //向上跳n行
nG //跳到行号为n的行
G //跳至文件的底部

七、设置行号(vi命令模式下使用)
:set nu //显示行号
:set nonu //取消显示行号

八、复制、粘贴(vi命令模式下使用)
yy //将当前行复制到缓存区,也可以用 “ayy 复制,”a 为缓冲区,a也可以替换为a到z的任意字母,可以完成多个复制任务。
nyy //将当前行向下n行复制到缓冲区,也可以用 “anyy 复制,”a 为缓冲区,a也可以替换为a到z的任意字母,可以完成多个复制任务。
yw //复制从光标开始到词尾的字符。
nyw //复制从光标开始的n个单词。
y^ //复制从光标到行首的内容。 VPS侦探
y$ //复制从光标到行尾的内容。
p //粘贴剪切板里的内容在光标后,如果使用了前面的自定义缓冲区,建议使用”ap 进行粘贴。
P //粘贴剪切板里的内容在光标前,如果使用了前面的自定义缓冲区,建议使用”aP 进行粘贴。

九、替换(vi命令模式下使用)
:s/old/new //用new替换行中首次出现的old
:s/old/new/g //用new替换行中所有的old
:n,m s/old/new/g //用new替换从n到m行里所有的old
:%s/old/new/g //用new替换当前文件里所有的old

十、编辑其他文件
:e otherfilename //编辑文件名为otherfilename的文件。

十一、修改文件格式
:set fileformat=unix //将文件修改为unix格式,如win下面的文本文件在linux下会出现^M。

十二、查找帮助
当你不知道怎么处理时,直接输入help可以看到帮助文档的起点,ZZ是退出或者:q,不建议使用
查找关于某个字母的命令 :help x 查找关于x的命令。

参考:http://blog.csdn.net/chenxiaochen32/article/details/7378127

Leetcode-Palindrome Partitioning(Java)

Question:

Given a string s, partition s such that every substring of the partition is a palindrome.

Return all possible palindrome partitioning of s.

For example, given s = “aab”,
Return

[
  ["aa","b"],
  ["a","a","b"]
]

Thinking:

We should get all the possible partition, it’s obvious we should use backtracing. We partition the string from beginning, and check if it’s palindrome, then check the rest of the substring. If all the substrings are palindrome, we put them in the result. We have to notice the API of Java substring(beginIndex, endIndex) that beginIndex is inclusive but endIndex is exclusive.

Solution:

public List<List<String>> partition(String s) {
    List<List<String>> res = new ArrayList<List<String>>();
    int len = s.length();
    if (palindrome(s)){
        List<String> tempres = new ArrayList<String>();
        tempres.add(s);
        res.add(tempres);
    }
    for (int i = 1; i < len; i++){
        if (palindrome(s.substring(0, i))){
            for (List<String> list: partition(s.substring(i))){
                list.add(0, s.substring(0, i));
                res.add(list);
            }
        }
    }
    return res;
}

private boolean palindrome(String s){
    int len = s.length();
    int left = 0;
    int right = len - 1;
    while (left < right){
        if (s.charAt(left) != s.charAt(right)){
            return false;
        }
        left++;
        right--;
    }
    return true;
}

Leetcode-Binary Tree Zigzag Level Order Traversal(Java)

Question:

Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).

For example:
Given binary tree {3,9,20,#,#,15,7},

  3
 / \
9  20
  /  \
 15   7

return its zigzag level order traversal as:

[
  [3],
  [20,9],
  [15,7]
]

Thinking:

It’s a BFS problem. It’s fine if we need use queue, but because we have to record as a zigzag order, it’s more efficient to use stack. What’s more, we should to notice the order of left and right child.

Solution:

public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
    int flag = 0;
    List<List<Integer>> res = new ArrayList<List<Integer>>();
    Stack<TreeNode> stack = new Stack<TreeNode>();
    Stack<TreeNode> tempstack = new Stack<TreeNode>();
    if (root == null)
        return res;
    stack.add(root);
    while (!stack.isEmpty() || !tempstack.isEmpty()){
        tempstack = new Stack<TreeNode>();
        List<Integer> tempres = new ArrayList<Integer>();
        while (!stack.isEmpty()){
            TreeNode node = stack.pop();
            tempres.add(node.val);
            if (flag == 0){
                if (node.left != null){
                    tempstack.add(node.left);
                }
                if (node.right != null){
                    tempstack.add(node.right);
                }
            }
            else{
                if (node.right != null){
                    tempstack.add(node.right);
                }
                if (node.left != null){
                    tempstack.add(node.left);
                }
            }

        }
        res.add(tempres);
        stack.addAll(tempstack);
        flag ^= 1;
    }

    return res;
}

Leetcode-4Sum(Java)

Question:

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:
Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0.

A solution set is:
(-1,  0, 0, 1)
(-2, -1, 1, 2)
(-2,  0, 0, 2)

Thinking:

This questions is very similar with 2Sum and 3Sum. There are several way to solve it.

Solution1:

The most common method may use the method of 3Sum, and do that n times.

public List<List<Integer>> fourSum1(int[] nums, int target) {
    List<List<Integer>> res = new ArrayList<List<Integer>>();
    Set<List<Integer>> tres = new HashSet<List<Integer>>();
    Arrays.sort(nums);
    int len = nums.length;


    for (int i = 0; i < len-3; i++){
        for (int j = i+1; j < len-2; j++){
            for (int k = j+1; k < len-1; k++){
                int l = len-1;
                while (k < l){
                    int sum = nums[i] + nums[j] + nums[k] + nums[l];
                    if (sum == target){
                        List<Integer> tempres = new ArrayList<Integer>();
                        tempres.add(nums[i]);
                        tempres.add(nums[j]);
                        tempres.add(nums[k]);
                        tempres.add(nums[l]);
                        tres.add(tempres);
                        k++;
                        l--;
                    }
                    else if (sum > target){
                        l--;
                    }
                    else{
                        k++;
                    }
                    while (k < l && nums[k+1] == nums[k]){
                        k++;
                    }
                    while (k < l && nums[l-1] == nums[l]){
                        l--;
                    }
                }
            }
        }
    }

    res.addAll(tres);
    return res;
}

Solution2:

The second solution is to combine every two numbers as a pair, and make it as a 2sum problem. But for implementing this method, we have to define the data structure by ourself and implement the interface of Comparator to sort the sum of them.

public List<List<Integer>> fourSum2(int[] nums, int target){
    List<List<Integer>> res = new ArrayList<List<Integer>>();
    int len = nums.length;
    if (len < 4)
        return res;
    Pair[] pairSum = new Pair[len*(len-1)/2];
    Set<List<Integer>> tres = new HashSet<List<Integer>>();

    int count = 0;
    for (int i = 0; i < len-1; i++){
        for (int j = i+1; j < len; j++){
            Pair ele = new Pair(nums[i], nums[j], i, j);
            pairSum[count++] = ele;
        }
    }
    Arrays.sort(pairSum, new SumComparator());

    int left = 0;
    int right = pairSum.length - 1;
    while (left < right){
        int sum = pairSum[left].getSum() + pairSum[right].getSum();
        if (sum == target){
            int endl = left;
            int endr = right;
            while (pairSum[endl].getSum() == pairSum[endl+1].getSum() && endl+1 < endr){
                endl++;
            }
            while (pairSum[endr].getSum() == pairSum[endr-1].getSum() && endl < endr-1){
                endr--;
            }
            for (int i = left; i <= endl; i++){
                for (int j = right; j>= endr; j--){
                    int i1 = pairSum[i].geti();
                    int i2 = pairSum[j].geti();
                    int j1 = pairSum[i].getj();
                    int j2 = pairSum[j].getj();
                    if (i1 == i2 || i1 == j2 || j1 == i2 || j1 == j2){
                        continue;
                    }
                    int[] temp = new int[4];
                    temp[0] = pairSum[i].getN1();
                    temp[1] = pairSum[i].getN2();
                    temp[2] = pairSum[j].getN1();
                    temp[3] = pairSum[j].getN2();
                    Arrays.sort(temp);
                    List<Integer> tempres = new ArrayList<Integer>();
                    tempres.add(temp[0]);
                    tempres.add(temp[1]);
                    tempres.add(temp[2]);
                    tempres.add(temp[3]);
                    tres.add(tempres);
                }
            }
            left = endl+1;
            right = endr-1;
        }
        else if (sum > target){
            right--;
        }
        else{
            left++;
        }
    }

    res.addAll(tres);
    return res;
}

class SumComparator implements Comparator{

    @Override
    public final int compare(Object o1, Object o2) {
        // TODO Auto-generated method stub
        Pair l1 = (Pair)o1;
        Pair l2 = (Pair)o2;
        if (l1.getSum() > l2.getSum())
            return 1;
        else if (l1.getSum() < l2.getSum())
            return -1;
        else
            return 0;
    }
}

class Pair{
    int i;
    int j;
    int n1;
    int n2;
    int sum;
    Pair(int num1, int num2, int i, int j){
        this.n1 = num1;
        this.n2 = num2;
        this.sum = num1 + num2;
        this.i = i;
        this.j = j;
    }
    public int getSum(){
        return this.sum;
    }
    public int getN1(){
        return this.n1;
    }
    public int getN2(){
        return this.n2;
    }
    public int geti(){
        return this.i;
    }
    public int getj(){
        return this.j;
    }
}

Reference: http://blog.csdn.net/linhuanmars/article/details/24826871

Solution3:

The third method is the most efficient method. It has two loop of two points and use some method to break or return early. It really improve the performance of time running.

public List<List<Integer>> fourSum3(int[] nums, int target) {
    List<List<Integer>> list = new ArrayList<List<Integer>>();
    Arrays.sort(nums);
    int second = 0, third = 0, nexti = 0, nextj = 0;
    for(int i=0, L=nums.length; i<L-3; i++) {
        if(nums[i]<<2 > target) return list; // return immediately
        for(int j=L-1; j>i+2; j--) {
            if(nums[j]<<2 < target) break; // break immediately
            int rem = target-nums[i]-nums[j];
            int lo = i+1, hi=j-1;
            while(lo<hi) {
                int sum = nums[lo] + nums[hi];
                if(sum>rem) --hi;
                else if(sum<rem) ++lo;
                else {
                    list.add(Arrays.asList(nums[i],nums[lo],nums[hi],nums[j]));
                    while(++lo<=hi && nums[lo-1]==nums[lo]) continue; // avoid duplicate results
                    while(--hi>=lo && nums[hi]==nums[hi+1]) continue; // avoid duplicate results
                }
            }
            while(j>=1 && nums[j]==nums[j-1]) --j; // skip inner loop
        }
        while(i<L-1 && nums[i]==nums[i+1]) ++i; // skip outer loop
    }
    return list;
}

Reference: https://leetcode.com/discuss/78276/java-little-bit-faster-than-other-common-methods-9ms-beats-95%25

Leetcode-3Sum(Java)

Question:

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
The solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4},

A solution set is:
(-1, 0, 1)
(-1, -1, 2)

Thinking:

Sort the array first. Pick a number from the array, and use other two points to record the position of the array and determine which one should change by the sum of them. What’s more, the result set should has no duplicate, so we can use hashset here.

Solution:

public List<List<Integer>> threeSum(int[] nums) {
    int l = nums.length;
    List<List<Integer>> res = new ArrayList<List<Integer>>();
    Set<List<Integer>> tres = new HashSet<List<Integer>>();

    if (l < 3)
        return res;

    Arrays.sort(nums);

    for (int i = 0; i < l-2; i++){
        int k = l-1;
        for (int j = i+1; j < l; j++){
            while (j < k){
                int sum = nums[i] + nums[j] + nums[k];
                if (sum == 0){
                    List<Integer> tempres = new ArrayList<Integer>();
                    tempres.add(nums[i]);
                    tempres.add(nums[j]);
                    tempres.add(nums[k]);
                    tres.add(tempres);
                    while (j < k && nums[j] == tempres.get(1)){
                        j++;
                    }
                    k--;
                }
                else if (sum > 0){
                    k--;
                }
                else{
                    j++;
                }

            }
        }
    }
    res.addAll(tres);

    return res;
}