Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0a2d881

Browse files
authored
Sri Hari: Batch-6/Neetcode-150/Added-SwiftCode (neetcode-gh#3936)
* Batch-6/Neetcode-150/Added-swiftcode* Batch-6/Neetcode-150/Added-swiftcode* Batch-6/Neetcode-150/Added-swiftcode* Batch-6/Neetcode-150/Added-swiftcode* Batch-6/Neetcode-150/Added-swiftcode* Batch-6/Neetcode-150/Added-swiftcode* Batch-6/Neetcode-150/Added-swiftcode* Batch-6/Neetcode-150/Added-swiftcode* Batch-6/Neetcode-150/Added-swiftcode* Batch-6/Neetcode-150/Added-swiftcode* Batch-6/Neetcode-150/Added-swiftcode* Batch-6/Neetcode-150/Added-swiftcode* Batch-6/Neetcode-150/Added-swiftcode
1 parent822c53a commit0a2d881

File tree

150 files changed

+17199
-148
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

150 files changed

+17199
-148
lines changed

‎articles/add-two-numbers.md‎

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,40 @@ class Solution {
303303
}
304304
```
305305

306+
```swift
307+
/**
308+
* Definition for singly-linked list.
309+
* public class ListNode {
310+
* public var val: Int
311+
* public var next: ListNode?
312+
* public init() { self.val = 0; self.next = nil; }
313+
* public init(_ val: Int) { self.val = val; self.next = nil; }
314+
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
315+
* }
316+
*/
317+
classSolution {
318+
privatefuncadd(_l1: ListNode?,_l2: ListNode?,_carry:Int)-> ListNode? {
319+
if l1==nil&& l2==nil&& carry==0 {
320+
returnnil
321+
}
322+
323+
let v1= l1?.val??0
324+
let v2= l2?.val??0
325+
326+
let sum= v1+ v2+ carry
327+
let newCarry= sum/10
328+
let val= sum%10
329+
330+
let nextNode=add(l1?.next, l2?.next, newCarry)
331+
returnListNode(val, nextNode)
332+
}
333+
334+
funcaddTwoNumbers(_l1: ListNode?,_l2: ListNode?)-> ListNode? {
335+
returnadd(l1, l2,0)
336+
}
337+
}
338+
```
339+
306340
::tabs-end
307341

308342
###Time & Space Complexity
@@ -577,11 +611,49 @@ class Solution {
577611
}
578612
```
579613

614+
```swift
615+
/**
616+
* Definition for singly-linked list.
617+
* public class ListNode {
618+
* public var val: Int
619+
* public var next: ListNode?
620+
* public init() { self.val = 0; self.next = nil; }
621+
* public init(_ val: Int) { self.val = val; self.next = nil; }
622+
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
623+
* }
624+
*/
625+
classSolution {
626+
funcaddTwoNumbers(_l1: ListNode?,_l2: ListNode?)-> ListNode? {
627+
let dummy=ListNode(0)
628+
var cur= dummy
629+
var l1= l1, l2= l2
630+
var carry=0
631+
632+
while l1!=nil|| l2!=nil|| carry!=0 {
633+
let v1= l1?.val??0
634+
let v2= l2?.val??0
635+
636+
let sum= v1+ v2+ carry
637+
carry= sum/10
638+
let val= sum%10
639+
cur.next=ListNode(val)
640+
641+
cur= cur.next!
642+
l1= l1?.next
643+
l2= l2?.next
644+
}
645+
return dummy.next
646+
}
647+
}
648+
```
649+
580650
::tabs-end
581651

582652
###Time & Space Complexity
583653

584654
* Time complexity: $O(m + n)$
585-
* Space complexity: $O(1)$
655+
* Space complexity:
656+
* $O(1)$ extra space.
657+
* $O(max(m, n))$ for the output list.
586658

587659
>Where $m$ is the length of $l1$ and $n$ is the length of $l2$.

‎articles/anagram-groups.md‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,21 @@ class Solution {
125125
}
126126
```
127127

128+
```swift
129+
classSolution {
130+
funcgroupAnagrams(_strs: [String])-> [[String]] {
131+
var res= [String: [String]]()
132+
133+
for sin strs {
134+
let sortedS=String(s.sorted())
135+
res[sortedS,default: []].append(s)
136+
}
137+
138+
returnArray(res.values)
139+
}
140+
}
141+
```
142+
128143
::tabs-end
129144

130145
###Time & Space Complexity
@@ -277,11 +292,31 @@ class Solution {
277292
}
278293
```
279294

295+
```swift
296+
classSolution {
297+
funcgroupAnagrams(_strs: [String])-> [[String]] {
298+
var res= [Array<Int>: [String]]()
299+
300+
for sin strs {
301+
var count= [Int](repeating:0,count:26)
302+
for cin s {
303+
count[Int(c.asciiValue!)-97]+=1
304+
}
305+
res[count,default: []].append(s)
306+
}
307+
308+
returnArray(res.values)
309+
}
310+
}
311+
```
312+
280313
::tabs-end
281314

282315
###Time & Space Complexity
283316

284317
* Time complexity: $O(m * n)$
285-
* Space complexity: $O(m)$
318+
* Space complexity:
319+
* $O(m)$ extra space.
320+
* $O(m * n)$ space for the output list.
286321

287-
> Where $m$ is the number of stringsand $n$ is the length of the longest string.
322+
> Where $m$ is the number of stringsand $n$ is the length of the longest string.

‎articles/balanced-binary-tree.md‎

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,43 @@ class Solution {
254254
}
255255
```
256256

257+
```swift
258+
/**
259+
* Definition for a binary tree node.
260+
* public class TreeNode {
261+
* public var val: Int
262+
* public var left: TreeNode?
263+
* public var right: TreeNode?
264+
* public init() { self.val = 0; self.left = nil; self.right = nil; }
265+
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
266+
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
267+
* self.val = val
268+
* self.left = left
269+
* self.right = right
270+
* }
271+
* }
272+
*/
273+
classSolution {
274+
funcisBalanced(_root: TreeNode?)->Bool {
275+
guardlet root= rootelse {returntrue }
276+
277+
let left=height(root.left)
278+
let right=height(root.right)
279+
280+
ifabs(left- right)>1 {
281+
returnfalse
282+
}
283+
284+
returnisBalanced(root.left)&&isBalanced(root.right)
285+
}
286+
287+
privatefuncheight(_root: TreeNode?)->Int {
288+
guardlet root= rootelse {return0 }
289+
return1+max(height(root.left),height(root.right))
290+
}
291+
}
292+
```
293+
257294
::tabs-end
258295

259296
###Time & Space Complexity
@@ -521,6 +558,39 @@ class Solution {
521558
}
522559
```
523560

561+
```swift
562+
/**
563+
* Definition for a binary tree node.
564+
* public class TreeNode {
565+
* public var val: Int
566+
* public var left: TreeNode?
567+
* public var right: TreeNode?
568+
* public init() { self.val = 0; self.left = nil; self.right = nil; }
569+
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
570+
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
571+
* self.val = val
572+
* self.left = left
573+
* self.right = right
574+
* }
575+
* }
576+
*/
577+
classSolution {
578+
funcisBalanced(_root: TreeNode?)->Bool {
579+
returndfs(root).0
580+
}
581+
582+
privatefuncdfs(_root: TreeNode?)-> (Bool,Int) {
583+
guardlet root= rootelse {return (true,0) }
584+
585+
let left=dfs(root.left)
586+
let right=dfs(root.right)
587+
588+
let balanced= left.0&& right.0&&abs(left.1- right.1)<=1
589+
return (balanced,1+max(left.1, right.1))
590+
}
591+
}
592+
```
593+
524594
::tabs-end
525595

526596
###Time & Space Complexity
@@ -534,7 +604,7 @@ class Solution {
534604
535605
---
536606

537-
##3.Depth First Search (Stack)
607+
##3.Iterative DFS
538608

539609
::tabs-start
540610

@@ -866,6 +936,58 @@ class Solution {
866936
}
867937
```
868938

939+
```swift
940+
/**
941+
* Definition for a binary tree node.
942+
* public class TreeNode {
943+
* public var val: Int
944+
* public var left: TreeNode?
945+
* public var right: TreeNode?
946+
* public init() { self.val = 0; self.left = nil; self.right = nil; }
947+
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
948+
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
949+
* self.val = val
950+
* self.left = left
951+
* self.right = right
952+
* }
953+
* }
954+
*/
955+
classSolution {
956+
funcisBalanced(_root: TreeNode?)->Bool {
957+
var stack= [TreeNode]()
958+
var node= root
959+
var last: TreeNode?=nil
960+
var depths= [ObjectIdentifier:Int]()
961+
962+
while!stack.isEmpty|| node!=nil {
963+
iflet current= node {
964+
stack.append(current)
965+
node= current.left
966+
}else {
967+
guardlet current= stack.lastelse {break }
968+
if current.right==nil|| last=== current.right {
969+
stack.removeLast()
970+
971+
let leftDepth= current.left!=nil? depths[ObjectIdentifier(current.left!)]??0:0
972+
let rightDepth= current.right!=nil? depths[ObjectIdentifier(current.right!)]??0:0
973+
ifabs(leftDepth- rightDepth)>1 {
974+
returnfalse
975+
}
976+
977+
depths[ObjectIdentifier(current)]=1+max(leftDepth, rightDepth)
978+
last= current
979+
node=nil
980+
}else {
981+
node= current.right
982+
}
983+
}
984+
}
985+
986+
returntrue
987+
}
988+
}
989+
```
990+
869991
::tabs-end
870992

871993
###Time & Space Complexity

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp