Cosmoteer Modding Documentation

Popular Posts

Monday, 30 January 2023

LeetCode: 724. Find Pivot Index



Given an array of integers nums, calculate the pivot index of this array.


The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right.


If the index is on the left edge of the array, then the left sum is 0 because there are no elements to the left. This also applies to the right edge of the array.


Return the leftmost pivot index. If no such index exists, return -1.


 


class Solution {
    public int pivotIndex(int[] nums) {
       int total = 0;
   
        for(int i =0; i<nums.length; i++){
            total += nums[i];
        }

        int leftsum - 0;
        for(int i =0; i<nums.length; i++){
            if (i != 0) leftsum +=nums[i-1];
            if (total - leftsum - nums[i] == leftsum){
                return i;
            }
        }
        return -1;
    }

} 

LeetCode: 2469. Convert the Temperature

 You are given a non-negative floating point number rounded to two decimal places celsius, that denotes the temperature in Celsius.


You should convert Celsius into Kelvin and Fahrenheit and return it as an array ans = [kelvin, fahrenheit].


Return the array ans. Answers within 10-5 of the actual answer will be accepted.


Note that:

Kelvin = Celsius + 273.15

Fahrenheit = Celsius * 1.80 + 32.00


Too simple of a question to be asked by interviewers. 


class Solution {
    public double[] convertTemperature(double celsius) {
        double kelvin = celsius + 273.15;
        double fahrenheit = celsius * 1.80 + 32;

        double[] result = {kelvin, fahrenheit};


        return result;
    }
}

Leet Code: 2235. Add Two Integers

 Given two integers num1 and num2, return the sum of the two integers.


I don't think any company would ever ask for something so simple in their coding interview.


class Solution {
    public int sum(int num1, int num2) {
        int num3 = num1 + num2;
        return num3;
    }
}

Sunday, 29 January 2023

LeetCode: 206. Reverse Linked List

 


Given the head of a singly linked list, reverse the list, and return the reversed list.


class Solution {

    public ListNode reverseList(ListNode head) {
        if (head == null) 
            return null;

        ListNode cur = head, pre = null; 

        while (cur != null) {
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp; 
        }
        return pre; 
    }
}

Friday, 27 January 2023

Leet Problem 169: Majority Element

 Given an array nums of size n, return the majority element.


The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.


1, sort array

2. return majority value



 class Solution {

    public int majorityElement(int[] nums) {
        Arrays.sort(nums);
        return nums[nums.length/2];
    }
}

Find Last Digit In Array




1. sort array, this sorts numbers by least to most

2. find last digit by iterating through the array and doing -1 

3. print out the result

import java.util.Arrays;

class Solution {
    public int majorityElement(int[] nums) {
        Arrays.sort(nums);
        int size = nums[nums.length -1];
        System.out.print(size);

    return size;
    }
   
   
}

Thursday, 26 January 2023

Leet Code: Problem 1: Two Sum


Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.


 class Solution {

    public int[] twoSum(int[] nums, int target) {
       
        for (int i=0; i <nums.length; i++){

            for (int j= i + 1; j <nums.length; j++){
                if (nums[i] + nums[j] == target) {
                    return new int[] {i, j};
                }
            }
        }
    return nums;
}

}