插入排序、归并排序、快速排序等各类排序

插入排序

 

int insertion_sort(int a[])
{
  int j, t;
  for (int i=2; i<=N; ++i)
  {
    j = i;
    while (j > 1 && a[j] < a[j - 1])
    {
        t = a[j];
        a[j] = a[j-1];
        a[j-1] = t;
        j = j-1; 
    }
  }
}

 

链表相关算法题、编程题、面试题

如何实现单链表逆转,输入为一个头节点,输出一个头节点。

 

ListNote *listReverse(ListNote *head)
{
    ListNote *p, *q, *r;
    p = head;
    q = p->next;
    if (q) r = q->next;
    else r = NULL;
    if (head || head->next)
    {
        while (r)
        {
            q->next = p;
            p = q;
            q = r;
            r = r->next;
        }
        q->next = p;
        head->next = NULL;
    }
    return q;
}

 

如何去除链表中重复的元素,不许使用额外空间?

 

ListNote *removeDupList(ListNote *head)
{
    //q: the current node, p: the running node, r: the previous node
    ListNote *p, *q, *r;
    q = head->next;
    r = head;
    bool flag;
    while (q)
    {
        p = head;
        flag = false;
        while (p != q)
        {
            if (p->data == q->data)
            {
                flag = true;
                break;
            }
            p = p->next;
        }
        if (flag)
        {
            free(r->next);
            r->next = q->next;
        }
        else
        {
            r = r->next;
        }
        q = q->next;
    }
}

 

字符串、数组相关编程题面试题

 

Implement an algorithm to determine if a string has all unique characters What if you can not use additional data structures? 

如何判断字符串中是否有重复的字符?

bool isUniqueChar(string s)
{
    bool h[256]={0};
    for (int i=0; i<=s.size()-1; ++i)
    {
        if (h[s[i]]) return false;
        h[s[i]] = 1;
    }
    return true;
}

 

 

如何将C的字符数组(char *)逆转?

Write code to reverse a C-Style String (C-String means that “abcd” is represented as five characters, including the null character ) 

 

void cStringReverse(char *s)
{
    char *end = s;
    char tmp;
    if (s)
    {
        while (*end) ++end;
        --end;
        while (s < end)
        {
            *s++ = *end--;
        }

    }
}

 

如何将字符数组重复的字符去除?不允许使用额外的存储空间。

Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the array is not.

 

void removeDuplicateChar(char *s)
{
    char *end = s+1;
    char *p = s+1;
    if (s)
    {
        while (*p)
        {
            char *q = s;
            while (q < p)
            {
                if (*p == *q)
                {
                    break;
                }
                ++q;
            }
            if (p == q)
            {
                *end = *p;
                ++end;
            }
            ++p;
        }
        *end = '\0';
    }
}

 

Elance(外国版猪八戒)网上兼职 新手入门

 

Elance 是全球最大的外包网站之一。给我的印象是门槛高,服务好,项目多,项目质量好。比起国内的猪八戒之类的网站好不知多少倍。如果你是程序员、翻译、设计师等,同时英文又好,那一定要到Elance上来接项目,这样才能充分体现你的优势嘛。因为Elance是个国际平台,上面的价格是国际价格,换算成人民币怎么都划算啊。

注册

Elance的注册是需要验证电话号码的。注册完了之后,Elance的机器人会打电话过来,叫你把网页上的数字用英文念出来或者是输入进去。通过验证之后就算注册成功了。可以开始接项目了。

PS. 因为Elance涉及到钱,所以对帐号保护比较严,一定要记得你的密保答案啊,因为下次你换个地方登陆,它就会让你输入答案了。。。

包装自己

新手要顺利接到项目比较难,因为没有历史项目经历和别人给的评分。所以要靠自己profile上的自我简介,profilio和skill tests等来打动客户了。其中比较特别的是skill tests。Elance使用了第三方测试平台来衡量测试者的各种技能。在Elance网站上选择想要测试的技能,比如PHP5 Code Test,然后会进入一个第三方测试网站。一般选择类题目是40题,时间是40分钟。PHP5 Code Test属于代码实践题,需要测试者根据题目提供一段PHP程序,然后后台执行这段程序看看是否正确。

