This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void dfs(Node r) { | |
if(r == null) | |
return; | |
visit(r); | |
r.visited = true; // avoid cycles | |
for(Node n : r.adjacent) { | |
if(!n.visited) | |
dfs(n); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Determine if a binary tree is a binary search tree | |
boolean isBst(Node r) { | |
return isBst(r, null, null); | |
} | |
boolean isBst(Node r, Integer min, Integer max) { | |
if(r == null) | |
return true; | |
if((min != null && r.data < min) || (max != null && r.data > max)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Node reverse(Node n) { | |
// maintain previous node, rewriting all pointers to previous | |
Node prev = null; | |
while(n != null) { | |
Node next = n.next; | |
n.next = prev; | |
prev = n; | |
n = next; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
boolean isPalindrome(Node n){ | |
Node fast = n; | |
Node slow = n; | |
// push first half onto stack, then compare with second half | |
Stack<Integer> s = new Stack<Integer>(); | |
while(fast != null && fast.next != null) { | |
s.push(slow.data); | |
slow = slow.next; | |
fast = fast.next.next; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// aabccccaaa -> aabccccaaa | |
String compressString(String input) { | |
StringBuilder compressed = new StringBuilder(); | |
int count = 1; | |
String[] arr = input.split(""); | |
for(int i = 0; i < arr.length; i++) { | |
++count; | |
if(i + 1 >= arr.length || !arr[i + 1].equals(arr[i])) { | |
compressed.append(arr[i]); | |
compressed.append(Integer.toString(count)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pale, ple -> true | |
// pales, pale -> true | |
// pale, bale -> true | |
// pale, bake -> false | |
boolean isOneAway(String a, String b) { | |
int aLength = a.length(); | |
int bLength = b.length(); | |
int shortIndex = 0; | |
int longIndex = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// palindrome will have even letter counts and at most one odd letter count | |
boolean isPalindromPermutation(String input) { | |
Map<String, Integer> frequency = new HashMap<String, Integer>(); | |
for(String s : input.split("")) { | |
if(!frequency.containsKey(s)) { | |
frequency.put(s, 1); | |
} else { | |
frequency.put(s, 1 + frequency.get(s)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using StringBuffer | |
boolean isPalindrome(String s) { | |
StringBuffer stringBuffer = new StringBuffer(s); | |
StringBuffer reverse = new StringBuffer(s); | |
reverse.reverse(); | |
return stringBuffer.toString().equals(reverse.toString()); | |
} | |
// in place | |
boolean isPalindrome(String input) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var parse = require('csv-parse'); | |
var consumed = 0; | |
var burned = 0; | |
var numDays = 0; | |
var parser = parse({delimiter: ';'}, function(err, data){ | |
data.forEach(function(data){ | |
++numDays; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nmap -PN localhost | |
----------------------- | |
PORT STATE SERVICE | |
631/tcp open ipp | |
1023/tcp open netvenuechat |