Category Archives: LeetCode

LeetCode Çözümleri – 83. Remove Duplicates from Sorted List – [Facebook ve Google Mülakat Sorusu]

LeetCode içerisinde bulunan “Remove Duplicates from Sorted List”‘ sorusunun açıklaması ve çözümü.

🔥 LeetCode 83. Remove Duplicates from Sorted List: https://leetcode.com/problems/remove-duplicates-from-sorted-list/

🔥 Bahsi geçen Linked List yazısı: https://www.cs.cmu.edu/~adamchik/15-121/lectures/Linked%20Lists/linked%20lists.html

➡️ Problem açıklaması:

Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

Input: 1–1–2

Output: 1–2

Example 2:

Input: 1–1–2–3–3

Output: 1–2–3

LeetCode Çözümleri – 70. Climbing Stairs [Amazon ve Goldman Sachs Mülakat Sorusu]

LeetCode içerisinde bulunan “Climbing Stairs”‘ sorusunun açıklaması ve çözümü. Bu soruda N basamaktan oluşan bir merdivenimiz var ve bu merdiveni çıkarken ya 1 basamak, ya da 2 basamak çıkabiliyoruz tek adımda. Bu merdiveni kaç farklı şekilde tırmanabiliriz? Leetcode’a göre Amazon ve Goldman Sachs’ta mülakatlarında sorulmuş sorulardan biri.

🔥 LeetCode 70. Climbing Stairs: https://leetcode.com/problems/climbing-stairs/

➡️ Problem açıklaması:

You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Note: Given n will be a positive integer.

Example 1:

Input: 2

Output: 2

Explanation: There are two ways to climb to the top. 1. 1 step + 1 step 2. 2 steps

Example 2:

Input: 3

Output: 3

Explanation: There are three ways to climb to the top. 1. 1 step + 1 step + 1 step 2. 1 step + 2 steps 3. 2 steps + 1 step

LeetCode Çözümleri – 69. Sqrt(x)

LeetCode içerisinde bulunan “Sqrt(x)”‘ sorusunun açıklaması ve çözümü. Bu soruda tamsayı bir parametre alan metodunuzdan geriye o sayının karekökünün tam sayı halini döndürmenizi istiyor. Tabi bunun için hali hazırda kullandığınız programlama dilinde var olan sqrt() gibi metodları kullanmamanız gerekmekte.

🔥 LeetCode 69. Sqrt(x): https://leetcode.com/problems/sqrtx/

➡️ Problem açıklaması:

Implement int sqrt(int x).

Compute and return the square root of x, where x is guaranteed to be a non-negative integer.

Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.

Example 1:

Input: 4

Output: 2

Example 2:

Input: 8

Output: 2

Explanation: The square root of 8 is 2.82842…, and since the decimal part is truncated, 2 is returned.

LeetCode Çözümleri – 67. Add Binary – [Facebook Mülakat Sorusu]

LeetCode içerisinde bulunan “Add Binary”‘ sorusunun açıklaması ve çözümü. Klasik ilkokulda toplama işlemini nasıl yapıyorsak alt alta, bu soruda da bu istenmiş. En önemli kısmı “elde” hesaplaması gibi görünüyor. LeetCode’a göre Facebook mülakatlarında sorulmuş sorulardan biri.

🔥 LeetCode 67. Add Binary: https://leetcode.com/problems/add-binary/

➡️ Problem açıklaması:

Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0.

Example 1:

Input: a = “11”, b = “1”

Output: “100”

Example 2:

Input: a = “1010”,

b = “1011”

Output: “10101”

Constraints:

☢️ Each string consists only of ‘0’ or ‘1’ characters.

☢️ 1 less or equal a.length, b.length less or equal 10^4

☢️ Each string is either “0” or doesn’t contain any leading zero.

LeetCode Çözümleri – 38. Count And Say

LeetCode içerisinde bulunan “Count And Say”‘ sorusunun açıklaması ve çözümü.

🔥 LeetCode 38. Count And Say: https://leetcode.com/problems/count-and-say/

🔥 Look-and-say sequence: https://en.wikipedia.org/wiki/Look-and-say_sequence

➡️ Problem açıklaması:

The count-and-say sequence is the sequence of integers with the first five terms as following:

1. 1

2. 11

3. 21

4. 1211

5. 111221

1 is read off as “one 1” or 11.

11 is read off as “two 1s” or 21.

21 is read off as “one 2, then one 1” or 1211.

Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence. You can do so recursively, in other words from the previous member read off the digits, counting the number of digits in groups of the same digit.

☢️ Note: Each term of the sequence of integers will be represented as a string.

