전체 글 (306)
2022-02-11 02:43:15
반응형

Prime Number of Set Bits in Binary Representation

 

 문제 설명

 

Given two integers left and right, return the count of numbers in the inclusive range [left, right] having a prime number of set bits in their binary representation.

Recall that the number of set bits an integer has is the number of 1's present when written in binary.

  • For example, 21 written in binary is 10101, which has 3 set bits.
왼쪽과 오른쪽 두 개의 정수가 주어지면 이진 표현에 소수의 세트 비트를 갖는 포함 범위[좌, 우]의 숫자 카운트를 반환합니다.

정수가 갖는 세트 비트의 수는 이진수로 쓸 때 존재하는 1의 수라는 것을 기억하라.

예를 들어, 이진법으로 작성된 21은 10101이고, 3개의 세트 비트를 가지고 있다.

 

 제한 사항

 

  • 1 <= left <= right <= 106
  • 0 <= right - left <= 104

 

 입출력 예

 

Example 1:

Input: left = 6, right = 10
Output: 4
Explanation:
6  -> 110 (2 set bits, 2 is prime)
7  -> 111 (3 set bits, 3 is prime)
8  -> 1000 (1 set bit, 1 is not prime)
9  -> 1001 (2 set bits, 2 is prime)
10 -> 1010 (2 set bits, 2 is prime)
4 numbers have a prime number of set bits.

Example 2:

Input: left = 10, right = 15
Output: 5
Explanation:
10 -> 1010 (2 set bits, 2 is prime)
11 -> 1011 (3 set bits, 3 is prime)
12 -> 1100 (2 set bits, 2 is prime)
13 -> 1101 (3 set bits, 3 is prime)
14 -> 1110 (3 set bits, 3 is prime)
15 -> 1111 (4 set bits, 4 is not prime)
5 numbers have a prime number of set bits.

 

 Python 코드

 

Python code 

class Solution:
    def countPrimeSetBits(self, L: int, R: int) -> int:
        return sum(bin(i).count('1') in [2,3,5,7,11,13,17,19] for i in range(L, R+1))

bin(i).count('1') # 이진 표현에서 1의 수가 소수인 경우

bin(i).count('1') in [2, 3, 4, 5, 7, 11, 13, 17, 19]  

# 이진 표현에서 1의 수가 [2, 3, 4, 5, 7, 11, 13, 17, 19]에 빈도가 있는 경우

sum(bin(i).count('1') in [2, 3, 5, 7, 11, 13, 17, 19] for i in range(L, R+1))

# sum( ) 함수를 이용하여 count를 증가시킨다.

 

* 참고 링크 1 : https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/discuss/801535/Python-3-One-Line

 

 

 C++ 코드

 

C ++ code

// c++ code
#include <cmath>
class Solution {
public:
    int countPrimeSetBits(int L, int R) {
        int res = 0;
        for (int num = L; num <= R; num++) {
            int count = countOne(num);
            if (isPrime(count))
                res++;
        }
        return res;
    }
    
    bool isPrime(int num) {
        if (num <= 3)
            return num > 1;
        
        int square_root = sqrt(num);
        for (int i = 2; i <= square_root; i++) {
            if (num % i == 0)
                return false;
        }
        return true;
    }
    
    int countOne(int num) {
        vector<int> bin = dec2bin(num);
        int count = 0;
        for (auto i = bin.begin(); i < bin.end(); i++) {
            if (*i == 1)
                count++;
        }
        return count;
    }
    
    vector<int> dec2bin(int num) {
        vector<int> bin;
        while (num != 0) {
            bin.push_back(num % 2);
            num /= 2;
        }
        reverse(bin.begin(), bin.end());
        return bin;
    }
};

* 참고 링크 : https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/discuss/376499/C%2B%2B-and-Python-3-bad-performance-need-to-improve

 

 출처

 

https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/

반응형
2022-02-11 01:17:01
반응형

Power of Four

 

 문제 설명

 

Given an integer n, return true if it is a power of four. Otherwise, return false.

An integer n is a power of four, if there exists an integer x such that n == 4x.

 

 제한 사항

 

  • -231 <= n <= 231 - 1

 

 입출력 예

 

