20. Valid Parentheses

class Solution {
    bool isValid(string s) {
        stack<char> stk;
        int len = s.length();
        if (len == 0) return true;
        if (len == 1) return false;
        for (int i = 0; i < len; ++i) {
            if (s[i] == '(' || s[i] == '{' || s[i] == '[') stk.push(s[i]);
            else {
                if (stk.empty()) return false;
                else {
                    char tmp =;
                    if ((s[i] == ')' && tmp == '(') || (s[i] == '}' && tmp == '{') || (s[i] == ']' && tmp == '[')) stk.pop();
                    else return false;
        if (stk.empty()) return true;
        return false;

Hash Table

1. Two Sum

class Solution {
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int, int> hashtable;
        for (int i = 0; i < nums.size(); ++i) {
            int res = target - nums[i];
            if (hashtable.count(res) == 0){
              hashtable[nums[i]] = i;  
            else {
                return {i, hashtable[res]};
        return {-1, -1};

219. Contains Duplicate II

class Solution {
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        unordered_map<int, int> hashmap;
        for (int i = 0; i < nums.size(); ++i) {
            if(hashmap.count(nums[i]) != 0) {
                int ind = i - hashmap[nums[i]];
                if (ind <= k) return true;
                else hashmap[nums[i]] = i;
            else hashmap[nums[i]] = i;
        return false;


PyCharm Related

I use PyCharm as my python IDE in Ubuntu. It was running fine, suddenly an error message come out:

Import Error: cannot open shared object file: No such file or directory

I have CUDA and cuDNN installed and working correctly, I also set the environment path both on system level(.profile, .bashrc) and in PyCharm (in Setting->Project Interpreter). I finally find the solution here and here, which is called “PyCharm environment different than command line”

I solved it by typing following command in PyCharm console

sudo ldconfig /usr/local/cuda/lib64


  • ctrl+l performs the same as clear
  • echo -e and \n start a new line
$ echo -e "Hello World\nand other things"
Hello World
and other things
  • Write text in file with/without (> / >>) overwrite
$ echo "Hello World" > output.txt
$ echo "Hello World" >> output.txt

Errors related to Python packages, installation, etc.

1. RuntimeError: module compiled against API version a but this version of numpy is 9
check numpy path and version

import numpy
print numpy.__path__
print numpy.__version__

In my case I have multiple numpy installed in /usr/lib/python2.7/dist-packages/numpy (version 1.8.2) and in /usr/local/lib/python2.7/dist-packages/numpy (version 1.10.4). By default, the system import the numpy from /usr/lib/python2.7. I just simply remove it through command apt-get

sudo apt-get remove python-numpy

Then import numpy, to check the path and version again.

Caffe Installation Guide

Refer to caffe official installation guide. I skipped some steps because I already have cuda installed from tensorflow installation guide. Thus, the first step for me is to install dependencies, followed by download caffe, install python dependencies, etc. everything works fine until compile caffe, after typing make all. There are some errors come out related to opencv.

CXX/LD -o .build_release/tools/convert_imageset.bin
.build_release/lib/ undefined reference to cv::imread(cv::String const&, int)' .build_release/lib/ undefined reference tocv::imencode(cv::String const&, cv::_InputArray const&, std::vector >&, std::vector > const&)'
.build_release/lib/ undefined reference to `cv::imdecode(cv::_InputArray const&, int)'
collect2: error: ld returned 1 exit status
make: *** [.build_release/tools/convert_imageset.bin] Error 1

I found the solution here.

add "opencv_imgcodecs" in Makefile.(LIBRARIES += glog gflags protobuf leveldb snappy \
lmdb boost_system hdf5_hl hdf5 m \
opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs)
If you input "make all",the problem is the same again.But if you delete all the file in build(rm -rf ./build/*) before "make all",you will success.I just success

Following sources are also very useful. I recommend read them before installation:

Second Annual Data Science Bowl Deep Learning Tutorial