Skip to content

Commit

Permalink
hamming-distance updated
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyanxing committed Feb 20, 2015
1 parent dfb526f commit a7fff62
Show file tree
Hide file tree
Showing 26 changed files with 294 additions and 131 deletions.
33 changes: 33 additions & 0 deletions app/problems/hamming-distance.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "Hamming Distance",
"id": "hamming-distance",
"discuss_link": "http://www.fgdsb.com/2015/01/03/hamming-distance/",
"desc": "<p>Write a function to calculate Hamming distance between two integers</p>",
"difficulty": "Easy",
"source": "Microsoft",
"time": "2015-02-19 22:14:25",
"tags": [
"Math"
],
"code_cpp": "int hamming(int a , int b) {\n}",
"code_java": "public class Solution {\n public int hamming(int a , int b) {\n }\n}",
"code_ruby": "# @param a: integer, b: integer\n# @return integer\ndef hamming(a, b)\nend",
"code_python": "# @param a: integer, b: integer\n# @return integer\ndef hamming(a, b):",
"code_lua": "-- @param a: integer, b: integer\n-- @return integer\nfunction hamming(a, b)\nend",
"in_type_cpp": [
""
],
"ret_type_cpp": "",
"out_type_cpp": "",
"judge_type_cpp": "equal",
"in_type_java": [
""
],
"ret_type_java": "",
"out_type_java": "",
"judge_type_java": "equal",
"judge_type_ruby": "equal",
"judge_type_python": "equal",
"judge_type_lua": "equal",
"judge_call": "function(@)"
}
4 changes: 2 additions & 2 deletions app/problems/lca-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "Lowest Common Ancestor",
"id": "lca-1",
"discuss_link": "http://www.fgdsb.com/2015/01/03/lowest-common-ancestor/",
"desc": "<p>Given an arbitrary binary tree and two values, write a function to find the least common ancestor.</p>",
"desc": "<p>Given an arbitrary binary tree and two values, write a function to find the lowest common ancestor.</p>",
"difficulty": "Medium",
"source": "CC150",
"source": "LinkedIn",
"time": "2015-02-03 20:14:36",
"tags": [],
"code_cpp": "/*\nstruct TreeNode {\n TreeNode(int v = 0) :val(v){}\n int val{ 0 };\n TreeNode* left{ nullptr };\n TreeNode* right{ nullptr };\n};\n*/\nTreeNode* lca(TreeNode* root, int m, int n) {\n}",
Expand Down
2 changes: 1 addition & 1 deletion app/problems/lca-2.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"discuss_link": "http://www.fgdsb.com/2015/01/03/lowest-common-ancestor/",
"desc": "<p>Follow up for \"Lowest Common Ancestor\":<br><br>How if the given binary tree are binary search tree?</p>",
"difficulty": "Medium",
"source": "CC150",
"source": "LinkedIn",
"time": "2015-02-03 22:21:15",
"tags": [],
"code_cpp": "/*\nstruct TreeNode {\n TreeNode(int v = 0) :val(v){}\n int val{ 0 };\n TreeNode* left{ nullptr };\n TreeNode* right{ nullptr };\n};\n*/\nTreeNode* lca(TreeNode* root, int m, int n) {\n}",
Expand Down
11 changes: 9 additions & 2 deletions app/problems/problems.json
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,14 @@
"id": "lca-1",
"name": "Lowest Common Ancestor",
"difficulty": "Medium",
"source": "CC150"
"source": "LinkedIn"
},
{
"time": "2015-02-03 22:21:15",
"id": "lca-2",
"name": "Lowest Common Ancestor II",
"difficulty": "Medium",
"source": "CC150"
"source": "LinkedIn"
},
{
"time": "2015-02-08 16:59:46",
Expand Down Expand Up @@ -299,5 +299,12 @@
"name": "Zigzag Iterator",
"difficulty": "Easy",
"source": "Google"
},
{
"time": "2015-02-19 22:14:25",
"id": "hamming-distance",
"name": "Hamming Distance",
"difficulty": "Easy",
"source": "Microsoft"
}
]
17 changes: 17 additions & 0 deletions judge/build_tests/hamming-distance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require './common'
require '../ruby/common'

class Test_hamming_distance < TestBase
def initialize(name)
super(name)
end

def add_test
end

def gen_tests
@test_in, @test_out = [[]], []
end
end

Test_hamming_distance.new 'hamming-distance'
Binary file modified judge/cpp/out
Binary file not shown.
44 changes: 18 additions & 26 deletions judge/cpp/solution.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
/*
class Iterator {
public:
int get_next();
bool has_next();
};
*/

