class Solution {
public:
int divide(int dividend, int divisor){
unsigned long dvd = dividend < 0 ? -dividend : dividend;
unsigned long dvs = divisor < 0 ? -divisor : divisor;
if (dvd < dvs) return 0;
bool nega = (dividend > 0) ^ (divisor > 0); //nega = true: -xxx; =false: +xxx.
unsigned long originDvs = dvs;
int power = 0;
unsigned long result = 0;
while (dvs < dvd){
dvs = dvs << 1;
power++;
} //dvd < dvs now
while (dvd >= originDvs){
if (dvd >= dvs){
dvd -= dvs;
result += (unsigned long) 1 << power;
}
while(dvd < dvs){ //to get biggest dvs < dev everytime
dvs = dvs >> 1;
power--;
}
}
return nega? -result : result;
}
};
Wednesday, October 7, 2015
Thread-safe blocking queue -- LIKI
import java.util.LinkedList;
import java.util.List;
public class BlockingQueue {
private List queue = new LinkedList();
private int limit = 10;
public BlockingQueue(int limit){
this.limit = limit;
}
public synchronized void enqueue(Object item)
throws InterruptedException {
while(this.queue.size() == this.limit) {
wait();
}
if(this.queue.size() == 0) {
notifyAll();
}
this.queue.add(item);
}
public synchronized Object dequeue()
throws InterruptedException{
while(this.queue.size() == 0){
wait();
}
if(this.queue.size() == this.limit){
notifyAll();
}
return this.queue.remove(0);
}
}
HashMap Implementation -- LIKI
import java.lang.Object;
public class HashMap {
int SIZE_OF_TABLE = 128;
HashEntry[] table;
HashMap() {
table = new HashEntry[SIZE_OF_TABLE];
for (int i = 0; i < SIZE_OF_TABLE; i++) {
table[i] = null;
}
}
public void put(int key, int value) {
int index = hashCodeNew(key);
HashEntry hashEntry = new HashEntry(key, value);
if (table[index] == null) {
table[index] = hashEntry;
} else {
HashEntry runner = table[index];
while (runner.next != null) {
if (runner.key == hashEntry.key) {
runner.value = hashEntry.value;
break;
} else {
runner = runner.next;
}
}
if (runner.next == null) {
if (runner.key == hashEntry.key) {
runner.value = hashEntry.value;
} else {
runner.next = hashEntry;
}
}
}
}
public int get(int key) {
int index = hashCodeNew(key);
if (table[index] == null) {
return -1;
} else {
HashEntry runner = table[index];
if (runner.key == key) {
return runner.value;
}
while (runner.next != null) {
if (runner.key == key) {
return runner.value;
}
}
return -1;
}
}
public int hashCodeNew(int h) {
h ^= (h >>> 20) ^ (h >>> 12);
int hashCode = h ^ (h >>> 7) ^ (h >>> 4);
return hashCode % SIZE_OF_TABLE;
}
}
class HashEntry {
int key;
int value;
HashEntry next = null;
HashEntry() {
}
public HashEntry(int key, int value) {
this.key = key;
this.value = value;
}
public int getKey() {
return this.key;
}
public int getValue() {
return this.value;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap hashMap = new HashMap();
hashMap.put(10, 20);
hashMap.put(20, 11);
hashMap.put(21, 1);
hashMap.put(20, 10);
int value = hashMap.get(20);
assert value != 10;
}
}
Infix To postfix -- LIKI
public String InToPost(String infixString) { String postfixString = " "; for (int index = 0; index < infixString.length(); ++index) { char chValue = infixString.charAt(index); if (chValue == '(') { push('('); } else if (chValue == ')') { Character oper = peek(); while (!(oper.equals('(')) && !(isEmpty())) { postfixString += oper.charValue(); pop(); oper = peek(); } pop(); } else if (chValue == '+' || chValue == '-') { //Stack is empty if (isEmpty()) { push(chValue); //current Stack is not empty } else { Character oper = peek(); while (!(isEmpty() || oper.equals(new Character('(')) || oper.equals(new Character(')')))) { pop(); postfixString += oper.charValue(); } push(chValue); } } else if (chValue == '*' || chValue == '/') { if (isEmpty()) { push(chValue); } else { Character oper = peek(); while (!oper.equals(new Character('+')) && !oper.equals(new Character('-')) && !isEmpty()) { pop(); postfixString += oper.charValue(); } push(chValue); } } else { postfixString += chValue; } } while (!isEmpty()) { Character oper = peek(); if (!oper.equals(new Character('('))) { pop(); postfixString += oper.charValue(); } } return postfixString; }
Subscribe to:
Posts (Atom)