2013年12月5日

位运算的巧妙应用

摘要: 位运算的符号 与运算:& 或运算:| 异或运算:^ 非运算:~ 移位运算:>>和<<判断一个数是否是2的N次方 题目要求:用一个表达式,判断一个数X是否是2的N次方,即2,4,8,16……等,要求不可以用循环语句。 解析:2,4,8,16这样的数转化成二进制是10,100,1000,10000。 如果X减去1后(低一位并且二进制的每一位都是1),这个数与X做与运算,答案若是0,则X是2的N次方。 所以答案是:!(X&(X-1))两个数的交换 题目要求:将a,b两个数的值进行交换,并且不使用任何的中间变量。解法1: a = a+b; ... 阅读全文

posted @ 2013-12-05 13:10 Step-BY-Step 阅读(134) 评论(0) 推荐(0) 编辑

Class Object

摘要: java.langClass Objectjava.lang.Objectpublic class ObjectClassObjectis the root of the class hierarchy. Every class hasObjectas a superclass. All objects, including arrays, implement the methods of this class.MethodsModifier and TypeMethod and DescriptionprotectedObjectclone()Creates and returns a co 阅读全文

posted @ 2013-12-05 13:04 Step-BY-Step 阅读(246) 评论(0) 推荐(0) 编辑

CC150 上面重要的题目总结

摘要: 第一章 :全部重要 (1.6, 1.7 Leetcode上有)。1.5 面A碰到 (string compression)1.7面Z碰到 (set 0)1.8面Bigfish碰到 (string rotation)第二章 (2.4, 2.5 Leetcode上有):全部重要。2.2面Bigfish碰到 (find kth)第三章 :感觉就是3.2 (min stack), 3.5 (two stack queue) 重要。两道题面M被问到过。3.6(sort stack)感觉也有可能被考到。第四章 (4.1, 4.3, 4.5 Leetcode上有):感觉4.2, 4.3, 4.5,4.6, 阅读全文

posted @ 2013-12-05 12:57 Step-BY-Step 阅读(1033) 评论(0) 推荐(0) 编辑

Reorder List

摘要: Given a singly linked listL:L0→L1→…→Ln-1→Ln,reorder it to:L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given{1,2,3,4}, reorder it to{1,4,2,3}.分析:先用快慢指针找到链表的中点,然后翻转链表后半部分,再和前半部分组合。需要注意的是把链表分成两半时,前半段的尾节点要置为NULL,翻转链表时也要把尾节点置为NULL。 1 /** 2 * Definit 阅读全文

posted @ 2013-12-05 05:22 Step-BY-Step 阅读(238) 评论(0) 推荐(0) 编辑

Sort List

摘要: Sort a linked list inO(nlogn) time using constant space complexity.nlogn的排序有快速排序、归并排序、堆排序。双向链表用快排比较适合,堆排序也可以用于链表,单向链表适合用归并排序。以下是用归并排序的代码: 1 /** 2 * Definition for singly-linked list. 3 * class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * val = x; 8 ... 阅读全文

posted @ 2013-12-05 04:48 Step-BY-Step 阅读(314) 评论(0) 推荐(0) 编辑

Binary Search Tree In-Order Traversal Iterative Solution

摘要: Given a binary search tree, print the elements in-order iteratively without using recursion.Note:Before you attempt this problem, you might want to tr... 阅读全文

posted @ 2013-12-05 04:48 Step-BY-Step 阅读(692) 评论(0) 推荐(0) 编辑

导航