## DEV Community is a community of 639,856 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Leetcode Algorithms Solution

Pankaj Ravi Updated on ・2 min read
1. Reverse number ttps://leetcode.com/problems/reverse-integer/ Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

Example 1:
Input: x = 123
Output: 321

Example 2:
Input: x = -123
Output: -321

Example 3:
Input: x = 120
Output: 21

Example 4:
Input: x = 0
Output: 0
Constraints:

## -231 <= x <= 231 - 1

class Solution {
public int reverse(int x) {class Solution {
public int reverse(int x) {
int reversed=0;
int pop;
while(x!=0)
{
pop=(x%10);
x/=10;

if(reversed>Integer.MAX_VALUE/10 ||reversed == Integer.MAX_VALUE &&
pop>7)
return 0;
if(reversed<Integer.MIN_VALUE/10 || reversed==Integer.MIN_VALUE/10&&
pop < -8)
return 0;

reversed=(reversed*10)+pop;
}
return reversed;
}
}
int reversed=0;
int pop;
while(x!=0)
{
pop=(x%10);
x/=10;

if(reversed>Integer.MAX_VALUE/10 ||reversed == Integer.MAX_VALUE &&
pop>7)
return 0;
if(reversed<Integer.MIN_VALUE/10 || reversed==Integer.MIN_VALUE/10&&
pop < -8)
return 0;

reversed=(reversed*10)+pop;
}
return reversed;
}

## }

1. Third Maximum Number

https://leetcode.com/problems/third-maximum-number/

Given integer array nums, return the third maximum number in this array. If the third maximum does not exist, return the maximum number.

Example 1:
Input: nums = [3,2,1]
Output: 1
Explanation: The third maximum is 1.
Example 2:
Input: nums = [1,2]

Output: 2
Explanation: The third maximum does not exist, so the maximum (2) is returned instead.

Example 3:
Input: nums = [2,2,3,1]
Output: 1
Explanation: Note that the third maximum here means the third maximum distinct number.
Both numbers with value 2 are both considered as second maximum.
Constraints:
1 <= nums.length <= 104
-231 <= nums[i] <= 231 - 1

## Follow up: Can you find an O(n) solution?

class Solution {
public int thirdMax(int[] nums) {

Integer max1=null;
Integer max2=null;
Integer max3=null;

for(Integer n :nums)
{
if (n.equals(max1) || n.equals(max2) || n.equals(max3)) continue;
if(max1 ==null|| n> max1)
{
max3=max2;
max2=max1;
max1=n;
}else if(max2==null || n > max2)
{
max3=max2;
max2=n;
}else if(max3==null || n> max3)
{
max3=n;
}
}
return max3 == null ? max1 : max3;
}
}
solution 2
public int thirdMax(int[] nums) {
PriorityQueue pq = new PriorityQueue<>();
Set set = new HashSet<>();
for (int i : nums) {
if (!set.contains(i)) {