This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
func removeElement(_ nums: [Int], val: Int) -> Int { | |
// filter可以篩選目標數字,$0代表nums陣列的每一個數字,篩選條件是當數字不等於val則留下,最後回傳剩餘nums的總數 | |
nums = nums.filter { $0 != val } | |
return nums.count | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func twoSum(_ nums: [Int], _ target: Int) -> [Int] { | |
// 拿來儲存數字(Key)對應的index(Value) | |
var dict = [Int: Int]() | |
/* | |
nums.enumerated()結果 | |
(0, 2) | |
(1, 7) | |
(2, 11) | |
(3, 15) | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// left | right | root | |
func postOrderTraverse(_ root: Node?) { | |
if let root = root { | |
postOrderTraverse(root.left) | |
postOrderTraverse(root.right) | |
print(root.value, terminator: " ") | |
} | |
} | |
postOrderTraverse(node_1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// root | left | right | |
func preOrderTraverse(_ root: Node?) { | |
if let root = root { | |
print(root.value, terminator: " ") | |
preOrderTraverse(root.left) | |
preOrderTraverse(root.right) | |
} | |
} | |
preOrderTraverse(node_1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// left | root | right | |
func inOrderTraverse(_ root: Node?) { | |
if let root = root { | |
inOrderTraverse(root.left) | |
print(root.value, terminator: " ") | |
inOrderTraverse(root.right) | |
} | |
} | |
inOrderTraverse(node_1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
public class Node { | |
var value: Int | |
var left: Node? | |
var right: Node? | |
public init(_ value: Int) { | |
self.value = value | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let nestedGroup = NSCollectionLayoutGroup.vertical(layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), | |
heightDimension: .fractionalWidth(16/9)), | |
subitems: [fullPhotoItem, mainWithPairGroup, tripletGroup, mainWithPairReversedGroup] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let mainWithPairReversedGroup = NSCollectionLayoutGroup.horizontal(layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), | |
heightDimension: .fractionalWidth(4/9)), | |
subitems: [trailingGroup, mainItem]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let tripleItem = NSCollectionLayoutItem(layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1/3), | |
heightDimension: .fractionalHeight(1)) | |
tripleItem.contentInsets = NSDirectionalEdgeInsets(top: 2, leading: 2, bottom: 2, trailing: 2) | |
let tripletGroup = NSCollectionLayoutGroup.horizontal(layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), | |
heightDimension: .fractionalWidth(2/9)), | |
subitem: tripleItem, | |
count: 3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let mainItem = NSCollectionLayoutItem(layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(2/3), | |
heightDimension: .fractionalHeight(1.0)) | |
mainItem.contentInsets = NSDirectionalEdgeInsets(top: 2, leading: 2, bottom: 2, trailing: 2) | |
let pairItem = NSCollectionLayoutItem(layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1/3), | |
heightDimension: .fractionalHeight(1/2)) | |
pairItem.contentInsets = NSDirectionalEdgeInsets(top: 2, leading: 2, bottom: 2, trailing: 2) | |
NewerOlder