Author Archives: wudi

Emacs – EShell mode to dired mode

M-x shell and M-x eshell are two major mode we use daily.

however, in the shell mode, it is not easy to switch to dired mode.

but in M-x eshell mode, command “dired .” will open dired mode for current path

1

Leetcode – Count of Smaller Numbers After Self

Here is leetcode’s new hard level question:
https://leetcode.com/problems/count-of-smaller-numbers-after-self/

The basic idea is using two for to loop though to counter smaller number on its left.
Solution1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public List<Integer> countSmaller(int[] nums) {
List<Integer> result = new ArrayList<>();
 
result.add(0);
 
for(int i = nums.length - 2;i>=0;i--)
{
    int target = nums[i];
    int count = 0;
    for(int j=i+1;j<=nums.length-1;j++)
    {
        if(nums[j]<=target)
            count++;
    }
    result.add(count);
}
 
Collections.reverse(result);
 
return result;
}

Solution 2:

Solution 1 is using two loops to go though to compare.

This solution is faster the second loop using tree.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
 
package leetcodetest;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
/**
 *
 * @author wudi
 */
public class CountSmaller2 {
 
    public class xTreenode
    {
        public xTreenode left = null;
        public xTreenode right = null;
        int count = 1;
 
        int val;
 
        public xTreenode(int x)
        {
            val = x;
        }
 
    }
 
    public int add(xTreenode root,int x)
    {
        int count = 0;
 
        while(true)
        {
            if(x <= root.val)
            {
                root.count++;
 
                if(root.left == null){
                    root.left = new xTreenode(x);
                    break;
                }
 
                root = root.left;
            }
            else
            {
                count += root.count;
                if(root.right == null)
                {
                    root.right = new xTreenode(x);
                    break;
                }
                root = root.right;
            }
        }
 
        return count;
    }
 
    public List<Integer> countSmaller(int[] nums) {
        List<Integer> result = new ArrayList<>();
        if(nums == null || nums.length == 0) 
            return result;
        xTreenode root = new xTreenode(nums[nums.length - 1]);
        result.add(0);
 
        for(int i=nums.length - 2;i>=0;i--)
        {
            int count = add(root,nums[i]);
            result.add(count);
        }
 
        Collections.reverse(result);
 
        return result;
    }
     public static void main(String[] args) {
         int[] nums = {2,0,1};
 
        CountSmaller2 c = new CountSmaller2();
        System.out.println(c.countSmaller(nums));
     }
}

google chart api for marker pin

In our project on google map, we have to display some custom marker pins use png.

google api provide great function for this purposes.
Here is a example URL
https://chart.googleapis.com/chart?chst=d_map_spin&chld=0.3|0|78909A|13|b|
you could change scale 0.3 to any size with fill in color 78909A,
font size 13 is for font to put into pin text

click on the url to view excellent markers.

radgrid

var grid = $find(“<%=RadGrid1.ClientID %>“);
var ctrl_hiddenfield = $telerik.findControl(grid.get_element(),”hiddenfield_name”);

http://docs.telerik.com/devtools/aspnet-ajax/controls/listbox/client-side-programming/overview

libtar and its api

libtar is small but effective library, although it has some bugs when dealing with long path.

The Steps are following:
download libtar-1.2.11 and uncompress it, the main path is /path/libtar-1.2.11

build with command “./configure && make”, if you want to debug, add “-g” for gcc to build.

make “test” folder “/path/libtar-1.2.11/test”. find header file and libtar.a file

put 1.c in “/path/libtar-1.2.11/test”

so finally, the build command will be “g++ 1.c -o 1 ../lib/libtar.a -I ../bin/libtar-1.2.11/include”

some important code, for example, char* subfile1 = “./sub1/1.txt”
this is for file under sub folder.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 #include <tar.h>  
 #include <libtar.h>  
 #include <iostream>                                                             
 #include <fcntl.h>    
 
 using namespace std;                               
 
 int main(){                                                        
     // tar                         
     if(1){                                                          
         TAR* tar_handle;
         char* tar_fname = "test.tar";
         tar_open(&amp;tar_handle, tar_fname, NULL,  O_WRONLY | O_CREAT,  0644,  TAR_GNU);
         char* subfile1 = "./sub1/1.txt";
         tar_append_file(tar_handle, subfile1,  subfile1);
         char* subfile2 = "test1.c";
         tar_append_file(tar_handle, subfile2,  subfile2);
         tar_close(tar_handle);
     }
 
     // untar
     if(0){
         TAR* tar_handle;
         char* tar_fname = "test.tar";
         tar_open(&amp;tar_handle, tar_fname, NULL,  O_RDONLY,  0644,  TAR_GNU);
         char* savefold = "temp";
         tar_extract_all(tar_handle, "temp");
         tar_close(tar_handle);
     }
 
     return 0;
 }

example of jquery bxslider

Here is a full example of how to use bxslider to show image slider.

bxslider is also very good at fade in/out transition in div

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<html>
  <title></title>
  <head>
    <!-- jQuery library (served from Google) -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<!-- bxSlider Javascript file -->
<script src="jquery.bxslider.min.js"></script>
<!-- bxSlider CSS file -->
<link href="jquery.bxslider.css" rel="stylesheet" />
 
<script language="javascript">
$(document).ready(function(){
$('.bxslider').bxSlider({
  mode: 'fade',
  captions: true
});
});
</script>
  </head>
 
  <body>
    <ul class="bxslider">
  <li><img src="pic1.jpg" /></li>
  <li><img src="pic2.jpg" /></li>
  <li><img src="pic3.jpg" /></li>
</ul>
  </body>
</html>

rdesktop , RDP for linux

use rdesktop to connect to windows desktop

currently reading rdesktop source code to see RDP protocol. It is excellent stable in linux to control windows remotely.

1
   $ rdesktop -g 80% -x l ip-address &

-g 80% means the window of rdesktop will not pop up with full screen but with 80% of full screen.

Fedora 20 ethernet

p1p1 is Fedora 20 default ethernet interface name.

Comparing to traditional “eth0”, p1p1 is very different.

Mac address of p1p1 is 00:00:00:00:00 by default. The driver is not problem actually, however, I don’t know why Mac address is 00.

ifconfig p1p1 hw ether 02:01:02:03:04:08

ifconfig p1p1 up

 

then interface p1p1 will up and get ip address from dhcp server automatically.