Skip to content
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

add java solution for problem 136 & update readme #286

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ LeetCode
|139|[Word Break](https://leetcode.com/problems/word-break/)| [C++](./algorithms/cpp/wordBreak/wordBreak.cpp)|Medium|
|138|[Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/)| [C++](./algorithms/cpp/copyListWithRandomPointer/copyListWithRandomPointer.cpp), [Python](./algorithms/python/CopyListWithRandomPointer/copyRandomList.py)|Hard|
|137|[Single Number II](https://leetcode.com/problems/single-number-ii/)| [C++](./algorithms/cpp/singleNumber/singleNumber.II.cpp), [Python](./algorithms/python/SingleNumberII/SingleNumberII.py)|Medium|
|136|[Single Number](https://leetcode.com/problems/single-number/)| [C++](./algorithms/cpp/singleNumber/singleNumber.cpp)|Medium|
|136|[Single Number](https://leetcode.com/problems/single-number/)| [C++](./algorithms/cpp/singleNumber/singleNumber.cpp), [Java](./algorithms/java/src/SingleNumber/SingleNumber.java)|Medium|
|135|[Candy](https://leetcode.com/problems/candy/)| [C++](./algorithms/cpp/candy/candy.cpp)|Hard|
|134|[Gas Station](https://leetcode.com/problems/gas-station/)| [C++](./algorithms/cpp/gasStation/gasStation.cpp)|Medium|
|133|[Clone Graph](https://leetcode.com/problems/clone-graph/)| [C++](./algorithms/cpp/cloneGraph/cloneGraph.cpp)|Medium|
Expand Down Expand Up @@ -496,7 +496,7 @@ LeetCode
|69|[Sqrt(x)](https://leetcode.com/problems/sqrtx/)| [C++](./algorithms/cpp/sqrt/sqrt.cpp)|Medium|
|68|[Text Justification](https://leetcode.com/problems/text-justification/)| [C++](./algorithms/cpp/textJustification/textJustification.cpp)|Hard|
|67|[Add Binary](https://leetcode.com/problems/add-binary/)| [C++](./algorithms/cpp/addBinary/addBinary.cpp)|Easy|
|66|[Plus One](https://leetcode.com/problems/plus-one/)| [C++](./algorithms/cpp/plusOne/plusOne.cpp)|Easy|
|66|[Plus One](https://leetcode.com/problems/plus-one/)| [C++](./algorithms/cpp/plusOne/plusOne.cpp) [Java](./algorithms/java/src/PlusOne/PlusOne.java)|Easy|
|65|[Valid Number](https://leetcode.com/problems/valid-number/)| [C++](./algorithms/cpp/validNumber/validNumber.cpp)|Easy|
|64|[Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/)| [C++](./algorithms/cpp/minimumPathSum/minimumPathSum.cpp), [Java](./algorithms/java/src/dynamicProgramming/minimumPathSum/minimumPathSum.java)|Medium|
|63|[Unique Paths II](https://leetcode.com/problems/unique-paths-ii/)| [C++](./algorithms/cpp/uniquePaths/uniquePaths.II.cpp), [Java](./algorithms/java/src/dynamicProgramming/uniquePaths/uniquePathsII.java)|Medium|
Expand Down
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]);

}
}
50 changes: 50 additions & 0 deletions algorithms/java/src/SingleNumber/SingleNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Source : https://leetcode.com/problems/single-number/description/
// Author : Anas Mak
// Date : 2023-01-25

/**********************************************************************************
Given a non-empty array of integers nums, every element appears twice except for one.
Find that single one.

You must implement a solution with a linear runtime complexity and
use only constant extra space.

Here are few examples.
Example 1:

Input: nums = [2,2,1]
Output: 1
Example 2:

Input: nums = [4,1,2,1,2]
Output: 4
Example 3:

Input: nums = [1]
Output: 1

*
**********************************************************************************/
package Solution;

class Solution {
public int singleNumber(int[] nums) {

Set <Integer> single = new HashSet <> ();
for(int i : nums){
if(!single.contains(i)){
single.add(i);
}

else{
single.remove(i);
}
}

for(int result : single){
return result;
}

return -1;
}
}
21 changes: 21 additions & 0 deletions algorithms/java/src/SingleNumber/SingleNumberTest.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 136. Single Number
*/
public class SingleNumber {
@Test
public void test() {
Solution solution = new Solution();
int[] array1 = [2,2,1];
Assert.assertTrue(solution.SingleNumber(array1) == 1);
int[] array2 = [4,1,2,1,2];
Assert.assertTrue(solution.SingleNumber(array2) == 4);
int[] array3 = [1];
Assert.assertTrue(solution.SingleNumber(array3) == 1);

}
}