Determine if a string has all unique characters
Problem Implement an algorithm to determine if a string has all the characters…
September 03, 2020
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
Input: digits = [1,2,3]
Output: [1,2,4]
It has very simple solution, just by iterating array from the end. And, keeping track of carry.
A simple complexity here is that result can be bigger than original array if we got a carry for the last sum.
public int[] plusOne(int[] digits) {
int l = digits.length;
//initializing carry with the number we want to add for first time.
int carry = 1;
for (int i=l-1; i>=0; i--) {
digits[i] = digits[i] + carry;
carry = digits[i]/10;
digits[i] = digits[i]%10;
}
// copy result to another array
int targetSize = carry == 1 ? l+1 : l;
int[] res = new int[targetSize];
int i=0;
if (carry == 1) {
res[0] = carry;
i = 1;
}
for (; i<targetSize; i++) {
res[i] = digits[i-carry];
}
return res;
}
Its O(n)
Problem Implement an algorithm to determine if a string has all the characters…
Problem Statement Roman numerals are represented by seven different symbols: I…
Problem Statement The string “PAYPALISHIRING” is written in a zigzag pattern on…
Problem Statement Maximum Length of Subarray With Positive Product. Given an…
Problem Statement Given a linked list, swap every two adjacent nodes and return…
In this post, we will see some of the frequently used concepts/vocabulary in…
Introduction Strapi is a backend system provides basic crud operations with…
Introduction I had to create many repositories in an Github organization. I…
Introduction I was trying to download some youtube videos for my kids. As I have…
Introduction In this post, we will explore some useful command line options for…
Introduction In this post, we will see how we can apply a patch to Python and…
Introduction We will introduce a Package Manager for Windows: . In automations…