Problem No

Solution For Problem no

Unfortunately, there is no problem titled “no problem” on leetcode. Can you please provide more information or a specific link to the problem?

Step by Step Implementation For Problem no

Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.



class Solution {
    public int[] sortedSquares(int[] A) {
        for(int i = 0; i < A.length; i++){
            A[i] = A[i] * A[i]; // squaring each element in the array
        }
        Arrays.sort(A); // sorting the array in non-decreasing order
        return A;
    }
}
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.

Examples:

s = "leetcode"
return 0.

s = "loveleetcode",
return 2.

def firstUniqChar(self, s: str) -> int:
        #initialize a dictionary 
        dic = {}
        
        #loop through the string
        for i in range(len(s)):
            #if the character is not in the dictionary, add it 
            if s[i] not in dic:
                dic[s[i]] = 1
            #if the character is already in the dictionary, increment the value 
            else:
                dic[s[i]] += 1
        
        #loop through the string again        
        for i in range(len(s)):
            #if the character has a value of 1, return the index 
            if dic[s[i]] == 1:
                return i
        #if no unique characters are found, return -1        
        return -1
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

// Solution: 

var twoSum = function(nums, target) {
    
    // create an empty object
    let hash = {};
    
    // loop through the array
    for(let i = 0; i < nums.length; i++) {
        
        // set the current number as a key in the object
        // with the value as the index
        hash[nums[i]] = i;
    }
    
    // loop through the array again
    for(let i = 0; i < nums.length; i++) {
        
        // set a variable equal to the difference between
        // the current number and the target
        let diff = target - nums[i];
        
        // check to see if the difference exists in the object
        // and that it's not the same index as the current number
        if(hash.hasOwnProperty(diff) && hash[diff] !== i) {
            
            // return the indices of the two numbers that add up to the target
            return [i, hash[diff]];
        }
    }
};
Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

public class Solution {
    public int[] TwoSum(int[] nums, int target) {
        // Initialize an empty array to store the indices of the two numbers that add up to the target
        int[] indices = new int[2];
        
        // Use a for loop to iterate through the array
        for (int i = 0; i < nums.Length; i++) {
            // Initialize a variable to store the value of the current element
            int current = nums[i];
            
            // Use another for loop to iterate through the remaining elements in the array
            for (int j = i + 1; j < nums.Length; j++) {
                // Initialize a variable to store the value of the next element
                int next = nums[j];
                
                // If the sum of the current and next element equals the target, store the indices of the two elements in the array and return it
                if (current + next == target) {
                    indices[0] = i;
                    indices[1] = j;
                    return indices;
                }
            }
        }
        
        // If no two elements in the array add up to the target, return an empty array
        return new int[0];
    }
}


Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]