测试的结果是按你在测试者中占的百分比来的,而且貌似只有top 1%,5%,10%,20%,30%(上次我做了个JS测试的top 2%,结果给我显示5%,冤屈啊。。。),其他的就用图形来显示你的测试成绩和平均测试成绩。免费会员可以选择5个测试成绩显示在自己的profile上。如果升级成付费会员则可以显示更多的成绩。如果某个测试你觉得不满意,没关系,14天之后可以再次做这个测试(题目大部分都一样的哦)。这些成绩是很有说服力的。比如你刚注册,但是在Javascript测试中获得了top 5%的成绩,那么你在投标的时候就可以说 “我刚来Elance,但是我在前端方面有 X年的经验,不信你去看我的profile,我在Javascript测试中排前5%哦。”。然后报价再稍微比别人低一点,获得项目的机会就会大很多。

项目流程

Elance的项目主要分为固定价格的项目和按小时计费的项目。先说说fixed price project。

首先在网站上搜索自己感兴趣又力所能及的项目。 然后就是投标了。

投一个$500以下的项目需要1个Connect(Elance上的一种点数吧),$500-$1000的项目需要2个Connect ... $2000以上的貌似都是5个Connect。而且这个点数是不退的,也就是说对方发个项目,你去投,然后对方没选中你,甚至对方把项目关了,你都会一样损失这些点数。免费会员每月是15个点,在Billing Cycle Date的那一天点数会刷新。点数不够怎么办?交钱加会员的话每个月点数会增加,或者点下面这个链接注册成功并验证了手机之后会免费得到多10个Connect。

https://www.elance.com/?rid=2E6H1

这种点数的规则就限制了像猪八戒上那些胡乱杀价的人,所以Elance上看到的基本上都是正儿八经的人报的合理价格。

然后投标内容一般用正式的英文书信格式,例如:

Hi Alex,

I am very interested in your project. I ....

Regards,
Chunlong

而且内容一定要根据对方的项目需求来写。切记写一个模板到处去投标。比如对方想做个图片编辑器,你就应该说你以前做过类似的东西,然后把demo地址贴上去。这样对方会很感兴趣的。

这段时间有可能对方会跟你来回通信几次,一旦他确定给你做,就会把项目award给你,然后你在elance后台就会看到这个项目,里面有各种设置和功能。一般项目award给你之后,对方就会把项目款打到Elance,如果没打,你可以等对方打了钱再开始做。这种第三方担保类似支付宝,需要双方同意钱才能转移。此时,你就可以跟对方交流任何内容,比如Skype帐号(上面的人大部分都用Skype,因为必要时候可以视频通话)。

项目后台最常用的是message功能,可以收发消息,还可以带附件,每个消息还会forward到你的邮箱,甚至你可以用邮箱直接回复。另外还有一个是status reports,项目开始后记得用这个来向对方报告项目进度。Terms & Milestones是一个必须双方都同意的协议,里面涉及到项目的里程碑(可以在里程碑上协定支付部分项目款),项目截止日期,项目总价等。每个修改都需要双方同意之后才会生效。

当项目结束后,如果对方确认了你的成果。那么就可以把status reports里面设置为completed。并且提醒对方放款。

对于按小时付费的项目,大部分流程都是一样的。但是投标的时候的金额是每小时多少钱而不是项目总共多少钱。然后开始做之后,需要你下载一个Elance的Tracker软件(是一个Adobe AIR软件)。这个软件可以记录你的项目时间和屏幕截图。开始做项目的时候,进这个软件点击开始。然后就老老实实做这个项目,因为这个软件会不定期截屏上传的!万一被看到你在玩,那是有损声誉的事情啊。

 

 

其他类似网站

 

www.freelancer.com

国外最大的外包平台之一。任务比较多样,大多数是印度人在上面接活。

 

 

原文转自 http://php.js.cn/blog/elance-freelancer-abc/ 部分内容有改动

 

jQuery fadeIn、fadeOut无法在IE下正常显示解决办法

jQuery的fadeIn、fadeOut函数还蛮好使的,但是在IE系列下没有效果,研究了一下找到了解决办法。

例如$element是fadeIn or fadeOut的对象,那么可以在fadeIn/fadeOut函数内加上

if(jQuery.browser.msie) $element.get(0).style.removeAttribute('filter');

原因貌似是Ie实现overflow:hidden的时候是用滤镜设置成透明的,那么去掉滤镜就Ok了。

Ubuntu Linux下 安装配置 LAMP开发环境

