question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
binary-tree-zigzag-level-order-traversal
Binary Tree Zigzag Level Order Traversal [C++]
binary-tree-zigzag-level-order-traversal-g2x2
IntuitionThe problem involves traversing a binary tree in a zigzag (alternating) order, level by level. This can be thought of as a variation of the standard le
moveeeax
NORMAL
2025-01-26T04:19:53.400161+00:00
2025-01-26T04:19:53.400161+00:00
1,911
false
# Intuition The problem involves traversing a binary tree in a zigzag (alternating) order, level by level. This can be thought of as a variation of the standard level-order traversal with the additional requirement of reversing the order of nodes at alternate levels. # Approach 1. **Use a Queue for BFS Traversal**: ...
11
0
['C++']
0
binary-tree-zigzag-level-order-traversal
[C++] [Using DEQUE] Runtime: 0 ms,Memory Usage: 12.2 MB.
c-using-deque-runtime-0-msmemory-usage-1-ildw
\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0)
gs_panwar
NORMAL
2021-05-31T10:45:16.543266+00:00
2021-05-31T10:45:38.452989+00:00
599
false
```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right...
11
0
['Queue', 'C']
0
binary-tree-zigzag-level-order-traversal
Easy to understand C++ 10 liner (no reverse used)
easy-to-understand-c-10-liner-no-reverse-9dbm
Idea\nAll you have to do is do the standard bfs(with two stacks instead) with two adjustments -\n\n1) instead of dequeueing from a queue, pop() from our first s
numbart
NORMAL
2020-10-04T01:52:53.950921+00:00
2020-10-04T02:05:36.947842+00:00
2,295
false
**Idea**\nAll you have to do is do the standard bfs(with two stacks instead) with two adjustments -\n\n1) instead of dequeueing from a queue, pop() from our first stack pointer. Simmilarly instead of enqueueing push to our second stack pointer.\n2) In queue you will push in left child then right child in every level. H...
11
1
['Stack', 'Breadth-First Search', 'C', 'C++']
1
binary-tree-zigzag-level-order-traversal
Fastest zigzag: 0 ms Beats 100.00% Analyze Complexity Memory 4.45 MB Beats 95.97%
fastest-zigzag-0-ms-beats-10000-analyze-c0qzx
ApproachRecursive approachComplexity Time complexity: 0 ms Beats 100.00% Space complexity: 4.45 MB Beats 95.97% Code
sushilmaxbhile
NORMAL
2025-04-01T04:57:40.986370+00:00
2025-04-01T04:57:40.986370+00:00
539
false
# Approach Recursive approach # Complexity - Time complexity: 0 ms Beats 100.00% - Space complexity: 4.45 MB Beats 95.97% # Code ```golang [] import "slices" /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func zigzagLevelOrder...
10
0
['Go']
0
binary-tree-zigzag-level-order-traversal
[JAVA] easy solution with level order traversal using queue.
java-easy-solution-with-level-order-trav-g7tr
I traverse by level using queue.\nFor even level elements are reversed and the other uses original order.\n\n\nclass Solution {\n public static List<List<Int
duck67
NORMAL
2020-02-07T10:01:20.648111+00:00
2020-02-07T10:06:50.626734+00:00
999
false
I traverse by level using queue.\nFor even level elements are reversed and the other uses original order.\n\n```\nclass Solution {\n public static List<List<Integer>> zigzagLevelOrder(TreeNode root) {\n List<List<Integer>> res = new ArrayList<>();\n if(root == null) return res;\n \n Queue...
10
0
['Queue', 'Java']
1
binary-tree-zigzag-level-order-traversal
C# queue
c-queue-by-bacon-wst8
\npublic class Solution {\n public IList<IList<int>> ZigzagLevelOrder(TreeNode root) {\n var result = new List<IList<int>>();\n if (root == nul
bacon
NORMAL
2019-05-04T05:15:13.404144+00:00
2019-05-04T05:15:13.404174+00:00
895
false
```\npublic class Solution {\n public IList<IList<int>> ZigzagLevelOrder(TreeNode root) {\n var result = new List<IList<int>>();\n if (root == null) return result;\n\n var queue = new Queue<TreeNode>();\n queue.Enqueue(root);\n\n while (queue.Any()) {\n var size = queue....
10
0
[]
2
binary-tree-zigzag-level-order-traversal
ZigZag Traversal | No Reverse | Deque | 1 ms
zigzag-traversal-no-reverse-deque-1-ms-b-hr4k
IntuitionThe interviewer might be interested in actual zig zag traversal and may not care about reversing the level's elements based on level parity. We can sim
ramuked
NORMAL
2025-02-22T07:21:09.008200+00:00
2025-02-22T07:21:09.008200+00:00
1,689
false
# Intuition The interviewer might be interested in actual zig zag traversal and may not care about reversing the level's elements based on level parity. We can simulate actual level order traversal by using a deque. # Approach Based on level parity, we can add elements to either at the front of the deque or at the end....
9
0
['Java']
0
binary-tree-zigzag-level-order-traversal
💥☠️🎯Easiest Simple⚡Tree🌲 C++✔️Python3🐍✔️Java✔️C✔️Python🐍✔️-Beats⚡🎯💯Simplified💢💢
easiest-simpletree-cpython3javacpython-b-63z4
Intuition\n\n\n\n\n\n# This is a simplemLevel order application check out the image + vedio solution.. of mine.. \nhttps://leetcode.com/problems/binary-tree-lev
Edwards310
NORMAL
2024-04-22T10:45:29.848845+00:00
2024-04-22T11:08:21.494779+00:00
962
false
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/09fc34c1-dfc6-4dbe-8edb-8f9a23e5ee93_1713781228.5565534.jpeg)\n![Screenshot 2024-04-22 154139.png](https://assets.leetcode.com/users/images/62578198-ed5d-43c0-8deb-be8e4ef73114_1713781237.45546.png)\n![Screenshot 2024-04-22 154114.png](https://a...
9
0
['C', 'Python', 'C++', 'Java', 'Python3']
3
binary-tree-zigzag-level-order-traversal
Two stack approach (c++ solution)
two-stack-approach-c-solution-by-femish_-bdt2
This can be solve using two stack current and next.\n\n\nclass Solution {\npublic:\n \n void solve(TreeNode* root,vector<vector<int>> &ans){\n if(r
Femish_20
NORMAL
2022-06-21T10:00:05.906489+00:00
2022-06-21T14:27:44.838645+00:00
404
false
This can be solve using two stack current and next.\n\n```\nclass Solution {\npublic:\n \n void solve(TreeNode* root,vector<vector<int>> &ans){\n if(root==NULL) return ;\n \n stack<TreeNode*> curr;\n stack<TreeNode*> next;\n \n bool leftToRight=true;\n \n\t\t//push...
9
0
['Stack', 'C++']
0
binary-tree-zigzag-level-order-traversal
O(n)time/BEATS 99.97% MEMORY/SPEED 0ms MAY 2022
ontimebeats-9997-memoryspeed-0ms-may-202-ps8o
\n\n\n(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, please upvote this post.)\nTake care broth
darian-catalin-cucer
NORMAL
2022-05-04T20:14:09.343851+00:00
2022-05-04T20:14:46.943796+00:00
2,268
false
```\n```\n\n(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, ***please upvote*** this post.)\n***Take care brother, peace, love!***\n\n```\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 38.2MB*** (beats 92.04% / 24.00%).\n* *...
9
0
['Breadth-First Search', 'Queue', 'Swift', 'C', 'Python', 'Java', 'Python3', 'Kotlin', 'JavaScript']
4
binary-tree-zigzag-level-order-traversal
Easy to understand | 2 Solution | DFS | BFS | Simple | Python solution
easy-to-understand-2-solution-dfs-bfs-si-xish
\n def recursive(self, root):\n #inspired by the solution somewhat\n out = []\n def rec(node, height):\n if node:\n
mrmagician
NORMAL
2020-05-21T00:13:04.711927+00:00
2020-05-21T00:13:04.711974+00:00
1,392
false
```\n def recursive(self, root):\n #inspired by the solution somewhat\n out = []\n def rec(node, height):\n if node:\n if len(out) <= height:\n out.append([])\n out[height].append(node.val)\n rec(node.left, height + 1)\n ...
9
0
['Depth-First Search', 'Breadth-First Search', 'Python', 'Python3']
0
binary-tree-zigzag-level-order-traversal
Java, easy understand recursive methods, beats 96% (attach easy BFS methods)
java-easy-understand-recursive-methods-b-b227
//recursive method\n public List<List<Integer>> zigzagLevelOrder(TreeNode root) {\n List<LinkedList<Integer>> res = new ArrayList<>();\n
ruihuang
NORMAL
2016-04-09T16:38:32+00:00
2018-09-20T03:30:26.443192+00:00
2,288
false
//recursive method\n public List<List<Integer>> zigzagLevelOrder(TreeNode root) {\n List<LinkedList<Integer>> res = new ArrayList<>();\n \n helper(res,root,0);\n \n List<List<Integer>> finalRes = new ArrayList<>();\n finalRes.addAll(res);\n ...
9
0
[]
0
binary-tree-zigzag-level-order-traversal
[C++] Clean Code
c-clean-code-by-alexander-pgv3
\n/**\n * -> 1->\n * <- 2 < 3 <-\n * -> 4 > 5 > 6 > 7 ->\n * <- 8 9 10 11 12 13 14 15 <-\n *\n * level % 2 = 0 - forward popping. push to
alexander
NORMAL
2017-09-30T13:52:35.245000+00:00
2017-09-30T13:52:35.245000+00:00
1,065
false
```\n/**\n * -> 1->\n * <- 2 < 3 <-\n * -> 4 > 5 > 6 > 7 ->\n * <- 8 9 10 11 12 13 14 15 <-\n *\n * level % 2 = 0 - forward popping. push to back: left + right\n * level % 2 = 1 - backward popping. push to front: right + left \n */\n```\n```\nclass Solution {\npublic:\n vector<vector<int>> zi...
9
0
[]
0
binary-tree-zigzag-level-order-traversal
"Optimal Zigzag Level Order Traversal"
optimal-zigzag-level-order-traversal-by-n6jyt
Intuition\nThe problem asks us to perform a zigzag (or spiral) level order traversal of a binary tree. This means traversing the tree level by level but alterna
santrupt_29
NORMAL
2024-10-07T17:45:07.968661+00:00
2024-10-07T17:45:07.968691+00:00
542
false
# Intuition\nThe problem asks us to perform a zigzag (or spiral) level order traversal of a binary tree. This means traversing the tree level by level but alternating the direction at each level:\n\nAt the first level, we traverse from left to right.\nAt the second level, we traverse from right to left.\nWe continue al...
8
0
['C++']
0
binary-tree-zigzag-level-order-traversal
EASIEST JAVA SOLUTION😎✌️ || STEP BY STEP EXPLAINED😁
easiest-java-solution-step-by-step-expla-ia9e
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
abhiyadav05
NORMAL
2023-04-01T14:34:16.588710+00:00
2023-04-01T14:34:16.588764+00:00
473
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->O(N)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$...
8
0
['Tree', 'Breadth-First Search', 'Queue', 'Binary Tree', 'Java']
1
binary-tree-zigzag-level-order-traversal
[Python3] Clean Solution using Queue Level Order Traversal
python3-clean-solution-using-queue-level-k507
\nclass Solution:\n def zigzagLevelOrder(self, root):\n \n res = []\n if not root: return res\n zigzag = True\n \n
SamirPaulb
NORMAL
2022-06-01T15:19:34.492329+00:00
2022-06-01T15:19:34.492370+00:00
1,012
false
```\nclass Solution:\n def zigzagLevelOrder(self, root):\n \n res = []\n if not root: return res\n zigzag = True\n \n q = collections.deque()\n q.append(root)\n \n while q:\n n = len(q)\n nodesOfThisLevel = []\n \n ...
8
0
['Queue', 'Python', 'Python3']
0
binary-tree-zigzag-level-order-traversal
Easy to understand (Python Code)
easy-to-understand-python-code-by-vaibha-ugnb
\n def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:\n if root is None:\n return\n myqueue=queue.Queue()\n
vaibhavsharma30
NORMAL
2022-02-17T13:21:38.001378+00:00
2022-02-17T18:01:52.374611+00:00
291
false
```\n def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:\n if root is None:\n return\n myqueue=queue.Queue()\n myqueue.put(root)\n myqueue.put(None)\n final=[]\n small=[]\n left2right=True\n while myqueue.empty()==False:\n ...
8
0
['Queue', 'Binary Tree', 'Python']
0
binary-tree-zigzag-level-order-traversal
Swift BFS
swift-bfs-by-diegostamigni-ehmt
The idea here is to simply proceed our level order traverse left to right but append items to end or to begin of sub arrays following current swapping direction
diegostamigni
NORMAL
2019-05-01T16:21:19.475523+00:00
2019-05-01T16:29:23.405032+00:00
574
false
The idea here is to simply proceed our level order traverse left to right but append items to end or to begin of sub arrays following current swapping direction.\n```\nclass Solution {\n private enum Direction {\n case right\n case left\n }\n\n func zigzagLevelOrder(_ root: TreeNode?) -> [[Int]] ...
8
0
['Breadth-First Search', 'Swift']
0
binary-tree-zigzag-level-order-traversal
Clean C++ Solution with Queue, Easy to Understand and beats 100% Users
clean-c-solution-with-queue-easy-to-unde-9fh9
Intuition\nThe idea is to traverse the tree level by level, using a queue to manage nodes at each level. For every level, nodes\' values are collected, and for
_sxrthakk
NORMAL
2024-07-09T14:32:58.834312+00:00
2024-07-09T14:32:58.834373+00:00
374
false
# Intuition\nThe idea is to traverse the tree level by level, using a queue to manage nodes at each level. For every level, nodes\' values are collected, and for odd levels, the collected values are reversed to achieve the zigzag pattern.\n\n# Approach\n1. **Initial Checks and Setup :**\n- If the root is null, return a...
7
0
['Array', 'Tree', 'Breadth-First Search', 'Queue', 'Binary Tree', 'C++']
1
binary-tree-zigzag-level-order-traversal
PuTtA EaSY Solution C++ ✅ | Beats 100% 🔥Runtime 0ms 🔥| Funday
putta-easy-solution-c-beats-100-runtime-ire71
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nUsing Queue , placing t
Saisreeramputta
NORMAL
2023-02-19T11:08:02.928284+00:00
2023-02-19T11:08:29.577964+00:00
1,026
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUsing Queue , placing the values at right indexes .\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode ...
7
0
['Tree', 'Breadth-First Search', 'Binary Tree', 'C++']
2
binary-tree-zigzag-level-order-traversal
🐍Python || ✅C++ || 💥Simple Solution Using BFS✔ || 🚀Iterative Manner using queue
python-c-simple-solution-using-bfs-itera-8jm7
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
santhosh1608
NORMAL
2023-02-19T04:07:32.043450+00:00
2023-02-19T04:07:32.043498+00:00
1,234
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N)\n<!-- Add your space complexity here, e.g. $$O...
7
1
['Tree', 'Breadth-First Search', 'Binary Tree', 'Python']
0
binary-tree-zigzag-level-order-traversal
C++✅✅ | Level Order Traversal + BFS | Faster🧭 than 100%🔥 | Clean & Concise Code |
c-level-order-traversal-bfs-faster-than-tdikn
\n\n# Code\n# Please Do Upvote!!!!\n##### Connect with me on Linkedin -> https://www.linkedin.com/in/md-kamran-55b98521a/\n\n\n\nclass Solution {\npublic:\n\n
mr_kamran
NORMAL
2022-12-20T10:08:39.714549+00:00
2023-02-19T02:29:28.324814+00:00
1,087
false
\n\n# Code\n# Please Do Upvote!!!!\n##### Connect with me on Linkedin -> https://www.linkedin.com/in/md-kamran-55b98521a/\n```\n\n\nclass Solution {\npublic:\n\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n \n vector<vector<int>>res;\n\n if(root == NULL) return res;\n\n queue...
7
0
['C++']
1
binary-tree-zigzag-level-order-traversal
Intuitive Solution using Deque with clean code and Visual Representation of the Deque
intuitive-solution-using-deque-with-clea-1m32
Approach:\n\n When we have to print in right direction: We take the nodes from the front of the deque and push the children in the back of the deque\n When we h
achitj
NORMAL
2021-07-23T12:11:13.328638+00:00
2021-07-23T12:11:13.328768+00:00
153
false
### Approach:\n\n* **When we have to print in right direction**: We take the nodes from the front of the deque and push the children in the back of the deque\n* **When we have to print in left direction**: We take the nodes from the back of the deque and push the children in the front of the deque in reverse order sinc...
7
0
[]
0
binary-tree-zigzag-level-order-traversal
javascript clean solution
javascript-clean-solution-by-jiakang-g4wv
\nvar zigzagLevelOrder = function(root) {\n let res = [];\n helper(root, 0, res);\n return res;\n};\n\nvar helper = function(node, level, res){\n if
jiakang
NORMAL
2018-09-06T22:31:01.587655+00:00
2018-10-23T18:53:10.812219+00:00
823
false
```\nvar zigzagLevelOrder = function(root) {\n let res = [];\n helper(root, 0, res);\n return res;\n};\n\nvar helper = function(node, level, res){\n if(!node) return;\n if(!res[level]) res[level] = [];\n level % 2 ? res[level].unshift(node.val) : res[level].push(node.val);\n helper(node.left, level...
7
2
[]
2
binary-tree-zigzag-level-order-traversal
Consice recursive C++ solution
consice-recursive-c-solution-by-elogeel-ig5g
The idea is to solve the problem normally if it was about traversing every level separately then reverse odd rows.\n\n class Solution {\n public:\n
elogeel
NORMAL
2015-07-14T01:38:10+00:00
2015-07-14T01:38:10+00:00
967
false
The idea is to solve the problem normally if it was about traversing every level separately then reverse odd rows.\n\n class Solution {\n public:\n void build(TreeNode* n, vector<vector<int>>& R, int d) {\n if (!n) return;\n if (d == R.size()) R.push_back(vector<int>());\n ...
7
0
[]
0
binary-tree-zigzag-level-order-traversal
Accepted C++ recursive solution with no queues
accepted-c-recursive-solution-with-no-qu-s7d7
Simple algorithm: \n\n 1. do depth first recursive tree search\n 2. populate all vectors for each tree level from left to right\n 3. reverse even levels to conf
paul7
NORMAL
2014-10-29T06:56:28+00:00
2014-10-29T06:56:28+00:00
4,159
false
Simple algorithm: \n\n 1. do depth first recursive tree search\n 2. populate all vectors for each tree level from left to right\n 3. reverse even levels to conform with zigzar requirement\n\n.\n\n class Solution {\n vector<vector<int> > result;\n public:\n vector<vector<int> > zigzagLevelOrder(TreeNode *roo...
7
0
[]
3
binary-tree-zigzag-level-order-traversal
4ms Concise DFS C++ Implementation
4ms-concise-dfs-c-implementation-by-alle-tz0l
1 Calculate depth in each recursion.\n2 Switch directions for adding current value based on (depth % 2).\n\n vector> zigzagLevelOrder(TreeNode root) {\n
allenwow
NORMAL
2016-01-11T02:13:43+00:00
2016-01-11T02:13:43+00:00
1,608
false
1 Calculate depth in each recursion.\n2 Switch directions for adding current value based on (depth % 2).\n\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n vector<vector<int>> rst;\n traverse(root, 0, rst);\n return rst;\n }\n \n void traverse(TreeNode* r...
7
0
['C++']
4
binary-tree-zigzag-level-order-traversal
Simple Java Solution. Same as level order traversal with a flag. No recursion
simple-java-solution-same-as-level-order-nzpy
\npublic List<List<Integer>> zigzagLevelOrder(TreeNode root) {\n List<List<Integer>> result = new ArrayList<>();\n if(root==null) return result;\n
prateek470
NORMAL
2016-10-22T09:05:47.258000+00:00
2016-10-22T09:05:47.258000+00:00
924
false
```\npublic List<List<Integer>> zigzagLevelOrder(TreeNode root) {\n List<List<Integer>> result = new ArrayList<>();\n if(root==null) return result;\n \n Queue<TreeNode> q = new LinkedList<>();\n q.add(root);\n Boolean reverse = false;\n while(!q.isEmpty()){\n ...
7
0
[]
3
binary-tree-zigzag-level-order-traversal
Big Brain Approach || JAVA || 200 IQ
big-brain-approach-java-200-iq-by-armaan-wdrz
Intuition\nI just took the normal level order traversal, stored each level in a different list, inside the main list and then reversed all the alternate lists.\
ArmaanSharma_19
NORMAL
2024-10-22T05:32:14.886876+00:00
2024-10-22T05:32:59.728535+00:00
947
false
# Intuition\nI just took the normal level order traversal, stored each level in a different list, inside the main list and then reversed all the alternate lists.\n\n# Approach\n1. Use a queue to perform a level order traversal of the tree. This allows us to process each level of the tree one at a time.\n2. For each lev...
6
0
['Java']
1
binary-tree-zigzag-level-order-traversal
✅💯🔥Simple Code🚀📌|| 🔥✔️Easy to understand🎯 || 🎓🧠Beats 100%🔥|| Beginner friendly💀💯
simple-code-easy-to-understand-beats-100-ftrm
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
atishayj4in
NORMAL
2024-08-01T20:17:00.940908+00:00
2024-08-01T20:17:00.940925+00:00
917
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
6
1
['Tree', 'Breadth-First Search', 'C', 'Binary Tree', 'Python', 'C++', 'Java']
0
binary-tree-zigzag-level-order-traversal
Easy and Optimized code Best Explanation🔥🔥| TC-O(N).
easy-and-optimized-code-best-explanation-5wow
PLZ UPVOTE\n\n# Intuition\nThe code uses a breadth-first search (BFS) approach to traverse the binary tree level by level. It maintains a boolean variable flag
rohitkumar9897
NORMAL
2023-12-27T18:34:20.891913+00:00
2023-12-28T05:47:27.814277+00:00
453
false
# **PLZ UPVOTE**\n\n# Intuition\nThe code uses a breadth-first search (BFS) approach to traverse the binary tree level by level. It maintains a boolean variable flag to determine whether to add elements from left to right or right to left in each level. If flag is true, elements are added from left to right, and if fla...
6
0
['Breadth-First Search', 'Queue', 'Python', 'C++', 'Java']
1
binary-tree-zigzag-level-order-traversal
C++ (Easy Approach) | Binary Tree Zigzag Level Order Traversal | LeetCode Solution
c-easy-approach-binary-tree-zigzag-level-gqqf
Intuition\nFor printing zigzag traversal we have to follow level order printing techniques, hence we can use bfs traversal technique for the level order printin
Darshil_Thakkar
NORMAL
2023-12-01T17:35:12.201115+00:00
2023-12-01T17:58:45.658148+00:00
240
false
# Intuition\nFor printing zigzag traversal we have to follow level order printing techniques, hence we can use bfs traversal technique for the level order printing which we can implement using queue data structure.\n# Approach\nWe\'ll take queue data structure which stores the TreeNode pointer and at the very first we ...
6
0
['Tree', 'Breadth-First Search', 'Queue', 'C++']
2
binary-tree-zigzag-level-order-traversal
Intutive C++ solution (PLEASE UPVOTE)
intutive-c-solution-please-upvote-by-bai-1o4y
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
baibhavsingh07
NORMAL
2023-05-12T15:36:48.566399+00:00
2023-05-12T15:36:48.566431+00:00
459
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$O...
6
0
['Tree', 'Breadth-First Search', 'Binary Tree', 'C++']
0
binary-tree-zigzag-level-order-traversal
[C++] 3 Methods ||2queue->1queue||
c-3-methods-2queue-1queue-by-gauravsharm-bhjq
\n\n# Method-1 -> By 2 Stacks\n\n\nclass Solution {\npublic:\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n if(root==NULL)return {};\n
gauravsharma459
NORMAL
2023-02-19T07:08:00.562573+00:00
2023-02-19T07:08:32.132148+00:00
640
false
\n\n# Method-1 -> By 2 Stacks\n```\n\nclass Solution {\npublic:\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n if(root==NULL)return {};\n stack<TreeNode*>st1,st2;\n st1.push(root);\n vector<vector<int>>res;\n while(!st1.empty()){\n vector<int>t;\n ...
6
0
['Stack', 'Queue', 'C++']
0
binary-tree-zigzag-level-order-traversal
✅Python3 30ms 🔥🔥 easiest solution
python3-30ms-easiest-solution-by-shivam_-666m
Intuition\n Describe your first thoughts on how to solve this problem. \nUsing BFS level order traversal we can solve this.\n\n# Approach\n Describe your approa
shivam_1110
NORMAL
2023-02-19T05:04:24.151847+00:00
2023-02-19T05:16:56.914515+00:00
1,144
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUsing BFS level order traversal we can solve this.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- very simple approach.\n- traverse left most node then right nodes.\n- while traversing keep track of level.\n- if...
6
0
['Tree', 'Depth-First Search', 'Breadth-First Search', 'Binary Tree', 'Python3']
1
binary-tree-zigzag-level-order-traversal
C++ | BFS | Reverse | Easy | ~93% Space ~41% Time
c-bfs-reverse-easy-93-space-41-time-by-a-xs9q
\nclass Solution {\npublic:\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n vector<vector<int>> ans;\n if(!root) return {};\n queue
amanswarnakar
NORMAL
2023-02-19T03:40:14.229365+00:00
2023-02-19T03:46:49.530229+00:00
738
false
```\nclass Solution {\npublic:\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n vector<vector<int>> ans;\n if(!root) return {};\n queue<TreeNode *> q;\n q.emplace(root);\n while(!q.empty()){\n int sz = q.size();\n vector<int> temp;\n for(int i = 0; i < sz; i++)...
6
2
['Breadth-First Search', 'Queue', 'C', 'C++']
1
binary-tree-zigzag-level-order-traversal
✅C++ || ✅O(N) - 100% fast using LEVEL ORDER TRAVERSAL || Explained using comments✅||
c-on-100-fast-using-level-order-traversa-04hh
\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nWe will do Level Order Traversal for each level.\nTake a integer and for each incr
shakya_kr_02
NORMAL
2023-02-17T14:15:26.007928+00:00
2023-02-17T14:15:26.007966+00:00
112
false
\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nWe will do Level Order Traversal for each level.\nTake a integer and for each increasing level we will also increment this integer and also push elements of level in a 1D vector and when this integer is divisible by 2, we will reverse ...
6
0
['Queue', 'Binary Tree', 'C++']
0
binary-tree-zigzag-level-order-traversal
JavaScript || Easy and well explained code
javascript-easy-and-well-explained-code-s83iz
Explanation:\n\n1. If the root is null, return an empty array.\n1. Initialize a result array result, a queue q to store nodes and a level counter level.\n1. Whi
AdnanAd
NORMAL
2023-01-30T20:05:49.210174+00:00
2023-01-30T20:05:49.210222+00:00
732
false
# Explanation:\n\n1. If the root is null, return an empty array.\n1. Initialize a result array result, a queue q to store nodes and a level counter level.\n1. While the queue q is not empty, do the following:\n1. Get the size of the queue, and initialize an array currLevel to store the current level\'s values.\n1. Loop...
6
0
['Tree', 'Breadth-First Search', 'Binary Tree', 'JavaScript']
0
binary-tree-zigzag-level-order-traversal
✅Easy Java solution||Straight Forward||Beginner Friendly🔥
easy-java-solutionstraight-forwardbeginn-aioh
If you really found my solution helpful please upvote it, as it motivates me to post such kind of codes and help the coding community, if you have some queries
deepVashisth
NORMAL
2022-09-14T19:44:25.604150+00:00
2022-09-14T19:44:25.604183+00:00
357
false
**If you really found my solution helpful please upvote it, as it motivates me to post such kind of codes and help the coding community, if you have some queries or some improvements please feel free to comment and share your views.**\n```\nclass Solution {\n public List<List<Integer>> zigzagLevelOrder(TreeNode root...
6
0
['Java']
0
binary-tree-zigzag-level-order-traversal
Java DFS based solution || Beats 100%
java-dfs-based-solution-beats-100-by-san-ddbt
Using dfs to generate level order traversal and then reversing every odd level.\n\npublic List<List<Integer>> zigzagLevelOrder(TreeNode root) {\n List<Li
sanchayharjai37
NORMAL
2021-11-16T12:29:01.164350+00:00
2021-11-16T12:29:01.164396+00:00
132
false
Using dfs to generate level order traversal and then reversing every odd level.\n```\npublic List<List<Integer>> zigzagLevelOrder(TreeNode root) {\n List<List<Integer>> ans = new ArrayList<>();\n dfs(root,0,ans);\n for(int i = 0; i < ans.size(); i++) if((i&1) == 1) Collections.reverse(ans.get(i));\...
6
0
[]
0
binary-tree-zigzag-level-order-traversal
My C++ 0ms (faster than 100%) solution with simple explanation.
my-c-0ms-faster-than-100-solution-with-s-18ka
One thing to notice is that whenever we encounter a problem having something related to level order than BFS must come in our mind. Hence by using queue we can
QsR11
NORMAL
2021-09-24T09:48:00.892696+00:00
2021-09-24T13:27:20.344367+00:00
244
false
One thing to notice is that whenever we encounter a problem having something related to level order than BFS must come in our mind. Hence by using queue we can push all children level by level and do the required thing as asked in problem statement.\n\nHere we can see that on zero level we want ans vector to have eleme...
6
0
['Breadth-First Search', 'Queue']
0
binary-tree-zigzag-level-order-traversal
BFS C++ 0ms(simple solution)
bfs-c-0mssimple-solution-by-vp-yxyf
This is not the best solution to this problem. This is just an approach in which I am using a bool variable for the zigzag traversal along with an eliminator fo
Vp-
NORMAL
2021-06-29T18:52:54.014909+00:00
2021-06-29T18:52:54.014993+00:00
182
false
This is not the best solution to this problem. This is just an approach in which I am using a bool variable for the zigzag traversal along with an eliminator for clearing out temp before reaching next level.\n \n vector<vector<int>> zigzagLevelOrder(TreeNode* root)\n {\n \n\t\tif(root == NULL)\n ...
6
0
[]
0
binary-tree-zigzag-level-order-traversal
Java solution using BFS with Comments: Time and Space: O(n)
java-solution-using-bfs-with-comments-ti-od0c
This is a typical level order traversal with the only difference being that we need to keep track of the direction in which our node values in each level will b
annedevies
NORMAL
2021-05-27T03:18:18.589884+00:00
2021-05-27T03:18:18.589921+00:00
590
false
This is a typical level order traversal with the only difference being that we need to keep track of the direction in which our node values in each level will be returned. If we want a left to right result, we insert values at the end of the list. Otherwise, we insert values at the beginning of the list to achieve a ri...
6
0
['Breadth-First Search', 'Queue', 'Java']
1
binary-tree-zigzag-level-order-traversal
Python with Queue
python-with-queue-by-sunjppp-87vo
\nclass Solution:\n def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:\n if not root:\n return []\n \n res = []\n
sunjppp
NORMAL
2019-06-25T22:26:06.183534+00:00
2019-06-26T20:35:00.416801+00:00
851
false
```\nclass Solution:\n def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:\n if not root:\n return []\n \n res = []\n queue = []\n \n queue.append((root,1))\n while queue:\n n = len(queue)\n nodeList = []\n for _ ...
6
0
['Python']
2
binary-tree-zigzag-level-order-traversal
Java Four Solutions With Explanations
java-four-solutions-with-explanations-by-b3ti
Four solutions can be used to solve this problem that are:\n\n- Deque\n- Queue with Linked List\n- Recursive\n- Double Stack\n\nPlease check below for details.\
steve027
NORMAL
2019-03-12T05:09:50.300198+00:00
2019-03-12T05:09:50.300260+00:00
258
false
Four solutions can be used to solve this problem that are:\n\n- Deque\n- Queue with Linked List\n- Recursive\n- Double Stack\n\nPlease check below for details.\n\n### Solution 1: Deque\n**Idea:**\n- If direction is left->right, poll nodes from end, offer left and right childs to the front\n- If direction is right->left...
6
0
[]
0
binary-tree-zigzag-level-order-traversal
Optimal Approach | 0ms 100% beats | BFS Approach 💯
optimal-approach-0ms-100-beats-bfs-appro-8byf
Complexity Time complexity:O(n) Space complexity:O(n) Code
azhan-born-to-win
NORMAL
2025-02-22T03:42:07.706824+00:00
2025-02-22T03:42:07.706824+00:00
1,313
false
# Complexity - Time complexity: $$O(n)$$ - Space complexity: $$O(n)$$ # Code ```python3 [] # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def zigzagLev...
5
0
['Tree', 'Breadth-First Search', 'Binary Tree', 'Python3']
0
binary-tree-zigzag-level-order-traversal
Efficient BFS solution!
efficient-bfs-solution-by-soberrrj-tbsn
IntuitionThe zigzag level order traversal is a variation of the level order traversal (BFS). The key difference is alternating the order of nodes at each level:
SoberrrJ_
NORMAL
2024-12-13T04:45:30.052760+00:00
2024-12-13T04:45:30.052760+00:00
977
false
# Intuition\nThe zigzag level order traversal is a variation of the level order traversal (BFS). The key difference is alternating the order of nodes at each level: left-to-right for one level and right-to-left for the next. We can achieve this by traversing each level normally and reversing the values on alternating l...
5
0
['Python3']
0
binary-tree-zigzag-level-order-traversal
Accepted C++ solution using deque
accepted-c-solution-using-deque-by-harsh-c3vy
\n\n# Approach\n Describe your approach to solving the problem. \nAfter seeing the test cases, we can see a pattern that when we are at odd level (assuming leve
harshsri1602
NORMAL
2024-03-27T00:39:11.913640+00:00
2024-03-27T00:39:11.913669+00:00
28
false
\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nAfter seeing the test cases, we can see a pattern that when we are at odd level (assuming level of root to be 0), we are taking the Node from the front of the deque and pushing the it\'s right and left node at back of the deque. After seeing this ...
5
0
['C++']
0
binary-tree-zigzag-level-order-traversal
Zigzag Level Order Traversal of Binary Tree c++
zigzag-level-order-traversal-of-binary-t-77su
Intuition\nThe intuition behind this code is to traverse a binary tree in a zigzag manner, where each level of the tree is traversed alternately from left to ri
Stella_Winx
NORMAL
2024-03-11T12:10:16.087760+00:00
2024-03-11T12:10:16.087785+00:00
375
false
# Intuition\nThe intuition behind this code is to traverse a binary tree in a zigzag manner, where each level of the tree is traversed alternately from left to right and right to left.\n This zigzag traversal allows us to create a 2D vector where each inner vector represents a level of the tree, and the values with...
5
0
['C++']
1
binary-tree-zigzag-level-order-traversal
Recursion without using reverse(), Beats 100%
recursion-without-using-reverse-beats-10-mi4k
Intuition\n Describe your first thoughts on how to solve this problem. The given problem is like the level order traversal with a slight change.\nHere, every ev
Kartik79
NORMAL
2024-01-11T18:39:04.889111+00:00
2024-01-11T18:39:04.889137+00:00
291
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->The given problem is like the level order traversal with a slight change.\nHere, every even level is reversed.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nInstead of using reverse() function we initialse a variab...
5
0
['Recursion', 'C++']
0
binary-tree-zigzag-level-order-traversal
✅Easy & Clear Solution Python 3✅
easy-clear-solution-python-3-by-moazmar-qfp1
\n\n# Code\n\nclass Solution:\n def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:\n res=[]\n def add(i,tree):\n
moazmar
NORMAL
2023-04-04T06:06:43.496511+00:00
2023-04-04T06:06:43.496550+00:00
1,957
false
\n\n# Code\n```\nclass Solution:\n def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:\n res=[]\n def add(i,tree):\n if len(res)<=i:\n res.append([tree.val])\n else:\n res[i].append(tree.val)\n i+=1\n if tree...
5
0
['Recursion', 'Python3']
0
binary-tree-zigzag-level-order-traversal
🚀️🔥️ Java/Kotlin 2 simple BFS approaches, both O(n) time & space
javakotlin-2-simple-bfs-approaches-both-pu1gr
Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Approach 1\nJust do the classic BFS and on every odd distance reverse the iterated level.
Klemo1997
NORMAL
2023-02-19T18:12:13.854790+00:00
2023-02-19T18:12:13.854838+00:00
233
false
# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(n)$$\n\n# Approach 1\nJust do the classic BFS and on every odd distance reverse the iterated level. A bit slower, but quite simple.\n\n```kotlin []\nclass Solution {\n fun zigzagLevelOrder(root: TreeNode?): List<List<Int>> {\n if (root == ...
5
0
['Linked List', 'Breadth-First Search', 'Java', 'Kotlin']
1
binary-tree-zigzag-level-order-traversal
BFS Level Order Traversal C++
bfs-level-order-traversal-c-by-heisenber-exy9
Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(N)\n\n# Code\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;
Heisenberg2003
NORMAL
2023-02-19T09:41:18.342491+00:00
2023-02-19T09:41:18.342536+00:00
8,617
false
# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(N)\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(null...
5
0
['C++']
0
binary-tree-zigzag-level-order-traversal
📌📌Python3 || ⚡26 ms, faster than 97.08% of Python3
python3-26-ms-faster-than-9708-of-python-s90u
\n\nclass Solution:\n def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:\n if not root:\n return []\n\n queue
harshithdshetty
NORMAL
2023-02-19T04:33:58.330007+00:00
2023-02-19T04:33:58.330036+00:00
977
false
![image](https://assets.leetcode.com/users/images/3ac0be42-4841-44a1-87f0-a32a71f4b453_1676781049.3290617.png)\n```\nclass Solution:\n def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:\n if not root:\n return []\n\n queue = deque([root])\n result, direction = []...
5
0
['Breadth-First Search', 'Queue', 'Python', 'Python3']
0
binary-tree-zigzag-level-order-traversal
[Python] - BFS - Clean & Simple
python-bfs-clean-simple-by-yash_visavadi-fhuo
\n# Code\nPython [1]\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val
yash_visavadia
NORMAL
2023-02-19T04:13:28.307886+00:00
2023-02-19T04:13:28.307941+00:00
724
false
\n# Code\n``` Python [1]\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:\n ...
5
0
['Python3']
0
binary-tree-zigzag-level-order-traversal
Binary Tree Zigzag Level Order Traversal with step by step explanation
binary-tree-zigzag-level-order-traversal-gfgo
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nTo traverse the binary tree in a zigzag manner, we can use a breadth-firs
Marlen09
NORMAL
2023-02-16T05:24:26.883577+00:00
2023-02-16T05:24:26.883630+00:00
1,358
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nTo traverse the binary tree in a zigzag manner, we can use a breadth-first search (BFS) approach. We can use a queue to store the nodes of the binary tree, and we can use a flag to indicate the direction of the zigzag traver...
5
0
['Tree', 'Breadth-First Search', 'Binary Tree', 'Python', 'Python3']
1
binary-tree-zigzag-level-order-traversal
Using two stacks, optimised solution, C++, Commented and Easy to Understand
using-two-stacks-optimised-solution-c-co-relm
Intuition\n Describe your first thoughts on how to solve this problem. \nI\'m using two stacks for this problem as it would take lesser time as each node will g
iaadityaa
NORMAL
2023-01-28T13:00:11.288226+00:00
2023-02-01T12:47:08.916761+00:00
538
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI\'m using two stacks for this problem as it would take lesser time as each node will go inside the stack once and come out of the stack once. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe will create two sta...
5
0
['C++']
1
binary-tree-zigzag-level-order-traversal
fastest C++ solution
fastest-c-solution-by-tanayhacks08-zwoh
class Solution {\npublic:\n vector> zigzagLevelOrder(TreeNode* root) {\n \n vector> v;\n \n if(root==NULL)\n return v;\n
tanayhacks08
NORMAL
2021-11-29T18:56:26.491990+00:00
2021-11-29T18:56:26.492036+00:00
144
false
class Solution {\npublic:\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n \n vector<vector<int>> v;\n \n if(root==NULL)\n return v;\n queue <TreeNode *>q;\n q.push(root);int flag=0;\n while(!q.empty())\n { int n=q.size();\n ve...
5
0
[]
3
binary-tree-zigzag-level-order-traversal
C++ Simple Solution || Space and time less than 82.08% || BFS
c-simple-solution-space-and-time-less-th-y1jq
In this question , we again use BFS for level order traversal , and as we can predict from output that levels on odd layers are being reversed . So thats the te
kush980
NORMAL
2021-05-16T08:07:16.782024+00:00
2021-05-16T08:07:16.782055+00:00
479
false
In this question , we again use **BFS** for level order traversal , and as we can predict from output that levels on odd layers are being reversed . So thats the technique we have used here.\n\n```\nclass Solution {\npublic:\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n queue<TreeNode *> q; //...
5
0
['Breadth-First Search', 'C', 'C++']
0
binary-tree-zigzag-level-order-traversal
Concise Java Solution with BFS
concise-java-solution-with-bfs-by-charle-pqyz
\nclass Solution {\n public List<List<Integer>> zigzagLevelOrder(TreeNode root) {\n \n List<List<Integer>> result = new ArrayList<>();\n
CharlesFly
NORMAL
2020-09-01T21:11:23.944655+00:00
2020-09-01T21:11:23.944704+00:00
152
false
```\nclass Solution {\n public List<List<Integer>> zigzagLevelOrder(TreeNode root) {\n \n List<List<Integer>> result = new ArrayList<>();\n \n if (root == null)\n return result;\n \n boolean reversed = false;\n Queue<TreeNode> queue = new LinkedList<>();\n ...
5
0
[]
1
number-of-good-pairs
JAVA | STORY BASED | 0ms | SINGLE PASS | EASY TO UNDERSTAND | SIMPLE | HASHMAP
java-story-based-0ms-single-pass-easy-to-cysx
\n# HANDSHAKES IN GATHERING\n\n# YOU ALL CAN BUY ME A *BEER \uD83C\uDF7A AT \uD83D\uDC47*\n\nhttps://www.buymeacoffee.com/deepakgupta\n\nImagine this problem li
deepak08
NORMAL
2021-09-11T16:59:02.946611+00:00
2023-05-13T11:28:43.596847+00:00
28,009
false
\n# HANDSHAKES IN GATHERING\n\n# **YOU ALL CAN BUY ME A ******BEER \uD83C\uDF7A****** AT \uD83D\uDC47**\n\n**https://www.buymeacoffee.com/deepakgupta**\n\nImagine this problem like, There is a gathering organized by some guy, the guest list is [1,2,3,1,1,3].\nThe problem with the guest is they only handshake with like ...
426
3
['Java']
34
number-of-good-pairs
[Java/C++/Python] Count
javacpython-count-by-lee215-b15l
Explanation\ncount the occurrence of the same elements.\nFor each new element a,\nthere will be more count[a] pairs,\nwith A[i] == A[j] and i < j\n\n\n## Comple
lee215
NORMAL
2020-07-12T04:01:37.640818+00:00
2020-07-13T15:35:24.118977+00:00
59,356
false
## **Explanation**\n`count` the occurrence of the same elements.\nFor each new element `a`,\nthere will be more `count[a]` pairs,\nwith `A[i] == A[j]` and `i < j`\n<br>\n\n## **Complexity**\nTime `O(N)`\nSpace `O(N)`\n<br>\n\n**Java:**\n```java\n public int numIdenticalPairs(int[] A) {\n int res = 0, count[] ...
410
29
[]
86
number-of-good-pairs
✅one line|BEATS 100% Runtime||Explanation✅
one-linebeats-100-runtimeexplanation-by-hf919
Intution\nwe have to count the occurrence of the same elements\nwithA[i] == A[j] and i < j\n\n# Approach\n- We will intiliaze ans with 0 and an emptyunordered m
vishnoi29
NORMAL
2023-10-03T00:04:12.645446+00:00
2023-10-03T06:31:48.946664+00:00
45,850
false
# Intution\nwe have to count the occurrence of the same elements\nwith` A[i] == A[j]` and `i < j`\n\n# Approach\n- We will `intiliaze ans with 0` and an empty` unordered map` to store the occurrence of the element \n- For each element in the given array:\n- Here there will be 2 cases\n 1. if element/number is `present...
342
4
['Hash Table', 'PHP', 'C++', 'Java', 'Go', 'Python3', 'Rust', 'Ruby', 'JavaScript', 'C#']
23
number-of-good-pairs
[Python] Simple O(n) Solution
python-simple-on-solution-by-eellaup-r9bd
Solution Idea\nThe idea is storing the number of repeated elements in a dictionary/hash table and using mathmatics to calculate the number of combinations.\n\nF
eellaup
NORMAL
2020-07-14T23:29:16.575613+00:00
2020-07-16T18:40:48.861074+00:00
30,200
false
**Solution Idea**\nThe idea is storing the number of repeated elements in a dictionary/hash table and using mathmatics to calculate the number of combinations.\n\n**Fundamental Math Concept (Combinations)**\nThe "# of pairs" can be calculated by summing each value from range 0 to n-1, where n is the "# of times repeate...
166
2
['Hash Table', 'Math', 'Python', 'Python3']
17
number-of-good-pairs
Java 100% faster | 100% space easy solution
java-100-faster-100-space-easy-solution-ju5lz
Method - 1: We can use two for loops and check is nums[i] = nums[j] and i < j and simply increase count by one every time. I think it will give error of time
hardik_ahir
NORMAL
2020-07-13T03:52:39.324901+00:00
2020-07-13T03:52:52.986792+00:00
15,328
false
Method - 1: We can use two for loops and check is <b> nums[i] = nums[j] </b> and i < j and simply increase count by one every time. I think it will give error of time limit exceeded.\n\nMethod - 2 : First we can count the frequency of each numbers using array. If a number appears n times, then n * (n \u2013 1) / 2 pair...
132
3
['Java']
35
number-of-good-pairs
Python O(n) simple dictionary solution
python-on-simple-dictionary-solution-by-j57vr
\nclass Solution:\n def numIdenticalPairs(self, nums: List[int]) -> int:\n hashMap = {}\n res = 0\n for number in nums: \n
arturo001
NORMAL
2020-07-22T10:39:55.935855+00:00
2020-10-12T22:23:57.502513+00:00
10,357
false
```\nclass Solution:\n def numIdenticalPairs(self, nums: List[int]) -> int:\n hashMap = {}\n res = 0\n for number in nums: \n if number in hashMap:\n res += hashMap[number]\n hashMap[number] += 1\n else:\n hashMap[numb...
110
0
['Python3']
8
number-of-good-pairs
Java HashMap O(n)
java-hashmap-on-by-hobiter-knx7
for each i, finds all j where, j < i && nums[j] == nums[i];\n\n public int numIdenticalPairs(int[] nums) {\n int res = 0;\n Map<Integer, Integ
hobiter
NORMAL
2020-07-12T04:01:59.268703+00:00
2020-08-26T02:58:10.172941+00:00
9,705
false
for each i, finds all j where, j < i && nums[j] == nums[i];\n```\n public int numIdenticalPairs(int[] nums) {\n int res = 0;\n Map<Integer, Integer> map = new HashMap<>();\n for (int n : nums) {\n map.put(n, map.getOrDefault(n, 0) + 1);\n res += map.get(n) - 1; // addtion...
69
9
[]
10
number-of-good-pairs
✅98.44%🔥Easy Solution🔥Array & Math🔥
9844easy-solutionarray-math-by-mrake-snfs
Problem\n#### The problem you described is a classic counting problem that asks you to find the number of "good pairs" in an array of integers. A good pair is d
MrAke
NORMAL
2023-10-03T01:38:43.859563+00:00
2023-10-03T01:38:43.859582+00:00
12,898
false
# Problem\n#### The problem you described is a classic counting problem that asks you to find the number of "good pairs" in an array of integers. A good pair is defined as a pair of indices (i, j) where i < j and the elements at those indices in the array are equal (nums[i] == nums[j]).\n---\n# Solution\n#### The given...
63
2
['Array', 'Hash Table', 'Math', 'C', 'Python', 'C++', 'Java', 'Python3', 'JavaScript', 'C#']
8
number-of-good-pairs
【Video】How we think about a solution - Python, JavaScript, Java, C++
video-how-we-think-about-a-solution-pyth-gw0w
Welcome to my post! This post starts with "How we think about a solution". In other words, that is my thought process to solve the question. This post explains
niits
NORMAL
2025-01-14T16:07:41.716551+00:00
2025-01-14T17:53:48.686841+00:00
2,239
false
Welcome to my post! This post starts with "How we think about a solution". In other words, that is my thought process to solve the question. This post explains how I get to my solution instead of just posting solution codes or out of blue algorithms. I hope it is helpful for someone. # Intuition Using HashMap --- # ...
61
0
['Array', 'Hash Table', 'Math', 'Counting', 'C++', 'Java', 'Python3', 'JavaScript']
2
number-of-good-pairs
💯Faster✅💯 Lesser✅4 Methods🔥HashMap🔥Using Combination🔥Two-Pointers Approach🔥Simple Math
faster-lesser4-methodshashmapusing-combi-yjsf
\uD83D\uDE80 Hi, I\'m Mohammed Raziullah Ansari, and I\'m excited to share 4 ways to solve this question with detailed explanation of each approach:\n\n# Proble
Mohammed_Raziullah_Ansari
NORMAL
2023-10-03T02:58:04.170242+00:00
2023-10-03T02:58:04.170265+00:00
5,648
false
# \uD83D\uDE80 Hi, I\'m [Mohammed Raziullah Ansari](https://leetcode.com/Mohammed_Raziullah_Ansari/), and I\'m excited to share 4 ways to solve this question with detailed explanation of each approach:\n\n# Problem Explaination: \n\nThe "Number of Good Pairs" problem is a common coding problem in which we are given an ...
60
2
['Array', 'Hash Table', 'Math', 'Two Pointers', 'C', 'Counting', 'C++', 'Java', 'TypeScript', 'Python3']
11
number-of-good-pairs
Simplest C++ Explanation O(n2) and O(n)
simplest-c-explanation-on2-and-on-by-sac-7r8s
\nO(n2) [100% less memory usage] [75% less time usage]\n\nint numIdenticalPairs(vector<int>& nums) {\n\tint counter = 0;\n\tfor(int i=0;i<nums.size()-1;++i)\n\t
sachuverma
NORMAL
2020-07-12T04:44:46.521090+00:00
2020-07-12T17:42:11.274727+00:00
6,233
false
\n**O(n2)** [100% less memory usage] [75% less time usage]\n```\nint numIdenticalPairs(vector<int>& nums) {\n\tint counter = 0;\n\tfor(int i=0;i<nums.size()-1;++i)\n\t for(int j=i+1;j<nums.size();++j)\n\t\tif(nums[i]==nums[j]) counter++;\n\treturn counter;\n}\n```\n\n\n**O(n)** [100% less memory usage] [100% less time...
59
17
[]
8
number-of-good-pairs
✅ 95.45% Easy Count
9545-easy-count-by-vanamsen-scwe
Intuition\nWhen confronted with the problem of identifying pairs of identical numbers, it\'s natural to consider tracking the occurrences of each number. By kno
vanAmsen
NORMAL
2023-10-03T00:12:12.429807+00:00
2023-10-03T01:07:44.678309+00:00
9,048
false
# Intuition\nWhen confronted with the problem of identifying pairs of identical numbers, it\'s natural to consider tracking the occurrences of each number. By knowing how many times a number has appeared before, we can infer how many pairs can be made with this number.\n\n## Live Coding & Explain\nhttps://youtu.be/87-u...
55
9
['Hash Table', 'PHP', 'C++', 'Java', 'Go', 'Python3', 'Rust', 'JavaScript', 'C#']
7
number-of-good-pairs
C++/Java O(n)
cjava-on-by-votrubac-xxsf
We can just count each value. Then, n elements with the same value can form n * (n - 1) / 2 pairs.\n\n> Why? The first element forms n - 1 pairs, the second - n
votrubac
NORMAL
2020-07-12T04:04:21.389859+00:00
2020-07-12T19:21:24.974125+00:00
8,742
false
We can just count each value. Then, `n` elements with the same value can form `n * (n - 1) / 2` pairs.\n\n> Why? The first element forms `n - 1` pairs, the second - `n - 2` pairs and so on. So the sum of the [1, n - 1] progression is `n * (n - 1) / 2`.\n\n**C++**\n```cpp\nint numIdenticalPairs(vector<int>& nums) {\n ...
55
3
[]
8
number-of-good-pairs
Clean JavaScript Solution
clean-javascript-solution-by-shimphillip-g8uu
\n// time O(N^2) space O(1)\n var numIdenticalPairs = function(nums) {\n let count = 0\n \n for(let i=0; i<nums.length; i++) {\n for(let j=i+
shimphillip
NORMAL
2020-10-26T23:36:59.924842+00:00
2020-11-13T05:34:17.546126+00:00
5,958
false
```\n// time O(N^2) space O(1)\n var numIdenticalPairs = function(nums) {\n let count = 0\n \n for(let i=0; i<nums.length; i++) {\n for(let j=i+1; j<nums.length; j++) {\n if(nums[i] === nums[j]) {\n count++\n }\n }\n }\n \n return count\n };\...
51
0
['JavaScript']
5
number-of-good-pairs
WEEB EXPLAINS PYTHON(BEATS 97.65%)
weeb-explains-pythonbeats-9765-by-skywal-p9ei
\nfirst try btw and its already 97%\n\nOkay, my code looks weird at first glance but its actually pretty easy, just plug in the formula for quadratic sequence\n
Skywalker5423
NORMAL
2021-05-11T07:56:39.770316+00:00
2021-06-23T03:51:39.542439+00:00
3,577
false
![image](https://assets.leetcode.com/users/images/6a27264b-cfe3-42e3-92e0-bfcee79d7d1e_1620717462.3697262.png)\nfirst try btw and its already 97%\n\nOkay, my code looks weird at first glance but its actually pretty easy, just plug in the formula for quadratic sequence\n\t\n\tclass Solution:\n\t\tdef numIdenticalPairs(s...
47
1
['Python', 'Python3']
11
number-of-good-pairs
[Java 1 PASS] - One Pass Solution + Intuitive Explanation
java-1-pass-one-pass-solution-intuitive-snn12
1512. Number of Good Pairs\n\nclass Solution {\n public int numIdenticalPairs(int[] nums) {\n HashMap<Integer,Integer> map = new HashMap<Integer,Integ
allenjue
NORMAL
2020-11-17T22:34:02.860239+00:00
2020-12-28T01:02:53.451394+00:00
3,497
false
**1512. Number of Good Pairs**\n```\nclass Solution {\n public int numIdenticalPairs(int[] nums) {\n HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();\n int answer = 0;\n for(int i: nums){\n if(map.containsKey(i)){ // if number has occurred before\n int tem...
44
0
['Java']
3
number-of-good-pairs
C++ {Speed,Mem} = {O(n), O(n)} w/o Map + Video
c-speedmem-on-on-wo-map-video-by-crab_10-igia
Since the given nums.length() <= 100, space complexity is O(n).\nhttps://www.youtube.com/watch?v=FlFxSnK2SmY\n\nclass Solution {\npublic:\n int numIdenticalP
crab_10legs
NORMAL
2020-07-15T15:23:39.311214+00:00
2020-08-05T16:48:08.190188+00:00
6,213
false
Since the given nums.length() <= 100, space complexity is O(n).\nhttps://www.youtube.com/watch?v=FlFxSnK2SmY\n```\nclass Solution {\npublic:\n int numIdenticalPairs(vector<int>& nums) {\n \n int mem[101] ={0};\n int sum=0;\n \n for(int i=0; i < nums.size(); i++){\n sum +...
40
3
['C', 'C++']
12
number-of-good-pairs
Python 99.27%, O(n), easy to understand, (its math)
python-9927-on-easy-to-understand-its-ma-qxbj
Because pairs are only created if nums[i] == nums[j] and i < j, we can infer a arithmethic sequence from this condition. \n\nA hint was also given from the exam
berelt
NORMAL
2020-08-31T18:41:40.373831+00:00
2020-08-31T18:41:40.373887+00:00
6,409
false
Because pairs are only created if **nums[i] == nums[j]** and i < j, we can infer a arithmethic sequence from this condition. \n\nA hint was also given from the examples:\n[1,1,1,1] have 6 combination\nThe first 1 have 3 pairs\nThe second 1 have 2 pairs\nThe third 1 have 1 pairs\nHence for 4 of 1\'s, we can have 3 + 2 +...
39
2
['Math', 'Python', 'Python3']
9
number-of-good-pairs
HASH_TABLE|| EXPLAINED line by line || Faster
hash_table-explained-line-by-line-faster-yl60
Before solving the question we will keep two thing in mind.\n1.The use of Vectors\n2.The use of Hashing.\n\nHere, this question can be solved by a direct formul
smritipradhan545
NORMAL
2021-02-24T05:36:25.538934+00:00
2021-02-24T05:36:25.538970+00:00
3,018
false
Before solving the question we will keep two thing in mind.\n1.The use of Vectors\n2.The use of Hashing.\n\nHere, this question can be solved by a direct formula which we will use to find the number of good pairs.\nThe formula is (n*(n-1))/2.\n\n*First we find the count of number occurences of a number. \n*Then store t...
29
0
['Hash Table', 'C', 'C++']
6
number-of-good-pairs
[C++] Single-pass Solution Explained, 100% Time, ~96% Space
c-single-pass-solution-explained-100-tim-t6zi
This is a nice one that can be solved trivially, but it has some more challenge if you plan on tackling it in a more optimised way. My core intuition was that f
ajna
NORMAL
2020-12-09T18:58:05.977751+00:00
2020-12-09T18:58:05.977795+00:00
3,968
false
This is a nice one that can be solved trivially, but it has some more challenge if you plan on tackling it in a more optimised way. My core intuition was that for each number `n` we basically need to apply the Gaussian formula to its frequency `f`, `-1` (since numbers can\'t form couples with themselves in this problem...
29
0
['Array', 'C', 'Counting', 'C++']
1
number-of-good-pairs
【Video】How we think about a solution - Python, JavaScript, Java, C++
video-how-we-think-about-a-solution-pyth-kcf8
Welcome to my post! This post starts with "How we think about a solution". In other words, that is my thought process to solve the question. This post explains
niits
NORMAL
2023-10-03T03:13:29.697982+00:00
2023-10-04T20:52:45.430213+00:00
1,585
false
Welcome to my post! This post starts with "How we think about a solution". In other words, that is my thought process to solve the question. This post explains how I get to my solution instead of just posting solution codes or out of blue algorithms. I hope it is helpful for someone.\n\n# Intuition\nUsing HashMap\n\n--...
28
1
['C++', 'Java', 'Python3', 'JavaScript']
5
number-of-good-pairs
One Pass | O(n) time | Using HashMap
one-pass-on-time-using-hashmap-by-guywit-37cc
This is a basic concept of combinations:\n\nn_C_r = n! / r! * (n-r)!\n\nwhere:\nn_C_r\t= \tnumber of combinations\nn\t= \ttotal number of objects in the set\nr\
guywithimpostersyndrome
NORMAL
2020-12-17T07:06:55.783787+00:00
2022-03-20T18:53:26.820029+00:00
2,361
false
This is a basic concept of **combinations**:\n```\nn_C_r = n! / r! * (n-r)!\n\nwhere:\nn_C_r\t= \tnumber of combinations\nn\t= \ttotal number of objects in the set\nr\t= \tnumber of choosing objects from the set\n```\nHere:\n* The **set** would be with respect to a unique number at a time. (combinations for each distin...
26
2
['Java']
4
number-of-good-pairs
My Favorite Story in Maths -- "The Prince of Mathematicians”
my-favorite-story-in-maths-the-prince-of-jksv
https://nrich.maths.org/2478 & https://nrich.maths.org/2478\n\nCarl Friedrich Gauss (1777-1855) is recognised as being one of the greatest mathematicians of all
mcclay
NORMAL
2020-07-12T17:59:21.547653+00:00
2020-07-12T18:02:04.445624+00:00
1,077
false
https://nrich.maths.org/2478 & https://nrich.maths.org/2478\n\nCarl Friedrich Gauss (1777-1855) is recognised as being one of the greatest mathematicians of all time.\n\nThe most well-known story is a tale from when Gauss was still at primary school. One day Gauss\' teacher asked his class to add together all the numbe...
24
1
[]
4
number-of-good-pairs
✅ 100% | Simple & Mathmatical Approach || Cpp , Java ,Python , Javascript
100-simple-mathmatical-approach-cpp-java-0918
Read article Explaination and codes :https://www.nileshblog.tech/leetcode-1512-number-of-good-pairs/\n\nThe Bruteforce is simplest approach ,we used .The Loopin
user6845R
NORMAL
2023-10-03T07:22:14.456584+00:00
2023-10-03T07:22:14.456607+00:00
1,811
false
# **Read article Explaination and codes :https://www.nileshblog.tech/leetcode-1512-number-of-good-pairs/\n\nThe Bruteforce is simplest approach ,we used .The Looping approach contain two loops and it check every possible pair is good pair or not and we count no of good pairs .\n\n**The Time complexityO(N^2)**\n\nThe Br...
22
0
['Python', 'C++', 'Java', 'JavaScript']
1
number-of-good-pairs
[C++/Java/C#/Python] Easy to understand || Clean Code with Comment
cjavacpython-easy-to-understand-clean-co-5c0f
SolutionThis code implements an optimized solution to solve the "Number of Good Pairs" problem. The problem requires counting the number of good pairs in an arr
nitishhsinghhh
NORMAL
2022-05-23T10:22:49.440325+00:00
2025-01-22T11:36:52.622405+00:00
773
false
# Solution This code implements an optimized solution to solve the "Number of Good Pairs" problem. The problem requires counting the number of good pairs in an array of integers. To solve the problem, the code follows these steps: - ##### Initialization: An array called "count" is created with a size of 101. T...
22
0
['Array', 'Hash Table', 'Math', 'Counting', 'Python', 'C++', 'Java', 'C#']
0
number-of-good-pairs
Good pairs, Java simple Solution.
good-pairs-java-simple-solution-by-nikhi-gdac
\nclass Solution {\n public int numIdenticalPairs(int[] nums) {\n int counter = 0;\n\n for(int i = 0; i < nums.length; i++){\n for(int j = i+
Nikhil_Swapper
NORMAL
2023-03-25T18:45:54.178740+00:00
2023-03-25T18:45:54.178774+00:00
4,119
false
```\nclass Solution {\n public int numIdenticalPairs(int[] nums) {\n int counter = 0;\n\n for(int i = 0; i < nums.length; i++){\n for(int j = i+1; j < nums.length; j++){\n if(nums[i] == nums[j]){\n counter++;\n }\n }\n }\n return cou...
21
0
['Array', 'Java']
3
number-of-good-pairs
Simple C++ Solution
simple-c-solution-by-lokeshsk1-re3t
\nclass Solution {\npublic:\n int numIdenticalPairs(vector<int>& nums) {\n unordered_map<int,int> map;\n int res=0;\n for(int i:nums)\n res+=
lokeshsk1
NORMAL
2020-11-19T08:12:28.730402+00:00
2021-01-03T13:22:40.238550+00:00
1,960
false
```\nclass Solution {\npublic:\n int numIdenticalPairs(vector<int>& nums) {\n unordered_map<int,int> map;\n int res=0;\n for(int i:nums)\n res+=map[i]++;\n return res;\n }\n};\n```\n\n**Explanation:**\n\n[1,2,3,1,1,3]\nconsider the above example\n\nres=0\n\n* iteration 1: map[1]=0 so res=0+0 =0...
21
1
['C', 'C++']
4
number-of-good-pairs
Golang O(n) Solution
golang-on-solution-by-aplotd-xvwe
\nfunc numIdenticalPairs(nums []int) int {\n cnt := make(map[int]int)\n var pairs int\n \n for _, num := range nums {\n\t\tpairs += cnt[num] //i
aplotd
NORMAL
2021-01-18T21:06:33.156600+00:00
2021-01-18T22:22:06.320568+00:00
841
false
```\nfunc numIdenticalPairs(nums []int) int {\n cnt := make(map[int]int)\n var pairs int\n \n for _, num := range nums {\n\t\tpairs += cnt[num] //if num not in hash map "cnt", map returns default value of int (ie 0)\n cnt[num]++\n } \n return pairs\n}\n```
20
0
['Go']
3
number-of-good-pairs
Javascript -- 3 solutions
javascript-3-solutions-by-torilov123-xoyq
Brute force solution:\nO(N^2) time + O(1) space\nlogic:\n- nested loop i (start from beginning) & j (start from end)\n- if nums[i] === nums[j], increment count\
torilov123
NORMAL
2020-07-16T04:48:05.336756+00:00
2020-07-16T04:48:05.336790+00:00
2,061
false
**Brute force solution:**\nO(N^2) time + O(1) space\nlogic:\n- nested loop i (start from beginning) & j (start from end)\n- if `nums[i] === nums[j]`, increment count\n```\nvar numIdenticalPairs = function(nums) {\n let count = 0; \n for (let i = 0; i < nums.length; i++) {\n for (let j = nums.length - 1; j ...
20
1
['JavaScript']
2
number-of-good-pairs
Python Simple Solutions
python-simple-solutions-by-lokeshsk1-ikpe
Solution 1: Using count\n\n\nclass Solution:\n def numIdenticalPairs(self, nums: List[int]) -> int:\n c=0\n for i in range(len(nums)):\n
lokeshsk1
NORMAL
2020-11-19T07:55:30.465347+00:00
2020-11-19T07:55:30.465378+00:00
1,935
false
#### Solution 1: Using count\n\n```\nclass Solution:\n def numIdenticalPairs(self, nums: List[int]) -> int:\n c=0\n for i in range(len(nums)):\n c+=nums[:i].count(nums[i])\n return c\n```\n\n#### Solution 2: Using dictionary\n```\nclass Solution:\n def numIdenticalPairs(self, nums:...
19
1
['Python', 'Python3']
2
number-of-good-pairs
2-lines in JavaScript using counter for O(n), brute O(n^2)
2-lines-in-javascript-using-counter-for-oycva
Short and sweet:\n\nfunction numIdenticalPairs(nums) { // O(n)\n const map = nums.reduce((m, n, i) => m.set(n, (m.get(n)||0) + 1), new Map());\n return [...ma
adriansky
NORMAL
2020-07-18T21:30:22.310434+00:00
2020-07-18T21:30:22.310477+00:00
2,970
false
Short and sweet:\n```\nfunction numIdenticalPairs(nums) { // O(n)\n const map = nums.reduce((m, n, i) => m.set(n, (m.get(n)||0) + 1), new Map());\n return [...map.values()].reduce((num, n) => num + n * (n - 1) / 2, 0);\n};\n```\n\nFirst line, count how many times each number appears.\n2nd line, use the `n(n-1)/2` to...
18
0
['JavaScript']
1
number-of-good-pairs
Swift: Number of Good Pairs
swift-number-of-good-pairs-by-asahiocean-eapa
swift\nclass Solution {\n func numIdenticalPairs(_ nums: [Int]) -> Int {\n var res = 0, map = [Int:Int]()\n nums.forEach {\n res +=
AsahiOcean
NORMAL
2021-04-11T21:11:38.056553+00:00
2021-07-12T19:42:14.850563+00:00
778
false
```swift\nclass Solution {\n func numIdenticalPairs(_ nums: [Int]) -> Int {\n var res = 0, map = [Int:Int]()\n nums.forEach {\n res += map[$0] ?? 0\n map[$0,default: 0] += 1\n }\n return res\n }\n}\n```\n```swift\nimport XCTest\n\n// Executed 3 tests, with 0 failu...
16
0
['Swift']
2
number-of-good-pairs
Basic Java Solution
basic-java-solution-by-hbhavsar389-nliq
\nclass Solution {\n public int numIdenticalPairs(int[] nums) {\n int i=0,j=0,c=0;\n \n for(i=0;i<nums.length;i++)\n {\n
hbhavsar389
NORMAL
2020-07-13T05:35:31.470394+00:00
2020-07-13T05:35:31.470427+00:00
1,492
false
```\nclass Solution {\n public int numIdenticalPairs(int[] nums) {\n int i=0,j=0,c=0;\n \n for(i=0;i<nums.length;i++)\n {\n for(j=i+1;j<nums.length;j++)\n {\n if(nums[i]==nums[j])\n c++;;\n }\n }\n return c;\...
14
1
['Java']
2
number-of-good-pairs
Simple C++ Code, beats 100% time
simple-c-code-beats-100-time-by-kyouma45-0kby
\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nTake two iterators, traverse one
Kyouma45
NORMAL
2023-04-14T17:21:40.264815+00:00
2023-04-15T06:12:25.129675+00:00
6,529
false
\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTake two iterators, traverse one iterator from other till the end of the vector and check if both have same value or not.\n# Complexity\n- Time complexity: O(n^2)\n<!-- Add your ...
13
0
['C++']
4
number-of-good-pairs
Python O(n) simple solution
python-on-simple-solution-by-tovam-wff0
Python :\n\n\ndef numIdenticalPairs(self, nums: List[int]) -> int:\n\tcountPairs = 0\n\tcounter = {}\n\n\tfor n in nums:\n\t\tif n in counter:\n\t\t\tcountPairs
TovAm
NORMAL
2021-11-07T15:17:37.069296+00:00
2021-11-07T15:17:37.069360+00:00
1,047
false
**Python :**\n\n```\ndef numIdenticalPairs(self, nums: List[int]) -> int:\n\tcountPairs = 0\n\tcounter = {}\n\n\tfor n in nums:\n\t\tif n in counter:\n\t\t\tcountPairs += counter[n]\n\t\t\tcounter[n] += 1\n\n\t\telse:\n\t\t\tcounter[n] = 1\n\n\treturn countPairs\n```\n\n**Like it ? please upvote !**
13
0
['Python', 'Python3']
2
number-of-good-pairs
Clear explanation using Combinations for pairs
clear-explanation-using-combinations-for-ygnl
We are asked to return the total number of pairs in a list of numbers (pair (i,j) is called good if nums[i] == nums[j] and i < j).\n\nFirst, let\'s consider a
alexanco
NORMAL
2021-03-22T19:28:08.499951+00:00
2021-03-23T03:30:04.925451+00:00
1,200
false
We are asked to return the total number of pairs in a list of numbers (`pair (i,j)` is called good if `nums[i] == nums[j]` and `i < j`).\n\nFirst, let\'s consider a list of just one number, e.g. `nums = [1]`. Here, this number cannot be paired with any others (other than itself), so the result is zero.\n\nNext, let\'...
13
1
['Python', 'Python3']
1
number-of-good-pairs
Java HashMap 100% faster
java-hashmap-100-faster-by-abhishekpatel-2lzp
1. we make a HashMap , which we will use to store freq of each num (How many times every num has appeared in array) \n\n2. we traverse through nums array and fo
abhishekpatel_
NORMAL
2021-05-07T08:55:30.200937+00:00
2021-07-11T03:19:41.192918+00:00
1,789
false
**1.** we make a HashMap<Integer, Integer> , which we will use to store freq of each num (How many times every num has appeared in array) \n\n**2.** we traverse through nums array and for each num :\n* we check if ( it is already present in our HashMap or not ):\n* * A) num is not already present in HashMap : *Then it...
12
1
['Hash Table', 'Java']
2
number-of-good-pairs
JAVA || Beats 100% in Time || O(N) Time O(1) Space || Easy To Understand
java-beats-100-in-time-on-time-o1-space-alxcf
\n\n# Youtube Video Link:\nhttps://youtu.be/DDQvDDY3L2U\n\nPlease like, share,subscribe my YouTube channel.\n\n# Intuition\n- The problem is to count the number
millenium103
NORMAL
2023-10-03T02:33:00.511039+00:00
2023-10-03T02:33:00.511060+00:00
1,169
false
![Screenshot 2023-10-03 075116.png](https://assets.leetcode.com/users/images/bf9eede4-caf0-49ec-b810-22b01518dbad_1696300001.6432285.png)\n\n# Youtube Video Link:\n[https://youtu.be/DDQvDDY3L2U]()\n\nPlease like, share,subscribe my YouTube channel.\n\n# Intuition\n- The problem is to count the number of good pairs in t...
10
0
['Java']
4
number-of-good-pairs
✅ Beats 100% || 💡 C++ Easy Map-Based Solution || 🎬 Animation
beats-100-c-easy-map-based-solution-anim-mlrk
Intuition\n Describe your first thoughts on how to solve this problem. \n\n\n\n# Approach\n1. Create an unordered map called mp to store the frequency of each n
Tyrex_19
NORMAL
2023-10-03T02:08:37.569447+00:00
2023-10-03T03:29:26.967811+00:00
530
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n![ezgif.com-video-to-gif (12).gif](https://assets.leetcode.com/users/images/07fa1459-e7b2-410a-8656-69459d02a7f5_1696303312.0705419.gif)\n\n\n# Approach\n1. Create an unordered map called mp to store the frequency of each number in the in...
10
0
['C++']
1
number-of-good-pairs
C# - Simple O(N ^ 2) solution
c-simple-on-2-solution-by-christris-k3cr
csharp\npublic int NumIdenticalPairs(int[] nums) \n{\n\tint count = 0;\n\n\tfor(int i = 0; i < nums.Length; i++)\n\t{\n\t\tfor(int j = i + 1; j < nums.Length; j
christris
NORMAL
2020-07-12T04:11:35.061356+00:00
2020-07-12T04:11:35.061398+00:00
638
false
```csharp\npublic int NumIdenticalPairs(int[] nums) \n{\n\tint count = 0;\n\n\tfor(int i = 0; i < nums.Length; i++)\n\t{\n\t\tfor(int j = i + 1; j < nums.Length; j++)\n\t\t{\n\t\t\tif(nums[i] == nums[j])\n\t\t\t{\n\t\t\t\tcount++;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn count;\n}\n```
10
2
[]
0