Skip to content

Instantly share code, notes, and snippets.

@WOLOHAHA
WOLOHAHA / CC150-5.8.java
Last active August 29, 2015 14:04
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). The height of the screen, of course, can be derived from the length of the array and the width. Implement a function drawHorizontalLi…
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).
@WOLOHAHA
WOLOHAHA / CC150-5.7.java
Last active August 29, 2015 14:04
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 jth bit of A[j]," which takes constant time. Write code to fin…
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
@WOLOHAHA
WOLOHAHA / CC150-5.6.java
Created July 27, 2014 05:54
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).
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).
*
*
@WOLOHAHA
WOLOHAHA / CC150-5.5.java
Created July 26, 2014 15:27
Write a function to determine the number of bits required to convert integer A to integer B.
package POJ;
public class Main{
/**
*
* 5.5 Write a function to determine the number of bits required to convert integer A to integer B.
*
*
*/
@WOLOHAHA
WOLOHAHA / CC150-5.3.java
Created July 26, 2014 10:09
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.
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.
*
*
@WOLOHAHA
WOLOHAHA / CC150-5.2.java
Created July 23, 2014 16:05
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."
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."
*
*
@WOLOHAHA
WOLOHAHA / CC150-5.1.java
Created July 23, 2014 15:34
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)
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:
@WOLOHAHA
WOLOHAHA / CC150-4.9.java
Created July 23, 2014 04:27
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.
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.
*
*
@WOLOHAHA
WOLOHAHA / CC150-4.8.java
Created July 23, 2014 03:00
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.
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.
@WOLOHAHA
WOLOHAHA / CC150-4.7.java
Created July 22, 2014 15:54
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.
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: