Computer Science/Algorithm (30) 썸네일형 리스트형 배열의 길이를 2의 거듭제곱으로 만들기: 이진수의 보수, & 연산 문제 풀이 def solution(arr): l = len(arr) while bin(l).count('1') != 1: l += l & (-l) return arr + [0] * (l - len(arr)) 이진수 풀이법이 마음에 들었는데, 이해가 안되는 부분이 있었고 공부하다보니 2의 보수를 복습할 수 있었다.2의 보수란, 십진수 12를 -12로 만들 때는 -만 붙이면 되지만 12를 이진수로 표현할 때는 -가 없으므로 이를 표현하기 위한 방법을 말한다.기존 이진수의 bit들을 반대로 만들고, 맨 끝에 +1을 하면 된다. 이진수로: 00001100 (12) l = 00001100-l = 11110100 (2의 보수 표현) 또한 l & -l 연산을 .. git으로 통합함 https://github.com/CJ0823/python_algorithm_practice [Leetcode] [작성중] 11. Container With Most Water 1. 문제 You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). Find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store. Notice that you may not slant the container. Input: height = [1.. [Leetcode] [미결] 10. Regular Expression Matching 1. 문제 Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where: '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). Example 1: Input: s = "aa", p = "a" Output: false Explanation: "a" does not match the entire string "aa". Example 2: Input:.. [Leetcode] 9. Palindrome Number 1. 문제 Given an integer x, return true if x is a palindrome, and false otherwise. Example 1: Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from right to left. Example 2: Input: x = -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: x = 10 Output: false Explan.. [Leetcode] 8. String to Integer (atoi) 비추천이 너무 많아서 살펴보니 쓸데없는 복잡한 케이스를 만들어서 if문을 너무 많이 강제한다는 의견들이 많다. 테스트 자체에 대한 생각 외에 더 이상 탐색하는건 시간 낭비인 문제인 것 같다. 간단해보이는 문제라도 많은 케이스가 존재한다. 처음부터 차분히 풀어야한다. 여러 번 돌려보면서 케이스 스터디를 하는게 낫다. 문제를 푸는 입장이라면 내가 케이스들을 다 생각할 필요가 없다. 영어 해석도 일단 대충하고 실행을 여러 번하는게 낫다. ---------------------------------아래 내용은 안 봐도 됨 1. 문제 Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar .. [LeetCode] 7. Reverse Integer 1. 문제 Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. Assume the environment does not allow you to store 64-bit integers (signed or unsigned). Example 1: Input: x = 123 Output: 321 Example 2: Input: x = -123 Output: -321 Example 3: Input: x = 120 Output: 21 Constraint.. [LeetCode][작성중] 6. Zigzag Conversion 1. 문제 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows: string convert(string s, int numRows); Example 1: Input: s.. 이전 1 2 3 4 다음