# Leetcode 15. 3sum two pointers

## Question

`input array: nums = [-1,0,1,2,-1,-4]sort input array: nums = [-4,-1,-1,0,1,2]`

# Encapsulation

Encapsulation is binding variables and methods together. In encapsulation, the variables of a class are private, other classes can only access they (modify and review the variables values) through the public getter and setter methods you provided. So encapsulation is also known as data hiding.

`public class Encap{   private String name   public String getName(){      return name…`

# Binary Search

“Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.”

# Template and Explanation

`var binarySearch=function(l, r){  while (l <= r){    m = Math.floor(l +…`

# Stacks

## Definition

“Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).”

## Implement Stacks Using Arrays

`class Stack{  constructor(){    this.items = []  }isEmpty(){    return this.items.length === 0  }push(data){    this.items.push(data)  }pop(){    if(this.isEmpty()){      return null    }    return this.items.pop()  }}let s = new Stack()s.push(1)s.push(2)s.push(3)console.log(s)…`

# Time Complexity

“In computer science, the time complexity is the computational complexity that describes the amount of time it takes to run an algorithm.”

` 5n² + 3n + log2(n)`

# LeetCode 5 Expand Around Center Solution in Javascript

## How to find a palindromic substring (expand around center)

Starting from the center of a substring to evaluate wether the substring is a palindrome. If the letter on the left side of the center `s[center-1]` equals the letter on the right side of the center `s[center+1]`, we keep moving to the next letter on each side, until we hit unequal:

`s[center…`

# null vs undefined

## primitive values

JavaScript sets a value to one of six primitive data types:

• Undefined (a variable with no defined value)
• Null (a single null value)
• Boolean (true or false)
• Number (this includes `Infinity` and `NaN` – not a number!)
• String (textual data)
• Symbol (a unique and immutable primitive new to ES6/2015)

## Falsy values

• `false`
• `…`

# Javascript var, let and const

## Scope

Var can be used for declaring globally scoped variable and function scoped variable.

`var a = 1function example(){  var b = 2  console.log(b) // 2 }console.log(a) // 1`

# Closure in Javascript

## Key Points:

Closure is a function returns another function.

## Example:

We have an outer function called outer(). It creates a local variable called counter and a function called inner(). …

## Cindy Zheng

Get the Medium app