leetcode

A collection of 11 post

Two Sum

Problem Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. Example 1: Example…

Reverse Integer

Problem Given a 32-bit signed integer, reverse digits of an integer. Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: . For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows…

Palindrome Number

Problem Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Follow up: Could you solve it without converting the integer to a string? Example 1: Example 2: Example 3: Example 4: Constraints: -231 <= x <= 231 - 1 My Solution success…

Roman to Integer

Problem Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, is written as in Roman numeral, just two one’s added together. is written as , which is simply . The number is written as , which is . Roman…

Longest Common Prefix

Problem Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Example 2: Constraints: 0 <= strs.length <= 200 0 <= strsi.length <= 200 strsi consists of only lower-case English letters. My Solution…

Valid Parentheses

Problem Given a string s containing just the characters , , , , and , determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Example 1: Example 2: Example 3: Example…

Merge Two Sorted Lists

Problem Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists. Example 1: Example 2: Example 3: Constraints: The number of nodes in both lists is in the range 0, 50. -100 <= Node.val <= 100 Both l1 and l2 are…

Remove Duplicates from Sorted Array

Problem Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Clarification: Confused why the…

Remove Element

Problem Given an array nums and a value , remove all instances of that value in-place and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with extra memory. The order of elements can be changed. It doesn’t matter what you…

Implement strStr()

Problem Implement . Return the index of the first occurrence of needle in haystack, or -1 if is not part of . Clarification: What should we return when is an empty string? This is a great question to ask during an interview. For the purpose of this problem, we will return 0 when is an empty…

Search Insert Position

Problem Given a sorted array of distinct integers 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. Example 1: Example 2: Example 3: Example 4: Example 5: Constraints: 1 <= nums.length <= 104 -104 <= numsi <= 10…