26. Remove Duplicates from Sorted Array
Given an integer array nums
sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums
.
Consider the number of unique elements of nums
to be k
, to get accepted, you need to do the following things:
- Change the array
nums
such that the firstk
elements ofnums
contain the unique elements in the order they were present innums
initially. The remaining elements ofnums
are not important as well as the size ofnums
. - Return
k
.
//1 Mine
class Solution {
public int removeDuplicates(int[] nums) {
int m = 1;
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] == nums[i + 1]) {
} else {
nums[m] = nums[i + 1];
m++;
}
}
return m;
}
}
//2 Standard
class Solution {
public int removeDuplicates(int[] nums) {
int j = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i] != nums[i - 1]) {
nums[j] = nums[i];
j++;
}
}
return j;
}
}
27. Remove Element
Given an integer array nums
and an integer val
, remove all occurrences of val
in nums
in-place. The order of the elements may be changed. Then return the number of elements in nums
which are not equal to val
.
Consider the number of elements in nums
which are not equal to val
be k
, to get accepted, you need to do the following things:
- Change the array
nums
such that the firstk
elements ofnums
contain the elements which are not equal toval
. The remaining elements ofnums
are not important as well as the size ofnums
. - Return
k
.
Example 1:
Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,_,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
It does not matter what you leave beyond the returned k (hence they are underscores).
class Solution {
public int removeElement(int[] nums, int val) {
int j = 0;
for (int i = 0; i < nums.length; i++){
if (nums[i] != val){
nums[j] = nums [i];
j++;
}
}
return j;
}
}
35. Search Insert Position
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.
You must write an algorithm with O(log n)
runtime complexity.
Example 1:
Input: nums = [1,3,5,6], target = 5
Output: 2
Example 2:
Input: nums = [1,3,5,6], target = 2
Output: 1
Example 3:
Input: nums = [1,3,5,6], target = 7
Output: 4
class Solution {
public int searchInsert(int[] nums, int target) {
for (int i = 0; i < nums.length; i++){
if (nums[i] >= target) {
return i;
}
}
return nums.length;
}
}
9. Palindrome Number
Given an integer x, return true if x is a palindrome , and false otherwise.
class Solution {
public boolean isPalindrome(int x) {
if (x < 0) {
return false;
} else {
int re = 0;
int o = x;
while (x > 0){
re = (re * 10) + (x % 10);
x/= 10; // x/=10 means x = x / 10.
}
return re == o;
}
}
}
66. Plus One
You are given a large integer represented as an integer array digits
, where each digits[i]
is the ith
digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0
's.
Increment the large integer by one and return the resulting array of digits.
**Input:** digits = [1,2,3]
**Output:** [1,2,4]
**Input:** digits = [9]
**Output:** [1,0]
By using reverse for loop:
class Solution {
public int[] plusOne(int[] digits) {
for (int i = digits.length - 1; i >= 0; i--){
if (digits[i] != 9) {
digits[i] += 1;
return digits;
} else {
digits[i] = 0;
}
}
int[] nd = new int[digits.length + 1];
nd[0] = 1;
return nd;
}
}
13. Roman to Integer
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, 2
is written as II
in Roman numeral, just two ones added together. 12
is written as XII
, which is simply X + II
. The number 27
is written as XXVII
, which is XX + V + II
.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII
. Instead, the number four is written as IV
. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX
. There are six instances where subtraction is used:
I
can be placed beforeV
(5) andX
(10) to make 4 and 9.X
can be placed beforeL
(50) andC
(100) to make 40 and 90.C
can be placed beforeD
(500) andM
(1000) to make 400 and 900.
Given a roman numeral, convert it to an integer.
class Solution {
public int romanToInt(String s) {
int ans = 0, num = 0;
for (int i = s.length() - 1; i >= 0; i--) {
switch (s.charAt(i)) {
case 'I':
num = 1;
break;
case 'V':
num = 5;
break;
case 'X':
num = 10;
break;
case 'L':
num = 50;
break;
case 'C':
num = 100;
break;
case 'D':
num = 500;
break;
case 'M':
num = 1000;
break;
}
if (4 * num < ans)
ans -= num;
else
ans += num;
}
return ans;
}
}
14. Longest Common Prefix
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:**
**Input:** strs = ["flower","flow","flight"]
**Output:** "fl"
**Example 2:**
**Input:** strs = ["dog","racecar","car"]
**Output:** ""
**Explanation:** There is no common prefix among the input strings.
class Solution {
public String longestCommonPrefix(String[] strs) {
int min = Integer.MAX_VALUE;
for (String str: strs) {
min = Math.min(min, str.length());
}
for (int i = 0; i < min; i++) {
char compare = strs[0].charAt(i);
for (int j = 1; j < strs.length; j++) {
if (strs[j].charAt(i) != compare) {
return strs[0].substring(0, i);
}
}
}
return strs[0].substring(0, min);
}
}
20. Valid Parentheses
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.
- Every close bracket has a corresponding open bracket of the same type.
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "()[]{}"
Output: true
Example 3:
Input: s = "(]"
Output: false
Example 4:
Input: s = "([])"
Output: true
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for (char p:s.toCharArray()){
switch(p){
case '(':
case '{':
case '[':
stack.push(p);
break;
case ')':
if (stack.isEmpty() || stack.pop()!= '('){return false;}
break;
case ']':
if (stack.isEmpty() || stack.pop()!= '['){return false;}
break;
case '}':
if (stack.isEmpty() || stack.pop()!= '{'){return false;}
break;
default:
return false;
}
}
return stack.isEmpty();
}
}