Skip to content

Commit aa03332

Browse files
authored
Merge pull request #9 from SJ-Cipher/ReverseInteger
Solution#7 SJ-Cipher/Edited Second Edit
2 parents 3554883 + 9a26e77 commit aa03332

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Rotate Image : Explanation
2+
3+
So in this problem we can see that we are given with an array 1 2 3 4 5 6 7 8 9. We are required to rotate it in the right(as a square) direction once (result: 7 4 1 6 5 2 9 8 3). If we carefully observe the original matrix and the rotated matrix we are provided with, we can see that the first row becomes the last column, second row becomes the second last column and so on till the last row becomes the first column. So this gives us a clue that we need to transpose this matrix. After transposing 1 2 3 4 5 6 7 8 9 we get 1 4 7 2 5 6 3 6 9. Now if you look closely once again, this is the mirror of our solution. So to get the solution we reverse each of the rows thus reaching our desired solution.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include<stdio.h>
2+
3+
void rotate( int matrixSize, int* matrixColSize, int matrix[][*matrixColSize]) {
4+
for(int i=0;i<matrixSize;i++){
5+
for(int j=i;j<(*matrixColSize);j++){
6+
int temp=matrix[i][j];
7+
matrix[i][j]=matrix[j][i];
8+
matrix[j][i]=temp;
9+
}
10+
}
11+
12+
for(int i=0;i<matrixSize;i++){
13+
for(int j=0;j<(*matrixColSize)/2;j++){
14+
int temp=matrix[i][j];
15+
matrix[i][j]=matrix[i][*matrixColSize-1-j];
16+
matrix[i][*matrixColSize-1-j]=temp;
17+
}
18+
}
19+
//AFTER ROTATE
20+
for(int i=0;i<3;i++){
21+
for(int j=0;j<3;j++){
22+
printf("%d ",matrix[i][j]);
23+
}
24+
printf("\n");
25+
}
26+
}
27+
int main(){
28+
int arr[3][3]={1,2,3,4,5,6,7,8,9};
29+
int matrixSize=3, matrixColSize =3;
30+
//BEFORE ROTATE
31+
for(int i=0;i<3;i++){
32+
for(int j=0;j<3;j++){
33+
printf("%d ",arr[i][j]);
34+
}
35+
printf("\n");
36+
}
37+
rotate(matrixSize,&matrixColSize,arr);
38+
39+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Explanation: (Reverse Integer)
2+
3+
->First let us review the objective of the question. The main objective is to return a reversed integer or return 0 if the reversed number is greater than 2^31-1 or lesser than -2^31. So for a basic solution, we first need to store the maximum and minimum values. Then we need to store the sign of the number so that we can work with a positive number. But we need to note that if the number is -2^31, it will exceed our maximum value by 1. So here we can check and return 0 or we can create a new variable(in case of languages like C). Now we reverse the number and check if it is above or below our limit. If true then we return 0 or if within the limit we return the reversed number.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
#include<stdio.h>
3+
int reverse(int x){
4+
int MAX_INT=pow(2,31)-1;
5+
int MIN_INT=pow(-2,31);
6+
if (x==0 || (x>-10 && x<10)){
7+
return x;
8+
}
9+
int sign;
10+
if(x>=0) sign=1; else sign=-1;
11+
long int xx=labs(x);
12+
long int rev=0;
13+
while(xx){
14+
rev=rev*10 + xx%10;
15+
xx=xx/10;
16+
}
17+
if(rev*sign>MAX_INT || rev*sign<MIN_INT){
18+
return 0;
19+
}
20+
return rev*sign;
21+
22+
}
23+
24+
int main(){
25+
printf("%d",reverse(13452));
26+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
def Solution( x: int) -> int:
3+
MAX_INT=(2**31)-1
4+
MIN_INT=-2**31
5+
if -10<x<10==0:
6+
return x
7+
sign = (x)//abs(x)
8+
rev = int(str(abs(x))[::-1])
9+
if(sign==-1 and rev*sign<MIN_INT):
10+
return 0
11+
elif(sign==1 and rev>MAX_INT):
12+
return 0
13+
else:
14+
return rev*sign
15+
16+
if __name__ == "__main__":
17+
print(Solution(456))

0 commit comments

Comments
 (0)