-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #419 from m-oliv/patch-1
- Added the reverse integer algo.
- Loading branch information
Showing
3 changed files
with
94 additions
and
2 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Name: [Miftahul J.](https://github.com/jiadibo)<br/> | ||
Name: [Miftahul J.](https://github.com/jiadibo)<br/> | ||
Place: Indonesia<br/> | ||
About: I'm moslem programmer<br/> | ||
Programming Language: Java, kotlin, flutter<br/> | ||
|
@@ -63,6 +63,7 @@ About: I am 3rd yearComputer science student at poornima college of engineering, | |
Programming Languages: C, C++,python</br> | ||
|
||
|
||
|
||
Name: [Henry McCreery](github.com/hmccree) | ||
Place: Portland, Oregon, USA | ||
About: High school robotics team member project manager | ||
|
@@ -395,22 +396,26 @@ About: I am a sophomore at IIT Mandi<br/> | |
Programming Languages: C, C++, JavaScript<br/> | ||
Email: [email protected] | ||
|
||
|
||
Name: [Samyak](https://github.com/samyak-sopho) | ||
Place: Allahabad | ||
About: Will do anything for free shirt | ||
Programming Language: C++, JS, Java | ||
|
||
|
||
Name: [Marlene Oliveira](https://github.com/m-oliv) | ||
Place: Lisbon, Portugal | ||
About: Software Engineer | ||
Programming Language: Java, C, C#, Python, JavaScript, HTML, CSS | ||
|
||
|
||
Name: [Trevin Edinger](https://github.com/ArkTrevelyan) | ||
Place: Elkins, WV, USA | ||
About: High School Graduate. Self teaching to be a programmer. | ||
Programming Languages: Java, C. | ||
Email: [email protected] | ||
|
||
|
||
Name: [Tsaruk Alex](https://github.com/WildTiger404)<br/> | ||
Place: Minsk, Belarus<br/> | ||
About: I am a 3rd year stundent. I like traveling and events like this one<br/> | ||
|
@@ -423,6 +428,7 @@ About: I am a 3rd Year Computer Science student at JSSATE Noida<br/> | |
Programming Languages: C, Java<br/> | ||
Email: [email protected] | ||
|
||
|
||
Name: [Darshan D V](https://github.com/darshandv)<br/> | ||
Place: Mangalore, Karnataka ,India<br/> | ||
About: I am a Computer Science student at NITK Surathkal<br/> | ||
|
@@ -435,6 +441,7 @@ About: Btech student from JIIT<br/> | |
Programming Languages: C,C++,python3<br/> | ||
email: [email protected]<br/> | ||
|
||
|
||
Name: [Kartik Singh](https://github.com/Kartik212112)<br/> | ||
Place: Patna,India<br/> | ||
About: Btech student from IITP<br/> | ||
|
@@ -705,4 +712,5 @@ Programming Languages: php, java, javascript<br /> | |
Name: [Felipe](https://github.com/ja1felipe)<br/> | ||
Place: Brazil<br/> | ||
About: I am studying computer science.<br/> | ||
Programming Language:Python, java<br/> | ||
Programming Language:Python, java<br/> | ||
|
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 @@ | ||
class ReverseInteger { | ||
public static int reverse(int x) { | ||
int reversed = 0; | ||
int y = 0; | ||
|
||
while(x!=0){ | ||
reversed = y * 10 + x % 10; | ||
if((reversed - x % 10)/10 == y) { | ||
y = reversed; | ||
}else { | ||
return 0; | ||
} | ||
x = x/10; | ||
} | ||
|
||
if(x < 0) { | ||
return y * -1; | ||
} | ||
|
||
return y; | ||
} | ||
|
||
public static void main(String[] args) { | ||
System.out.println(reverse(12345)); | ||
} | ||
} |
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,58 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
A := []int{3, 5, 1, 6, 1, 7, 2, 4, 5} | ||
fmt.Println(sort(A)) | ||
} | ||
|
||
// top-down approach | ||
func sort(A []int) []int { | ||
if len(A) <= 1 { | ||
return A | ||
} | ||
|
||
left, right := split(A) | ||
left = sort(left) | ||
right = sort(right) | ||
return merge(left, right) | ||
} | ||
|
||
// split array into two | ||
func split(A []int) ([]int, []int) { | ||
return A[0:len(A) / 2], A[len(A) / 2:] | ||
} | ||
|
||
// assumes that A and B are sorted | ||
func merge(A, B []int) []int { | ||
arr := make([]int, len(A) + len(B)) | ||
|
||
// index j for A, k for B | ||
j, k := 0, 0 | ||
|
||
for i := 0; i < len(arr); i++ { | ||
// fix for index out of range without using sentinel | ||
if j >= len(A) { | ||
arr[i] = B[k] | ||
k++ | ||
continue | ||
} else if k >= len(B) { | ||
arr[i] = A[j] | ||
j++ | ||
continue | ||
} | ||
// default loop condition | ||
if A[j] > B[k] { | ||
arr[i] = B[k] | ||
k++ | ||
} else { | ||
arr[i] = A[j] | ||
j++ | ||
} | ||
} | ||
|
||
return arr | ||
} |