这篇文章是看到过的Ubuntu Linux下配置Lamp环境介绍最好的一篇文章,故转载过来供大家参考

 

ubuntu Linux 安装配置 LAMP环境

 

PHP开发和服务器运行环境首选 LAMP 组合,即 Linux+Apache+MySQL+Php/Perl /Python,能最优化服务器性能。如何在本地电脑 Ubuntu 中安装和配置 LAMP环境? Ubuntu10.04本身就是基于 Linux内核,所以 Linux是现成的了。使用 Ubuntu LAMP Server软件包可以很简单地实现 Linux Apache MySQL  Php的统一安装和配置,也不再需要一个一个来安装配置了。

Ubuntu环境下如何安装 LAMP组件

1,使用 Ubuntu新立得管理器 
系统 ->系统管理 ->新立得软件包管理器 ->编辑 ->使用任务标记分组软件包 ->LAMP Server(勾选 )->确定 ->返回到上一个窗口点击应用 ( System->Administration->Synaptic Package Manager->Edit->Mark packages by Task->LAMP Server->OK)。然后系统会自动下载安装 lamp环境软件包,几分钟就下载搞定。安装过程中会要求设置 MySQL root帐号的密码,设置好了记住。另外当 Ubuntu系统升级时 lamp环境组件也会同时更新到最新版本。

安装完毕测试 :打开 Firefox浏览器在地址栏输入 http://127.0.0.1,显示 It works!表明 Apache服务器已经开始工作了, LAMP安装 也就这样完成了。

2,使用命令行

当然不使用 Gnome,使用终端命令也很简单: 
直接一条命令 : apt-get install apache2 mysql-server mysql-client php5 php5-gd php5-mysql

或者:

Install Apache

To start off we will install Apache.

1. Open up the Terminal (Applications > Accessories > Terminal).

2. Copy/Paste the following line of code into Terminal and then press enter:

sudo apt-get install apache2

3. The Terminal will then ask you for you're password, type it and then press enter.

 

Testing Apache

To make sure everything installed correctly we will now test Apache to ensure it is working properly.

1. Open up any web browser and then enter the following into the web address:

http://localhost/

You should see a folder entitled apache2-default/. Open it and you will see a message saying "It works!" , congrats to you!

 

Install PHP

In this part we will install PHP 5.

Step 1. Again open up the Terminal (Applications > Accessories > Terminal).

Step 2. Copy/Paste the following line into Terminal and press enter:

sudo apt-get install php5 libapache2-mod-php5

Step 3. In order for PHP to work and be compatible with Apache we must restart it. Type the following code in Terminal to do this:

sudo /etc/init.d/apache2 restart

 

Test PHP

To ensure there are no issues with PHP let's give it a quick test run.

Step 1. In the terminal copy/paste the following line:

sudo gedit /var/www/testphp.php

This will open up a file called phptest.php.

Step 2. Copy/Paste this line into the phptest file:

<?php phpinfo(); ?>

Step 3. Save and close the file.

Step 4. Now open you're web browser and type the following into the web address:

http://localhost/testphp.php

Install MySQL

To finish this guide up we will install MySQL. (Note - Out of Apache and PHP, MySQL is the most difficult to set up. I will provide some great resources for anyone having trouble at the end of this guide.)

Step 1. Once again open up the amazing Terminal and then copy/paste this line:

sudo apt-get install mysql-server

Step 2 (optional). In order for other computers on your network to view the server you have created, you must first edit the "Bind Address". Begin by opening up Terminal to edit the my.cnf file.

gksudo gedit /etc/mysql/my.cnf

Change the line

bind-address = 127.0.0.1

And change the 127.0.0.1 to your IP address.

Step 3. This is where things may start to get tricky. Begin by typing the following into Terminal:

mysql -u root

Following that copy/paste this line:

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('yourpassword');

(Make sure to change yourpassword to a password of your choice.)

Step 4. We are now going to install a program called phpMyAdmin which is an easy tool to edit your databases. Copy/paste the following line into Terminal:

sudo apt-get install libapache2-mod-auth-mysql php5-mysql phpmyadmin

After that is installed our next task is to get PHP to work with MySQL. To do this we will need to open a file entitled php.ini. To open it type the following:

gksudo gedit /etc/php5/apache2/php.ini

Now we are going to have to uncomment the following line by taking out the semicolon (;).

Change this line:

