diff --git a/Java/BrokenCalculator.java b/Java/BrokenCalculator.java new file mode 100644 index 00000000..4a119a05 --- /dev/null +++ b/Java/BrokenCalculator.java @@ -0,0 +1,16 @@ +// https://leetcode.com/problems/broken-calculator/ + + public int brokenCalc(int startValue, int target) { + + + if(startValue >= target){ + return startValue - target; + } + + if(target % 2 == 0){ + return 1+brokenCalc(startValue,target/2); + } + + return 1+brokenCalc(startValue,target+1); + + } diff --git a/Java/FizzBuzz.java b/Java/FizzBuzz.java new file mode 100644 index 00000000..9034c2c0 --- /dev/null +++ b/Java/FizzBuzz.java @@ -0,0 +1,23 @@ + +import java.util.ArrayList; +import java.util.List; + +// https://leetcode.com/problems/fizz-buzz/ + + + public List fizzBuzz(int n) { + List s = new ArrayList<>(); + + for (int i = 1; i <= n ; i++) { + if( i % 15 == 0){ + s.add("FizzBuzz"); + }else if( i % 3 == 0){ + s.add("Fizz"); + }else if(i % 5 == 0){ + s.add("Buzz"); + }else{ + s.add(Integer.toString(i)); + } + } + return s; + } diff --git a/Java/SignOf.java b/Java/SignOf.java new file mode 100644 index 00000000..3077aca4 --- /dev/null +++ b/Java/SignOf.java @@ -0,0 +1,18 @@ + + +// https://leetcode.com/problems/sign-of-the-product-of-an-array/ + + public int arraySign(int[] nums) { + int count = 0; + for(int i : nums){ + if(i == 0){ + return 0; + }else if(i < 0){ + count++; + } + } + if(count % 2 == 0){ + return 1; + } + return -1; + } diff --git a/Java/WaterBottles.java b/Java/WaterBottles.java new file mode 100644 index 00000000..6e0a9cc0 --- /dev/null +++ b/Java/WaterBottles.java @@ -0,0 +1,17 @@ + + +// https://leetcode.com/problems/water-bottles/ + + + public int numWaterBottles(int numBottles, int numExchange) { + + int total = numBottles; + while(numBottles>=numExchange) + { + int exchange=numBottles/numExchange; + int rem=numBottles%numExchange; + total+=exchange; + numBottles=exchange+rem; + } + return total; + } diff --git a/README.md b/README.md index bcd3ee1a..36126cd5 100644 --- a/README.md +++ b/README.md @@ -313,6 +313,10 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer) | [Java](./Java/roman-to-integer.java)
[C++](./C++/Roman_to_Integer.cpp)| _O(n)_ | _O(1)_ | Easy | Math | | | 14 | [Arithmetic Subarrays](https://leetcode.com/problems/arithmetic-subarrays/) | [Java](./Java/Arithmetic-Subarrays.java) | _O(m\*n)_ | _O(n)_ | Medium | Math | Pattern Count | | 263 | [Ugly Number](https://leetcode.com/problems/ugly-number/) | [Java](./Java/Ugly-Number.java) | _O(n)_ | _O(n)_ | Easy | Math | | +| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Java](./Java/FizzBuzz.java) | _O(n)_ | _O(n)_ | Easy | Math | | +| 1518 | [Water Bottles](https://leetcode.com/problems/water-bottles/) | [Java](./Java/WaterBottles.java) | _O(n)_ | _O(n)_ | Easy | Math | | +| 1822 | [Sign Of Product](https://leetcode.com/problems/sign-of-the-product-of-an-array/) | [Java](./Java/SignOf.java) | _O(n)_ | _O(n)_ | Easy | Math | | +| 991 | [Broken Calculator](https://leetcode.com/problems/broken-calculator/) | [Java](./Java/BrokenCalculator.java) | _O(n)_ | _O(n)_ | Medium | Math | |