Sunday, 17 October 2021

Solve LeetCode design problem with different languages

Here is a LeetCode problem. Now will solve this design problem in different language.

  1. Java
  2. Python
  3. Ruby
  4. PHP
  5. JavaScript ES6
  6. JavaScript

Java


class Bank {

    private long[] balance;
    private int n;
    public Bank(long[] balance) {
        this.balance = balance;
        this.n = balance.length;
        
    }
    
    private boolean goodA(int account) {
        return account >=1 && account<= this.n;
    }
    
    public boolean transfer(int account1, int account2, long money) {
        if (!goodA(account1) || !goodA(account2)) {
            return false;
        }
        
        if (this.balance[account1-1] < money) {
            return false;
        }
        
        this.balance[account1-1] = this.balance[account1-1] - money;
        this.balance[account2-1] = this.balance[account2-1] + money;
        return true;
        
        
    }
    
    public boolean deposit(int account, long money) {
        if (!goodA(account)) {
            return false;
        }
        
        this.balance[account-1] = this.balance[account-1] + money;
        return true;
        
    }
    
    public boolean withdraw(int account, long money) {
        if (!goodA(account)) {
            return false;
        }
        
        if (this.balance[account-1] < money) {
            return false;
        }
        
        this.balance[account-1] = this.balance[account-1] - money;
        return true;
        
    }
}

Python


class Bank:

    def __init__(self, balance: List[int]):
        self.balance = balance
        self.n = len(balance)
    
    def goodA(self, account):
        return account>=1 and account<=self.n
        

    def transfer(self, account1: int, account2: int, money: int) -> bool:
        if not self.goodA(account1) or not self.goodA(account2):
            return False
        if self.balance[account1-1] < money:
            return False
        
        self.balance[account1-1] = self.balance[account1-1] - money
        self.balance[account2-1] = self.balance[account2-1] + money
        return True
        

    def deposit(self, account: int, money: int) -> bool:
        if not self.goodA(account):
            return False
        
        self.balance[account-1] = self.balance[account-1] + money
        return True
        

    def withdraw(self, account: int, money: int) -> bool:
        if not self.goodA(account):
            return False
        
        if self.balance[account-1] < money:
            return False
        
        self.balance[account-1] = self.balance[account-1] - money
        
        return True

Ruby


class Bank

=begin
    :type balance: Integer[]
=end
    def initialize(balance)
        @balance = balance
        @n = balance.length
        
    end


=begin
    :type account1: Integer
    :type account2: Integer
    :type money: Integer
    :rtype: Boolean
=end
    def transfer(account1, account2, money)
        if account1<1 || account1>@n
            return false
        end
        
        if account2<1 || account2>@n
            return false
        end
        
        if @balance[account1-1] < money
            return false
        end
        
        @balance[account1-1] = @balance[account1-1] - money
        @balance[account2-1] = @balance[account2-1] + money
        return true
 
    end
=begin
    :type account: Integer
    :type money: Integer
    :rtype: Boolean
=end
    def deposit(account, money)
        if account<1 || account>@n
            return false
        end
        
        @balance[account-1] = @balance[account-1] + money
        return true
 
    end


=begin
    :type account: Integer
    :type money: Integer
    :rtype: Boolean
=end
    def withdraw(account, money)
        if account<1 || account>@n
            return false
        end
        
        if @balance[account-1] < money
            return false
        end
        
        @balance[account-1] = @balance[account-1] - money
        return true
        
    end

end

PHP


class Bank {
    private $balance;
    private $n;
    /**
     * @param Integer[] $balance
     */
    function __construct($balance) {
        $this->balance = $balance;
        $this->n = count($balance);
        
    }
  
    /**
     * @param Integer $account1
     * @param Integer $account2
     * @param Integer $money
     * @return Boolean
     */
    function transfer($account1, $account2, $money) {
        if ($account1 > $this->n || $account1 <1) {
            return false;
        }
        
        if ($account2 > $this->n || $account2 <1) {
            return false;
        }
        
        if ($this->balance[$account1 -1] < $money) {
            return false;
        }
        
        //do transfer
        $this->balance[$account1 -1] = $this->balance[$account1 -1] - $money;
        $this->balance[$account2 -1] = $this->balance[$account2 -1] + $money;
        
        return true;
        
    }
    
     /**
     * @param Integer $account
     * @param Integer $money
     * @return Boolean
     */
    function deposit($account, $money) {
         if ($account > $this->n || $account <1) {
            return false;
        }
        
        $this->balance[$account -1] = $this->balance[$account -1] + $money;
        return true;
        
    }
  
    /**
     * @param Integer $account
     * @param Integer $money
     * @return Boolean
     */
    function withdraw($account, $money) {
         if ($account > $this->n || $account <1) {
            return false;
        }
        
         if ($this->balance[$account -1] < $money) {
            return false;
        }
        
        $this->balance[$account -1] = $this->balance[$account -1] - $money;
        return true;
    }
}

Javascript ES6


class Bank {
    constructor(balance) {
        this.balance = balance
    }
    
    notValid(account) {
        return account<1 || account > this.balance.length
    }
    
    transfer(account1,account2,money) {
        if (this.notValid(account1) || this.notValid(account2)) {
            return false
        }
        
        if (money > this.balance[account1-1]) {
            return false;
        }
        
        this.balance[account1-1] = this.balance[account1-1] - money
        this.balance[account2-1] = this.balance[account2-1] + money
        return true
    }
    
    deposit(account,money) {
        if (this.notValid(account)) {
            return false;
        }
        this.balance[account-1] = this.balance[account-1] + money
        return true
    }
    
    withdraw(account,money) {
        if (this.notValid(account)) {
            return false;
        }
         
        if (money > this.balance[account-1]) {
            return false;
        }
         
        this.balance[account-1] = this.balance[account-1] - money
        return true
    }
    
}

JavaScript


/**
 * @param {number[]} balance
 */
var Bank = function(balance) {
    this.balance = balance
    
};

Bank.prototype.check = function(account) {
    return account>=1 && account<= this.balance.length
    
};

/** 
 * @param {number} account1 
 * @param {number} account2 
 * @param {number} money
 * @return {boolean}
 */
Bank.prototype.transfer = function(account1, account2, money) {
    if (!this.check(account1) || !this.check(account2)) {
        return false;
    }
    
    if (this.balance[account1-1] < money) {
        return false;
    }
    
    this.balance[account1-1] = this.balance[account1-1] - money;
    
    this.balance[account2-1] = this.balance[account2-1] + money;
    return true;
    
};

/** 
 * @param {number} account 
 * @param {number} money
 * @return {boolean}
 */
Bank.prototype.deposit = function(account, money) {
    if (!this.check(account)) {
        return false
    }
    
    this.balance[account-1] = this.balance[account-1] + money;
    return true;
    
};

/** 
 * @param {number} account 
 * @param {number} money
 * @return {boolean}
 */
Bank.prototype.withdraw = function(account, money) {
    if (!this.check(account)) {
        return false
    }
    
    if (this.balance[account-1] < money) {
        return false;
    }
    
    this.balance[account-1] = this.balance[account-1] - money;
    
    return true;
    
};

No comments:

Post a Comment