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, 6, 3, 5, 1, 3, 0, 7, 0, 0, 8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 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;
}
}
Hi I would like to get 2d array out of a method where I use switch statement. I would like to get out num1 or num2, so the other methods can use this numbers. Is this possible, how?
This is my code:
public int[][] gameNumbers(){
int rand = (int)(Math.random() * 2) + 1;
switch(rand) {
case 1:
int [][] num1= {{4,3,5,2,6,9,7,8,1},{6,8,2,5,7,1,4,9,3},{1,9,7,8,3,4,5,6,2},{8,2,6,1,9,5,3,4,7},{3,7,4,6,8,2,9,1,5},{9,5,1,7,4,3,6,2,8},{5,1,9,3,2,6,8,7,4},{2,4,8,9,5,7,1,3,6},{7,6,3,4,1,8,2,5,9}};
break;
case 2:
int [][] num2= {{1,7,4,3,5,2,9,6,8},{5,3,6,8,7,1,6,4,5,3},{2,9,8,7,1,6,4,5,3},{4,2,3,1,7,8,6,9,5},{8,5,7,2,6,9,3,4,1},{9,6,1,4,3,5,7,8,2},{3,8,2,6,4,7,5,1,9},{7,4,9,5,2,1,8,3,6},{6,1,5,9,8,3,2,7,4}};
break;
default:
break;
}
return rand;
}
Since you want to get one of possible ten 9x9 arrays you can try it like this. You place those ten arrays in another array and then just return one at random.
private static int[][][] arrays = new int[][][]{
{{4, 3, 5, 2, 6, 9, 7, 8, 1}, {6, 8, 2, 5, 7, 1, 4, 9, 3}, {1, 9, 7, 8, 3, 4, 5, 6, 2}, {8, 2, 6, 1, 9, 5, 3, 4, 7}, {3, 7, 4, 6, 8, 2, 9, 1, 5}, {9, 5, 1, 7, 4, 3, 6, 2, 8}, {5, 1, 9, 3, 2, 6, 8, 7, 4}, {2, 4, 8, 9, 5, 7, 1, 3, 6}, {7, 6, 3, 4, 1, 8, 2, 5, 9}},
{{1, 7, 4, 3, 5, 2, 9, 6, 8}, {5, 3, 6, 8, 7, 1, 6, 4, 5, 3}, {2, 9, 8, 7, 1, 6, 4, 5, 3}, {4, 2, 3, 1, 7, 8, 6, 9, 5}, {8, 5, 7, 2, 6, 9, 3, 4, 1}, {9, 6, 1, 4, 3, 5, 7, 8, 2}, {3, 8, 2, 6, 4, 7, 5, 1, 9}, {7, 4, 9, 5, 2, 1, 8, 3, 6}, {6, 1, 5, 9, 8, 3, 2, 7, 4}}
// ... more 8 arrays
};
public int[][] gameNumbers() {
int idx = (int) (Math.random() * 10);
return arrays[idx];
}
You are pretty close. This should do it.
Declare the result variable before the switch statement, and assign it inside the switch.
public int[][] gameNumbers(){
// Declare this before the switch so it stays in scope.
int[][] result;
int rand = (int)(Math.random() * 2) + 1;
switch(rand) {
case 1:
result = new int[][]{{4,3,5,2,6,9,7,8,1},{6,8,2,5,7,1,4,9,3},{1,9,7,8,3,4,5,6,2},{8,2,6,1,9,5,3,4,7},{3,7,4,6,8,2,9,1,5},{9,5,1,7,4,3,6,2,8},{5,1,9,3,2,6,8,7,4},{2,4,8,9,5,7,1,3,6},{7,6,3,4,1,8,2,5,9}};
break;
case 2:
result = new int[][]{{1,7,4,3,5,2,9,6,8},{5,3,6,8,7,1,6,4,5,3},{2,9,8,7,1,6,4,5,3},{4,2,3,1,7,8,6,9,5},{8,5,7,2,6,9,3,4,1},{9,6,1,4,3,5,7,8,2},{3,8,2,6,4,7,5,1,9},{7,4,9,5,2,1,8,3,6},{6,1,5,9,8,3,2,7,4}};
break;
default:
result = null;
break;
}
return result;
}
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.