| https://www.linkedin.com/in/ursangst/ |
| https://www.linkedin.com/in/ursangst/ |
#python mit graphics
from graphics import *
linealCanv = GraphWin('Ein Lineal', 1000, 50)
def strich(x, h):
l = Line(Point(x, 0), Point(x, h))
l.draw(linealCanv)
def lineal(l, r, h):
step = 5
if (h < 1):
return
m = (l + r) / 2
strich(m, h)
lineal(l, m, h - step)
lineal(m, r, h - step)
# Function Call
lineal(0, 1000, 50)
# Wait for a mouse click, then close (with error handling)
try:
linealCanv.getMouse()
linealCanv.close()
except:
pass # Window was already closed, that's fine
import java.util.*;
public class RecurringCharacter {
public Integer firstDuplicate(List<Integer> array) {
Set<Integer> seen = new HashSet<>();
for (Integer item : array) { // O(n)
if (seen.contains(item)) { // O(1)
return item;
}
seen.add(item); // O(1)
}
return null; // Return null instead of 0 (matches the "undefined" requirement, which is from js anyway)
}
public static void main(String[] args) {
RecurringCharacter test = new RecurringCharacter();
System.out.println(test.firstDuplicate(Arrays.asList(2, 5, 1, 2, 3, 5, 1, 2, 4))); // 2
System.out.println(test.firstDuplicate(Arrays.asList(2, 1, 1, 2, 3, 5, 1, 2, 4))); // 1
System.out.println(test.firstDuplicate(Arrays.asList(2, 3, 4, 5))); // null
}
}
/* Important: HashSet is what makes the solution optimal in terms of time & space complexity */