Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Swift Warmup : power of three
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Johnlin
December 13, 2016
Programming
97
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Swift Warmup : power of three
Johnlin
December 13, 2016
More Decks by Johnlin
See All by Johnlin
用 MLIR 實作 一個 Ruby IR (intermediate representation)
johnlinvc
0
340
Unearth Ruby builtin Gems 發掘 Ruby 的內建 Gems
johnlinvc
0
420
Ruby 型別檢查工具簡介
johnlinvc
0
640
Swift Actor 實作探索
johnlinvc
0
210
用 mruby 來寫跨平台工具
johnlinvc
0
130
Actor model 簡介
johnlinvc
0
240
一起玩 Helm 3
johnlinvc
1
170
為什麼 App 卡卡的
johnlinvc
2
1.3k
如何使用 byebug 來除錯 Ruby 程式
johnlinvc
0
280
Other Decks in Programming
See All in Programming
kubernetes コンポーネント開発入門 / 新卒N年目の勉強会&交流会!〜〇〇への誘い〜 #n_study
mazrean
0
210
Swift愛好会100回記念 第1回を振り返る
jollyjoester
0
120
AI駆動開発にグラフDBを重ねてみた
satoshi256kbyte
2
780
AIを上手に使っていこうとしたら越境せざるを得なくなった話 〜実践1年で見えた境界を越えなければならない理由と進め方〜 / Crossing borders with AI
tomoyakitaura
4
1.1k
Swift愛好会と私(ウホーイ) / Swift Fan Club and Uhooi
uhooi
0
150
Heart of Swift Concurrency
koher
0
230
MIZARU@SPAJAM2026 第二回予選
1901drama
0
110
Go × SIMDで高速化するベクトル検索 ~ルーフラインモデルでSIMDが効く境界を探れ! ~
po3rin
1
230
GraphRAGのKnowledge Graphを 直接!見る/View-GraphRAG's-KnowledgeGraph-directly!
tyumugi1113
1
280
ソフトウェアラスタライザ
fadis
1
800
ALB ログから Trace を気合で繋げる技術
fohte
7
880
Claude Codeを組織的に動かして月400PRを実現した話
happy_ryo
0
270
Featured
See All Featured
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.3k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.2k
Raft: Consensus for Rubyists
vanstee
141
7.7k
How to Talk to Developers About Accessibility
jct
2
540
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.9k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
How STYLIGHT went responsive
nonsquared
100
6.3k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
470
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
370
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
230
Transcript
Swift ఔࣜখ Power of Three John Lin
326. Power of Three • https://leetcode.com/problems/power-of-three/ • Given an integer,
write a function to determine if it is a power of three. • څ㟬ҰݸᏐɼሜҰݸവᏐᏗଞੋෆੋ 3 త࣍ํɻ
• Follow up: Could you do it without using any
loop / recursion? • Ճ: ՄҎෆ༻౸ ᅲ / ᬇ बղग़Ҋ䆩ʁ
• ༌ೖ: 81 • ༌ग़: true • 3*3*3*3 == 81
• ༌ೖ: 5566 • ༌ग़: false • 2 * 11
* 11 * 23 == 5566
ࠎՍ class Solution { func isPowerOfThree(_ n: Int) -> Bool
{ } }
ԋࢉ๏ 0 • ೳෆೳඃ 3 আɼՄҎతबআҎ 3ɻ • Ұ၏౸ෆೳඃ 3
আɼ҃ੋԙ 0 ҝࢭɼಹ ݸᏐࣈੋෆੋ 1
class Solution1 { func isPowerOfThree(_ n: Int) -> Bool {
var nn = n while(nn%3==0 && nn>0){ nn = nn / 3 } return nn==1 } }
class Solution1 { func isPowerOfThree(_ n: Int) -> Bool {
var nn = n while(nn%3==0 && nn>0){ nn = nn / 3 } return nn==1 } } ࣌ؒෳᯑ: O(log(n)) ۭؒෳᯑ: O(1) Runtime: 289 ms
ԋࢉ๏ 1 • ᏐࣈࡾਐҐɻ 5566 => “21122011” • 1 =>
“1”, 3 => “10”, 9 => “100”, 27 => “1000” • 3 త࣍ํҰఆ။Ṝᒬ 100000….. ɼबੋআྃ ։಄ੋ 1ɼଖଞੋ 0 • ᒾҰ༗ᔒ༗Ṝࠣಛੑबɻ
class Solution { func isPowerOfThree(_ n: Int) -> Bool {
let threeBased = String(n, radix:3) guard let head = threeBased.characters.first, head == "1" else { return false } guard threeBased.characters.count > 1 else { return true } let tail = String(threeBased.characters.dropFirst()) return Int(tail) == 0 } } ᔒ༗༻౸ᅲ
class Solution { func isPowerOfThree(_ n: Int) -> Bool {
let threeBased = String(n, radix:3) guard let head = threeBased.characters.first, head == "1" else { return false } guard threeBased.characters.count > 1 else { return true } let tail = String(threeBased.characters.dropFirst()) return Int(tail) == 0 } } ෆա䋯ಘຫ ࣌ؒෳᯑ: O(log(n)) ۭؒෳᯑ: O(log(n)) Runtime: 415 ms
ԋࢉ๏ 2 • ኺവᏐతఆٛ func isPowerOfThree(_ n: Int) -> Bool
ՄҎग़༌ೖᆴੋ༗্ݶతɼबੋ Int.max = 9223372036854775807 • ॴҎཁፙ౸্ݶ㚎࠷େత3త࣍ํɼ࠶ n ೳෆ ೳআଞɼबಓ n ੋෆੋ 3 త࣍ํྃɻ
class Solution { func isPowerOfThree(_ n: Int) -> Bool {
let ms = String(Int.max, radix:3) let max3 = "1" + repeatElement("0", count: ms.characters.count-1).joined() let max3i = Int(max3, radix:3)! //4052555153018976267 return n > 0 && max3i % n == 0 } }
class Solution { func isPowerOfThree(_ n: Int) -> Bool {
return n > 0 && 4052555153018976267 % n == 0 } } ࣌ؒෳᯑ: O(1) ۭؒෳᯑ: O(1) Runtime: 255 ms
Q&A