指针与引用的比较

1.初始化要求:引用在创建之初就必须初始化,而指针却没有这个要求 2.可修改性:引用只能进行一次,不能同时引用两个对象,即在初始时候已经决定了引用的对象;指针可以变换指向的对象 3.不存在NULL引用,存在NULL指针 4.测试时候:指针需要经常测试是否为空指针,而引用不需要,故引用的代码效率比较高    

Continue reading 指针与引用的比较 - 全文阅读

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • RSS
  • Slashdot
  • Technorati
  • TwitThis

比较Java中几种数据cache方式

现在很多网站一说到cache就想到memcached,memcached确实是非常优秀的系统,但是在某些场合,特别在不是分布式应用的场合,或者某些数据不需要分布式的访问,那它就未必是最佳的选择。

下面比较3种cache方式,用测试结果说话

Map,严格的说不能算cache,它适合本机访问,没有过期时间,不适合大容量,不能预计长度的数据,可能会使内存耗尽。 ehcache,可设过期时间,当超过指定内存数量,可设置淘汰算法,可输出到磁盘,可适合本机访问,也适合用作分布式缓存,分布式缓存配置和原理稍复杂,没有memcached直观,本测试未使用ehHcache分布式支持。 Memcached, 适合分布式缓存,可设过期时间

Continue reading 比较Java中几种数据cache方式 - 全文阅读

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • RSS
  • Slashdot
  • Technorati
  • TwitThis

python非贪婪,多行匹配正则表达式

一些regular的tips:

1 非贪婪flag

>>> re.findall(r”a(\d+?)”, “a23b”)
        ['2']
>>> re.findall(r”a(\d+)”, “a23b”)
        ['23']

注意比较这种情况:

>>> re.findall(r”a(\d+)b”, “a23b”)
        ['23']
>>> re.findall(r”a(\d+?)b”, “a23b”)
        ['23']

Continue reading python非贪婪,多行匹配正则表达式 - 全文阅读

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • RSS
  • Slashdot
  • Technorati
  • TwitThis

动态规划:背包问题

问题描述:有不同大小于价值的物品,现只有一个容积为M的背包来装物品。请找出一种最优组合是价值最大化。

递归实现:

int knap(int cap)
{
  int i, space, max, t;
  for (i = 0, max = 0; i < N; i++)
    if ((space = cap-items[i].size) >= 0)
      if ((t = knap(space) + items[i].val) > max)
    max = t;
  return max;
}

 

 

Continue reading 动态规划:背包问题 - 全文阅读

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • RSS
  • Slashdot
  • Technorati
  • TwitThis

Python程序员常用的IDE和其它工具

总是找python的IDE,在网上看到这个比较全,就转来了。 Python程序员常用的IDE和其它工具 1. 概述

“工欲善其事,必先利其器”,如果说编程是程序员的手艺,那么IDE就是程序员的吃饭家伙了。

IDE的全称是Integration Development Environment(集成开发环境),一般以代码编辑器为核心,包括一系列周边组件和附属功能。一个优秀的IDE,最重要的就是在普通文本编辑之外,提供针对特定语言的各种快捷编辑功能,让程序员尽可能快捷、舒适、清晰的浏览、输入、修改代码。对于一个现代的IDE来说,语法着色、错误提示、代码折叠、代码完成、代码块定位、重构,与调试器、版本控制系统(VCS)的集成等等都是重要的功能。以插件、扩展系统为代表的可定制框架,是现代IDE的另一个流行趋势。

 

Continue reading Python程序员常用的IDE和其它工具 - 全文阅读

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • RSS
  • Slashdot
  • Technorati
  • TwitThis

Built-in Functions

abs(x)
Return the absolute value of a number. The argument may be a plain or long integer or a floating point number. If the argument is a complex number, its magnitude is returned.解析:返回一个数字的绝对值。

>>> abs(1)
1
>>> abs(-1)
1
>>> abs(0)
0
>>> abs(-1.1)
1.1000000000000001
>>> abs(1.1)
1.1000000000000001
>>>

all(iterable)

Continue reading Built-in Functions - 全文阅读

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • RSS
  • Slashdot
  • Technorati
  • TwitThis

