10000 add java solution for problem 136 & update readme by anasmak04 · Pull Request #286 · haoel/leetcode · GitHub
[go: up one dir, main page]

Skip to content

add java solution for problem 136 & update readme #286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
I added new solution implemented in java
  • Loading branch information
anasmak04 committed Jan 26, 2023
commit 7448c2ed6ec47101b84904d06ca95e54b2cb8af9
32 changes: 32 additions & 0 deletions algorithms/java/src/PlusOne/PlusOne.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Source : https://leetcode.com/problems/plus-one/description/
// Author : Anas Mak
// Date : 2023-01-26

/**********************************************************************************
* 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.
**********************************************************************************/

package Solution;

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[] array = new int[digits.length + 1];
array[0] = 1;
return array;
}
}
21 changes: 21 additions & 0 deletions algorithms/java/src/PlusOne/PlusOneTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package Solution;

import org.junit.Assert;
import org.junit.Test;

/**
* Test for 66. Plus One
*/
public class PlusOne {
@Test
public void test() {
Solution solution = new Solution();
int[] array1 = [4,3,2,1];
Assert.assertTrue(solution.PlusOne(array1) == [4,3,2,2]);
int[] array2 = [1,2,3];
Assert.assertTrue(solution.SingleNumber(array2) == [1,2,4]);
int[] array3 = [9];
Assert.assertTrue(solution.SingleNumber(array3) == [1,0]);

}
}
4 changes: 2 additions & 2 deletions algorithms/java/src/SingleNumber/SingleNumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import org.junit.Test;

/**
* Test for 58. Length of Last Word
* Test for 136. Single Number
*/
public class TestLengthOfLastWord {
public class SingleNumber {
@Test
public void test() {
Solution solution = new Solution();
Expand Down
0