﻿﻿﻿﻿﻿ java Archives - Page 3 of 10 - Coddicted

## Unique Binary Search Trees

Title: Unique Binary Search Trees Source: leetcode.com Given n, how many structurally unique BST’s (binary search trees) that store values 1…n? For example, Given n = 3, there are a total of 5 unique BST’s. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ ...

## Ugly Number II

Title: Ugly Number II Source: leetcode.com Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers. Note that 1 is typically treated ...

## Sudoku Solver

Title: Sudoku Solver Source: leetcode.com 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. Java solution Java /* https://leetcode.com/problems/sudoku-solver/ */ class SudokuSolver { public void solveSudoku(char[][] board) { int N = board.length; ...

## Sqrt

Title: Sqrt(x) Source: leetcode.com Implement int sqrt(int x). Compute and return the square root of x. Java solution Java /* https://leetcode.com/problems/sqrtx/ */ class Sqrt { public static void main(String args[]) throws Exception { Sqrt solution = new Sqrt(); System.out.println(solution.mySqrt(2147395599)); } public int mySqrt(int x) throws Exception { if(x==0 || x==1) return x; long lo = ...

## Sort Colors

Title: Sort Colors Source: leetcode.com Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue ...

## Single Number III

Title: Single Number III Source: leetcode.com Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. For example: Given nums = [1, 2, 1, 3, 2, 5], return [3, 5]. Note: The order of the ...

## 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} }; ...

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