TypeScript Missing Integer
function missingInteger(a: number[]): number {
let min = 1;
const unique = [...new Set(a)].sort((x, y) => x - y);
for (const v of unique) {
if (v > 0) {
if (min !== v) {
break;
}
min++;
}
}
return min;
}
This records the positive numbers that exist, then returns the smallest positive value that is still missing.
Bash Nesting
nesting() {
local _s=$1
if [[ -z "$_s" ]]; then echo 1; return; fi
local -a _stack=()
local _i _c
for ((_i = 0; _i < ${#_s}; _i++)); do
_c=${_s:_i:1}
if [[ "$_c" == ")" ]]; then
if (( ${#_stack[@]} == 0 )) || [[ "${_stack[-1]}" != "(" ]]; then
echo 0; return
fi
unset '_stack[-1]'
else
_stack+=("$_c")
fi
done
if (( ${#_stack[@]} == 0 )); then echo 1; else echo 0; fi
}
This treats the string like a balance counter: open parentheses add one, closing ones remove one.
C++ Nesting
#include <string>
#include <vector>
int nesting(const std::string& s)
{
if (s.empty()) {
return 1;
}
std::vector<char> stack;
for (char c : s) {
if (c == ')') {
if (stack.empty() || stack.back() != '(') {
return 0;
}
stack.pop_back();
} else {
stack.push_back(c);
}
}
return stack.empty() ? 1 : 0;
}
This treats the string like a balance counter: open parentheses add one, closing ones remove one.
C# Nesting
static int Nesting(string s)
{
if (string.IsNullOrEmpty(s))
{
return 1;
}
var stack = new Stack<char>();
foreach (var v in s)
{
if (v == ')')
{
if (stack.Count == 0 || stack.Pop() != '(')
{
return 0;
}
}
else
{
stack.Push(v);
}
}
return stack.Count == 0 ? 1 : 0;
}
This treats the string like a balance counter: open parentheses add one, closing ones remove one.
Elixir Nesting
defmodule Nesting do
def nesting(""), do: 1
def nesting(s) do
result =
s
|> String.graphemes()
|> Enum.reduce_while([], fn ch, stack ->
case ch do
")" ->
case stack do
["(" | rest] -> {:cont, rest}
_ -> {:halt, :fail}
end
other ->
{:cont, [other | stack]}
end
end)
if result == [], do: 1, else: 0
end
end
This treats the string like a balance counter: open parentheses add one, closing ones remove one.
Erlang Nesting
-module(nesting).
-export([nesting/1]).
nesting(S) ->
case close_stack(S, []) of
[] -> 1;
_ -> 0
end.
close_stack([], Stack) -> Stack;
close_stack([$) | T], [$( | Rest]) -> close_stack(T, Rest);
close_stack([$) | _], _Stack) -> error;
close_stack([C | T], Stack) -> close_stack(T, [C | Stack]).
This treats the string like a balance counter: open parentheses add one, closing ones remove one.
Go Nesting
func nesting(s string) int {
if s == "" {
return 1
}
depth := 0
for i := 0; i < len(s); i++ {
if s[i] == ')' {
if depth == 0 {
return 0
}
depth--
} else {
depth++
}
}
if depth == 0 {
return 1
}
return 0
}
This treats the string like a balance counter: open parentheses add one, closing ones remove one.
Haskell Nesting
nesting :: String -> Int
nesting s
| null s = 1
| otherwise = go s []
where
go [] stack = if null stack then 1 else 0
go (c : cs) stack
| c == ')' = pop cs stack
| otherwise = go cs (c : stack)
pop cs (top : rest)
| top == '(' = go cs rest
pop _ _ = 0
This treats the string like a balance counter: open parentheses add one, closing ones remove one.
Java Nesting
import java.util.ArrayDeque;
import java.util.Deque;
public class Solution {
public static int nesting(String s) {
if (s.isEmpty()) {
return 1;
}
Deque<Character> stack = new ArrayDeque<>();
for (char v : s.toCharArray()) {
if (v == ')') {
if (stack.isEmpty() || stack.pop() != '(') {
return 0;
}
} else {
stack.push(v);
}
}
return stack.isEmpty() ? 1 : 0;
}
}
This treats the string like a balance counter: open parentheses add one, closing ones remove one.
Lisp Nesting
(defun nesting (s)
(if (zerop (length s))
1
(let ((stack '()))
(loop for v across s
do (if (char= v #\))
(if (or (null stack) (char/= (pop stack) #\())
(return-from nesting 0))
(push v stack)))
(if (null stack) 1 0))))
This treats the string like a balance counter: open parentheses add one, closing ones remove one.