This file contains hidden or 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
| package POJ; | |
| import java.util.ArrayList; | |
| public class Main{ | |
| /** | |
| * | |
| * 5.8 A monochrome screen is stored as a single array of bytes, allowing eight consecutive pixels to be stored | |
| * in one byte. The screen has width w, where w is divisible by 8 (that is, no byte will be split across rows). |
This file contains hidden or 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
| package POJ; | |
| import java.util.ArrayList; | |
| public class Main{ | |
| /** | |
| * | |
| * 5.7 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 |
This file contains hidden or 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
| package POJ; | |
| public class Main{ | |
| /** | |
| * | |
| * 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). | |
| * | |
| * |
This file contains hidden or 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
| package POJ; | |
| public class Main{ | |
| /** | |
| * | |
| * 5.5 Write a function to determine the number of bits required to convert integer A to integer B. | |
| * | |
| * | |
| */ |
This file contains hidden or 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
| package POJ; | |
| public class Main{ | |
| /** | |
| * | |
| * 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. | |
| * | |
| * |
This file contains hidden or 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
| package POJ; | |
| public class Main{ | |
| /** | |
| * | |
| * 5.2 Given a real number between 0 and 1 (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." | |
| * | |
| * |
This file contains hidden or 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
| package POJ; | |
| public class Main{ | |
| /** | |
| * | |
| * 5.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: |
This file contains hidden or 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
| package POJ; | |
| public class Main{ | |
| /** | |
| * | |
| * 4.9 You are given a binary tree in which each node contains a value. Design an algorithm to print | |
| * all paths which sum to a given value. The path does not need to start or end at the root or a leaf. | |
| * | |
| * |
This file contains hidden or 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
| package POJ; | |
| public class Main{ | |
| /** | |
| * | |
| * 4.8 You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. | |
| * Create an algorithm to decide if T2 is a subtree of Tl. | |
| * A tree T2 is a subtree of Tl if there exists a node n in Tl such that the subtree of n is identical to | |
| * T2. That is, if you cut off the tree at node n, the two trees would be identical. |
This file contains hidden or 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
| package POJ; | |
| public class Main{ | |
| /** | |
| * | |
| * 4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. | |
| * Avoid storing additional nodes in a data structure. NOTE: This is not necessarily a binary search tree. | |
| * | |
| * Solution: |