0%

Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.

**Example:**For num = 5, you should return [0,1,1,2,1,2].

题目

给定一个非负整数num,计算出从0到num的每个数的二进制中包含1的个数

方法

对于数字n,它二进制表示形式中1的个数bits[n] = bits[n>>1]+n&1

c代码

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
#include <assert.h>
#include <stdlib.h>

/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* countBits(int num, int* returnSize) {
    int i = 0;
    int* bits = (int *)malloc(sizeof(int) * (num+1));
    bits[0] = 0;
    for(i = 0; i <= num; i++) {
        bits[i] = bits[i>>1] + (i&1);
    }
    *returnSize = num+1;
    return bits;
}

int main() {
    int returnSize = 0;
    int* bits = countBits(5, &returnSize);
    assert(bits[0] == 0);
    assert(bits[1] == 1);
    assert(bits[2] == 1);
    assert(bits[3] == 2);
    assert(bits[4] == 1);
    assert(bits[5] == 2);
    assert(returnSize == 6);

    return 0;
}

Problem

Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution.
Example:

1
2
3
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

Solution

对数组排序后,第一个数字从数组开始遍历,二分查找满足条件的第二个数字,返回2个数字的位置。
由于排序导致数字位置发生了变换,因此需要一个数组记录变化后第i个数字之前的位置numsLocation[i]

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
#include <assert.h>
#include <stdlib.h>

int* sort(int* nums, int numsSize) {
int *numsLocations = (int *)malloc(sizeof(int) * numsSize);
int i = 0, j = 0;
for(i = 0; i < numsSize; i++)
numsLocations[i] = i;

for(i = 0; i < numsSize; i++) {
int sorted = 1;
for(j = 0; j < numsSize-1; j++) {
if(nums[j] > nums[j+1]) {
int temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
sorted = 0;
temp = numsLocations[j];
numsLocations[j] = numsLocations[j+1];
numsLocations[j+1] = temp;
}
}
if(sorted)
break;
}
return numsLocations;
}

int find(int* nums, int num, int start, int end) {
if(start > end)
return -1;
int i = (start+end)/2;
if(num > nums[i])
return find(nums, num, i+1, end);
else if(num < nums[i])
return find(nums, num, start, i-1);
return i;
}

/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target) {
int *numsLocations = sort(nums, numsSize);
int i = 0;
int num;
int *locations = (int *)malloc(sizeof(int) * 2);
int j;
for(i = 0; i < numsSize; i++) {
num = nums[i];
if((j=find(nums, target-num, i+1, numsSize-1)) >= 0) {
locations[1] = numsLocations[j];
locations[0] = numsLocations[i];
return locations;
}
}
return NULL;
}

int main() {
int nums[4] = {2, 7, 11, 15};
int *locations = twoSum(nums, 4, 9);
assert(locations[0] == 0);
assert(locations[1] == 1);

int nums2[3] = {5, 75, 25};
locations = twoSum(nums2, 3, 100);
assert(locations[0] == 2);
assert(locations[1] == 1);

return 0;
}

Markdown is a text-to-HTML conversion tool for web writers. Markdown allows you to write using an easy-to-read, easy-to-write plain text format, then convert it to structurally valid XHTML (or HTML).

用markdown写东西感觉很赞,方便简洁,给人带来一种想写东西的快感

基本语法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 一级标题
## 二级标题
...
###### 六级标题

> 这是一句引用

*斜体*
**粗体**
***斜粗体***

* 列表1
* 列表2
* 列表3

*** // 分割线

// 上标,下标
num^2^
num~i~