class ZigzagIterator {
public:
ZigzagIterator(Iterator& a, Iterator& b) :_its{a,b} {
_pointer = a.has_next() ? 0 : 1;
vector<int> longest_subarray(vector<int>& arr) {
cout << arr << endl;
int sum = 0, length = 0, begin = -1;
unordered_map<int, int> record = {{0,-1}};
for(int i = 0; i < arr.size(); ++i) {
sum += arr[i];
if(record.count(sum)) {
auto first = record[sum];
if(i - first > length) {
length = i - first;
begin = first + 1;
}
} else {
record[sum] = i;
}
}

int get_next() {
int ret = _its[_pointer].get_next(), old = _pointer;
do {
if(++_pointer >= 2) _pointer = 0;
} while(!_its[_pointer].has_next() && _pointer != old);
return ret;
}

bool has_next() { return _its[_pointer].has_next(); }
private:
Iterator _its[2];
int _pointer;
};
if(begin < 0) return {};
else return vector<int>(arr.begin()+begin, arr.begin()+begin+length);
}
2 changes: 1 addition & 1 deletion judge/cpp/src.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "common.h"
#include "solution.h"
#include "tests/zigzag-iterator.h"
#include "tests/longest-zero-subarray.h"

int main() {
judge();
Expand Down
24 changes: 3 additions & 21 deletions judge/java/Solution.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
package judge;import java.util.*;import java.lang.*;import java.io.*;import datastruct.*; /*
public class Iterator {
public int get_next();
public boolean has_next();
}
*/

public class PeekIterator {
public PeekIterator(Iterator it) {
}

public int peek() {
return 1;
}

public boolean has_next() {
return false;
}

public int get_next() {
return 2;
package judge;import java.util.*;import java.lang.*;import java.io.*;import datastruct.*; public class Solution {
public List<Integer> longest_seq(int[][] mat) {
return new ArrayList<Integer>();
}
}
Binary file modified judge/java/judge/Solution.class
Binary file not shown.
Binary file modified judge/java/judge/src.class
Binary file not shown.
4 changes: 2 additions & 2 deletions judge/java/src.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import java.io.*;
import tests.test_common;
import datastruct.*;
import tests.zigzag_iterator;
import tests.longest_inc_seq_matrix;

public class src {
public static void main (String[] args) throws java.lang.Exception {
zigzag_iterator.judge();
longest_inc_seq_matrix.judge();
}
}
Binary file modified judge/java/tests/lca_1.class
Binary file not shown.
37 changes: 20 additions & 17 deletions judge/lua/solution.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,30 @@
-- function Iterator:get_next()
-- end

ZigzagIterator = class()
PeekIterator = class()

function ZigzagIterator: ctor(i0, i1)
self.its = {i0,i1}
if i0:has_next() then
self.pointer = 1
else
self.pointer = 2
function PeekIterator: ctor(it)
self.it = it
self.peeks = nil
end

function PeekIterator: peek()
if self.peeks == nil then
self.peeks = self.it:get_next()
end
return self.peeks
end

function ZigzagIterator: has_next()
return self.its[self.pointer]:has_next()
function PeekIterator: has_next()
return self.it:has_next() or self.peeks
end

function ZigzagIterator: get_next()
local ret, old = self.its[self.pointer]:get_next(), self.pointer;
while true do
self.pointer = self.pointer + 1
if self.pointer > 2 then self.pointer = 1 end
if self.its[self.pointer]:has_next() or self.pointer == old then break end
function PeekIterator: get_next()
if self.peeks == nil then
return self.it:get_next()
else
ret = self.peeks
self.peeks = nil
return ret
end
return ret
end
end
2 changes: 1 addition & 1 deletion judge/lua/src.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package.path = package.path .. ';/Users/yanxingwang/Projects/node_proj/fgdsb_judge/judge/lua/?.lua;'
require "common"
require "tests/zigzag-iterator"
require "tests/peek-iterator"
judge()
29 changes: 4 additions & 25 deletions judge/python/solution.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
from common import *
# class Iterator:
# def get_next(self):
# ...
#
# def has_next(self):
# ...

class ZigzagIterator:
# @param i0: Iterator object, i1: Iterator object
def __init__(self, i0, i1):
self.its = [i0,i1]
if i0.has_next(): self.pointer = 0
else: self.pointer = 1

# @return boolean
def has_next(self):
return self.its[self.pointer].has_next()

# @return integer
def get_next(self):
ret, old = self.its[self.pointer].get_next(), self.pointer;
while True:
self.pointer = (self.pointer + 1) % 2
if self.its[self.pointer].has_next() or self.pointer == old: break
return ret
def plus_num(a, b):
print a
print b
return a + b
Binary file modified judge/python/solution.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion judge/python/src.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from common import *
from solution import *
from tests.zigzag_iterator import *
from tests.a_plus_b import *

judge()
Binary file modified judge/python/tests/a_plus_b.pyc
Binary file not shown.
Binary file added judge/python/tests/coin_change.pyc
Binary file not shown.
Binary file added judge/python/tests/coin_change_2.pyc
Binary file not shown.
Binary file modified judge/python/tests/lca_1.pyc
Binary file not shown.
43 changes: 14 additions & 29 deletions judge/ruby/solution.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
# class Iterator
# def get_next()
# ...
# end
#
# def has_next()
# ...
# end
# end
# class TreeNode
# attr_accessor :left, :right, :val
# def initialize(v)
# @val, @left, @right = v, nil, nil
# end

class ZigzagIterator
# @param i0: Iterator object, i1: Iterator object
def initialize(i0, i1)
@its, @pointer = [i0,i1], i0.has_next() ? 0 : 1
end

# @return boolean
def has_next()
@its[@pointer].has_next()
end

# @return integer
def get_next()
ret, old = @its[@pointer].get_next, @pointer;
loop do
@pointer = (@pointer + 1) % 2
break if @its[@pointer].has_next or @pointer == old
end
ret
end
# @param root, TreeNode
# @param m,n, integer
# @return TreeNode
def lca(root, m, n)
return nil if root.nil?
return root if root.val == m or root.val == n
l, r = lca(root.left, m, n), lca(root.right, m, n)
return root if l and r
return l ? l : r
end
2 changes: 1 addition & 1 deletion judge/ruby/src.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require './judge/ruby/common'
require './judge/ruby/solution'
require './judge/ruby/tests/zigzag-iterator'
require './judge/ruby/tests/lca-1'

judge
Loading

0 comments on commit a7fff62

Please sign in to comment.