Ignore testing syntax highlighting

My Signature Avatar

Nicholas Njoki

1 min read

Code

You can ignore this note.

function twoSum(nums: number[], target: number): number[] {
  let output: number[] = [];
  for (let i = 0; i < nums.length; i++) {
    for (let j = i + 1; j < nums.length; j++) {
      if (nums[i] + nums[j] === target) {
        output.push(i, j);
        break;
      }
    }
  }
  return output;
}
class MinStack {
  private stack: number[];
  private min: number[];

  constructor() {
    this.stack = [];
    this.min = [];
  }

  push(val: number): void {
    if (this.min.length === 0) {
      this.min.push(val);
    } else {
      if (val <= this.min[this.min.length - 1]) {
        this.min.push(val);
      }
    }
    this.stack.push(val);
  }

  pop(): void {
    if (this.top() === this.min[this.min.length - 1]) {
      this.min.pop();
    }
    this.stack.pop();
  }

  top(): number {
    return this.stack[this.stack.length - 1];
  }

  getMin(): number {
    return this.min[this.min.length - 1];
  }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * var obj = new MinStack()
 * obj.push(val)
 * obj.pop()
 * var param_3 = obj.top()
 * var param_4 = obj.getMin()
 */
function reverseList(head: ListNode | null): ListNode | null {
  let current = head;
  let prev = null;
  while (current !== null) {
    let temp = current.next;
    current.next = prev;
    prev = current;
    current = temp;
  }
  return prev;
}

Enjoyed the post?

Subscribe if you're interested in nerdy stuff.

0  subscribers • View all issues  • 
My Signature Avatar
Edit this page on GitHub

Last Updated: 

Go back to note