Number of Digit One

Title: Number of Digit One Source: leetcode.com Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example: Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. Hints: Beware of overflow. Java ...

N-Queens

Title: N-Queens Source: leetcode.com The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. image source: leetcode.com Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of the n-queens’ placement, where 'Q' and ...

Move Zeroes

Title: Move Zeroes Source: leetcode.com Given an array nums, write a function to move all 0‘s to the end of it while maintaining the relative order of the non-zero elements. For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0]. Note: You must ...

Missing Number

Title: Missing Number Source: leetcode.com Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example, Given nums = [0, 1, 3] return 2. Note: Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra ...

Minimum Size Subarray Sum

Title: Minimum Size Subarray Sum Source: leetcode.com Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn’t one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7, the subarray [4,3] has the minimal ...

Minimum Path Sum

Title: Minimum Path Sum Source: leetcode.com Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. Java solution Java /* https://leetcode.com/problems/minimum-path-sum/ */ ...

Merge Intervals

Title: Merge Intervals Source: leetcode.com Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. Java solution Java /* https://leetcode.com/problems/merge-intervals/ */ import java.util.List; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Stack; public class MergeIntervals { public List<Interval> merge(List<Interval> intervals) { this.sort(intervals); Stack<Interval> stack = new Stack<>(); for(Interval interval : intervals) ...

Maximum Subarray

Title: Maximum Subarray Source: leetcode.com Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have figured out the O(n) solution, try coding another solution using the divide ...

Kth Smallest Element in a BST

Title: Kth Smallest Element in a BST Source: leetcode.com Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST’s total elements. Follow up: What if the BST is modified (insert/delete operations) often and you need ...

Implement Stack using Queues

Title: Implement Stack using Queues Source: leetcode.com Implement the following operations of a stack using queues. push(x) — Push element x onto stack. pop() — Removes the element on top of the stack. top() — Get the top element. empty() — Return whether the stack is empty. Notes: You must use only standard operations of ...