Tag Archives: leetcode türkçe

LeetCode Çözümleri – 500. Keyboard Row

LeetCode içerisinde bulunan “Keyboard Row” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir string dizisi içerisinde, Amerikan klavyesi üzerinde aynı satırdaki harflerden oluşan string’leri geri döndürmeniz isteniyor.

► LeetCode 500. Keyboard Row: https://leetcode.com/problems/keyboard-row/

► Problem açıklaması:

Given an array of strings words, return the words that can be typed using letters of the alphabet on only one row of American keyboard like the image below.

In the American keyboard:

the first row consists of the characters “qwertyuiop”,

the second row consists of the characters “asdfghjkl”, and

the third row consists of the characters “zxcvbnm”.

Example 1:

Input: words = [“Hello”,”Alaska”,”Dad”,”Peace”]

Output: [“Alaska”,”Dad”]

Example 2:

Input: words = [“omk”]

Output: []

Example 3:

Input: words = [“adsdf”,”sfd”]

Output: [“adsdf”,”sfd”]

Constraints: 1 <= words.length <= 20

1 <= words[i].length <= 100

words[i] consists of English letters (both lowercase and uppercase).

LeetCode Çözümleri – 342. Power of Four

LeetCode içerisinde bulunan “Power of Four” sorusunun açıklaması ve çözümü. Bu soruda size verilen 32-bit signed bir tam sayının 4’ün tam kuvveti olup olmadığını bulmanız isteniyor.

► LeetCode 342. Power of Four: https://leetcode.com/problems/power-of-four/

► Problem açıklaması:

Given an integer n, return true if it is a power of four. Otherwise, return false.

An integer n is a power of four, if there exists an integer x such that n == 4^x.

Example 1:

Input: n = 16

Output: true

Example 2:

Input: n = 5

Output: false

Example 3:

Input: n = 1

Output: true

Constraints: -2^31 lessEqual n lessEqual 2^31 – 1

LeetCode Çözümleri – 190. Reverse Bits

LeetCode içerisinde bulunan “Reverse Bits” sorusunun açıklaması ve çözümü. Soruda size verilen 32-bit unsigned bir tam sayının bitlerini tersine çevirmeniz isteniyor.

► LeetCode 190. Reverse Bits: https://leetcode.com/problems/reverse-bits/

► Problem açıklaması:

Reverse bits of a given 32 bits unsigned integer.

Note:

Note that in some languages such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer’s internal binary representation is the same, whether it is signed or unsigned. In Java, the compiler represents the signed integers using 2’s complement notation. Therefore, in Example 2 above, the input represents the signed integer -3 and the output represents the signed integer -1073741825.

Follow up:

If this function is called many times, how would you optimize it?

Example 1:

Input: n = 00000010100101000001111010011100

Output: 964176192 (00111001011110000010100101000000)

Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.

Example 2:

Input: n = 11111111111111111111111111111101

Output: 3221225471 (10111111111111111111111111111111)

Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10111111111111111111111111111111.

Constraints:

The input must be a binary string of length 32.

LeetCode Çözümleri – 695. Max Area of Island

LeetCode içerisinde bulunan “Max Area of Island” sorusunun açıklaması ve çözümü. Soruda size verilen 2 boyutlu bir grid içerisinde oluşan kara parçalarının (1) oluşturduğu en büyük ada alanını bulmanız isteniyor.

► LeetCode 191. Number of 1 Bits: https://leetcode.com/problems/number-of-1-bits/

► Problem açıklaması:

Given a non-empty 2D array grid of 0’s and 1’s, an island is a group of 1’s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)

Example 1:

[[0,0,1,0,0,0,0,1,0,0,0,0,0],

[0,0,0,0,0,0,0,1,1,1,0,0,0],

[0,1,1,0,1,0,0,0,0,0,0,0,0],

[0,1,0,0,1,1,0,0,1,0,1,0,0],

[0,1,0,0,1,1,0,0,1,1,1,0,0],

[0,0,0,0,0,0,0,0,0,0,1,0,0],

[0,0,0,0,0,0,0,1,1,1,0,0,0],

[0,0,0,0,0,0,0,1,1,0,0,0,0]]

Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.

Example 2:

[[0,0,0,0,0,0,0,0]]

Given the above grid, return 0.

Note: The length of each dimension in the given grid does not exceed 50.

LeetCode Çözümleri – 191. Number of 1 Bits

LeetCode içerisinde bulunan “Number of 1 Bits” sorusunun açıklaması ve çözümü. Soruda size verilen bir unsigned integer’ın binary gösteriminde kaç adet 1 olduğunu bulmanız isteniyor.

► LeetCode 191. Number of 1 Bits: https://leetcode.com/problems/number-of-1-bits/

► Problem açıklaması:

Write a function that takes an unsigned integer and returns the number of ‘1’ bits it has (also known as the Hamming weight).

Note:

Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer’s internal binary representation is the same, whether it is signed or unsigned. In Java, the compiler represents the signed integers using 2’s complement notation. Therefore, in Example 3, the input represents the signed integer. -3.

Example 1:

Input: n = 00000000000000000000000000001011

Output: 3

Explanation: The input binary string 00000000000000000000000000001011 has a total of three ‘1’ bits.

Example 2:

Input: n = 00000000000000000000000010000000

Output: 1

Explanation: The input binary string 00000000000000000000000010000000 has a total of one ‘1’ bit.

Example 3:

