forked from 7oSkaaa/LeetCode_DailyChallenge_2023
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '7oSkaaa:main' into main
- Loading branch information
Showing
6 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
04- April/26- Add Digits/26- Add Digits (Noura Algohary).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Author: Noura Algohary | ||
// beats 100% in runtime | ||
|
||
class Solution { | ||
public: | ||
int recursiveAddition(string num) | ||
{ | ||
// the ending contidion is reaching a one digit number | ||
if(num.size() == 1) | ||
return (num[0] - '0'); | ||
|
||
// to store the summation of digits of num | ||
int sumInt = 0; | ||
|
||
for(int i=num.size() - 1; i>=0; i--) | ||
{ | ||
// convert the digit num[i] from char to int, then sum it | ||
sumInt += num[i] - '0'; | ||
} | ||
|
||
return recursiveAddition(to_string(sumInt)); | ||
} | ||
int addDigits(int num) { | ||
return recursiveAddition(to_string(num)); | ||
} | ||
}; |
19 changes: 19 additions & 0 deletions
19
04- April/26- Add Digits/26- Add Digits (Noura Algohary).py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Author: Noura Algohary | ||
|
||
class Solution: | ||
def addDigits(self, num: int) -> int: | ||
|
||
def recursiveAddition(num): | ||
# the ending contidion is reaching a one digit number | ||
if len(num) == 1: | ||
return int(num) | ||
|
||
# to store the summation of digits of num | ||
sumInt = 0 | ||
|
||
for i in range(len(num) - 1, -1, -1): | ||
sumInt += int(num[i]) | ||
|
||
return recursiveAddition(str(sumInt)) | ||
|
||
return recursiveAddition(str(num)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Author: RotenKiwi | ||
class Solution { | ||
public: | ||
int addDigits(int num) { | ||
if(num<10)return num; // If number is less than 10, retrn it. | ||
int curr=0; // Variable to store the digit sum. | ||
while(num){ // Calculation of digit sum. | ||
curr=curr+(num%10); | ||
num=num/10; | ||
} | ||
return addDigits(curr); // Recurse the same for the new number | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Author: Zeinab Mohy | ||
|
||
class Solution { | ||
public: | ||
int addDigits(int num) { | ||
//convert num into string to apply my idea | ||
string s=to_string(num); | ||
//if the length of string is greater than 1 then .. | ||
if(s.length()>1){ | ||
while(s.length()>1){ | ||
/*initialize the summation of the digits with 0 | ||
then sum the digits in string after convert all into integer | ||
then convert summatiomn into string again and so on | ||
until the length of string equal 1*/ | ||
int sum=0; | ||
string ss; | ||
for(int i=0;i<s.length();i++){ | ||
ss=s[i]; | ||
int x=stoi(ss); | ||
sum+=x; | ||
} | ||
s=to_string(sum); | ||
} | ||
// after that convert string into integer to return the result | ||
int ans=stoi(s); | ||
return ans; | ||
}else{ | ||
return num; | ||
} | ||
} | ||
}; |
15 changes: 15 additions & 0 deletions
15
04- April/27- Bulb Switcher/27- Bulb Switcher (Mahmoud Aboelsoud).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Author: Mahmoud Aboelsoud | ||
|
||
class Solution { | ||
public: | ||
int bulbSwitch(int n) { | ||
// for this problem we need to know how many light bulbs are on after n rounds | ||
// to know that you need to know for each light bulb to stay on at the end it needs to have odd number of divisors | ||
// the only numbers that have odd number of divisors are perfect squares | ||
// so the answer is the number of perfect squares less than or equal to n | ||
// which is sqrt(n) | ||
|
||
|
||
return sqrt(n); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters