2018年4月2日星期一

Bash 的基本语法规则

1. 函数传参

#!/bin/bash

test_function() {
    echo $1 $2
}
test_function "hello" "world"

Result:
~$ ./example.sh 
hello world


2. 分割字符串

#!/bin/bash

LINE="test_name,test_address,test_phone_number"
name=$(echo $LINE | cut -d ',' -f 1)
address=$(echo $LINE | cut -d ',' -f 2)
phone_number=$(echo $LINE | cut -d ',' -f 3)
echo $name
echo $address
echo $phone_number

Result:
~$ ./example.sh 
test_name
test_address
test_phone_number


3. 读取文件

#!/bin/bash

i=0
SPACE=" "
while read line
do
    echo $i $SPACE $line
    i=$((i+1))
done < test.txt

Result:
~$ cat test.txt
hahaha
haha
ha
~$ ./example.sh 
0 hahaha
1 haha
2 ha


4. 正则表达式匹配

#!/bin/bash

LINE="This is a test string. Let's see if the UUID will be matched: ea77b80d-d17f-476a-b55f-916423d02dcc. Hahaha!"
uuid=$(echo $LINE | grep -E -o '[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}')
echo $uuid

Result:
~$ ./example.sh 
ea77b80d-d17f-476a-b55f-916423d02dcc











2018年4月1日星期日

Hazelcast 学习笔记

~$ ls /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home


(3) Exception in thread "main" com.hazelcast.license.exception.InvalidLicenseException: License Key not configured!

(4) Replication vs. partitioning

(5) Most common use case: being a cache on top of a database

(6) Hazelcast has two modes: embedded mode and client-server mode

2017年11月29日星期三

About memory leak

(WIP)
* jvisualvm
** ssh -X
* jmap
* example of memory leak: https://dzone.com/articles/memory-leak-andjava-code

2017年9月27日星期三

Trie Implementation

 public class Trie {  
      static final int ALPHABET_SIZE = 26;  
      static class TrieNode {  
           TrieNode[] children = new TrieNode[ALPHABET_SIZE];  
           boolean isEndOfWord;  
           TrieNode() {  
                isEndOfWord = false;  
                for (int i = 0; i < ALPHABET_SIZE; i++) {  
                     children[i] = null;  
                }  
           }  
      }  
      static TrieNode root;  
      // if not parent, inserts key into trie  
      // if the key is prefix of trie node, just mark leaf node  
      static void insert(String key) {  
           int level;  
           int length = key.length();  
           int index;  
           TrieNode pCrawl = root;  
           for (level = 0; level < length; level++) {  
                index = key.charAt(level) - 'a';  
                if (pCrawl.children[index] == null) {  
                     pCrawl.children[index] = new TrieNode();  
                }  
                pCrawl = pCrawl.children[index];  
           }  
           // mark the last node as leaf  
           pCrawl.isEndOfWord = true;  
      }  
      // Returns true if key presnts in trie, else false  
      static boolean search(String key) {  
           int level;  
           int length = key.length();  
           int index;  
           TrieNode pCrawl = root;  
           for (level = 0; level < length; level++) {  
                index = key.charAt(level) - 'a';  
                if (pCrawl.children[index] == null) {  
                     return false;  
                }  
                pCrawl = pCrawl.children[index];  
           }  
           return pCrawl != null && pCrawl.isEndOfWord;  
      }   
 }  

2016年10月8日星期六

[LeetCode] #200 Number of Islands

public class Solution {
    public int numIslands(char[][] grid) {
        int result = 0;
        if (grid == null || grid.length == 0 || grid[0].length == 0) {
            return result;
        }
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == '1') {
                    result++;
                    dfs(grid, i, j);
                }
            }
        }
        return result;
    }
    
    private void dfs(char[][] grid, int i, int j) {
        if (grid[i][j] != '1') {
            return;
        }
        
        grid[i][j] = '2';
        if (i - 1 >= 0) {
            dfs(grid, i - 1, j);
        }
        if (j - 1 >= 0) {
            dfs(grid, i, j - 1);
        }
        if (i + 1 < grid.length) {
            dfs(grid, i + 1, j);
        }
        if (j + 1 < grid[0].length) {
            dfs(grid, i, j + 1);
        }
    }
}

2016年9月27日星期二

[LeetCode] 287. Find the Duplicate Number

1. Imagine the array is a linked list. Take the value of each element as a pointer to next element. if there is a duplicate element, there must be a loop in the linked list. Use fast, slow pointer to find the loop. O(n)
2. BitMap - TODO

public class Solution {
    public int findDuplicate(int[] nums) {
        if (nums == null || nums.length == 0) {
            return -1;
        }
        
        int slow = nums[0];
        int fast = nums[nums[0]];
        
        while (slow != fast) {
            slow = nums[slow];
            fast = nums[nums[fast]];
        }
        
        fast = 0;
        while (slow != fast) {
            slow = nums[slow];
            fast = nums[fast];
        }
        
        return slow;
    }
}

2016年9月26日星期一

[LeetCode] 289. Game of Life

(1) Add a new 2D array to hold the new state - O(n ^ 2) space
(2) Bit operation - in place

public class Solution {
    public void gameOfLife(int[][] board) {
        if (board == null || board.length == 0 || board[0].length == 0) {
            return;
        }
        int[][] newState = new int[board.length][board[0].length];
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                board[i][j] = isLive(i, j, board);
            }
        }
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                board[i][j] = board[i][j] >> 1;
            }
        }
    }
    
    private int isLive(int i, int j, int[][] board) {
        int liveNeighbor = 0;
        for (int m = i - 1; m < board.length && m <= i + 1; m++) {
            for (int n = j - 1; n < board[0].length && n <= j + 1; n++) {
                if (m < 0 || n < 0 || (m == i && n == j)) {
                    continue;
                }
                if ((board[m][n] & 0x0001) == 1) {
                    liveNeighbor++;
                }
            }
        }
        if (liveNeighbor <= 1 || liveNeighbor >= 4) {
            return board[i][j];
        } 
        if (liveNeighbor == 3) {
            return (board[i][j] | 2);
        }
        return (board[i][j] | (board[i][j] << 1));
    }
}