Skip to content

Instantly share code, notes, and snippets.

import java.util.Scanner;
/**
* Created by xynophon on 15.1.27.
*/
public class roman_2_integer{
// I 1
// IV 4
import java.util.Arrays;
import java.util.HashMap;
/**
* Created by xynophon on 15.2.27.
*/
public class majorityElement {
public int majorityElement(int[] num) {
//bad solution using Hash. O(n). it took 343ms.
int half = num.length/2;
@xynophon
xynophon / SameTree.java
Last active August 29, 2015 14:24
LeetCode SameTree
import java.util.*;
public class SameTree {
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
@xynophon
xynophon / StackList.java
Last active August 29, 2015 14:24
CrackingCodingInterview. Implement Stack with LinkedList
class StackList {
Node top;
void push(Object data){
Node t = new Node(data);
t.next = top;
top = t;
}
Object pop(){
@xynophon
xynophon / NumberOf1Bits.java
Last active August 29, 2015 14:24
LeetCode. Number Of 1Bits. JDK7
import java.util.*;
public static int hammingWeight(int n) {
int cnt = 0;
while(n != 0){
if(0 != n%2){ //if(1 == n%2){
cnt++;
}
n = n >>> 1;
}
@xynophon
xynophon / BinaryTreePreorderTraversal.java
Last active August 29, 2015 14:24
LeetCode. Binary Tree Preorder Traversal. Recursive
import java.util.*;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
@xynophon
xynophon / BinaryTreeInorderTraversal.java
Created July 9, 2015 02:56
LeetCode. Binary Tree Inorder Traversal recursive, iterative
import java.util.*;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
@xynophon
xynophon / InvertBinaryTree.java
Created July 10, 2015 05:04
LeetCode Invert Binary Tree
import java.util.*;
public class InvertBinaryTree {
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
@xynophon
xynophon / BSTIterator.java
Created July 11, 2015 06:32
LeetCode Binary Search Tree Iterator
import java.util.*;
public class BSTIterator {
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
@xynophon
xynophon / Permutations.java
Created August 15, 2015 13:03
LeetCode Permutations
import java.util.*;
public class Permutations {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> l0 = new ArrayList<>();
l0.add(nums[0]);
result.add(l0);
for (int i = 1; i < nums.length; i++) {