-
Notifications
You must be signed in to change notification settings - Fork 0
5-bishoe01 #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
5-bishoe01 #28
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import Foundation | ||
|
|
||
| // abs(sum - target)๊ฐ ๊ฐ์ฅ ์์์ ์ฐพ๊ธฐ | ||
|
|
||
| class Solution { | ||
| func threeSumClosest(_ nums: [Int], _ target: Int) -> Int { | ||
| func caculate(_ sum: Int) -> Int { | ||
| return abs(sum - target) | ||
| } | ||
|
|
||
| // var answer = 3000 // ์ต๋๊ฐ ๋ฒ์ 3๊ฐ ๋ํ ๊ฒ ์ผ๋ก ํด์คฌ๋๋ฐ, target์ด ๋์์ ์ด๋ ๊ฒ ํด์ฃผ๋ฉด ํ๋ฆฌ๋ค๊ณ ๋ธ ! | ||
| // var answer = Int.max // ํฐ์ง - max๊ฐ์ +๊ฐ ์กฐ๊ธ์ด๋ผ๋ ๋๋๊น ํ ํฐ์ ธ๋ฒ๋ฆฌ๋ ์ด์๊ฐ ์์ | ||
|
Comment on lines
+11
to
+12
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PR์๋ ์์ง๋ง ์ค๋ฒํ๋ก์ฐ ๋ฌธ์ ๋ฐ๊ฒฌํ๊ณ ์ฃผ์์ผ๋ก ๋จ๊ฒจ์ฃผ๋๊ฑฐ ์ข๋ค์! ์๊ณ ๋ฆฌ์ฆ ์คํฐ๋์์ ์ข์ ์๋ก ๋ณด์ ๋๋ค๐ |
||
| var answer = Int(Int32.max) // ๊ทธ๋์ ๊ทธ๋๋ง ๊ฐ์ด ์กฐ๊ธ ์์ ํฐ๊ฐ์ผ๋ก ํด๊ฒฐ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๋๋ฒ๊น ๊ณผ์ ํฅ๋ฏธ๋กญ๊ฒ ๋ดค์ต๋๋ค ~~ ์์ฐ ์์ ๋ฅ๋ค์ด๋น! ์ด ๋ถ๋ถ์ ํจ๋ ์ฝ๋์ฒ๋ผ ์ค์ ๊ฐ ๊ธฐ๋ฐ์ผ๋ก ์ด๊ธฐํํด์ฃผ๋๊ฒ |
||
| var nums = nums | ||
| nums.sort(by: <) | ||
|
Comment on lines
+14
to
+15
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ด ๋ถ๋ถ์ ์ฝ๋๋๋ก ์ ๋ ฌ์ ์ํด ๋ณต์ฌ๋ฅผ ํ์ ๊ฒ ๊ฐ์๋ฐ let sortedNums = nums.sorted()ํ ์ค๋ก ์ฐ๋๊ฒ ์ข์๋ณด์ ๋๋ค!!~ |
||
| let LEN = nums.count | ||
| for i in 0 ..< LEN - 2 { | ||
| var left = i + 1 | ||
| var right = LEN - 1 | ||
| while left < right { | ||
| let totalSum = nums[i] + nums[left] + nums[right] | ||
| // print(nums[i], nums[left], nums[right]) | ||
| // print(totalSum) | ||
| // print(caculate(totalSum)) | ||
| if caculate(totalSum) < caculate(answer) { | ||
| // print(nums[i], nums[left], nums[right]) | ||
|
Comment on lines
+22
to
+26
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. print๋ฌธ์ ๋ง์ง๋ง commit์์ ์ ์ธ์์ผ๋ ์ข์ ๊ฒ ๊ฐ์์!
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๋ค ์ฐธ๊ณ ํ๊ฒ ์ต๋๋ค ! ๐ |
||
| answer = totalSum | ||
| } | ||
| if totalSum < target { | ||
| left += 1 | ||
| } else if totalSum > target { | ||
| right -= 1 | ||
| } else { | ||
| return target | ||
| } | ||
|
Comment on lines
+33
to
+35
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๋ฐ๋ก target์ผ๋ก ํ์ถํ๊ตฐ์! |
||
| } | ||
| } | ||
| return answer | ||
| } | ||
| } | ||
|
|
||
| print(Solution().threeSumClosest([1, 1, 1, 1], -100)) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์คํธ abs๋ก target๊ณผ ๋น๊ตํ๋๊ฑธ
calculate? ํจ์๋ก ๋นผ์ ง๊ตฐ์!caculate๋ ์คํ ๋ง์ผ์ค๊น์?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ค์ฐ .. ๊ทธ๋ ๋ค์.. ์คํ๊ฐ ๋ง์ต๋๋ค..