Skip to content

Commit

Permalink
added BitManipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
kdn251 committed Feb 14, 2017
1 parent 712dc32 commit 77e804a
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 0 deletions.
45 changes: 45 additions & 0 deletions BitManipulation/binaryWatch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).

// Each LED represents a zero or one, with the least significant bit on the right.

// For example, the above binary watch reads "3:25".

// Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent.

// Example:

// Input: n = 1
// Return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]
// Note:
// The order of output does not matter.
// The hour must not contain a leading zero, for example "01:00" is not valid, it should be "1:00".
// The minute must be consist of two digits and may contain a leading zero, for example "10:2" is not valid, it should be "10:02".

public class Solution {

public List<String> readBinaryWatch(int num) {

ArrayList<String> allTimes = new ArrayList<String>();

//iterate through all possible time combinations
for(int i = 0; i < 12; i++) {

for(int j = 0; j < 60; j++) {

//if the current number and n have the same number of bits the time is possible
if(Integer.bitCount(i * 64 + j) == num) {

//add the current time to all times arraylist
allTimes.add(String.format("%d:%02d", i, j));

}

}

}

return allTimes;

}

}
23 changes: 23 additions & 0 deletions BitManipulation/countingBits.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.

// Example:
// For num = 5 you should return [0,1,1,2,1,2].

public class Solution {
public int[] countBits(int num) {

int[] bits = new int[num + 1];

bits[0] = 0;

for(int i = 1; i <= num; i++) {

bits[i] = bits[i >> 1] + (i & 1);

}

return bits;

}

}
29 changes: 29 additions & 0 deletions BitManipulation/hammingDistance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

// Given two integers x and y, calculate the Hamming distance.

// Note:
// 0 ≤ x, y < 2^31.

// Example:

// Input: x = 1, y = 4

// Output: 2

// Explanation:
// 1 (0 0 0 1)
// 4 (0 1 0 0)
// ↑ ↑

// The above arrows point to positions where the corresponding bits are different.

public class Solution {

public int hammingDistance(int x, int y) {

return Integer.bitCount(x ^ y);

}

}
61 changes: 61 additions & 0 deletions BitManipulation/maximumProductOfWordLengths.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.

// Example 1:
// Given ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]
// Return 16
// The two words can be "abcw", "xtfn".

// Example 2:
// Given ["a", "ab", "abc", "d", "cd", "bcd", "abcd"]
// Return 4
// The two words can be "ab", "cd".

// Example 3:
// Given ["a", "aa", "aaa", "aaaa"]
// Return 0
// No such pair of words.

public class Solution {

public int maxProduct(String[] words) {

if(words.length == 0 || words == null) return 0;

int length = words.length;
int[] value = new int[length];
int max = 0;

for(int i = 0; i < length; i++) {

String temp = words[i];

value[i] = 0;

for(int j = 0; j < temp.length(); j++) {

value[i] |= 1 << (temp.charAt(j) - 'a');

}

}


for(int i = 0; i < length; i++) {

for(int j = 1; j < length; j++) {

if((value[i] & value[j]) == 0 && (words[i].length() * words[j].length()) > max) {

max = words[i].length() * words[j].length();

}

}

}

return max;

}

}
25 changes: 25 additions & 0 deletions BitManipulation/numberOf1Bits.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).

// For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.

public class Solution {

// you need to treat n as an unsigned value
public int hammingWeight(int n) {

if(n == 0) return 0;

int count = 0;

while(n != 0) {

count += (n) & 1;
n >>>= 1;

}

return count;

}

}
25 changes: 25 additions & 0 deletions BitManipulation/sumOfTwoIntegers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

// Example:
// Given a = 1 and b = 2, return 3.

public class Solution {

public int getSum(int a, int b) {

if(a == 0) return b;
if(b == 0) return a;

while(b != 0) {

int carry = a & b;
a = a ^ b;
b = carry << 1;

}

return a;

}

}
63 changes: 63 additions & 0 deletions BitManipulation/utf-8Validation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:

// For 1-byte character, the first bit is a 0, followed by its unicode code.
// For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10.
// This is how the UTF-8 encoding would work:

// Char. number range | UTF-8 octet sequence
// (hexadecimal) | (binary)
// --------------------+---------------------------------------------
// 0000 0000-0000 007F | 0xxxxxxx
// 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
// 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
// 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
// Given an array of integers representing the data, return whether it is a valid utf-8 encoding.

// Note:
// The input is an array of integers. Only the least significant 8 bits of each integer is used to store the data. This means each integer represents only 1 byte of data.

// Example 1:

// data = [197, 130, 1], which represents the octet sequence: 11000101 10000010 00000001.

// Return true.
// It is a valid utf-8 encoding for a 2-bytes character followed by a 1-byte character.
// Example 2:

// data = [235, 140, 4], which represented the octet sequence: 11101011 10001100 00000100.

// Return false.
// The first 3 bits are all one's and the 4th bit is 0 means it is a 3-bytes character.
// The next byte is a continuation byte which starts with 10 and that's correct.
// But the second continuation byte does not start with 10, so it is invalid.

public class Solution {

public boolean validUtf8(int[] data) {

int count = 0;
for(int i : data) {

if(count == 0) {

if((i >> 5) == 0b110) count = 1;
else if((i >> 4) == 0b1110) count = 2;
else if((i >> 3) == 0b11110) count = 3;
else if((i >> 7) == 0b1) return false;

}

else {

if((i >> 6) != 0b10) return false;
count--;

}

}

return count == 0;

}

}

0 comments on commit 77e804a

Please sign in to comment.