Skip to content

Instantly share code, notes, and snippets.

View UncleGarden's full-sized avatar

JIATENG WANG UncleGarden

  • Apple
  • United States
View GitHub Profile

Mou

Mou icon

Overview

Mou, the missing Markdown editor for web developers.

Syntax

@UncleGarden
UncleGarden / CC 5.7
Last active August 29, 2015 14:04
CareerCup 150
/**
* 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
*
@UncleGarden
UncleGarden / CC 5.6
Created July 29, 2014 20:27
CareerCup 150
/**
* 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) {
@UncleGarden
UncleGarden / CC 5.5
Created July 29, 2014 09:31
CareerCup 150
/**
* 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));
@UncleGarden
UncleGarden / CC 5.3
Created July 29, 2014 05:00
CareerCup 150
/**
* 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;
@UncleGarden
UncleGarden / C.C 5.2
Created July 28, 2014 08:34
CareerCup 150
/**
*
* 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 {
@UncleGarden
UncleGarden / C.C 5.1
Created July 28, 2014 00:04
CareerCup 150
/**
* 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
*
*
@UncleGarden
UncleGarden / C.C 2.6
Created June 24, 2014 18:02
CareerCup 150
/**
* 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]
*
@UncleGarden
UncleGarden / C.C 2.5
Last active August 29, 2015 14:02
CareerCup 150
/**
* 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.
*
@UncleGarden
UncleGarden / C.C 2.4
Created June 23, 2014 20:01
CareerCup 150
/**
* 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();