Skip to content

Instantly share code, notes, and snippets.

@so77id
Created September 8, 2020 21:28
Show Gist options
  • Save so77id/b4d6960f45d4d81b13a91a7b6f6dadd4 to your computer and use it in GitHub Desktop.
Save so77id/b4d6960f45d4d81b13a91a7b6f6dadd4 to your computer and use it in GitHub Desktop.
Sol ev 1 - intento 1
public static int bin2dec(SinglyLinkedListNode head) {
SinglyLinkedListNode tmp = head;
int count, num;
count = num = 0;
while(tmp != null) {
count++;
tmp = tmp.next;
}
tmp = head;
for(; count > 0; count--) {
num += tmp.data * Math.pow(2, count-1);
tmp = tmp.next;
}
return num;
}
public static List<Integer> ForecastTermometer(List<Integer> T) {
List<Integer> ans = new ArrayList<Integer>();
Stack<Integer> stack = new Stack();
for (int i = T.size() - 1; i >= 0; --i) {
while (!stack.isEmpty() && T.get(i) >= T.get(stack.peek())) stack.pop();
ans.add(stack.isEmpty() ? 0 : stack.peek() - i);
stack.push(i);
}
Collections.reverse(ans);
return ans;
}
public static SinglyLinkedListNode MergeTwoOrderedList(SinglyLinkedListNode l1, SinglyLinkedListNode l2) {
if (l1 == null) {
return l2;
}
else if (l2 == null) {
return l1;
}
else if (l1.data < l2.data) {
l1.next = MergeTwoOrderedList(l1.next, l2);
return l1;
}
else {
l2.next = MergeTwoOrderedList(l1, l2.next);
return l2;
}
}
public static String PairRemove(String s) {
Stack<Character> stack = new Stack<Character>();
for(char c:s.toCharArray()) {
if(!stack.empty()) {
if(stack.peek() == c) stack.pop();
else stack.push(c);
} else stack.push(c);
}
String sol = "";
while(!stack.empty()) {
sol = stack.peek() + sol;
stack.pop();
}
return sol;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment