Not able to solve Plus One Java problem on Leetcode [duplicate] - java

This question already has answers here:
Plus one leetcode
(7 answers)
Closed 4 months ago.
I am trying to solve Plus One on Leetcode
The description is as follows:
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.
Example 1:
Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Incrementing by one gives 123 + 1 = 124.
Thus, the result should be [1,2,4].
My code is:
class Solution {
public int[] plusOne(int[] digits) {
long temp=0;int c=0;
for(int i:digits)
{
temp=temp*10+i;
//System.out.println(temp);
}
//System.out.println(temp);
temp+=1;
//System.out.println(temp);
long copy=temp;
while(copy>0)
{
copy/=10;
c++;
}
int[] result=new int[c];
for(int i=c-1;i>=0;i--)
{
//System.out.println((int)temp%10);
result[i]=(int)temp%10;
// System.out.println(result[i]);
temp/=10;
}
return result;
}
}
The problem is when I am trying to extract the number in the unit's digit it is givig 9 instead of 1.
Expected output:[9,8,7,6,5,4,3,2,1,1]
My Output:[9,8,7,6,5,4,3,2,1,9]

There is nothing really wrong with your code...it just needs a little tweak.
The problem you are having is within the for loop that converts the incremented number held in temp to an int[] Array. Casting the temp % 10 to an int as in result[i] = (int) temp % 10; is going to produce an undesirable result. temp is a long and therefore shouldn't be cast to int until is has finished its modulo calculation. So, the simple solution would be to wrap the equation within parentheses, for example:
result[i] = (int) (temp % 10);
That should solve the immediate problem. An even bigger problem is the fact that you are limited to a numerical value that can only fit into a long data type. An int[] Array can hold an extremely large number if you were to take each element of that array and sequentially combine the digits. A long data type for example can only hold an unsigned value of 9223372036854775807. As you can see, this would create an int[] Array of 19 elements and that's a small Array.
What if the Array contained 100 elements (digits)? That's a pretty big number but not necessarily out of the realm of use. This is why you have been advised (in comments) to utilize the java.math.BigInteger class. With BigInteger, you can work with any size of integer number which is basically what you really need. Here is how you could carry out the same task using BigInteger:
public int[] plusOne(int[] digits) {
java.math.BigInteger num = java.math.BigInteger.ZERO;
// Convert int[] Array to a BigInteger number...
for (int b : digits) {
num = num.multiply(java.math.BigInteger.valueOf(10))
.add(java.math.BigInteger.valueOf(b));
}
// Add 1 to the determined BigInteger number
num = num.add(java.math.BigInteger.valueOf(1));
// Convert BigInteger number back to an Array
digits = num.toString().chars().map(c -> c - '0').toArray();
return digits;
}
As an example, let's run this method against an int[] Array of 100 elements. That would be an integer number consisting of 100 digits. Now that's a large number:
/* Auto-create an int[] array containing 100 elements of random digits
0 to 9 (the first digit of the Array will never be a 0). */
int[] array = new int[100];
for (int i = 0; i < 100; i++) {
int n = new java.util.Random().nextInt(9);
// Make sure the first digit isn't a 0.
if (i == 0 && n == 0) {
i--;
continue;
}
array[i] = new java.util.Random().nextInt(9);
}
System.out.println("Original: -> " + Arrays.toString(array));
/* Cycle the plusOne() method 12 times and display the array
within each cycle... */
for (int i = 0; i < 12; i++) {
array = plusOne(array);
// Display The array:
System.out.println("Plus One: -> " + Arrays.toString(array));
}
The Console Window should display something like this. You will need to scroll to the end to see the incrementing (plus one):
Original: -> [7, 8, 8, 3, 7, 3, 2, 1, 4, 6, 8, 8, 0, 7, 5, 8, 1, 7, 7, 0, 2, 2, 3, 2, 7, 2, 2, 2, 3, 4, 6, 7, 4, 6, 1, 4, 2, 4, 3, 1, 0, 4, 2, 8, 3, 4, 3, 8, 3, 6, 2, 5, 6, 6, 3, 5, 1, 3, 0, 7, 0, 0, 8, 7, 0, 0, 2, 4, 8, 5, 0, 6, 0, 2, 3, 5, 0, 1, 3, 3, 4, 1, 2, 8, 8, 1, 8, 3, 0, 5, 8, 8, 2, 2, 6, 6, 3, 5, 2, 6]
Plus One: -> [7, 8, 8, 3, 7, 3, 2, 1, 4, 6, 8, 8, 0, 7, 5, 8, 1, 7, 7, 0, 2, 2, 3, 2, 7, 2, 2, 2, 3, 4, 6, 7, 4, 6, 1, 4, 2, 4, 3, 1, 0, 4, 2, 8, 3, 4, 3, 8, 3, 6, 2, 5, 6,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2, 5, 6, 6, 3, 5, 1, 3, 0, 7, 0, 0, 8, 7, 0, 0, 2, 4, 8, 5, 0, 6, 0, 2, 3, 5, 0, 1, 3, 3, 4, 1, 2, 8, 8, 1, 8, 3, 0, 5, 8, 8, 2, 2, 6, 6, 3, 5, 3, 7]
Plus One: -> [7, 8, 8, 3, 7, 3, 2, 1, 4, 6, 8, 8, 0, 7, 5, 8, 1, 7, 7, 0, 2, 2, 3, 2, 7, 2, 2, 2, 3, 4, 6, 7, 4, 6, 1, 4, 2, 4, 3, 1, 0, 4, 2, 8, 3, 4, 3, 8, 3, 6, 2, 5, 6, 6, 3, 5, 1, 3, 0, 7, 0, 0, 8, 7, 0, 0, 2, 4, 8, 5, 0, 6, 0, 2, 3, 5, 0, 1, 3, 3, 4, 1, 2, 8, 8, 1, 8, 3, 0, 5, 8, 8, 2, 2, 6, 6, 3, 5, 3, 8]