;extension=mysql.so

To look like this:

extension=mysql.so

Now just restart Apache and you are all set!

sudo /etc/init.d/apache2 restart

 

The End

Quick note to anyone who encountered problems with setting up the MySQL password, please refer to this page: MysqlPasswordReset

I applaud everyone who has taken the time to read this guide. This guide is also my first ever so I would love to hear back from the public on what you guys think! Just don't be too harsh. ;)

If you have questions about installing any part of LAMP just drop them in the comment box and I will do my best to help you out.

设置 Ubuntu文件执行读写权限

LAMP组建安装好之后, PHP网络服务器根目录默认设置是在: /var/www。由于 Linux系统的安全性原则,改 目录下的文件读写权限是只允许 root用户操作的,所以我们不能在 www文件夹中新建 php文件,也不能修改和删除,必须要先修改 /var/www目录的 读写权限。在界面管理器中通过右键属性不能修改文件权限,得执行 root终端命令: sudo chmod 777 /var/www 。然后就可以写入 html php文件了。

如何安装 phpmyadmin-Mysql 数据库管理

使用界面管理器 : 
系统 ->系统管理 ->新立得软件包管理器 ->搜索 phpmyadmin->右键标记安装。 
或直接使用一条命令: sudo apt-get install phpmyadmin 安装开始。

phpmyadmin设置  
在安装过程中会要求选择 Web server apache2 lighttpd,选择 apache2,按 tab键然后确定。然后会要求输入设置的 Mysql数据库密码连接密码 Password of the database’s administrative user 
然后将 phpmyadmin apache2建立连接,以我的为例: www目录在 /var/www phpmyadmin /usr/share /phpmyadmin目录,所以就用命令: sudo ln -s /usr/share/phpmyadmin /var/www 建立连接。

如果没有配置好可以用以下命令重新配置:sudo dpkg-reconfigure -plow phpmyadmin

phpmyadmin测试 :在浏览器地址栏中打开 http://localhost/phpmyadmin

可能会显然如下错误: Cannot load mcrypt extension. Please check your PHP configuration.

解决方法: sudo apt-get install php5-mcrypt

sudo /etc/init.d/apache2 restart

 

 

Ubuntu LAMP 如何配置 Apache

1. 启用 mod_rewrite 模块 
终端命令: sudo a2enmod rewrite
重启 Apache服务器: sudo /etc/init.d/apache2 restart

Apache重启后我们可以测试一下,在 /var/www目录下新建文件 test.php,写入代码:  <?php phpinfo(); ?> 保存,在地址栏输入 http://127.0.0.1/test.php 或 http://localhost/test.php ,如果正确出现了 php 配置信息则表明LAMP Apache已经正常工作了 (记得重启 Apache服务器后再测试 )

2.设置 Apache支持 .htm .html .php 
sudo gedit /etc/apache2/apache2.conf
 sudo gedit /etc/apache2/mods-enabled/php5.conf
在打开的文件中加上 
AddType application/x-httpd-php .php .htm .html 即可。

LAMP配置之 Mysql测试

上面 php,Apache 都已经测试过了,下面我们再测试一下 Mysql 数据库是否已经正确启用。

 /var/www目录下新建 mysql_test.php

<?php $link = mysql_connect("localhost","root","020511"); if (!$link) { die('Could not connect: ' . mysql_error()); } else echo "Mysql已经正确配置 "; mysql_close($link); ?>

保存退出,在地址栏输入 http://127.0.0.1/mysql_test.php,显示” Mysql 已经正确配置”则表示 OK了,如果不行,重启 Apache服务器后再试一下。

解决 Firefox浏览器显示中文乱码等问题

上面在 FireFox浏览器中打开 mysql_test.php phpmyadmin 测试时,如果出现了中文乱码,则是默认语言设置问题,解决方法如下:

打开 apache配置文件: udo gedit /etc/apache2/apache2.conf,在最后面加上: AddDefaultCharset UTF-8 ,如果还是乱码的,再将 UTF-8改用 gb2312 
重启 Apache sudo /etc/init.d/apache2 restart  再刷新 mysql_test.php 中文乱码没有了。

如果要人工启动 mysql mysql -u root -p,根据提示输入密码。 
如果重启 Apache时出现: 
* Restarting web server apache2
apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName
apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName

则还是修改 apache配置文件: sudo gedit /etc/apache2/apache2.conf,在文件最后设置: ServerName 127.0.0.1

LAMP组件经常使用的几个终端命令

重启 apache sudo /etc/init.d/apache2 restart

重启 mysql sudo /etc/init.d/mysql restart

配置 php.ini sudo gedit /etc/php5/apache2/php.ini

配置 apache2.conf sudo gedit /etc/apache2/apache2.conf

PHP CGI  sudo /var/www/cgi-bin/

Ubuntu PHP 编辑器

最后 LAMP配置 就完成了,在 Ubuntu下进行简单的 php代码编辑,用 Gedit就可以了。 Gedit支持 HTMLPHP Javascsript等近几十种语言的代码高亮功能。如果是 PHP项目开发,建议使用 PHP IDE编辑器,比如Zend Studio  Eclipse。据说文本编辑 VIM也很不错。

 

如果是 Windows XP 下要搭建 LAMP 环境,建议大家试试 xampp 快速安装配置法,使用也很方便快捷,32位Linux下也可以使用XAMPP,大家可以试试。

jQuery实现文字轮播效果

function linkMarquee(lh,speed,delay){
    var t;
    var p = false; 
    o=$('#link_slide');
    o.html(o.html()+o.html()); 
    o.hover(function(){p=true},function(){p=false});
    o.scrollTop(0);
    function start(){
        t=setInterval(scrolling,speed);
        if(!p){ o.scrollTop(o.scrollTop()+1);} 
    } 
    function scrolling(){ 
        if(o.scrollTop()%lh!=0){
            o.scrollTop(o.scrollTop()+1); 
            if(o.scrollTop()>=o[0].scrollHeight/2) o.scrollTop(0);
        }else{ 
            clearInterval(t);
            setTimeout(start,delay); 
        }
    }
    setTimeout(start,delay);
}

ink_slide是该div的id,<li></li>中放置每行轮播的文字,注意css中须设置 overflow:hidden;

图片的滚动效果可以参考这里 http://cssglobe.com/post/5780/easy-slider-17-numeric-navigation-jquery-slider

在Linux(Ubuntu)下搭建Android开发环境

 

1.安装Eclipse

在Ubuntu下直接到软件中心里搜一下,安装就好了,否则的话去Eclipse官网(http://www.eclipse.org/downloads/)下一个,选择Eclipse Classic 3.x.x。 

2.安装ADT插件

(1)打开Eclipse(打开的时候若提示JDK/JRE没装就去下载安装一下,网址是 http://www.oracle.com/technetwork/java/javase/downloads/index.html

(2)点 Help->Install New Software,在对话框右上方点Add,Name中输入Android Plugin,Location输https://dl-ssl.google.com/android/eclipse/,然后点OK,打上钩,一直点Next,直到Finish。

有其他问题的详见http://androidappdocs.appspot.com/sdk/eclipse-adt.html

 

3.安装SDK

(1)在这里http://dl.google.com/android/android-sdk_r07-linux_x86.tgz下载,解压到一个目录里(这个最后安完比较大,一共有1G多,要留足够空间)。

(2)到home/user目录下,即主文件夹,点查看->显示隐藏文件,找到一个.bashrc(或者是.bash_profile)的文件,点开(点不开的话下载一个编辑文本的软件),在末尾加上一句

export PATH=${PATH}:《你刚刚解压出来文件夹的位置》/tools

例如:export PATH=${PATH}:/home/user/soft/android-sdk-linux_x86/tools

(3)下载SDK平台,因为不是在Windows下,没有exe可以点开,那么就打开Eclipse,选择Window->Preferences->Android,把刚才那个目录设置好,点OK(如果弹出没有权限什么的不要理它),然后点Window->Android SDK and AVD Manager->Available Packages,打上钩,点Accept All安装,然后慢慢下载吧,下完之后就可以新建Android项目了。

快速排序Quick Sort

 从Pascal语言转化过来的快排代码

void Qsort(long l,long r)
{
  long i,j,x,y;
  i=l; j=r; x= a[(l+r)/2];
  do
  {
    while (a[i]<x) ++i;
    while (x<a[j]) --j;
    if (i<=j)
    {
      y=a[i]; a[i]=a[j]; a[j]=y;
      ++i; --j;
      }
    }
  while(i<=j);
  if (l<j) Qsort(l, j);
  if (i<r) Qsort(i, r);
}