Posts

Showing posts from October, 2024

Leetcode problem 20. 'Valid Parentheses'

import java.util.Scanner; public class Main {     public static boolean isValid(String s) {         int parentheses = 0, curlyBraces = 0, squareBrackets = 0;         for (char ch : s.toCharArray()) {             if (ch == '(') parentheses++;             else if (ch == ')') {                 parentheses--;                 if (parentheses < 0) return false;             } else if (ch == '{') curlyBraces++;             else if (ch == '}') {                 curlyBraces--;                 if (curlyBraces < 0) return false;             } else if (ch == '[') squareBrackets++;             else if (ch == ']'...

Leetcode problem 136. 'Single Number'

 class Solution {     public int singleNumber(int[] nums) {         int c = 0, a = 0, b = 0;         for(int i = 0; i<nums.length; i++){             for(int j = 0; j<nums.length; j++){                 if(nums[i] == nums[j])                     c++;             }             if(c==1){                 a = 1;                 b = nums[i];                 break;             }             c=0;             }         if(a == 1)             return b;         else     ...

Leetcode problem 121. 'Best time to buy and sell stock'

class Solution {     public int maxProfit(int[] prices) {         if (prices == null || prices.length == 0)             return 0;         int minPrice = Integer.MAX_VALUE;         int maxProfit = 0;         for (int i = 0; i < prices.length; i++) {             if (prices[i] < minPrice) {                 minPrice = prices[i];             } else if (prices[i] - minPrice > maxProfit) {                 maxProfit = prices[i] - minPrice;             }         }         return maxProfit;     } }