Tag Archives: leetcode türkçe

Leetcode Çözümleri – Evaluate Reverse Polish Notation

Leetcode içerisinde bulunan “Evaluate Reverse Polish Notation” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir “reverse polish notation”‘ın sonucunu geriye döndürmeniz isteniyor.

➡️ Reverse Polish Notation nedir? https://en.wikipedia.org/wiki/Reverse_Polish_notation

➡️ LeetCode 150. Evaluate Reverse Polish Notation: https://leetcode.com/problems/evaluate-reverse-polish-notation/

➡️ Problem açıklaması:

Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Valid operators are +, -, *, and /. Each operand may be an integer or another expression.

Note that division between two integers should truncate toward zero.

It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.

Example 1:

Input: tokens = [“2″,”1″,”+”,”3″,”*”]

Output: 9

Explanation: ((2 + 1) * 3) = 9

Example 2:

Input: tokens = [“4″,”13″,”5″,”/”,”+”]

Output: 6

Explanation: (4 + (13 / 5)) = 6

Example 3:

Input: tokens = [“10″,”6″,”9″,”3″,”+”,”-11″,”*”,”/”,”*”,”17″,”+”,”5″,”+”]

Output: 22

Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5

= ((10 * (6 / (12 * -11))) + 17) + 5

= ((10 * (6 / -132)) + 17) + 5

= ((10 * 0) + 17) + 5

= (0 + 17) + 5

= 17 + 5

= 22

Constraints:

1 <= tokens.length <= 10^4

tokens[i] is either an operator: “+”, “-“, “*”, or “/”, or an integer in the range [-200, 200].

Microsoft Mülakat Sorusu | Insertion Sort List

Leetcode içerisinde bulunan “Insertion Sort List” sorusunun açıklaması ve çözümü. Bu soruda size verilen linked list içerisindeki node’ları insertion sort kullanarak sıralamanız isteniyor.

➡️ Leetcode 147. Insertion Sort List: https://leetcode.com/problems/insertion-sort-list/

➡️ Problem açıklaması:

Given the head of a singly linked list, sort the list using insertion sort, and return the sorted list’s head. The steps of the insertion sort algorithm:

– Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.

– At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there.

– It repeats until no input elements remain.

The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.

Example 1:

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

Output: [1,2,3,4]

Example 2:

Input: head = [-1,5,3,4,0]

Output: [-1,0,3,4,5]

Constraints:

The number of nodes in the list is in the range [1, 5000].

-5000 <= Node.val <= 5000

Leetcode Çözümleri | Three Consecutive Odds

Leetcode içerisinde bulunan “Three Consecutive Odds” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir tam sayı dizisinde, 3 tane yan yana tek sayı olup olmadığını bulmanız isteniyor.

➡️ Leetcode 1550. Three Consecutive Odds: https://leetcode.com/problems/three-consecutive-odds/

➡️ Problem açıklaması:

Given an integer array arr, return true if there are three consecutive odd numbers in the array. Otherwise, return false.

Example 1:

Input: arr = [2,6,4,1]

Output: false

Explanation: There are no three consecutive odds.

Example 2:

Input: arr = [1,2,34,3,4,5,7,23,12]

Output: true

Explanation: [5,7,23] are three consecutive odds.

Constraints:

1 <= arr.length <= 1000

1 <= arr[i] <= 1000

LeetCode Çözümleri | Add Digits

Leetcode içerisinde bulunan “Add Digits” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen bir tam sayının rakamları toplamı tek hane olana kadar toplayıp geriye döndürmeniz isteniyor.

➡️ LeetCode 258. Add Digits: https://leetcode.com/problems/add-digits/

➡️ Problem açıklaması:

Given an integer num, repeatedly add all its digits until the result has only one digit, and return it.

Example 1:

Input: num = 38

Output: 2

Explanation: The process is

38 —- 3 + 8 —- 11

11 —- 1 + 1 —- 2

Since 2 has only one digit, return it.

Example 2:

Input: num = 0

Output: 0

Constraints:

0 <= num <= 2^31 – 1

⚠️Follow up: Could you do it without any loop/recursion in O(1) runtime?

Leetcode Çözümleri | Determine Color of a Chessboard Square

Leetcode içerisinde bulunan “Determine Color of a Chessboard Square” sorusunun açıklaması ve çözümü. Bu soruda size verilen satranç tahtası üzerindeki karenin beyaz bir kare olup olmadığını bulmanız isteniyor.

➡️ Leetcode 1812. Determine Color of a Chessboard Square: https://leetcode.com/problems/determine-color-of-a-chessboard-square/

➡️ Problem açıklaması:

You are given coordinates, a string that represents the coordinates of a square of the chessboard. Below is a chessboard for your reference.

Return true if the square is white, and false if the square is black.

The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first, and the number second.

Example 1:

Input: coordinates = “a1”

Output: false

Explanation: From the chessboard above, the square with coordinates “a1” is black, so return false.

Example 2:

Input: coordinates = “h3”

Output: true

Explanation: From the chessboard above, the square with coordinates “h3” is white, so return true.

Example 3:

Input: coordinates = “c7”

Output: false

Constraints:

coordinates.length == 2

