Skip to content

Commit 16e4608

Browse files
author
Meera Parmar
committed
add math solutions
1 parent 5c2c538 commit 16e4608

14 files changed

+331
-5
lines changed

src/arrays/SortColors.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package arrays;
2+
/*
3+
* https://leetcode.com/problems/sort-colors/
4+
* Sort Colors:
5+
* Given an array with n objects colored red, white or blue, sort them in-place so that objects of
6+
* the same color are adjacent, with the colors in the order red, white and blue.
7+
* Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
8+
*/
9+
public class SortColors {
10+
public void sortColors(int[] nums) {
11+
int i=0,j=0,k=nums.length-1;
12+
while(j<=k){
13+
if(nums[j]==0){
14+
int temp=nums[i];
15+
nums[i]=nums[j];
16+
nums[j]=temp;
17+
i++;
18+
j++;
19+
}
20+
else if(nums[j]==2){
21+
int temp=nums[j];
22+
nums[j]=nums[k];
23+
nums[k]=temp;
24+
k--;
25+
}
26+
else{
27+
j++;
28+
}
29+
}
30+
}
31+
}

src/graphs/FindTheCelebrity.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package graphs;
2+
/*
3+
* https://leetcode.com/problems/find-the-celebrity/
4+
* Find the Celebrity:
5+
* Suppose you are at a party with n people (labeled from 0 to n - 1) and among them,
6+
* there may exist one celebrity. The definition of a celebrity is that all the other n - 1
7+
* people know him/her but he/she does not know any of them.
8+
* Now you want to find out who the celebrity is or verify that there is not one.
9+
* The only thing you are allowed to do is to ask questions like: "Hi, A. Do you know B?"
10+
* to get information of whether A knows B. You need to find out the celebrity
11+
* (or verify there is not one) by asking as few questions as possible (in the asymptotic sense).
12+
* You are given a helper function bool knows(a, b) which tells you whether A knows B.
13+
* Implement a function int findCelebrity(n). There will be exactly one celebrity if he/she is
14+
* in the party. Return the celebrity's label if there is a celebrity in the party.
15+
* If there is no celebrity, return -1.
16+
*/
17+
public class FindTheCelebrity extends Relation{
18+
public int findCelebrity(int n) {
19+
int celeb = 0;
20+
for(int i=0;i<n;i++){
21+
if(knows(celeb, i)){
22+
celeb=i;
23+
}
24+
}
25+
if(isCeleb(n,celeb)){
26+
return celeb;
27+
}
28+
return -1;
29+
}
30+
31+
private boolean isCeleb(int n, int celeb){
32+
for(int j=0;j<n;j++){
33+
if(celeb!=j){
34+
if(!knows(j, celeb) || knows(celeb, j)){
35+
return false;
36+
}
37+
}
38+
}
39+
return true;
40+
}
41+
}
42+
43+
class Relation{
44+
boolean knows(int a, int b) {
45+
return true;
46+
}
47+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package arrays;
1+
package math;
22
/*
33
* https://leetcode.com/problems/add-binary/
44
* Add Binary:

src/math/CountingBits.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package math;
2+
/*
3+
*
4+
* Counting Bits:
5+
* Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num
6+
* calculate the number of 1's in their binary representation and return them as an array.
7+
*/
8+
public class CountingBits {
9+
public int[] countBits(int num) {
10+
int result[]=new int[num+1];
11+
result[0]=0;
12+
13+
int twoN=1;
14+
int count=1;
15+
for(int i=1;i<=num;i++){
16+
if(i==twoN){
17+
twoN=twoN<<1;
18+
count=1;
19+
result[i]=1;
20+
}
21+
else{
22+
result[i]=result[count]+1;
23+
count++;
24+
}
25+
}
26+
return result;
27+
28+
}
29+
}

src/sorting_searching/DivideTwoIntegers.java renamed to src/math/DivideTwoIntegers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package sorting_searching;
1+
package math;
22
/*
33
* https://leetcode.com/problems/divide-two-integers/
44
*
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package math;
2+
3+
import java.util.HashMap;
4+
5+
/*
6+
* https://leetcode.com/problems/fraction-to-recurring-decimal/
7+
* Fraction to Recurring Decimal:
8+
* Given two integers representing the numerator and denominator of a fraction,
9+
* return the fraction in string format.
10+
* If the fractional part is repeating, enclose the repeating part in parentheses.
11+
*/
12+
public class FractionToRecurringDecimal {
13+
public String fractionToDecimal(int numerator, int denominator) {
14+
if(numerator==0){
15+
return "0";
16+
}
17+
if(denominator==0){
18+
return "";
19+
}
20+
21+
String result="";
22+
if((numerator<0) ^ (denominator<0)){
23+
result+='-';
24+
}
25+
long num=numerator;
26+
long den=denominator;
27+
num=Math.abs(num);
28+
den=Math.abs(den);
29+
long q=num/den;
30+
long r=num%den;
31+
result+=q;
32+
if(r==0){
33+
return result;
34+
}
35+
result+=".";
36+
r=r*10;
37+
HashMap<Long,Integer> map=new HashMap<>();
38+
while(r!=0){
39+
if(map.containsKey(r)){
40+
int b=map.get(r);
41+
return (result.substring(0,b)+"("+result.substring(b, result.length())+")");
42+
}
43+
map.put(r, result.length());
44+
q=r/den;
45+
result+=q;
46+
r=(r%den)*10;
47+
}
48+
return result;
49+
50+
}
51+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package strings;
1+
package math;
22
/*
33
* https://leetcode.com/problems/multiply-strings/
44
* Multiply Strings:

src/math/NthDigit.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package math;
2+
/*
3+
* https://leetcode.com/problems/nth-digit/
4+
* Nth Digit:
5+
* Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...
6+
*/
7+
public class NthDigit {
8+
public int findNthDigit(int n) {
9+
long m=n;
10+
long start=1, len=1, count=9;
11+
while(m>len*count){
12+
m=m-len*count;
13+
count*=10;
14+
len++;
15+
start*=10;
16+
}
17+
18+
start=start+(m-1)/len;
19+
20+
String str=String.valueOf(start);
21+
return str.charAt((int) ((m-1)%len))-'0';
22+
23+
24+
}
25+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package sorting_searching;
1+
package math;
22
/*
33
* https://leetcode.com/problems/powx-n/
44
*
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package strings;
1+
package math;
22
/*
33
* https://leetcode.com/problems/roman-to-integer/
44
*

0 commit comments

Comments
 (0)