<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Life - WeiG &#187; algorithm</title>
	<atom:link href="http://www.weigblog.com/tag/algorithm/feed" rel="self" type="application/rss+xml" />
	<link>http://www.weigblog.com</link>
	<description>Yet another Life</description>
	<lastBuildDate>Thu, 05 Jan 2012 03:13:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>一道超难的算法题-复制链表</title>
		<link>http://www.weigblog.com/2008/07/16/algorithm-question-linked-list.html</link>
		<comments>http://www.weigblog.com/2008/07/16/algorithm-question-linked-list.html#comments</comments>
		<pubDate>Wed, 16 Jul 2008 03:14:37 +0000</pubDate>
		<dc:creator>Wei</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[linked-list]]></category>

		<guid isPermaLink="false">http://www.weigblog.com/?p=359</guid>
		<description><![CDATA[Rui在参加面试后，给我分享了一道算法题。他在留言中说made me wanna kill myself when I knew the answer. 题目如下： Given a Linked List of node structure asstruct Node { type element; Node *next; Node *arb;};You are asked to create an exact copy this linked list. Pointer arb points to an arbitrary node in the linked list. 先说说我的算法吧。我选择了一个超笨的方法完成：创建一个新的linked list存储原始链表每个节点的顺序号，然后复制链表的时候，通过顺序号，找到arb指针所指向的节点。算法很笨，无论是空间效率还是时间效率，都很差。不过好歹是完成了。 不过，在我看完答案之后，我也wanna kill myslef。太神奇了。 算法首先在每两个节点之间插入一个新的节点。然后循环每组节点： 我顺手写了段实现代码，没编译，没调试，没测试。 [...]]]></description>
			<content:encoded><![CDATA[<p>Rui在参加面试后，给我分享了一道算法题。他在留言中说made me wanna kill myself when I knew the answer.</p>
<p>题目如下：</p>
<p>Given a Linked List of node structure asstruct Node { type element; Node *next; Node *arb;};You are asked to create an exact copy this linked list. Pointer arb points to an arbitrary node in the linked list.</p>
<p>先说说我的算法吧。我选择了一个超笨的方法完成：创建一个新的linked list存储原始链表每个节点的顺序号，然后复制链表的时候，通过顺序号，找到arb指针所指向的节点。算法很笨，无论是空间效率还是时间效率，都很差。不过好歹是完成了。</p>
<p>不过，在我看完答案之后，我也wanna kill myslef。太神奇了。</p>
<p><span id="more-319"></span></p>
<p>算法首先在每两个节点之间插入一个新的节点。然后循环每组节点：</p>
<pre class="brush: cpp; title: ; notranslate">
p-&gt;next-&gt;arb = p-&gt;arb-&gt;next;
p = p-&gt;next-&gt;next;
</pre>
<p>我顺手写了段实现代码，没编译，没调试，没测试。</p>
<pre class="brush: cpp; title: ; notranslate">
typedef struct {
    int data; // assuming data is an integer
    Node *next;
    Node *arb;
}Node;

void copy_linked_list(Node * src, Node ** dst) {
    Node *p;
    Node *q = NULL;

    for (p = src; p; p=p-&gt;next-&gt;next) {
        // Initialize the new node
        Note *nn = (Node *)malloc(sizeof(Node));
        nn-&gt;data = p-&gt;data;
        nn-&gt;arb = NULL;

        // Add the node to the right of the src node
        nn-&gt;next = p-&gt;next;
        p-&gt;next = nn;
    }

    // Create arb pointer
    for(p = src; p; p = p-&gt;next-&gt;next)
        p-&gt;next-&gt;arb = p-&gt;arb-&gt;next;

    // Detach copy node and create destination linked list
    for (p = src, q = *dst; p; p = p-&gt;next-next, q = q-&gt;next) {
        r = p-&gt;next;
        p-&gt;next = p-&gt;next-next;
        if (q-&gt;next)
            q-&gt;next = q-&gt;next-&gt;next;
        else
            q-&gt;next = NULL;
        if (q)
            q = r;
        else
            q-&gt;next = r;
    }
}
</pre>
<p>其实Rui早在3月份就给我发了这道题目，我也早在几个月前，看到了正确答案。一直想把这道题作为面试题，可觉得太难了，不想难为candidate了。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.weigblog.com/2008/07/16/algorithm-question-linked-list.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>快速平方根算法</title>
		<link>http://www.weigblog.com/2007/03/13/quick-invsqrt.html</link>
		<comments>http://www.weigblog.com/2007/03/13/quick-invsqrt.html#comments</comments>
		<pubDate>Tue, 13 Mar 2007 03:21:00 +0000</pubDate>
		<dc:creator>Wei</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[algorithm]]></category>

		<guid isPermaLink="false">http://www.weigblog.com/2007/03/13/quick-invsqrt.html</guid>
		<description><![CDATA[Carmack在QUAKE3中使用了下面的算法，它第一次在公众场合出现的时候，几乎震住了所有的人。据说该算法其实并不是Carmack发明的，它真正的作者是Nvidia的Gary Tarolli（未经证实）。 今天看到一个快速平方根算法，很是巧妙。据称是Carmack在QUAKE3中使用了下面的算法，它第一次在公众场合出现的时候，几乎震住了所有的人。 Chris Lomont写了篇论文，关于这个算法： http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf]]></description>
			<content:encoded><![CDATA[<p>Carmack在QUAKE3中使用了下面的算法，它第一次在公众场合出现的时候，几乎震住了所有的人。据说该算法其实并不是Carmack发明的，它真正的作者是Nvidia的Gary Tarolli（未经证实）。</p>
<p>今天看到一个快速平方根算法，很是巧妙。据称是Carmack在QUAKE3中使用了下面的算法，它第一次在公众场合出现的时候，几乎震住了所有的人。</p>
<pre class="brush: cpp; title: ; notranslate">
float InvSqrt(float x)
{
    float xhalf = 0.5f*x;
    int i = *(int*)&amp;x; // get bits for floating value
    i = 0x5f3759df - (i&gt;&gt;1); // gives initial guess y0
    x = *(float*)&amp;i; // convert bits back to float
    x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy
    return x;
}
</pre>
<p>Chris Lomont写了篇论文，关于这个算法：<br />
<a href="http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf">http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.weigblog.com/2007/03/13/quick-invsqrt.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>推荐本算法书</title>
		<link>http://www.weigblog.com/2006/09/12/book-of-algorithms.html</link>
		<comments>http://www.weigblog.com/2006/09/12/book-of-algorithms.html#comments</comments>
		<pubDate>Mon, 11 Sep 2006 17:29:28 +0000</pubDate>
		<dc:creator>Wei</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[book]]></category>

		<guid isPermaLink="false">http://www.weigblog.com/2006/09/12/%e6%8e%a8%e8%8d%90%e6%9c%ac%e7%ae%97%e6%b3%95%e4%b9%a6/</guid>
		<description><![CDATA[Algorithms by S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani 用伪代码写的。 Table of contents Preface Chapter 0: Prologue Chapter 1: Algorithms with numbers Chapter 2: Divide-and-conquer algorithms Chapter 3: Decompositions of graphs Chapter 4: Paths in graphs Chapter 5: Greedy algorithms Chapter 6: Dynamic programming Chapter 7: Linear programming Chapter 8: NP-complete problems Chapter 9: Coping [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.cse.ucsd.edu/users/dasgupta/mcgrawhill/"> Algorithms    by S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani</a><strong> </strong></p>
<p>用伪代码写的。</p>
<p><a href="http://www.cse.ucsd.edu/users/dasgupta/mcgrawhill/toc.pdf">Table of contents</a></p>
<p><a href="http://www.cse.ucsd.edu/users/dasgupta/mcgrawhill/preface.pdf">Preface</a></p>
<p>Chapter 0: <a href="http://www.cse.ucsd.edu/users/dasgupta/mcgrawhill/chap0.pdf">Prologue</a><br />
Chapter 1: <a href="http://www.cse.ucsd.edu/users/dasgupta/mcgrawhill/chap1.pdf">Algorithms with numbers</a><br />
Chapter 2: <a href="http://www.cse.ucsd.edu/users/dasgupta/mcgrawhill/chap2.pdf">Divide-and-conquer algorithms</a><br />
Chapter 3: <a href="http://www.cse.ucsd.edu/users/dasgupta/mcgrawhill/chap3.pdf">Decompositions of graphs</a><br />
Chapter 4: <a href="http://www.cse.ucsd.edu/users/dasgupta/mcgrawhill/chap4.pdf">Paths in graphs</a><br />
Chapter 5: <a href="http://www.cse.ucsd.edu/users/dasgupta/mcgrawhill/chap5.pdf">Greedy algorithms</a><br />
Chapter 6: <a href="http://www.cse.ucsd.edu/users/dasgupta/mcgrawhill/chap6.pdf">Dynamic programming</a><br />
Chapter 7: <a href="http://www.cse.ucsd.edu/users/dasgupta/mcgrawhill/chap7.pdf">Linear programming</a><br />
Chapter 8: <a href="http://www.cse.ucsd.edu/users/dasgupta/mcgrawhill/chap8.pdf">NP-complete problems</a><br />
Chapter 9: <a href="http://www.cse.ucsd.edu/users/dasgupta/mcgrawhill/chap9.pdf">Coping with NP-completeness</a><br />
Chapter 10: <a href="http://www.cse.ucsd.edu/users/dasgupta/mcgrawhill/chap10.pdf">Quantum algorithms</a></p>
<p><a href="http://www.box.net/index.php?rm=box_v2_download_shared_file&amp;file_id=f_19345679"><br />
</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.weigblog.com/2006/09/12/book-of-algorithms.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

