Mou, the missing Markdown editor for web developers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* An array A contains all the integers from 0 through n, except for one number | |
* which is missing. In this problem, we cannot access an entire integer in A | |
* with a single operation. The elements of A are represented in binary, and the | |
* only operation we can use to access them is "fetch the j-th bit of A[i]", | |
* which takes constant time. Write code to find the missing integer. Can you do | |
* it in O(n) time? | |
* | |
* Time Complexity: O(n) Space | |
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 5.6 Write a program to swap odd and even bits in an integer with as few | |
* instructions as possible (e.g., bit 0 and bit! are swapped, bit 2 and bit 3 | |
* are swapped, and so on). | |
* | |
* @author Garden | |
*/ | |
public class CC5_6 { | |
public static void swap(int a) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 5.5 Write a function to determine the number of bits required to convert | |
* integer A to integer B. | |
* | |
* @author Garden | |
*/ | |
public class CC5_5 { | |
public static void determine(int a, int b) { | |
System.out.println(Integer.toBinaryString(a)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 5.3 Given a positive integer, print the next smallest and the next largest | |
* number that have the same number of 7 bits in their binary representation. | |
* | |
* @author Garden | |
*/ | |
public class CC5_3 { | |
public static void getNextBig(int n) { | |
int c = n; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* 5.2 Given a real number between 0 and 7 (e.g., 0.72) that is passed in as a | |
* double, print the binary representation. If the number cannot be represented | |
* accurately in binary with at most 32 characters, print "ERROR." | |
* | |
* @author Garden | |
*/ | |
public class CC5_2 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 5.1 You are given two 32-bit numbers, N andM, and two bit positions, i and j. | |
* Write a method to insert M into Nsuch that M starts at bit j and ends at bit | |
* i. You can assume that the bits j through i have enough space to fit all ofM. | |
* That is, ifM= 10011, you can assume that there are at least 5 bits between j | |
* and i. You would not, for example, have j-3 and i=2, because M could not | |
* fully fit between bit 3 and bit 2. EXAMPLE: Input: N = 10000000000, M = | |
* 10011, i = 2, j = 6 Output: N = 10001001100 | |
* | |
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 2.6 Given a circular linked list, implement an algorithm which returns the | |
* node at the beginning of the loop. | |
* | |
* DEFINITION Circular linked list: A (corrupt) linked list in which a node's | |
* next pointer points to an earlier node, so as to make a loop in the linked | |
* list. | |
* | |
* EXAMPLE Input:A ->B->C->D->E-> C[thesameCasearlier] | |
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 2.5 You have two numbers represented by a linked list, where each node | |
* contains a single digit. The digits are stored in reverse order, such that | |
* the Ts digit is at the head of the list. Write a function that adds the two | |
* numbers and returns the sum as a linked list. | |
* | |
* EXAMPLE Input:(7-> 1 -> 6) + (5 -> 9 -> 2).Thatis,617 + 295. | |
* | |
* Output: 2 -> 1 -> 9.That is, 912. | |
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 2.4 Write code to partition a linked list around a value x, such that all | |
* nodes less than x come before all nodes greater than or equal to x. | |
* | |
* @author Jiateng | |
*/ | |
public class CC2_4 { | |
public static LinkedList partition(LinkedList list, int value) { | |
LinkedList less = new LinkedList(); |
NewerOlder