Roman to Integer

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. 1 2 3 4 5 6 7 8 Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II....

<span title='2020-09-06 00:00:00 +0000 UTC'>September 6, 2020</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;volyx

Plus One

Given a non-empty array of digits representing a non-negative integer, increment one to the integer. The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit. You may assume the integer does not contain any leading zero, except the number 0 itself. Example 1: 1 2 3 Input: [1,2,3] Output: [1,2,4] Explanation: The array represents the integer 123....

<span title='2020-07-12 00:00:00 +0000 UTC'>July 12, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, calculate the Hamming distance. Note: 0 ≤ x, y < 231. 1 2 3 4 5 6 7 8 9 10 11 Example: Input: x = 1, y = 4 Output: 2 Explanation: 1 (0 0 0 1) 4 (0 1 0 0) ↑ ↑ The above arrows point to positions where the corresponding bits are different....

<span title='2020-07-07 00:00:00 +0000 UTC'>July 7, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree [3,9,20,null,null,15,7], 1 2 3 4 5 3 / \ 9 20 / \ 15 7 return its bottom-up level order traversal as: 1 2 3 4 5 [ [15,7], [9,20], [3] ] Solution: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 /** * Definition for a binary tree node....

<span title='2020-07-03 00:00:00 +0000 UTC'>July 3, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Arranging Coins

You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can be formed. n is a non-negative integer and fits within the range of a 32-bit signed integer. Example 1: 1 2 3 4 5 6 n = 5 The coins can form the following rows: ¤ ¤ ¤ ¤ ¤ Because the 3rd row is incomplete, we return 2....

<span title='2020-07-02 00:00:00 +0000 UTC'>July 2, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Search in a Binary Search Tree

Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node’s value equals the given value. Return the subtree rooted with that node. If such node doesn’t exist, you should return NULL. For example, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Given the tree: 4 / \ 2 7 / \ 1 3 And the value to search: 2 You should return this subtree: 2 / \ 1 3 In the example above, if we want to search the value 5, since there is no node with value 5, we should return NULL....

<span title='2020-06-26 00:00:00 +0000 UTC'>June 26, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Climbing Stairs

You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Note: Given n will be a positive integer. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Example 1: Input: 2 Output: 2 Explanation: There are two ways to climb to the top....

<span title='2020-06-25 00:00:00 +0000 UTC'>June 25, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Longest Continuous Increasing Subsequence

Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray). 1 2 3 4 5 6 Example 1: Input: [1,3,5,4,7] Output: 3 Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3. Even though [1,3,5,7] is also an increasing subsequence, it's not a continuous one where 5 and 7 are separated by 4. 1 2 3 4 5 Example 2: Input: [2,2,2,2,2] Output: 1 Explanation: The longest continuous increasing subsequence is [2], its length is 1....

<span title='2020-06-23 00:00:00 +0000 UTC'>June 23, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Max Consecutive Ones

Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: 1 2 3 4 Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3. Note: The input array will only contain 0 and 1. The length of input array is a positive integer and will not exceed 10,000 Solution:...

<span title='2020-06-23 00:00:00 +0000 UTC'>June 23, 2020</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;volyx

Verifying an Alien Dictionary

In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters. Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographicaly in this alien language. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Example 1: Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz" Output: true Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted....

<span title='2020-06-23 00:00:00 +0000 UTC'>June 23, 2020</span>&nbsp;·&nbsp;4 min&nbsp;·&nbsp;volyx

Sort Array By Parity

Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A. You may return any answer array that satisfies this condition. 1 2 3 4 5 Example 1: Input: [3,1,2,4] Output: [2,4,3,1] The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted. Note: 1 <= A.length <= 5000 0 <= A[i] <= 5000 Solution: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class Solution { public int[] sortArrayByParity(int[] A) { int n = A....

<span title='2020-06-19 00:00:00 +0000 UTC'>June 19, 2020</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;volyx

Sort Array By Parity II

Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even. Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even. You may return any answer array that satisfies this condition. Example 1: 1 2 3 Input: [4,2,5,7] Output: [4,5,2,7] Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted. Note:...

<span title='2020-06-19 00:00:00 +0000 UTC'>June 19, 2020</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;volyx

53. Maximum Subarray

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Example: 1 2 3 Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6. Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle. Solution: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Solution { public int maxSubArray(int[] nums) { if (nums....

<span title='2020-06-17 00:00:00 +0000 UTC'>June 17, 2020</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;volyx

Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Example 1: 1 2 Input: [1,3,5,6], 5 Output: 2 Example 2: 1 2 Input: [1,3,5,6], 2 Output: 1 Example 3: 1 2 Input: [1,3,5,6], 7 Output: 4 Example 4: 1 2 Input: [1,3,5,6], 0 Output: 0 Solution:...

<span title='2020-06-16 00:00:00 +0000 UTC'>June 16, 2020</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;volyx

Toeplitz Matrix

A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given an M x N matrix, return True if and only if the matrix is Toeplitz. Example 1: 1 2 3 4 5 6 7 8 9 10 11 Input: matrix = [ [1,2,3,4], [5,1,2,3], [9,5,1,2] ] Output: True Explanation: In the above grid, the diagonals are: "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]"....

<span title='2020-06-09 00:00:00 +0000 UTC'>June 9, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx