Skip to content

Commit 87caedc

Browse files
Manish-kaseraManish
and
Manish
authored
Added FizzBuzz,WaterBottle,SignOf and BrokenCal Solution In Java (codedecks-in#203)
* Added FizzBuzz,WaterBottle,SignOf and BrokenCal Solution * Removed main function and unnecessary code * Updated README.md Co-authored-by: Manish <[email protected]>
1 parent 12881a2 commit 87caedc

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

Java/BrokenCalculator.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// https://leetcode.com/problems/broken-calculator/
2+
3+
public int brokenCalc(int startValue, int target) {
4+
5+
6+
if(startValue >= target){
7+
return startValue - target;
8+
}
9+
10+
if(target % 2 == 0){
11+
return 1+brokenCalc(startValue,target/2);
12+
}
13+
14+
return 1+brokenCalc(startValue,target+1);
15+
16+
}

Java/FizzBuzz.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
import java.util.ArrayList;
3+
import java.util.List;
4+
5+
// https://leetcode.com/problems/fizz-buzz/
6+
7+
8+
public List<String> fizzBuzz(int n) {
9+
List <String> s = new ArrayList<>();
10+
11+
for (int i = 1; i <= n ; i++) {
12+
if( i % 15 == 0){
13+
s.add("FizzBuzz");
14+
}else if( i % 3 == 0){
15+
s.add("Fizz");
16+
}else if(i % 5 == 0){
17+
s.add("Buzz");
18+
}else{
19+
s.add(Integer.toString(i));
20+
}
21+
}
22+
return s;
23+
}

Java/SignOf.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
3+
// https://leetcode.com/problems/sign-of-the-product-of-an-array/
4+
5+
public int arraySign(int[] nums) {
6+
int count = 0;
7+
for(int i : nums){
8+
if(i == 0){
9+
return 0;
10+
}else if(i < 0){
11+
count++;
12+
}
13+
}
14+
if(count % 2 == 0){
15+
return 1;
16+
}
17+
return -1;
18+
}

Java/WaterBottles.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
3+
// https://leetcode.com/problems/water-bottles/
4+
5+
6+
public int numWaterBottles(int numBottles, int numExchange) {
7+
8+
int total = numBottles;
9+
while(numBottles>=numExchange)
10+
{
11+
int exchange=numBottles/numExchange;
12+
int rem=numBottles%numExchange;
13+
total+=exchange;
14+
numBottles=exchange+rem;
15+
}
16+
return total;
17+
}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,10 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
314314
| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer) | [Java](./Java/roman-to-integer.java) <br> [C++](./C++/Roman_to_Integer.cpp)| _O(n)_ | _O(1)_ | Easy | Math | |
315315
| 14 | [Arithmetic Subarrays](https://leetcode.com/problems/arithmetic-subarrays/) | [Java](./Java/Arithmetic-Subarrays.java) | _O(m\*n)_ | _O(n)_ | Medium | Math | Pattern Count |
316316
| 263 | [Ugly Number](https://leetcode.com/problems/ugly-number/) | [Java](./Java/Ugly-Number.java) | _O(n)_ | _O(n)_ | Easy | Math | |
317+
| 412 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [Java](./Java/FizzBuzz.java) | _O(n)_ | _O(n)_ | Easy | Math | |
318+
| 1518 | [Water Bottles](https://leetcode.com/problems/water-bottles/) | [Java](./Java/WaterBottles.java) | _O(n)_ | _O(n)_ | Easy | Math | |
319+
| 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 | |
320+
| 991 | [Broken Calculator](https://leetcode.com/problems/broken-calculator/) | [Java](./Java/BrokenCalculator.java) | _O(n)_ | _O(n)_ | Medium | Math | |
317321

318322
<br/>
319323
<div align="right">

0 commit comments

Comments
 (0)