Example 1:

Input: n = 16
Output: true

Example 2:

Input: n = 5
Output: false

Example 3:

Input: n = 1
Output: true

 

 

 Python 코드

 

Python code 

class Solution:
    def isPowerOfFour(self, n: int) -> bool:
        return math.log(n, 1/4) % 1 == 0 if n > 0 else False

* 참고 링크 : https://leetcode.com/problems/power-of-four/discuss/637372/Python-3-Single-line

  • math.log( ) 함수 이용 // loops(반복문) 또는 recursion(재귀)를 피하는 방법 중 하나// x : 필수의 로그를 계산할 값을 지정// base : 선택적으로 사용할 로그 베이스 (Default 값 : e)
  • ( 값이 0 또는 음수이면 ValueError 반환, 값이 숫자가 아니면 TypeError를 반환)
  • math.log(x, base)

 

 C++ 코드

 

C ++ code

class Solution {
public:
    bool isPowerOfFour(int num) {
        return num > 0 && (num & (num - 1)) == 0 && (num - 1) % 3 == 0;
    }
};

* 참고 링크 : https://leetcode.com/problems/power-of-four/discuss/80460/1-line-C%2B%2B-solution-without-confusing-bit-manipulations

 

 출처

 

https://leetcode.com/problems/power-of-four/

반응형
2022-02-10 18:00:44
반응형

Session Tracking란?

 

 

 Session Tracking 특징

 

 

 

 출처

 

 

+ 강의 교재

반응형
2022-02-10 18:00:00
반응형

Filter API란?

 

 

 Filter API 특징

 

 

 

 출처

 

 

+ 강의 교재

반응형
2022-02-09 18:31:12
반응형

Web Application에서 DB 연동_MyBatis

 

 MyBatis Architecture

1) MyBatis 비 Web 환경 (Standalone 환경)

[Project] 마우스 우클릭 → [Build Path] → [Configure Build Path...] → [Java Build Path] → [Libraries] → [Add External JARs...]  ojdbc6_g.jar & mybatis.jar 추가

 

2) MyBatis Web 환경 

jar file 2개 설정
[Project] → [WebContent] → [WEB-INF] → [lib] → ojdbc6_g.jar & mybatis.jar 복사

DB 연동에 필요한 4가지 정보를 저장한 파일 : jdbc.propertis 
xml file 2개
* 환경설정 : Configuration.xml
* SQL 설정 : XXXMapper.xml
Configuration.xml file을 읽는 Class → MySqlSessionFactory.java

 

 출처

 

 

+ 강의 교재

반응형

'AI Bootcamp > Servlet JSP' 카테고리의 다른 글

[Servlet JSP] Session Tracking  (0) 2022.02.10
[Servlet JSP] Filter API  (0) 2022.02.10
[Servlet JSP] Web Application에서 DB 연동_JDBC  (0) 2022.02.09
[Servlet JSP] Scope & Scope Life Cycle  (0) 2022.02.08
[Servlet JSP] Servlet 정의  (0) 2022.02.08
2022-02-09 18:25:32
반응형

Web Application에서 DB 연동_JDBC

 JDBC Architecture

1) JDBC 비 Web 환경 (Standalone 환경)

[Project] 마우스 우클릭 → [Build Path] → [Configure Build Path...] → [Java Build Path] → [Libraries] → [Add External JARs...] → ojdbc6_g.jar 추가

 

2) JDBC Web 환경 

[Project] → [WebContent] → [WEB-INF] → [lib] → ojdbc6_g.jar 복사

 출처

 

 

+ 강의 교재

반응형

'AI Bootcamp > Servlet JSP' 카테고리의 다른 글

[Servlet JSP] Filter API  (0) 2022.02.10
[Servlet JSP] Web Application에서 DB 연동_MyBatis  (0) 2022.02.09
[Servlet JSP] Scope & Scope Life Cycle  (0) 2022.02.08
[Servlet JSP] Servlet 정의  (0) 2022.02.08
[Servlet JSP] 500 Error  (0) 2022.02.08