Skip to content

Instantly share code, notes, and snippets.

View astkaasa's full-sized avatar
🎯
Focusing

astkaasa

🎯
Focusing
View GitHub Profile
@astkaasa
astkaasa / gist:5406571
Created April 17, 2013 18:27
problem 1.8
public static void main( String[] args ) {
String s11 = s1 + s1;
if ( isSubstring( s11, s2 ) )
System.out.println( "s2 is a rotation of s1.");
else
System.out.println( "s2 is not a rotation of s1." );
}
//check if s2 is a substring of s1
public static boolean isSubstring( String s1, String s2 ) {
@astkaasa
astkaasa / gist:5406441
Created April 17, 2013 18:08
problem 1.5
import java.util.Scanner;
public class StringCompression {
private static Scanner in;
/**
* @param args
*/
public static void main(String[] args) {
@astkaasa
astkaasa / gist:5392911
Last active December 16, 2015 06:39
problem 5.8
//assume x1 < x2
drawHorizontalLine( byte[] screen, int width, int x1, int x2, int y ) {
int start = y * width / 8 + ( int ) x1 / 8;
int end = y * width / 8 + ( int ) x2 / 8;
for ( int i = start; i <= end; i++ ) {
screen[ i ] = 127;
}
if ( start == end ) {
screen[ start ] = screen[ start ] >> ( x1 % 8 );
@astkaasa
astkaasa / gist:5392789
Created April 16, 2013 02:04
problem 5.1
public class InsertBinary {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int n = 1089;
int m = 7;
int i = 2;
@astkaasa
astkaasa / gist:5392785
Created April 16, 2013 02:03
problem 5.2
public class TestFloat {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//System.out.println( "Please input a real number between 0 and 1, 0.72 for example:" );
@astkaasa
astkaasa / gist:5392781
Created April 16, 2013 02:01
problem 5.5
public class BitConvertNumbers {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println( bitSwapRequired( 1089, 7 ) );
}
@astkaasa
astkaasa / gist:5392778
Created April 16, 2013 02:00
problem 5.6
public class BitSwap {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 0b10001111;
int b = a >> 1;
@astkaasa
astkaasa / gist:5392765
Created April 16, 2013 01:57
problem 5.7
public static int findMissing( int[] a ) {
Arrays.sort( a );
if ( fetch( 0, 0 ) != 0 )
return 0;
int max = ( int ) Math.log( n ) / Math.log( 2 );
for ( int i = 1; i < n; i++ ) {
j = ( int ) Math.log( n ) / Math.log( i );
if ( fetch( j, i ) != 1 )
return i;
}
public class ArrayToBST {
public static void main( String[] args ) {
int[] x = { 9, 12, 14, 17, 19, 23, 50, 54, 67, 72, 76 };
BinarySearchTree t = new BinarySearchTree();
public class BinarySearchTree {
private TreeNode root;
//private int height;
public BinarySearchTree() {
root = null;
}