Articles
-
307 - Range Sum Query - Mutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. The update(i, val) function modifies nums by updating the element at index i to val.
-
306 - Additive Number
Additive number is a positive integer whose digits can form additive sequence. A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two.
-
Quick Sort
Quick sort an integer array
-
9 - Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.
-
6 - ZigZag Conversion
The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
-
14 - Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
-
22 - Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
-
30 - Substring with Concatenation of All Words
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.
-
32 - Longest Valid Parentheses
Given a string containing just the characters ‘(‘ and ‘)’, find the length of the longest valid (well-formed) parentheses substring.
-
37 - Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character ‘.’. You may assume that there will be only one unique solution.
-
43 - Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string.
-
59 - Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order.
-
54 - Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
-
Swap two digits in a integer
Swap two digits within a integer. For instance, 123456, swap 2nd and 4th digit, output 125436
-
68 - Text Justification
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ‘ ‘ when necessary so that each line has exactly L characters. Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right. For the last line of text, it should be left justified and no extra space is inserted between words.
-
85 - Maximal Rectangle
Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing all ones and return its area.
-
91 - Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:
-
93 - Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combinations.
-
304 - Range Sum Query 2D - Immutable
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by (row1, col1), (row2, col2).
-
99 - Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure.
-
100 - Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
-
101 - Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
-
113 - Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.
-
303 - Range Sum Query - Immutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
-
117 - Populating Next Right Pointers in Each Node II
Follow up for problem “Populating Next Right Pointers in Each Node”. What if the given tree could be any binary tree? Would your previous solution still work?
-
116 - Populating Next Right Pointers in Each Node
Given a binary tree, populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, all next pointers are set to NULL.
-
119 - Pascal's Triangle II
Given an index k, return the kth row of the Pascal’s triangle.
-
118 - Pascal's Triangle
Given numRows, generate the first numRows of Pascal’s triangle.
-
50 - Pow(x, n)
Implement pow(x, n).
-
What is the difference between a thread and a process?
A process is an executing instance of an application. What does that mean? Well, for example, when you double-click the Microsoft Word icon, you start a process that runs Word. A thread is a path of execution within a process. Also, a process can contain multiple threads. When you start Word, the operating system creates a process and begins executing the primary thread of that process.
It’s important to note that a thread can do anything a process can do. But since a process can consist of multiple threads, a thread could be considered a ‘lightweight’ process. Thus, the essential difference between a thread and a process is the work that each one is used to accomplish. Threads are used for small tasks, whereas processes are used for more ‘heavyweight’ tasks – basically the execution of applications.
Another difference between a thread and a process is that threads within the same process share the same address space, whereas different processes do not. This allows threads to read from and write to the same data structures and variables, and also facilitates communication between threads. Communication between processes – also known as IPC, or inter-process communication – is quite difficult and resource-intensive.
MultiThreading
Threads, of course, allow for multi-threading. A common example of the advantage of multithreading is the fact that you can have a word processor that prints a document using a background thread, but at the same time another thread is running that accepts user input, so that you can type up a new document.
If we were dealing with an application that uses only one thread, then the application would only be able to do one thing at a time – so printing and responding to user input at the same time would not be possible in a single threaded application.
Each process has it’s own address space, but the threads within the same process share that address space. Threads also share any other resources within that process. This means that it’s very easy to share data amongst threads, but it’s also easy for the threads to step on each other, which can lead to bad things.
Multithreaded programs must be carefully programmed to prevent those bad things from happening. Sections of code that modify data structures shared by multiple threads are called critical sections. When a critical section is running in one thread it’s extremely important that no other thread be allowed into that critical section. This is called synchronization, which we wont get into any further over here. But, the point is that multithreading requires careful programming.
Also, context switching between threads is generally less expensive than in processes. And finally, the overhead (the cost of communication) between threads is very low relative to processes.
Here’s a summary of the differences between threads and processes:
-
Threads are easier to create than processes since they
don't require a separate address space.</p> -
Multithreading requires careful programming since threads
share data strucures that should only be modified by one thread</p> at a time. Unlike threads, processes don't share the same</p> address space.</p> -
Threads are considered lightweight because they use far
less resources than processes.</p> -
Processes are independent of each other. Threads, since they
share the same address space are interdependent, so caution</p> must be taken so that different threads don't step on each other.</p> This is really another way of stating #2 above.</p> -
A process can consist of multiple threads.
-