Input: n = 11111111111111111111111111111101

Output: 31

Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one ‘1’ bits.

Constraints: The input must be a binary string of length 32. Follow up: If this function is called many times, how would you optimize it?

LeetCode Çözümleri – 1288. Remove Covered Intervals

LeetCode içerisinde bulunan “Remove Covered Intervals” sorusunun açıklaması ve çözümü. Size verilen bir “aralık”lar dizisi içinde, birbirini kapsayan aralıklardan birini silmeniz isteniyor.

► LeetCode 1288. Remove Covered Intervals: https://leetcode.com/problems/remove-covered-intervals/

► Problem açıklaması:

Given a list of intervals, remove all intervals that are covered by another interval in the list.

Interval [a,b) is covered by interval [c,d) if and only if c lessEqual a and b lessEqual d.

After doing so, return the number of remaining intervals.

Example 1:

Input: intervals = [[1,4],[3,6],[2,8]]

Output: 2

Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.

Example 2:

Input: intervals = [[1,4],[2,3]]

Output: 1

Example 3:

Input: intervals = [[0,10],[5,12]]

Output: 2

Example 4:

Input: intervals = [[3,10],[4,10],[5,11]]

Output: 2

Example 5:

Input: intervals = [[1,2],[1,4],[3,4]]

Output: 1

Constraints: All the intervals are unique.

LeetCode Çözümleri – 985. Sum of Even Numbers After Queries

LeetCode içerisinde bulunan “Sum of Even Numbers After Queries” sorusunun açıklaması ve çözümü. Size verilen bir tam sayı dizisi ve hangi elemana ne kadar ekleneceğini belirten bir jagged array ile, bu ekleme işlemi sonunda oluşan tüm çift sayıları geri döndürmeniz isteniyor.

► LeetCode 985. Sum of Even Numbers After Queries: https://leetcode.com/problems/sum-of-even-numbers-after-queries/

► Problem açıklaması:

We have an array A of integers, and an array queries of queries.

For the i-th query val = queries[i][0], index = queries[i][1], we add val to A[index]. Then, the answer to the i-th query is the sum of the even values of A.

(Here, the given index = queries[i][1] is a 0-based index, and each query permanently modifies the array A.)

Return the answer to all queries. Your answer array should have answer[i] as the answer to the i-th query.

Example 1:

Input: A = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]

Output: [8,6,2,4]

Explanation:

At the beginning, the array is [1,2,3,4].

After adding 1 to A[0], the array is [2,2,3,4], and the sum of even values is 2 + 2 + 4 = 8.

After adding -3 to A[1], the array is [2,-1,3,4], and the sum of even values is 2 + 4 = 6.

After adding -4 to A[0], the array is [-2,-1,3,4], and the sum of even values is -2 + 4 = 2.

After adding 2 to A[3], the array is [-2,-1,3,6], and the sum of even values is -2 + 6 = 4.

LeetCode Çözümleri – 200. Number of Islands [Amazon Mülakat Sorusu]

LeetCode içerisinde bulunan “Number of Islands” sorusunun açıklaması ve çözümü. Size verilen m * n boyutundaki bir grid üzerinde, kaç adet ada olduğunu bulmanız isteniyor. “Ada” tanımı olarak, 4 tarafı su ile çevrili olan kısımlar şeklinde belirtilmiş.

DFS algoritması nedir?

https://en.wikipedia.org/wiki/Depth-first_search

Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking.

► LeetCode 200. Number of Islands: https://leetcode.com/problems/number-of-islands/

► Problem açıklaması:

Given an m x n 2d grid map of ‘1’s (land) and ‘0’s (water), return the number of islands.

An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

Input: grid = [

[“1″,”1″,”1″,”1″,”0”],

[“1″,”1″,”0″,”1″,”0”],

[“1″,”1″,”0″,”0″,”0”],

[“0″,”0″,”0″,”0″,”0”]

]

Output: 1

Example 2:

Input: grid = [

[“1″,”1″,”0″,”0″,”0”],

[“1″,”1″,”0″,”0″,”0”],

[“0″,”0″,”1″,”0″,”0”],

[“0″,”0″,”0″,”1″,”1”]

]

Output: 3

LeetCode Çözümleri – 16. 3Sum Closest

LeetCode içerisinde bulunan “3Sum Closest” sorusunun açıklaması ve çözümü. Size verilen bir tam sayı dizisi ve “hedef” toplam için, sayı dizisi içerisindeki 3 sayının toplamı olarak bu “hedef” sayısına en yakın olanı bulmanız isteniyor.

► LeetCode 16. 3Sum Closest: https://leetcode.com/problems/3sum-closest/

► Problem açıklaması:

Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

Example 1:

Input: nums = [-1,2,1,-4], target = 1

Output: 2

Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

LeetCode Çözümleri – 101. Symmetric Tree

LeetCode içerisinde bulunan “Symmetric Tree” sorusunun açıklaması ve çözümü. Size root’u verilen bir binary tree‘nin merkezine göre simetrik olup olmadığını bulmanız isteniyor.

► LeetCode 101. Symmetric Tree: https://leetcode.com/problems/symmetric-tree/

► Problem açıklaması:

Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

Input: root = [1,2,2,3,4,4,3]

Output: true

Input: root = [1,2,2,null,3,null,3]

Output: false

Constraints:

The number of nodes in the tree is in the range [1, 1000].

-100 <= Node.val <= 100

Follow up: Could you solve it both recursively and iteratively?