diff --git a/README.md b/README.md index 2bf93af3..59a12807 100644 --- a/README.md +++ b/README.md @@ -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| @@ -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| diff --git a/algorithms/java/src/PlusOne/PlusOne.java b/algorithms/java/src/PlusOne/PlusOne.java new file mode 100644 index 00000000..ef9289b9 --- /dev/null +++ b/algorithms/java/src/PlusOne/PlusOne.java @@ -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; + } +} \ No newline at end of file diff --git a/algorithms/java/src/PlusOne/PlusOneTest.java b/algorithms/java/src/PlusOne/PlusOneTest.java new file mode 100644 index 00000000..e3dd6173 --- /dev/null +++ b/algorithms/java/src/PlusOne/PlusOneTest.java @@ -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]); + + } +} diff --git a/algorithms/java/src/SingleNumber/SingleNumber.java b/algorithms/java/src/SingleNumber/SingleNumber.java new file mode 100644 index 00000000..592d3da0 --- /dev/null +++ b/algorithms/java/src/SingleNumber/SingleNumber.java @@ -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; + } +} \ No newline at end of file diff --git a/algorithms/java/src/SingleNumber/SingleNumberTest.java b/algorithms/java/src/SingleNumber/SingleNumberTest.java new file mode 100644 index 00000000..bdd0cada --- /dev/null +++ b/algorithms/java/src/SingleNumber/SingleNumberTest.java @@ -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); + + } +}