This is pretty straight forward solution. Just what you do in the school: increment right to left with keeping carry for the next digit.
class Solution {
public int[] plusOne(int[] digits) {
boolean carry = true;
for (int i = digits.length - 1; carry && i >= 0; i--) {
carry = digits[i] == 9;
digits[i] = carry ? 0 : digits[i] + 1;
}
if (carry) {
int[] tmp = new int[digits.length + 1];
tmp[0] = 1;
System.arraycopy(digits, 0, tmp, 1, digits.length);
digits = tmp;
}
return digits;
}
}

Related

Java; saving array of arrays into collection

So I have this data
{ { 1, 3, 5, 3, 1 },
{ 3, 5, 6, 5, 1 },
{ 7, 2, 3, 5, 0 },
{ 12, 1, 5, 3, 0 },
{ 20, 6, 3, 6, 1 },
{ 20, 7, 4, 7, 1 } }
and i want to save it into some kind of collection, list or set. So if that collection was named List,if i were to type List[0][3] it would reffer to int 4.
I tried with
ArrayList<int[]> myNumberList = new ArrayList<int[]>();
but i have trouble putting that data into list
The array access operator [] is only applicable to arrays. So you can only create 2-dimensional array.
int a[][] = new int[][]{
{1, 3, 5, 3, 1},
{3, 5, 6, 5, 1},
{7, 2, 3, 5, 0},
{12, 1, 5, 3, 0},
{20, 6, 3, 6, 1},
{20, 7, 4, 7, 1}
};
System.out.println(a[0][3]);
But you can't create any type of collection that can use [] to access it's values.
Yoy can still use List of arrays. But you will have to index first dimension, using get() method
List<int[]> a2 = Arrays.asList(
new int[]{1, 3, 5, 3, 1},
new int[]{3, 5, 6, 5, 1},
new int[]{7, 2, 3, 5, 0},
new int[]{12, 1, 5, 3, 0},
new int[]{20, 6, 3, 6, 1},
new int[]{20, 7, 4, 7, 1}
);
System.out.println(a2.get(0)[3]);
You could make it an Integer[][] and create a List<List<Integer>>. Something like,
Integer[][] arr = { { 1, 3, 5, 3, 1 }, { 3, 5, 6, 5, 1 },
{ 7, 2, 3, 5, 0 }, { 12, 1, 5, 3, 0 }, { 20, 6, 3, 6, 1 },
{ 20, 7, 4, 7, 1 } };
System.out.println(Arrays.deepToString(arr));
List<List<Integer>> al = new ArrayList<>();
for (Integer[] inArr : arr) {
al.add(Arrays.asList(inArr));
}
System.out.println(al);
which outputs (formatted for this post)
[[1, 3, 5, 3, 1], [3, 5, 6, 5, 1], [7, 2, 3, 5, 0],
[12, 1, 5, 3, 0], [20, 6, 3, 6, 1], [20, 7, 4, 7, 1]]
[[1, 3, 5, 3, 1], [3, 5, 6, 5, 1], [7, 2, 3, 5, 0],
[12, 1, 5, 3, 0], [20, 6, 3, 6, 1], [20, 7, 4, 7, 1]]
Hard to answer what do you really need in your particular case. But in general list-equivalent of 2-dimensional array, which, I guess, you are looking for, will be List<List<Integer>> type, and in java-8 you can convert it in such way:
int a[][] = new int[][]{
{1, 3, 5, 3, 1},
{3, 5, 6, 5, 1},
{7, 2, 3, 5, 0},
{12, 1, 5, 3, 0},
{20, 6, 3, 6, 1},
{20, 7, 4, 7, 1}};
List<List<Integer>> l2 = new ArrayList<>();
Stream.of(a).forEach(a1 -> l2.add(Arrays.stream(a1).boxed().collect(Collectors.toList())));

Simple Java application not working

This pertains to a problem that I solved with far more elegant code. I was fooling around and trying to solve the problem in a different way using very basic code. The problem is that the number I am getting (2091059712) is not correct. The correct answer is 235146240000. This is driving me insane. Here is the broken code. I am trying to find the 13 adjacent digits within a 1000 digit number that have the largest product.
public class AdjacentProducts {
public static void main(String[] args) {
int[] x = new int[] { 7, 3, 1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 0, 6, 2,
4, 9, 1, 9, 2, 2, 5, 1, 1, 9, 6, 7, 4, 4, 2, 6, 5, 7, 4, 7, 4,
2, 3, 5, 5, 3, 4, 9, 1, 9, 4, 9, 3, 4, 9, 6, 9, 8, 3, 5, 2, 0,
3, 1, 2, 7, 7, 4, 5, 0, 6, 3, 2, 6, 2, 3, 9, 5, 7, 8, 3, 1, 8,
0, 1, 6, 9, 8, 4, 8, 0, 1, 8, 6, 9, 4, 7, 8, 8, 5, 1, 8, 4, 3,
8, 5, 8, 6, 1, 5, 6, 0, 7, 8, 9, 1, 1, 2, 9, 4, 9, 4, 9, 5, 4,
5, 9, 5, 0, 1, 7, 3, 7, 9, 5, 8, 3, 3, 1, 9, 5, 2, 8, 5, 3, 2,
0, 8, 8, 0, 5, 5, 1, 1, 1, 2, 5, 4, 0, 6, 9, 8, 7, 4, 7, 1, 5,
8, 5, 2, 3, 8, 6, 3, 0, 5, 0, 7, 1, 5, 6, 9, 3, 2, 9, 0, 9, 6,
3, 2, 9, 5, 2, 2, 7, 4, 4, 3, 0, 4, 3, 5, 5, 7, 6, 6, 8, 9, 6,
6, 4, 8, 9, 5, 0, 4, 4, 5, 2, 4, 4, 5, 2, 3, 1, 6, 1, 7, 3, 1,
8, 5, 6, 4, 0, 3, 0, 9, 8, 7, 1, 1, 1, 2, 1, 7, 2, 2, 3, 8, 3,
1, 1, 3, 6, 2, 2, 2, 9, 8, 9, 3, 4, 2, 3, 3, 8, 0, 3, 0, 8, 1,
3, 5, 3, 3, 6, 2, 7, 6, 6, 1, 4, 2, 8, 2, 8, 0, 6, 4, 4, 4, 4,
8, 6, 6, 4, 5, 2, 3, 8, 7, 4, 9, 3, 0, 3, 5, 8, 9, 0, 7, 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 7, 5, 2, 9, 6, 3, 4, 5, 0 };
int a = 0;
int b = 1;
int c = 2;
int d = 3;
int e = 4;
int f = 5;
int g = 6;
int h = 7;
int i = 8;
int j = 9;
int k = 10;
int l = 11;
int m = 12;
long result = 0;
long largest = 0;
while (m < 1000) {
result = x[a] * x[b] * x[c] * x[d] * x[e] * x[f] * x[g] * x[h]
* x[i] * x[j] * x[k] * x[l] * x[m];
if (result > largest) {
largest = result;
a++;
b++;
c++;
d++;
e++;
f++;
g++;
h++;
i++;
j++;
k++;
l++;
m++;
} else {
a++;
b++;
c++;
d++;
e++;
f++;
g++;
h++;
i++;
j++;
k++;
l++;
m++;
}
}
System.out.println(largest);
}
}
Every multiplication is another chance for overflow. Your answer overflows before it ever gets put in result. One solution would be to change the definition of your array to
long[] x = new long[]
The same problem shows up in Puzzler #3 in the Java Puzzlers book:
public class LongDivision {
public static void main(String[] args) {
final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;
final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY;
}
}
Here's the explanation Bloch and Gafter give:
The problem is that the computation of the constant MICROS_PER_DAY does overflow. Although the result of the computation fits in a long with room to spare, it doesn't fit in an int. The computation is performed entirely in int arithmetic and only after the computation completes is the result promoted to a long. By then, it's too late.
...
So why is the computation performed in int arithmetic? Because all the factors that are multiplied together are int values. When you multiply two int values you get another int value. Java does not have target typing, a language feature wherein the type of the variable in which a result is to be stored influences the type of the computation.
(Java uses target typing in certain contexts, in array initializers and now with lambdas, just not in general.)
The same text also points out that only the first value in the expression being multiplied needs to be a long, so you could keep the declaration of the array using ints and only change this line:
result = 1L * x[a] * x[b] * x[c] * x[d] * x[e] * x[f] * x[g] * x[h]
* x[i] * x[j] * x[k] * x[l] * x[m];

Java ArrayList clone improved run-time

This is the question:https://leetcode.com/problems/combinations/
This is my solution 1:
public class Solution {
public List<List<Integer>> combine(int n, int k){
List<List<Integer>> result = new ArrayList<List<Integer>>();
combine(n, k, 1, result, new ArrayList<Integer>());
return result;
}
public void combine(int n, int k , int start, List<List<Integer>> result, ArrayList<Integer> l){
if(k == 0){
result.add(l);
return;
}
for(int i = start; i <= n; ++i){
l.add(i);
combine(n, k - 1, i + 1, result, l);
}
}
}
Result: small test cases passed. But big test cases time exceed.
Submission Result: Time Limit Exceeded
Last executed input: 10, 5
Solution 2:
public class Solution {
public List<List<Integer>> combine(int n, int k){
List<List<Integer>> result = new ArrayList<List<Integer>>();
combine(n, k, 1, result, new ArrayList<Integer>());
return result;
}
public void combine(int n, int k , int start, List<List<Integer>> result, ArrayList<Integer> l){
if(k == 0){
result.add(l);
return;
}
for(int i = start; i <= n; ++i){
ArrayList<Integer> a = (ArrayList<Integer>) l.clone();
a.add(i);
combine(n, k - 1, i + 1, result, a);
}
}
}
Passed all test cases.
the main difference is clone of the list.
But why?
is the solution A wrong or just slow?
Why using clone is faster here?
Really confused.
The first solution is indeed incorrect. Try invoking combine(5,3), and sending it to System.out, and you'll see the output for the first is:
[[1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5]]
You'll notice that it's the same list in each index position - you really do need to create a new array each time. For the second, correct solution, the output is:
[[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5], [2, 3, 4], [2, 3, 5], [2, 4, 5], [3, 4, 5]]
This means that the first solution is slower because you're adding numbers to a larger and larger list each time. For higher values of n and k, that list can be very large, and copying the backing array of ArrayList when it needs to expand becomes a very expensive operation - much more expensive than copying/creating a number of small lists.

Socket / Telnet-connection / BufferedReader

i have a multi-threaded notification-service which uses a simple protocol. When testing it with telnet it works fine, just one thing keeps bugging me: When i close the session by just closing the (OSX)Terminal window the BufferedReader.readLine reads some weird bytes, and doesn't throw an IOException.
This is the receiving code:
try {
/* wait for input from client */
InputStream in = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charsets.UTF_8));
while (true) {
try {
/* read a line */
String line = reader.readLine();
if (line == null)
break;
L.debug("length {} bytes {}",line.length(),line.getBytes());
// do stuff
} catch (final ParseException e) {
L.error("{} parsing failed: {}", ClientConnection.this, e.getMessage());
}
}
} catch (final IOException e) {
L.error("{} IOException: {}", ClientConnection.this, e.getMessage());
}
When i close the telnet-session the code reads a line, the length is arbitray (400 or 232 or 123), and bytes[] ares always a series of 4s, like this:
2012-02-06 01:51:19,575 [pool-1-thread-5] DEBUG - as [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 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, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
Actually 4 means End of Transmission, so that should be the expected behaviour for a telnet session. (I guess it will stop sending 4s as soon as the socket really is closed)
see ascii table here: http://www.robelle.com/smugbook/ascii.html
I'm not sure why there are so many but 4 is US ASCII End of Transmission

Java array to multi dimensional

Is it possible to take an array of say 100 chars and turn it into a 2d array of 10*10?
Here you go
char[] chars = ("01234567890123456789012345678901234567890123456789" +
"01234567890123456789012345678901234567890123456789")
.toCharArray();
char[][] char2D = new char[10][10];
for (int i = 0; i < 100; i++)
char2D[i / 10][i % 10] = chars[i];
Now the this code...
System.out.println(Arrays.deepToString(char2D).replaceAll("],","],\n"));
...prints the following
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
Iterate throughout your list of 100 chars and divide it amongst the 10*10,
Modulus (%) will probably be very useful.
You could use 2 nested for loops to assign the chars of the array to the appropriate element.

Categories