Example 1:

Input: 1

Output: “1”

Explanation: This is the base case.

Example 2:

Input: 4

Output: “1211”

Explanation:

For n = 3 the term was “21” in which we have two groups “2” and “1”, “2” can be read as “12” which means frequency = 1 and value = 2, the same way “1” is read as “11”, so the answer is the concatenation of “12” and “11” which is “1211”.

LeetCode Çözümleri – 58. Length of Last Word

✔️ LeetCode içerisinde bulunan “Length of Last Word”‘ sorusunun açıklaması ve çözümü.

🔥 LeetCode 58. Length of Last Word: https://leetcode.com/problems/length-of-last-word/

➡️ Problem açıklaması:

Given a string s consists of upper/lower-case alphabets and empty space characters ‘ ‘, return the length of last word (last word means the last appearing word if we loop from left to right) in the string. If the last word does not exist, return 0.

Note: A word is defined as a maximal substring consisting of non-space characters only.

Example:

Input: “Hello World”

Output: 5

LeetCode Çözümleri – 66. Plus One [Google ve eBay Mülakat Sorusu]

LeetCode içerisinde bulunan “Plus One”‘ sorusunun açıklaması ve çözümü. Bu soruda bir tamsayı dizi olarak verilen dizi elemanlarının temsil ettiği tamsayı değerinin bir fazlasını yine bir tamsayı dizisi olarak geri döndürmemiz isteniyor. LeetCod’ea göre bu soru Google ve eBay mülakatlarında sorulmuş sorulardan biri.

🔥 LeetCode 66. Plus One: https://leetcode.com/problems/plus-one/

➡️ Problem açıklaması:

Given a non-empty array of digits representing a non-negative integer, plus one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.

Example 1:

Input: [1,2,3]

Output: [1,2,4]

Explanation: The array represents the integer 123.

Example 2:

Input: [4,3,2,1]

Output: [4,3,2,2]

Explanation: The array represents the integer 4321.

LeetCode May Challenge – Single Element in a Sorted Array – [Alternatif XOR çözümü] 12. Gün

LeetCode’un mayıs ayı için her gün bir programlama sorusu sorduğu “May Challenge”‘ın 12. gün sorusu “Single Element in a Sorted Array”‘in açıklaması ve çözümü.

🔥 LeetCode May Challenge: https://leetcode.com/explore/featured/card/may-leetcoding-challenge

➡️ Problem açıklaması:

You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once. Find this single element that appears only once.

Example 1:

Input: [1,1,2,3,3,4,4,8,8]

Output: 2

Example 2:

Input: [3,3,7,7,10,11,11]

Output: 10

☢️ Note: Your solution should run in O(log n) time and O(1) space.

LeetCode May Challenge – Find the Town Judge – [10. GÜN]

LeetCode’un mayıs ayı için her gün bir programlama sorusu sorduğu “May Challenge”‘ın 10. gün sorusu “Find the Town Judge””ın açıklaması ve çözümü.

🔥 LeetCode May Challenge: https://leetcode.com/explore/featured/card/may-leetcoding-challenge

➡️ Problem açıklaması:

In a town, there are N people labelled from 1 to N. There is a rumor that one of these people is secretly the town judge.

If the town judge exists, then:

– The town judge trusts nobody.

– Everybody (except for the town judge) trusts the town judge.

– There is exactly one person that satisfies properties 1 and 2.

You are given trust, an array of pairs trust[i] = [a, b] representing that the person labelled a trusts the person labelled b.

If the town judge exists and can be identified, return the label of the town judge. Otherwise, return -1.

Example 1:

Input: N = 2, trust = [[1,2]]

Output: 2

Example 2:

Input: N = 3, trust = [[1,3],[2,3]]

Output: 3

Example 3:

Input: N = 3, trust = [[1,3],[2,3],[3,1]]

Output: -1

Example 4:

Input: N = 3, trust = [[1,2],[2,3]]

Output: -1

Example 5:

Input: N = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]]

Output: 3

LeetCode May Challenge – Valid Perfect Square – [9. GÜN]

LeetCode’un mayıs ayı için her gün bir programlama sorusu sorduğu “May Challenge”‘ın 9. gün sorusu “Valid Perfect Square””in açıklaması ve çözümü.

🔥 LeetCode May Challenge: https://leetcode.com/explore/featured/card/may-leetcoding-challenge

➡️ Problem açıklaması:

Given a positive integer num, write a function which returns True if num is a perfect square else False.

☢️ Note: DO NOT use any built-in library function such as sqrt.

Example 1:

Input: 16

Output: true

Example 2:

Input: 14

Output: false