-
git에도 올리고 블로그에도 올리려니 정신이 없다 😮😮😮
binary gap
A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.
For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.
def solution(N): # write your code in Python 3.6 openFlag, closeFlag, zeroFlag = False, False, False #1001001 로 예를 들면 #가장 먼저 1이 있는지 체크 if N <3: return 0 binaryN = format(N,'b') checkOne = False checkZero = False #사이에 zero가 있었나? cntZero = 0 longestZero = 0 for n in binaryN: if n == '0': checkZero = True cntZero +=1 #1을 만나면 1 체크 if n == '1': if checkOne and checkZero :#이미 직전에 1을 만난적이 있고 사이에 0이 있었는지 체크함 if longestZero < cntZero : longestZero = cntZero #1001과 같은 경우이므로 이전에 더 큰 0이 있었는지 확인한 후 체인지함 cntZero = 0 else: checkOne = True #1을 처음만났음 return longestZero
CyclicRotation
An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place).
The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times.
Write a function:
class Solution { public int[] solution(int[] A, int K); }
that, given an array A consisting of N integers and an integer K, returns the array A rotated K times.
For example, given
A = [3, 8, 9, 7, 6] K = 3
the function should return [9, 7, 6, 3, 8]. Three rotations were made:
[3, 8, 9, 7, 6] -> [6, 3, 8, 9, 7] [6, 3, 8, 9, 7] -> [7, 6, 3, 8, 9] [7, 6, 3, 8, 9] -> [9, 7, 6, 3, 8]
For another example, given
A = [0, 0, 0] K = 1
the function should return [0, 0, 0]
Given
A = [1, 2, 3, 4] K = 4
the function should return [1, 2, 3, 4]
def solution(A, K): length = len(A) for _ in range(K): A = A[-1:]+A[:length-1] return A
OddOccurrencesInArray
A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired.
For example, in array A such that:
A[0] = 9 A[1] = 3 A[2] = 9 A[3] = 3 A[4] = 9 A[5] = 7 A[6] = 9
- the elements at indexes 0 and 2 have value 9,
- the elements at indexes 1 and 3 have value 3,
- the elements at indexes 4 and 6 have value 9,
- the element at index 5 has value 7 and is unpaired.
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A consisting of N integers fulfilling the above conditions, returns the value of the unpaired element.
For example, given array A such that:
A[0] = 9 A[1] = 3 A[2] = 9 A[3] = 3 A[4] = 9 A[5] = 7 A[6] = 9
the function should return 7, as explained in the example above.
from collections import Counter def solution(A): # write your code in Python 3.6 c = Counter(A) for k, v in c.items(): if v%2 != 0: return k
'2020년 > 코테' 카테고리의 다른 글
[프로그래머스 Lv1,Lv2 다풀기 프로젝트] 2016년 (0) 2020.10.03 [leetcode 추석맞이 3문제] minimum-absolute-, differencerepeated-dna-sequences, pseudo-palindromic-paths-in-a-binary-tree (0) 2020.10.01 [코테 연습] find-the-difference (0) 2020.09.24 [코테 연습] Product of Array Except Self (0) 2020.09.24 [코테 연습] Fair Candy Swap (0) 2020.09.24