Search a 2D Matrix II

Title: Search a 2D Matrix II Source: leetcode.com Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom. For example, ...

Rotate Image

Title: Rotate Image Source: leetcode.com You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? Java solution Java /* https://leetcode.com/problems/rotate-image/ */ class RotateImage { public static void main(String args[]) { int[][] image = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16} }; ...

Remove Element

Title: Remove Element Source: leetcode.com Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn’t matter what you leave beyond the new length. Java solution Java /* https://leetcode.com/problems/remove-element/ */ class RemoveElement { public int removeElement(int[] nums, int ...

Perfect Squares

Title: Perfect Squares Source: leetcode.com Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 ...

Number of Islands

Title: Number of Islands Source: leetcode.com Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. Example 1: 11110 ...

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 ...