Solution in C#


  • 0
    R
    public class Solution {
        public int Reverse(int value) {
            int result = 0;
            
            while(value != 0)
            {
                int remaind = 0;
                value = Math.DivRem(value, 10, out remaind);
    
                try
                {
                    result = checked(result * 10 + remaind);
                }
                catch(OverflowException)
                {
                    return 0;
                }
            }
            return result;
        }
    }
    

  • 0
    M

    @Ryouta said in Solution in C#:
    public int Reverse(int value) {

            int result = 0;
            bool isNegative = value < 0;
            if (isNegative)
            {
                value = value * -1;
            }
    
            while (value != 0)
            {
                int remaind = 0;
                value = Math.DivRem(value, 10, out remaind);
    
                try
                {
                    result = checked(result * 10 + remaind);
                }
                catch (OverflowException)
                {
                    return 0;
                }
            }
    
            if (isNegative)
            {
                result = result * -1;
            }
    
            return result;
        }

  • 0
    E

    '''
    public class Solution {

    public int Reverse(int x) {
         
        int result = 0;
        
        if(x >= Int32.MaxValue || x < Int32.MinValue){
            
            result = 0;
        }        
        else if(x > 0){
            
            if(x > 9) {
                
                string temp = x.ToString();
                char[] arr = temp.ToCharArray();
                Array.Reverse(arr);
            
                temp = "";
            
                foreach(char chr in arr){
                
                    temp += chr;
                }
            
                try {
                    result = int.Parse(temp);
                }
                catch (Exception){
                    
                    result = 0;
                }
            }
            else{
                
                result = x;
            }
    
        }
        else if(x < 0){
            
            if(x < -9){ 
                string temp = x.ToString();
                char[] arr = temp.ToCharArray();
                Array.Reverse(arr);
            
                temp = "";
            
                for(int i = 0; i < arr.Count() - 1; i++){
                
                    temp += arr[i];
                }
            
                temp = String.Format("{0}{1}", '-', temp);
            
                try {
                    
                    result = int.Parse(temp);
                }
                catch (Exception){
                    
                    result = 0;
                }
    
            }
            else{
                
                result = x;
            }
                       
        }
        
        return result;
     }
    

    }
    '''


  • 0
    N
    public class Solution {
        public int Reverse(int x) {
            int y=0, prevNum=0;
            int sign=1;
            if(x<0){
                sign=-1;
                x=x*sign;
            }         
              while(x>0){
                  int digit = x%10;
                y=y*10+digit;
                  if(y/10 != prevNum){
                     return 0; 
                  }
                  prevNum=y;
                x=x/10;
              }
                return (y*sign);       
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.