// Image
![Image](https://eazow.com/imgs/demo.jpg)
对应效果如下

一级标题

二级标题

六级标题

这是一句引用

  • 列表1
  • 列表2
  • 列表3

斜体
粗体
斜粗体

num^2^
numi


Demo

1. 创建仓库

在github上创建名称为username.github.io仓库

2. Clone

将仓库clone到本地

1
$ git clone https://github.com/username/username.github.io

3. Hello World

创建index.html

1
2
$ cd username.github.io
$ echo "Hello World" > index.html

4. push

提交代码并push

1
2
3
$ git add --all
$ git commit -m "Initial commit"
$ git push -u origin master

5. 验证

访问username.github.io,这样就能看到Hello World了

6. 域名映射

将域名映射到username.github.io

1
2
3
$ echo "www.yourdomain.com" > CNAME
$ git commit -m "Create CNAME"
$ git push origin master

在域名管理添加一条CNAME记录,将www.yourdomain.com指向username.github.io

7. 安装Hexo

7.1 什么是Hexo

Hexo is a fast, simple and powerful blog framework. You write posts in Markdown (or other languages) and Hexo generates static files with a beautiful theme in seconds.

7.2 安装Git
7.3 安装Node.js
1
2
$ wget -qO- https://raw.github.com/creationix/nvm/master/install.sh | sh
$ nvm install 4
7.4 安装Hexo
1
2
3
4
5
$ npm install -g hexo-cli
$ hexo init <folder>
$ cd <folder>
$ npm install
$ hexo server

这样就可以通过localhost:4000访问了

7.5 部署到github

安装hexo-deployer-git

1
$ npm install hexo-deployer-git --save

修改_config.yml

1
2
3
deploy:
type: git
repo: <repository url>

部署到github

1
$ hexo deploy

但是部署后会覆盖掉github上的CNAME和README.md,需要在hexo的source目录添加CNAME和README.md,但是执行hexo generate后README.md会生成README.html,因此需要配置下,不让其渲染README.md。
修改Hexo目录下的_config.yml

1
skip_render: README.md

然后运行

1
2
$ hexo g
$ hexo d

这样就部署到github上了,并且不会覆盖掉已写好的CNAME和README.md了。

7.6 写blog
1
$ hexo new "Markdown"

会在source/_posts/目录下生成Markdown.md文件,编辑该文件后

1
2
$ hexo g
$ hexo d

参考

https://github.io
https://hexo.io

折腾了差不多一、两天,终于通过虚拟机在Ubuntu下完成了通过编译内核,增加系统调用的实验,在此与大家分享一下,希望能够对看到的xdjm们或多或少地提供些帮助!

期间我换了几个内核版本,make了几次,浪费了大量的时间,主要还是对里面的文件不了解,找不到网上说的文件就很郁闷,甚至有些烦躁,最后换了个有同学成功的版本,终于坚持下来完成了!

其中make modules很费时间,而且用的是虚拟机,那就更慢了,估计2小时左右吧,当然这两小时也不闲着,外面阳光灿烂,出去好好的打了几次球 ^_^,劳逸结合!正当我打累了回来它差不多就好了,Ubuntu真给面子啊!

我用的 Ubuntu 7.10 ,编译的内核版本是2.6.23.12(更多版本可以到http://www.kernel.org去下载)

内核解压

将内核包解压到/usr/src下:

1
$ tar jvxf linux-2.6.23.12.tar.gz2

或者是

1
$ tar zxvf linux-2.6.23.12.tar.gz

增加系统调用

修改sys.c

1
2
$ cd /usr/src/linux-2.6.23.12
$ vim kernel/sys.c

添加一个简单的调用

1
2
3
4
asmlinkage int sys_mysyscall(int num) {
printk("hello");
return num;
}

修改系统调用表,syscall_table.S:

1
2
3
$ cd /usr/src/linux-2.6.23.12/arch/i386/kernel
//(没有i386文件夹的版本的貌似是进x86)
$ vim syscall_table.S

在最后加上

1
.long sys_mysyscall

增加新系统调用号,修改/usr/include/asm-i386/unisted.h, 添加

1
#define _ _NR_mysyscall 325

修改/usr/src/linux-2.6.23.12/include/asm-i386/unistd.h
添加#define __NR_mysyscall 325, 并将里面的#define NR_syscall 325改为326, 增加更多的系统调用依此类推

开始编译内核了

1
2
3
4
5
6
$ cd /usr/src/linux-2.6.23.12
$ make menuconfig
$ make bzImage
$ make modules
$ make modules_install
$ cp arch/i386/boot/bzImage /boot/vmlinuz-2.6.23.12 $ mkinitramfs -o initrd.img-2.6.23.12 2.6.23.12

/lib/modules会多出2.6.23.12这个文件夹

修改grub:

第5步后/boot 下会多出两个文件 vmlinuz-2.6.23.12 initrd.img-2.6.23.12
修改/boot/grub/menu.lst, 没有menu.lst可能会是grub.conf
仿照里面的格式

1
2
3
4
5
6
title Ubuntu 7.10, kernel 2.6.22-14-generic
root (hd0,0)
kernel /boot/vmlinuz-2.6.22-14-generic root=UUID=e2478f9d-7f5d-458f-b017-43458a8f62ea ro quiet
splash
initrd /boot/initrd.img-2.6.22-14-generic
quiet

添加

1
2
3
4
5
6
title Ubuntu 7.10, kernel 2.6.23-12
root (hd0,0)
kernel /boot/vmlinuz-2.6.23.12 root=UUID=e2478f9d-7f5d-458f-b017-43458a8f62ea ro quiet
splash
initrd /boot/initrd-2.6.23.12.img
quiet

将hiddenmenu注释掉 即#hiddenmenu

重启,导入新的内核,测试,新建test.c

1
2
3
4
5
6
7
8
9
10
11
12
#include <linux/unistd.h>
#include <stdio.h>
#include <asm/unistd.h>

#define __NR_mycall 325
#define __NR_myfilecopy 326 //增加的拷贝文件的系统调用

int main()
{
syscall(325, 6); //6为系统调用的参数,多参数的类推
return 1;
}

最后,预祝你们圆满成功!!