bit-manipulation(1)
摘要:You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e.g., M becomes a substring of N located at i and starting at j).EXAMPLE:Input: N = 10000000000, M = 10101, i = 2, j = 6Output: N = 10001010100solution中是有错误的假设n的
阅读全文
Linked Lists
摘要:创建一个链表:NOTE: When you’re discussing a linked list in an interview, make sure to understand whether it is a single linked list or a doubly linked list.1 class Node {2 Node next = null;3 int data;4 public Node(int d) { data = d; }5 void appendToTail(int d) {6 Node end = new Node(d);7 Node n = this;8 w
阅读全文
判断链表是否存在环,找出环的入口
摘要:一、判断链表是否存在环,办法为:设置两个指针(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
阅读全文