‘a’ <= coordinates[0] <= ‘h’

‘1’ <= coordinates[1] <= ‘8’

Bloomberg Mülakat Sorusu – Matrix Diagonal Sum

Leetcode içerisinde bulunan “Matrix Diagonal Sum” sorusunun açıklaması ve çözümü. Bu soruda sizi jagged array olarak verilen bir matris içerisindeki iki diagonalde bulunan elemanların toplamını bulmanız isteniyor.

► Leetcode 1572. Matrix Diagonal Sum: https://leetcode.com/problems/matrix-diagonal-sum/

► Problem açıklaması:

Given a square matrix mat, return the sum of the matrix diagonals. Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diagonal that are not part of the primary diagonal.

Example 1:

Input:

mat = [[1,2,3], [4,5,6], [7,8,9]]

Output: 25

Explanation: Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25 Notice that element mat[1][1] = 5 is counted only once.

Example 2:

Input:

mat = [[1,1,1,1], [1,1,1,1], [1,1,1,1], [1,1,1,1]]

Output: 8

Example 3:

Input:

mat = [[5]]

Output: 5

Constraints: n == mat.length == mat[i].length

1 <= n <= 100

1 <= mat[i][j] <= 100

Leetcode kolay seviyede Linked List soruları – 1. Kısım

Leetcode içerisindeki Linked List veri yapısı ile ilgili kolay soruları çözüyoruz.

Çözdüğümüz soruların linkleri:

1290. Convert Binary Number in a Linked List to Integer: https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/

876. Middle of the Linked List: https://leetcode.com/problems/middle-of-the-linked-list/

206. Reverse Linked List: https://leetcode.com/problems/reverse-linked-list/

21. Merge Two Sorted Lists: https://leetcode.com/problems/merge-two-sorted-lists/

Leetcode Çözümleri – Sum of Unique Elements

Leetcode içerisinde bulunan “Sum of Unique Elements” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen bir tam sayı dizisi içerisindeki unique elemanların toplamını bulmanız isteniyor.

► LeetCode 1748. Sum of Unique Elements: https://leetcode.com/problems/sum-of-unique-elements/

► Problem açıklaması:

You are given an integer array nums. The unique elements of an array are the elements that appear exactly once in the array.

Return the sum of all the unique elements of nums.

Example 1:

Input: nums = [1,2,3,2]

Output: 4

Explanation: The unique elements are [1,3], and the sum is 4.

Example 2:

Input: nums = [1,1,1,1,1]

Output: 0

Explanation: There are no unique elements, and the sum is 0.

Example 3:

Input: nums = [1,2,3,4,5]

Output: 15

Explanation: The unique elements are [1,2,3,4,5], and the sum is 15.

Constraints:

1 <= nums.length <= 100

1 <= nums[i] <= 100

Leetcode Çözümleri – Shuffle String

Leetcode içerisinde bulunan “Shuffle String” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen bir string‘in her bir karakteri, yine bir tam sayı dizisi olarak verilen index’lerde olacak şekilde shuffle etmeniz isteniyor.

► Leetcode 1528. Shuffle String: https://leetcode.com/problems/shuffle-string/

► Problem açıklaması:

Given a string s and an integer array indices of the same length.

The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string.

Return the shuffled string.

Example 1:

Input: s = “codeleet”, indices = [4,5,6,7,0,2,1,3]

Output: “leetcode”

Explanation: As shown, “codeleet” becomes “leetcode” after shuffling.

Example 2:

Input: s = “abc”, indices = [0,1,2]

Output: “abc”

Explanation: After shuffling, each character remains in its position.

Example 3:

Input: s = “aiohn”, indices = [3,1,4,2,0]

Output: “nihao”

Example 4:

Input: s = “aaiougrt”, indices = [4,0,2,6,7,3,1,5]

Output: “arigatou”

Example 5:

Input: s = “art”, indices = [1,0,2]

Output: “rat”

Constraints:

s.length == indices.length == n

1 <= n <= 100

s contains only lower-case English letters.

0 <= indices[i] < n

All values of indices are unique (i.e. indices is a permutation of the integers from 0 to n – 1).

Facebook Mülakat Sorusu – Lowest Common Ancestor of a Binary Search Tree

Leetcode içerisinde bulunan “Lowest Common Ancestor of a Binary Search Tree” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir binary search tree içerisinde, verilen iki tane TreeNode’un en aşağıdaki ortak parent’ını bulmanız isteniyor. Bence deneme olarak bunun iterative halini de siz yazabilirsiniz.

► LeetCode 235. Lowest Common Ancestor of a Binary Search Tree: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/

► Problem açıklaması:

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.

According to the definition of LCA on Wikipedia:

“The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

Example 1:

Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8

Output: 6

Explanation: The LCA of nodes 2 and 8 is 6.

Example 2:

Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4

Output: 2

Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.

Example 3:

Input: root = [2,1], p = 2, q = 1

Output: 2

Constraints:

The number of nodes in the tree is in the range [2, 105].

-109 <= The number of nodes in the tree is in the range [2, 10^5].

-10^9 <= Node.val <= 10^9

All Node.val are unique.

p != q

p and q will exist in the BST.