πΊπΏ Amaliyot. Stack.
Hammaga salom! Biz sizlar bilan Stack ma'lumotlar tuzilmasini [MT] ko'rib chiqdik. Keling endi shu MT bilan amaliy mashqlar bajaramiz.
π’ Oson: 20. Valid Parentheses
π¨βπ» Mening yechimim: Java 2 Solutions | Stack | Arraylist in just 1 ms (99.71% faster)
Aslida menikidan ancha chiroyliroq yechim bor. Lekin! Chiroylimi - yo'qmi, sizlar berilgan masalani Stack ishlatib yechishingiz lozim.
Omad! βοΈπ€
Hammaga salom! Biz sizlar bilan Stack ma'lumotlar tuzilmasini [MT] ko'rib chiqdik. Keling endi shu MT bilan amaliy mashqlar bajaramiz.
π’ Oson: 20. Valid Parentheses
π¨βπ» Mening yechimim: Java 2 Solutions | Stack | Arraylist in just 1 ms (99.71% faster)
Aslida menikidan ancha chiroyliroq yechim bor. Lekin! Chiroylimi - yo'qmi, sizlar berilgan masalani Stack ishlatib yechishingiz lozim.
Omad! βοΈπ€
LeetCode
Valid Parentheses - LeetCode
Can you solve this real interview question? Valid Parentheses - Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
1. Open brackets must be closed by theβ¦
An input string is valid if:
1. Open brackets must be closed by theβ¦
π8
π¬π§ 20. Valid Parentheses.
π’ Easy: 20. Valid Parentheses
π¨βπ» My solution: Java 2 Solutions | Stack | Arraylist in just 1 ms (99.71% faster)
Hi, guys. Today we will see the solution of phoenix13steve user on LeetCode. Link to this solution is here: Short java solution
1. First he creates a Stack.
2. Then he goes through the String letter by letter.
3. For each
- If yes, then he pushes closing parentheses to the relevant case.
- If no, then he checks the condition where Stack is empty or the last element of the Stack is not equal current
Let's have a closer look to the logic. Here if current
4. Finally, if the Stack is empty, then we return true. It means that all opening brackets had closing in the right order.
Peace! βοΈπ€
π’ Easy: 20. Valid Parentheses
π¨βπ» My solution: Java 2 Solutions | Stack | Arraylist in just 1 ms (99.71% faster)
Hi, guys. Today we will see the solution of phoenix13steve user on LeetCode. Link to this solution is here: Short java solution
1. First he creates a Stack.
2. Then he goes through the String letter by letter.
3. For each
char he check if it is '(' or '{', or '['. - If yes, then he pushes closing parentheses to the relevant case.
- If no, then he checks the condition where Stack is empty or the last element of the Stack is not equal current
char. If yes, then we return false.Let's have a closer look to the logic. Here if current
char is not opening parentheses, then we must have closing one for the latest until this moment.4. Finally, if the Stack is empty, then we return true. It means that all opening brackets had closing in the right order.
Peace! βοΈπ€
LeetCode
Valid Parentheses - LeetCode
Can you solve this real interview question? Valid Parentheses - Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
1. Open brackets must be closed by theβ¦
An input string is valid if:
1. Open brackets must be closed by theβ¦
π3
πΊπΏ 20. Valid Parentheses.
π’ Oson: 20. Valid Parentheses
π¨βπ» Mening yechimim: Java 2 Solutions | Stack | Arraylist in just 1 ms (99.71% faster)
Hammaga salom! Bugun sizlar bilan phoenix13steve nickname-ga ega LeetCoder-ning yechimini ko'ramiz. Yechimga havola: Short java solution
1. Boshida Stack yaratadi.
2. Keying foreach loop orqali String-ni aylanib chiqadi.
3. Har bir
- Agar shulardan bittasiga teng bo'lsa, Stack-ga shu qavsning mos yopadigan qavsini push() qiladi.
- Agar teng bo'lmasa, u Stack bo'sh yoki Stack-ning oxirgi elementi hozirgi char-ga teng emasligini tekshiradi. Agar shu shart true bo'lsa, metod false qaytaradi.
Keling shu harakatning tagidagi mantiqni ko'raylik. Agar loop-dagi hozirgi
4. Oxirida esa Stack-ning bo'shiligi tekshiriladi. Agar u bo'sh bo'lsa, bu hamma ochuvchi qavslar o'z yopuvchi qavslariga ega va ular to'g'ri ketma-ketlikda joylashganligini bildiradi.
Hammaga omad! βοΈπ€
π’ Oson: 20. Valid Parentheses
π¨βπ» Mening yechimim: Java 2 Solutions | Stack | Arraylist in just 1 ms (99.71% faster)
Hammaga salom! Bugun sizlar bilan phoenix13steve nickname-ga ega LeetCoder-ning yechimini ko'ramiz. Yechimga havola: Short java solution
1. Boshida Stack yaratadi.
2. Keying foreach loop orqali String-ni aylanib chiqadi.
3. Har bir
char-ni '(' yoki '{' yoki '[' tengligini tekshiradi.- Agar shulardan bittasiga teng bo'lsa, Stack-ga shu qavsning mos yopadigan qavsini push() qiladi.
- Agar teng bo'lmasa, u Stack bo'sh yoki Stack-ning oxirgi elementi hozirgi char-ga teng emasligini tekshiradi. Agar shu shart true bo'lsa, metod false qaytaradi.
Keling shu harakatning tagidagi mantiqni ko'raylik. Agar loop-dagi hozirgi
char ochuvchi qavs bo'lmasa, unda hozirgi char oxirgi uchragan ochuvchi qavsning yopuvchi jufti bo'lishi shart.4. Oxirida esa Stack-ning bo'shiligi tekshiriladi. Agar u bo'sh bo'lsa, bu hamma ochuvchi qavslar o'z yopuvchi qavslariga ega va ular to'g'ri ketma-ketlikda joylashganligini bildiradi.
Hammaga omad! βοΈπ€
LeetCode
Valid Parentheses - LeetCode
Can you solve this real interview question? Valid Parentheses - Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
1. Open brackets must be closed by theβ¦
An input string is valid if:
1. Open brackets must be closed by theβ¦
π7
π¬π§ 921. Minimum Add to Make Parentheses Valid
Hi, guys! Another day - another Stack challenge :). Let's do it.
π€¨ Medium: 921. Minimum Add to Make Parentheses Valid
π¨βπ» My solution: Java | Stack | O(N) Solution
Let's see how fast can you solve it! Share you results below.
Good luck! βοΈπ€
Hi, guys! Another day - another Stack challenge :). Let's do it.
π€¨ Medium: 921. Minimum Add to Make Parentheses Valid
π¨βπ» My solution: Java | Stack | O(N) Solution
Let's see how fast can you solve it! Share you results below.
Good luck! βοΈπ€
LeetCode
Minimum Add to Make Parentheses Valid - LeetCode
Can you solve this real interview question? Minimum Add to Make Parentheses Valid - A parentheses string is valid if and only if:
* It is the empty string,
* It can be written as AB (A concatenated with B), where A and B are valid strings, or
* It canβ¦
* It is the empty string,
* It can be written as AB (A concatenated with B), where A and B are valid strings, or
* It canβ¦
π3
πΊπΏ 921. Minimum Add to Make Parentheses Valid
Hammaga salom! Bugun ham amaliy mashq bajaramiz. Bugun yana o'sha Stack mavzusi.
[
LeetCode-chilar: Siz o'shamu?
Stack: Ha, men o'sha.
] π
π€¨ Medium: 921. Minimum Add to Make Parentheses Valid
π¨βπ» Mening yechimim: Java | Stack | O(N) Solution
Qani ko'raylikchi kim qancha tez yecha olgan ekan! Natijalarni comments-da qoldiring.
Omad! βοΈπ€
Hammaga salom! Bugun ham amaliy mashq bajaramiz. Bugun yana o'sha Stack mavzusi.
[
LeetCode-chilar: Siz o'shamu?
Stack: Ha, men o'sha.
] π
π€¨ Medium: 921. Minimum Add to Make Parentheses Valid
π¨βπ» Mening yechimim: Java | Stack | O(N) Solution
Qani ko'raylikchi kim qancha tez yecha olgan ekan! Natijalarni comments-da qoldiring.
Omad! βοΈπ€
LeetCode
Minimum Add to Make Parentheses Valid - LeetCode
Can you solve this real interview question? Minimum Add to Make Parentheses Valid - A parentheses string is valid if and only if:
* It is the empty string,
* It can be written as AB (A concatenated with B), where A and B are valid strings, or
* It canβ¦
* It is the empty string,
* It can be written as AB (A concatenated with B), where A and B are valid strings, or
* It canβ¦
π6
π¬π§ 921. Minimum Add to Make Parentheses Valid
π€¨ Medium: 921. Minimum Add to Make Parentheses Valid
π¨βπ» My solution: Java | Stack | O(N) Solution
Hi, guys! Let's check one of possible solutions using Stack Data Structure.
π¨βπ» Task
Our task is to compose valid bracket string by making minimum number of insertions and return this minimum number.
π‘ Idea
The idea behind the solution is to keep invalid brackets and return their it. Thus, we will know how many insertions we have to do in order to make the string valid.
π¨βπ» Solution
My solution is pretty short.
1. First, I create Stack.
2. Then I iterate over String
I check if the stack is empty. If it is, then I just push the
If it has some values then I check whether the last
- In this case I remove the last
- Otherwise, I add current
3. When loop finishes I will have Stack with
This is it :)
Good luck! βοΈπ€
π€¨ Medium: 921. Minimum Add to Make Parentheses Valid
π¨βπ» My solution: Java | Stack | O(N) Solution
Hi, guys! Let's check one of possible solutions using Stack Data Structure.
π¨βπ» Task
Our task is to compose valid bracket string by making minimum number of insertions and return this minimum number.
π‘ Idea
The idea behind the solution is to keep invalid brackets and return their it. Thus, we will know how many insertions we have to do in order to make the string valid.
π¨βπ» Solution
My solution is pretty short.
1. First, I create Stack.
2. Then I iterate over String
char by char.I check if the stack is empty. If it is, then I just push the
char.If it has some values then I check whether the last
char is opening bracket and current char is closing bracket. - In this case I remove the last
char from the stack.- Otherwise, I add current
char to the stack since it "incorrectly placed".3. When loop finishes I will have Stack with
chars which make given String invalid. So, I just return the size of the Stack.This is it :)
Good luck! βοΈπ€
LeetCode
Minimum Add to Make Parentheses Valid - LeetCode
Can you solve this real interview question? Minimum Add to Make Parentheses Valid - A parentheses string is valid if and only if:
* It is the empty string,
* It can be written as AB (A concatenated with B), where A and B are valid strings, or
* It canβ¦
* It is the empty string,
* It can be written as AB (A concatenated with B), where A and B are valid strings, or
* It canβ¦
π2
πΊπΏ 921. Minimum Add to Make Parentheses Valid
π€¨ Medium: 921. Minimum Add to Make Parentheses Valid
π¨βπ» Mening yechimim: Java | Stack | O(N) Solution
Hammaga salom! Keling shu masalaning Stack ishlatgan holda yechish mumkin bo'lgan usullaridan bittasini ko'rib chiqamiz.
π¨βπ» Vazifa
Bizning vazifamiz - bu berilgan (faqat qavslardan iborat) String-ni minimal o'zgarishlar orqali to'g'ri String qilish va shu o'zgarishlar sonini qaytarish.
π‘ G'oya
Mening yechimimning tagidagi g'oya - bu "noto'g'ri" qavslarni saqlash va ularning sonini qaytarish. Bu orqali String-ni to'g'ri qilish uchun biz nechta o'zgarishlar kiritishimiz kerakligini bilib olamiz.
π¨βπ» Yechim
Mening yechimim qisqa chiqdi.
1. Boshida Stack yarataman.
2. Keying esa String-ni har bir harfini [
Stack bo'shligini tekshiraman. Agar bo'sh bo'lsa unda
- Agar shu shart true bo'lsa, stack-ning oxirgi elementini olib tashlayman, chunki bu to'g'ri holat.
- Agar shu shart false bo'lsa, stack-ga hozirgi
3. Loop tugaganida menda noto'g'ri joylashgan qavslardan iborat Stack hosil bo'ladi va men faqat Stack-ning hajmini qaytaraman.
Bugunga shu :)
Omad! βοΈπ€
π€¨ Medium: 921. Minimum Add to Make Parentheses Valid
π¨βπ» Mening yechimim: Java | Stack | O(N) Solution
Hammaga salom! Keling shu masalaning Stack ishlatgan holda yechish mumkin bo'lgan usullaridan bittasini ko'rib chiqamiz.
π¨βπ» Vazifa
Bizning vazifamiz - bu berilgan (faqat qavslardan iborat) String-ni minimal o'zgarishlar orqali to'g'ri String qilish va shu o'zgarishlar sonini qaytarish.
π‘ G'oya
Mening yechimimning tagidagi g'oya - bu "noto'g'ri" qavslarni saqlash va ularning sonini qaytarish. Bu orqali String-ni to'g'ri qilish uchun biz nechta o'zgarishlar kiritishimiz kerakligini bilib olamiz.
π¨βπ» Yechim
Mening yechimim qisqa chiqdi.
1. Boshida Stack yarataman.
2. Keying esa String-ni har bir harfini [
char] foreach loop orqali ko'rib chiqaman. Loop-ni ichida:Stack bo'shligini tekshiraman. Agar bo'sh bo'lsa unda
char-ni stack-ga qo'shaman. Agar bo'sha bo'lmasa, men stack-dagi oxirgi char ochuvchi qavs va hozirgi char yopuvchi qavsligini tekshiraman:- Agar shu shart true bo'lsa, stack-ning oxirgi elementini olib tashlayman, chunki bu to'g'ri holat.
- Agar shu shart false bo'lsa, stack-ga hozirgi
char-ni qo'shaman, chunki bu joylashishi noto'g'ri qavsdir.3. Loop tugaganida menda noto'g'ri joylashgan qavslardan iborat Stack hosil bo'ladi va men faqat Stack-ning hajmini qaytaraman.
Bugunga shu :)
Omad! βοΈπ€
LeetCode
Minimum Add to Make Parentheses Valid - LeetCode
Can you solve this real interview question? Minimum Add to Make Parentheses Valid - A parentheses string is valid if and only if:
* It is the empty string,
* It can be written as AB (A concatenated with B), where A and B are valid strings, or
* It canβ¦
* It is the empty string,
* It can be written as AB (A concatenated with B), where A and B are valid strings, or
* It canβ¦
π4
π¬π§ Theory. Queue.
Hi, guys! After covering Stack Data Structure it's time to move on to the next one - Queue. Let's see what DS Queue is.
Basically saying, Queue is a data structure which allows to access only one data node - the first inserted node. When you remove [or poll] that item, then you can access the second added item and etc.
The approach implemented by this DS is called First-In-First-Out or FIFO. It means that oldest element added will be processed first. Imagine people standing in a queue: first come - first served. Or another example: when you come up to any shelve in Korzinka, you will always see the oldest products [and if you put your hand deeper you can find the fresher oneπ]
We can write our own Queue. The classes that we will need for this:
1. Node class. This is the same Node class that we utilized for Linked List - link. Basically, we build a linked list for Queue, but utilize it in a specific manner.
2. Queue class. Queue class contains a link to the first and the last nodes. If the first is null, then we don't have node in our Queue yet. Queue also has:
- constructor(s) to create our Queue with a certain state.
- methods: adding offer() or push() [depends on language], removing poll(), displaying the first element peek(), and etc.
3. There is an interesting thing: we can build a specific Linked List for our Queue. Let's call this list - FirstLast List. This name reflects the behavior of the list, i.e. we will need to keep two pointers
That's it for today. We will review the possible implementation in the next posts. Tune up!
Sources:
1. Data Structures and Algorithms in Java. R. Lafore.
2. Internet.
Good luck! βοΈπ€
Hi, guys! After covering Stack Data Structure it's time to move on to the next one - Queue. Let's see what DS Queue is.
Basically saying, Queue is a data structure which allows to access only one data node - the first inserted node. When you remove [or poll] that item, then you can access the second added item and etc.
The approach implemented by this DS is called First-In-First-Out or FIFO. It means that oldest element added will be processed first. Imagine people standing in a queue: first come - first served. Or another example: when you come up to any shelve in Korzinka, you will always see the oldest products [and if you put your hand deeper you can find the fresher oneπ]
We can write our own Queue. The classes that we will need for this:
1. Node class. This is the same Node class that we utilized for Linked List - link. Basically, we build a linked list for Queue, but utilize it in a specific manner.
2. Queue class. Queue class contains a link to the first and the last nodes. If the first is null, then we don't have node in our Queue yet. Queue also has:
- constructor(s) to create our Queue with a certain state.
- methods: adding offer() or push() [depends on language], removing poll(), displaying the first element peek(), and etc.
3. There is an interesting thing: we can build a specific Linked List for our Queue. Let's call this list - FirstLast List. This name reflects the behavior of the list, i.e. we will need to keep two pointers
first and last. first will be used for retrieving or removing the elements from Queue and last will be used for insertion.That's it for today. We will review the possible implementation in the next posts. Tune up!
Sources:
1. Data Structures and Algorithms in Java. R. Lafore.
2. Internet.
Good luck! βοΈπ€
Telegram
LeetCodin
π¬π§ Theory. Linked List.
Hi, guys! We covered theory on how linked list works. Let's see some pieces of code to understand how to build our own simple Linked list.
LinkedList class.
- LinkedList class contains a link to the first node. If it's null, thenβ¦
Hi, guys! We covered theory on how linked list works. Let's see some pieces of code to understand how to build our own simple Linked list.
LinkedList class.
- LinkedList class contains a link to the first node. If it's null, thenβ¦
π1π1
πΊπΏ Nazariya. Queue.
Hammaga salom! Stack ma'lumotlar tuzilmasidan [MT] keyin Queue-ni o'tishimiz shart. Keling shu postda Queue-ni ko'rib chiqamiz.
Queue [navbat] - bu faqat bitta, birinchi kiritilgan, tugun bilan foydalanish imkonini beradigan ma'lumot tuzilmasi. Shu elementni olib tashlaganingizdan [poll()] so'ng, keyingi [ikkinchi] element bilan foydalana olasiz.
Ushbu MT tomonidan qo'llaniladigan usul First-In-First-Out yoki FIFO deb ataladi. Bu degani MT-ga birinchi qo'shilgan element navbatda birinchi bo'lib ishlatiladi.
- Tasavvur qiling odamlar navbatda turibdilar: birinchi kelganga - birinchi navbatda xizmat ko'rsatiladi. Tanish-bilish bu ma'lumotlar tuzilmasida ishlatilmaydi! Qo'yaverasiz!
- Yana bitta misol: Korzinkada qaymoq olaman desangiz, birinchi o'rinda oldingizda eskiroq [kelgan] mahsulot turadi. Agar erinmasdan qo'lingizni sal ichkariroq cho'zsangiz - yangirog'ini topasiz.π
Biz ham o'zimizning sodda Queue MT-mizni yozishimiz mumkin. Bizga quyidagi klasslar kerak bo'ladi:
1. Node klassi. Bu huddi Linked List uchun ishlatgan Node klass kabi tuzilishga ega. O'zi Queue linked list ustida qurilgan desak bo'ladi.
2. Queue klassi. Queue klassi o'zida birinchi va oxirgi tugunlarga ko'rsatuvchi ko'rsatgichlarni saqlaydi. Agar birinchisi null bo'lsa, bizning Queue-miz bo'sh degani. Queue-da yana:
- ma'lum bir holat bilan Queue-ni yaratuvchi konstruktor;
- qo'shish offer() yoki push() [tilga qarab], olib tashlash poll(), oxirgi elementni ko'rsatish peek() kabi metodlar bo'ladi.
3. Bunday qilsak ham bo'ladi: Queue-miz uchun maxsus bir Linked List yozishimiz mumkin. Uning nomini FirstLast List qilamiz. Chunki bu nom uning behavior-ni yaxshi aks ettiradi, boshqa so'z bilan aytganda u ikkita
Bugunga shu. Bu MT-ning implementatsiyasini keyingi postlarda ko'rib chiqamiz.
P.S. Queue-ni o'qiyman deb qiynalmadingizlarmi?π Qanday qilib o'qiganingizni comments-da yozing.πππ
[kuye uye] deb o'qigan bo'lsangiz - sizga 5 baho!
Manbalar:
1. Data Structures and Algorithms in Java. R. Lafore.
2. Internet.
Hammaga omad! βοΈπ€
Hammaga salom! Stack ma'lumotlar tuzilmasidan [MT] keyin Queue-ni o'tishimiz shart. Keling shu postda Queue-ni ko'rib chiqamiz.
Queue [navbat] - bu faqat bitta, birinchi kiritilgan, tugun bilan foydalanish imkonini beradigan ma'lumot tuzilmasi. Shu elementni olib tashlaganingizdan [poll()] so'ng, keyingi [ikkinchi] element bilan foydalana olasiz.
Ushbu MT tomonidan qo'llaniladigan usul First-In-First-Out yoki FIFO deb ataladi. Bu degani MT-ga birinchi qo'shilgan element navbatda birinchi bo'lib ishlatiladi.
- Tasavvur qiling odamlar navbatda turibdilar: birinchi kelganga - birinchi navbatda xizmat ko'rsatiladi. Tanish-bilish bu ma'lumotlar tuzilmasida ishlatilmaydi! Qo'yaverasiz!
- Yana bitta misol: Korzinkada qaymoq olaman desangiz, birinchi o'rinda oldingizda eskiroq [kelgan] mahsulot turadi. Agar erinmasdan qo'lingizni sal ichkariroq cho'zsangiz - yangirog'ini topasiz.π
Biz ham o'zimizning sodda Queue MT-mizni yozishimiz mumkin. Bizga quyidagi klasslar kerak bo'ladi:
1. Node klassi. Bu huddi Linked List uchun ishlatgan Node klass kabi tuzilishga ega. O'zi Queue linked list ustida qurilgan desak bo'ladi.
2. Queue klassi. Queue klassi o'zida birinchi va oxirgi tugunlarga ko'rsatuvchi ko'rsatgichlarni saqlaydi. Agar birinchisi null bo'lsa, bizning Queue-miz bo'sh degani. Queue-da yana:
- ma'lum bir holat bilan Queue-ni yaratuvchi konstruktor;
- qo'shish offer() yoki push() [tilga qarab], olib tashlash poll(), oxirgi elementni ko'rsatish peek() kabi metodlar bo'ladi.
3. Bunday qilsak ham bo'ladi: Queue-miz uchun maxsus bir Linked List yozishimiz mumkin. Uning nomini FirstLast List qilamiz. Chunki bu nom uning behavior-ni yaxshi aks ettiradi, boshqa so'z bilan aytganda u ikkita
first va last ko'rsatgichlarini saqlaydi. first-ni birinchi elementni tekshirish va olib tashlash uchun ishlatsak, last-ni esa yangi elementni qo'shish uchun ishlatamiz.Bugunga shu. Bu MT-ning implementatsiyasini keyingi postlarda ko'rib chiqamiz.
P.S. Queue-ni o'qiyman deb qiynalmadingizlarmi?π Qanday qilib o'qiganingizni comments-da yozing.πππ
[kuye uye] deb o'qigan bo'lsangiz - sizga 5 baho!
Manbalar:
1. Data Structures and Algorithms in Java. R. Lafore.
2. Internet.
Hammaga omad! βοΈπ€
Telegram
LeetCodin
πΊπΏ Nazariya. Linked List.
Hammaga salom! Biz sizlar bilan Linked list qanday ishlashi haqidagi nazariyani ko'rib chiqdik. Keling, o'zimizning oddiy Linked list-imizni qanday yaratishni tushunish uchun ba'zi kod qismlarini ko'rib chiqaylik.
LinkedList klassidaβ¦
Hammaga salom! Biz sizlar bilan Linked list qanday ishlashi haqidagi nazariyani ko'rib chiqdik. Keling, o'zimizning oddiy Linked list-imizni qanday yaratishni tushunish uchun ba'zi kod qismlarini ko'rib chiqaylik.
LinkedList klassidaβ¦
π5π1π€£1
π¬π§ Queue methods and implementation.
Hi, guys! Let's see how can we build our own Queue.
As we discussed yesterday, we can build it using three classes:
1. It will contain two pointers
2. In constructor I just assign null to them.
3. We will need isEmpty() method, which returns boolean value. If
4. Method
- First create a new Node with given value.
- Then we check if the list is empty [see point 3]. If it is, then we assign new node to the
- In the end of the method we just assign the new node to
5. Method
- First we save data of the first node to a temporary variable.
- Then we check if we have an element after the first. If no, the we assign null to
- Then we assign
- Finally we return temporary value to show which value has been deleted.
Good! This is it for today.
Peace! βοΈπ€
Hi, guys! Let's see how can we build our own Queue.
As we discussed yesterday, we can build it using three classes:
Node, FirstLastList, and Queue itself. Since we already saw the code for Node, I'll review only two latter. Today we will see FirstLastList.FirstLastList:1. It will contain two pointers
first and last.2. In constructor I just assign null to them.
3. We will need isEmpty() method, which returns boolean value. If
first is null, then the list is empty. Make a pause here and think about it.4. Method
insertLast(int val). We need this method to insert a value to the end of the list. Inside of this method we:- First create a new Node with given value.
- Then we check if the list is empty [see point 3]. If it is, then we assign new node to the
first variable. Why? Because it is the first element in the queue. We need it for removing elements from queue. If it is not empty, then we assign new node to the next pointer of last node.- In the end of the method we just assign the new node to
last variable. Thus we keep a link the the last node of a queue.5. Method
deleteFirst(). We remove the first element from the list. - First we save data of the first node to a temporary variable.
- Then we check if we have an element after the first. If no, the we assign null to
last. Thus we remove an element if there is only one inside of the queue. - Then we assign
next pointer's value of first to first itself. It means that now first points to the second element in the queue.- Finally we return temporary value to show which value has been deleted.
Good! This is it for today.
Peace! βοΈπ€
πΊπΏ Queue metodlari va implementatsiyasi.
Hammaga salom! Bugun Queue mavzusini davom etamiz.
Kecha aytib o'tganimdek, Queue-ni uchta klass orqali tuzamiz. Bular
1. Ikkita
2. Parametrsiz konstruktor ham bo'ladi. Uning ichida tepadagi ikkita ko'rsatgichlarning qiymatini
3. Bizga yana isEmpty() [bo'sh] boolean qiymat qaytaruvchi metod kerak bo'ladi. Agar
4.
- Yangi berilgan val qiymatga ega Node yaratamiz.
- Keying biz list-imiz bo'shligini tekshiramiz [punkt 3]. Agar bo'sh bo'lsa biz yangi elementni unga yozib qo'yamiz. Nimaga? Chunki queue bo'sh va bu u uchun birinchi element. Agar queue bo'sh bo'lmasa, biz oxirgi elementning keyingi elementga ko'rsatuvchi ko'rsatgichiga yangi Node-ni yozib qo'yamiz.
- Metodni yakunida yangi Node-ni
5.
- Metodning boshida birinchi elementning ichidagi qiymatni yozib olish uchun vaqtincha
- Keyin
- Keyin biz
- Metodni oxirida biz olib tashlangan elementning ichidagi saqlab qo'yilgan qiymatini qaytaramiz.
Yaxshi! Bugun ham yangi narsa o'rgandik.
Omad! βοΈπ€
Hammaga salom! Bugun Queue mavzusini davom etamiz.
Kecha aytib o'tganimdek, Queue-ni uchta klass orqali tuzamiz. Bular
Node, FirstLastList va Queue klasslari. Node klassini bungacha ko'rib chiqqanmiz, shuning uchun faqat qolgan ikkitasini implementatsiyasini ko'ramiz. Keling bugun FirstLastList-da nimalar bo'lishini yozaman.FirstLastList:1. Ikkita
first va last Node type-dagi ko'rsatgichlar bo'ladi.2. Parametrsiz konstruktor ham bo'ladi. Uning ichida tepadagi ikkita ko'rsatgichlarning qiymatini
null qilamiz.3. Bizga yana isEmpty() [bo'sh] boolean qiymat qaytaruvchi metod kerak bo'ladi. Agar
first null-ga teng bo'lsa, [queue-ning tagida bo'lgan] list bo'shligini anglatadi. Bu yerda sal to'xtab, shu oxirgi gapni yana bir marta o'qib, nimaga bunday bo'lishini o'ylab ko'ring.4.
insertLast(int val). Bu metod orqali biz list-ning oxiriga yangi elementni qo'shib qo'yamiz. Uning ichida:- Yangi berilgan val qiymatga ega Node yaratamiz.
- Keying biz list-imiz bo'shligini tekshiramiz [punkt 3]. Agar bo'sh bo'lsa biz yangi elementni unga yozib qo'yamiz. Nimaga? Chunki queue bo'sh va bu u uchun birinchi element. Agar queue bo'sh bo'lmasa, biz oxirgi elementning keyingi elementga ko'rsatuvchi ko'rsatgichiga yangi Node-ni yozib qo'yamiz.
- Metodni yakunida yangi Node-ni
last ko'rsatgichiga yozamiz.5.
deleteFirst(). Queue-dan birinchi elementni olib tashlash.- Metodning boshida birinchi elementning ichidagi qiymatni yozib olish uchun vaqtincha
temp o'zgaruvchisini yaratamiz.- Keyin
first [birinchi] elementdan keyin yana element borligini tekshiramiz. Agar bo'lmasa, last [oxirgi] ko'rsatgichimizga null yozib qo'yamiz. Buning sababi - queue-da bitta element bo'lsa, uni olib tashlaymiz. Bu degani oxirgi [last] element null bo'lishi kerak.- Keyin biz
first-ning next ko'rsatgichini first-ning o'ziga yozib qo'yamiz. Bu orqali biz birinchi elementni olib tashlab, ikkinchi elementni navbatni [queue-ni] boshiga joylashtiramiz.- Metodni oxirida biz olib tashlangan elementning ichidagi saqlab qo'yilgan qiymatini qaytaramiz.
Yaxshi! Bugun ham yangi narsa o'rgandik.
Omad! βοΈπ€
π3
π¬π§ Queue methods and implementation. Part 2.
Hi, guys! Let's see what fields and methods
1. A pointer to
2. Constructor which will create new
3.
4.
5.
6.
This should be it. Hope you enjoyed it and everything was pretty clear.
Good luck! βοΈπ€
Hi, guys! Let's see what fields and methods
Queue class will consist of.Queue:1. A pointer to
FirstLastList class.2. Constructor which will create new
FirstLastList object.3.
isEmpty() method which will return boolean value.4.
push() method which adds new element to our queue [to the end of it].5.
poll() or remove method which removes first added element.6.
peek() method which shows first element's value. Here I added a method into FirstLastList class which returns data inside the first node of the list. Then I called this method inside of peek().This should be it. Hope you enjoyed it and everything was pretty clear.
Good luck! βοΈπ€
πΊπΏ Queue metodlari va implementatsiyasi. 2-qism.
Hammaga salom! Keling endi
1.
2.
3.
4.
5.
6.
Ana shu xolos. Umid qilamanki sizlarga bu tarzda ma'lumotlar tuzilmalarini ko'rib chiqishimiz yoqyapti va hammasi tushunarli.
Omad! βοΈπ€
Hammaga salom! Keling endi
Queue klassi nimalardan iborat bo'lishini ko'rib chiqamiz.Queue:1.
FirstLastList klassiga ko'rsatgich.2.
Queue konstruktorinig ichida yangi FirstLastList type-dagi object yaratamiz.3.
isEmpty() boolean qiymat qaytaradi. U Queue-bo'shligini bildiradi.4.
push() metodi navbatimizga [oxiriga] yangi elementni qo'shib qo'yadi.5.
poll() metodi birinchi elementni olib tashlaydi.6.
peek() birinchi elementning ichidagi ma'lumotning qiymatini qaytaradi. Buning uchun FirstLastList klassida yangi metod yaratdim. Bu metod birinchi element ichidagi qiymatni qaytaradi. Keyin bu metodni peek() ichida ishlatdim.Ana shu xolos. Umid qilamanki sizlarga bu tarzda ma'lumotlar tuzilmalarini ko'rib chiqishimiz yoqyapti va hammasi tushunarli.
Omad! βοΈπ€
π2π1
π¬π§ Stack n Queue.
Hi, guys! Let's wrap up our latest topics in this post.
Stack:
1. Theory. Stack.
2. Theory. Stack methods.
Queue:
1. Theory. Queue.
2. Queue methods and implementation.
2. Queue methods and implementation. Part 2.
These links are for those who want to come back to these topics in future.
Good luck! βοΈπ€
#stack #queue #wrapup
@leetcodin
Hi, guys! Let's wrap up our latest topics in this post.
Stack:
1. Theory. Stack.
2. Theory. Stack methods.
Queue:
1. Theory. Queue.
2. Queue methods and implementation.
2. Queue methods and implementation. Part 2.
These links are for those who want to come back to these topics in future.
Good luck! βοΈπ€
#stack #queue #wrapup
@leetcodin
Telegram
LeetCodin
π¬π§ Theory. Stack.
Hi, guys! Since we discussed Linked Lists, I think the logical continuation of the topic should be such Data Structures as Stack and Queue. So, let's start from Stack.
Stack is a data structure which allows access to only one data nodeβ¦
Hi, guys! Since we discussed Linked Lists, I think the logical continuation of the topic should be such Data Structures as Stack and Queue. So, let's start from Stack.
Stack is a data structure which allows access to only one data nodeβ¦
π7