判断链表是否存在环并找出环的入口

有一个单链表,其中可能有一个环,也就是某个节点的next指向的是链表中在它之前的节点,这样在链表的尾部形成一环。

问题:

1、如何判断一个链表是不是这类链表?
如果链表为存在环,如果找到环的入口点?

解答:

一、判断链表是否存在环,办法为:

设置两个指针(fast, slow),初始值都指向头,slow每次前进一步,fast每次前进二步,如果链表存在环,则fast必定先进入环,而slow后进入环,两个指针必定相遇。(当然,fast先行头到尾部为NULL,则为无环链表)程序如下:

bool IsExitsLoop(slist *head)
{
    slist *slow = head, *fast = head;

    while ( fast && fast->next )
    {
        slow = slow->next;
        fast = fast->next->next;
        if ( slow == fast ) break;
    }

    return !(fast == NULL || fast->next == NULL);
}

 

二、找到环的入口点

当fast若与slow相遇时,slow肯定没有走遍历完链表,而fast已经在环内循环了n圈(1<=n)。假设slow走了s步,则fast走了2s步(fast步数还等于s 加上在环上多转的n圈),设环长为r,则:

2s = s + nr
s= nr

设整个链表长L,入口环与相遇点距离为x,起点到环入口点的距离为a。
a + x = nr
a + x = (n – 1)r +r = (n-1)r + L - a
a = (n-1)r + (L – a – x)

(L – a – x)为相遇点到环入口点的距离,由此可知,从链表头到环入口点等于(n-1)循环内环+相遇点到环入口点,于是我们从链表头、与相遇点分别设一个指针,每次各走一步,两个指针必定相遇,且相遇第一点为环入口点。程序描述如下:

slist* FindLoopPort(slist *head)
{
    slist *slow = head, *fast = head;

    while ( fast && fast->next )
    {
        slow = slow->next;
        fast = fast->next->next;
        if ( slow == fast ) break;
    }

    if (fast == NULL || fast->next == NULL)
        return NULL;

    slow = head;
    while (slow != fast)
    {
         slow = slow->next;
         fast = fast->next;
    }

    return slow;
}

 

扩展问题:

判断两个单链表是否相交,如果相交,给出相交的第一个点(两个链表都不存在环)。

比较好的方法有两个:

将其中一个链表首尾相连,检测另外一个链表是否存在环,如果存在,则两个链表相交,而检测出来的依赖环入口即为相交的第一个点。

如果两个链表相交,那个两个链表从相交点到链表结束都是相同的节点,我们可以先遍历一个链表,直到尾部,再遍历另外一个链表,如果也可以走到同样的结尾点,则两个链表相交。
这时我们记下两个链表length,再遍历一次,长链表节点先出发前进(lengthMax-lengthMin)步,之后两个链表同时前进,每次一步,相遇的第一点即为两个链表相交的第一个点。

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • RSS
  • Slashdot
  • Technorati
  • TwitThis

c++文件调用c头文件注意事项

要注意用extern “C” { } 括起来,不然容易出让你抓狂的问题或者你在c的头文件中定义 #ifdef __cplusplus
extern “C” { …….
 }
#else
…….
#endif

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • RSS
  • Slashdot
  • Technorati
  • TwitThis

AVL树Source Code

/************************************************************************
 *                        AVL树 demo程序                                
 * Version:1.0    实现insert函数                                        
 * nizqsut 2009.1019 00:04 <nizqsut@163.com>                           
 *                                                                     
 ************************************************************************/
#include <stdio.h>
#include <stdlib.h>

#include “avl_tree.h”
#include “fatal.h”

static avl_tree make_empty(avl_tree t){

    if (t != NULL) {
        if (t->left != NULL) {
            t->left = make_empty(t->left);
        }
        if (t->right != NULL) {
            t->right = make_empty(t->right);
        }
        free(t);
    }

    return NULL;
}

 

Continue reading AVL树Source Code - 全文阅读

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • RSS
  • Slashdot
  • Technorati
  • TwitThis
 Page 3 of 7 « 1  2  3  4  5 » ...  Last » 

Contact us

Admin: Bryan Wu