파이썬 58

Leetcode 2006 풀이(파이썬, 스위프트)

for문을 전체 길이-1 까지 돌리고 그 안에 2중 for문으로 i+1부터 전체길이까지 두 값의 차이가 k인 것들의 개수를 +해준다. 파이썬: class Solution: def countKDifference(self, nums: List[int], k: int) -> int: cnt = 0 for i in range(len(nums)-1): for j in range(i+1, len(nums)): if abs(nums[i]-nums[j])==k: # 차이의 절대값이 k일때 cnt+=1 # cnt 1증가 return cnt 스위프트: class Solution { func countKDifference(_ nums: [Int], _ k: Int) -> Int { var cnt = 0 for i in 0..

카테고리 없음 2022.02.07

leetcode 1281 풀이(파이썬, 스위프트)

모르는게 많아서 블로그에 많은걸 포스팅하게 된 문제... map과 reduce에 익숙해지자... (파이썬으론 익숙해서 쉽다 파이썬짱) 숫자를 이루는 숫자들을 다뤄야하기 때문에 숫자를 그 숫자를 이루는 숫자들의 배열로 만들었고 for문으로 모두 곱한것에서 더한것을 빼줬다. 스위프트: class Solution { func subtractProductAndSum(_ n: Int) -> Int { var a = String(n).map{Int(String($0))!} return a.reduce(1){$0*$1} - a.reduce(0){$0+$1} } } 파이썬: class Solution: def subtractProductAndSum(self, n: int) -> int: n=[x for x in str..

리트코드 풀이 2022.02.07

스위프트의 배열과 문자열 꿀팁들

공부하다가 스위프트의 배열과 문자열은 파이썬과 달라서 정리해 봅니다. 주의할 점 위주로 정리해 봤습니다. 스위프트에서 문자열을 배열로 바꾸는 법 여러가지가 있으니 코드로 소개해 드리겠습니다. 열거형: let str = "ABC" var arr = [Character]() for i in str { arr.append(i) } print(arr) // ["A", "B", "C"] Array생성자로 직접 변환하기: let str = "ABC" let arr = Array(str) print(arr) // ["A", "B", "C"] map 이용하기: let str = "ABC".map { (n: Character) -> Character in return n } let str = "ABC".map { $0 ..

swift 2022.02.07

리트코드 771 풀이(파이썬, 스위프트)

stones문자열에 있는 문자가 jewel에 있으면 만들어둔 변수에 1을 더해줌 파이썬: class Solution: def numJewelsInStones(self, jewels: str, stones: str) -> int: jewels = [x for x in jewels] cnt=0 for i in range(len(stones)): if stones[i] in jewels: cnt+=1 return cnt 스위프트: class Solution { func numJewelsInStones(_ jewels: String, _ stones: String) -> Int { var a = 0 for s in stones where jewels.contains(s){ a+=1 } return a } }

리트코드 풀이 2022.02.06

Leetcode 1431 풀이(파이썬, 스위프트)

extraCandies를 캔디 배열 값에 더했을때 다른 수보다 작지 않을 수 있냐는 문제. max함수로 최대값을 미리 저장해 두고 포문을 돌리며 비교 스위프트: class Solution { func kidsWithCandies(_ candies: [Int], _ extraCandies: Int) -> [Bool] { var maxcan = candies.max() var a = [Bool]() for i in 0..= maxcan!{ a.append(true) } else{ a.append(false) } } return a } } 파이썬: class Solution: def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bo..

리트코드 풀이 2022.02.06

Leetcode 1108 풀이(파이썬, 스위프트)

스위프트는 replacingOccurrences 메소드, 파이썬은 replace 메소드를 사용할 줄 아는지 묻는 문제. 물론 풀이는 다양할 수 있다. 스위프트: class Solution { func defangIPaddr(_ address: String) -> String { var a = address a = a.replacingOccurrences(of: ".", with: "[.]") return a } } 파이썬: class Solution: def defangIPaddr(self, address: str) -> str: address=address.replace(".","[.]") return address

리트코드 풀이 2022.02.05

Leetcode 2114 풀이(파이썬, 스위프트)

단어의 개수가 가장 많은 인덱스를 고르는 문제이다 for문 연습하기 좋은 문제 스위프트 class Solution { func mostWordsFound(_ sentences: [String]) -> Int { var x = 0 for i in sentences{ x = max(x, numberOfWords(i)) } return x } func numberOfWords(_ s: String) -> Int { var w = 1 for i in s where i == " "{ w+=1 } return w } } 파이썬 class Solution: def mostWordsFound(self, sentences: List[str]) -> int: x=0 for i in range(len(sentences)):..

리트코드 풀이 2022.02.04

Leetcode 2011 풀이(파이썬, 스위프트)

++가 들어있으면 a라는 변수를 만들어서 +=1 해주고 아니면 -=1 해주는 단순한 문제 스위프트: class Solution { func finalValueAfterOperations(_ operations: [String]) -> Int { var a = 0 for i in operations{ if i.contains("++"){ a+=1 } else{ a-=1 } } return a } } 파이썬: class Solution: def finalValueAfterOperations(self, operations: List[str]) -> int: x=0 for i in range(len(operations)): if "++" in operations[i]: x+=1 else: x-=1 return x

리트코드 풀이 2022.02.04