Giter Club home page Giter Club logo

Comments (1)

WonYong-Jang avatar WonYong-Jang commented on June 16, 2024

2의 제곱수인지 확인 !

ex) 16 -> 2^4 ( true )
ex) 218 -> false

2의 제곱인 숫자를 비트연산자로 표현해 보면
1
10
100
1000 ...
으로 맨 좌측 숫자가 1이고 나머지는 0이다.

즉, 입력받은 숫자가 2의 제곱인지 확인하려면 위의 형태인지를 순차적으로 확인하는 방법이 있지만
int는 32bit만 표현이 가능하고 더 큰 경우는 시간 초과!

n & (n-1)

  • 위의 식이 0인지를 확인하면 된다!
  • n은 좌측이 1 나머지가 0이며, n-1은 좌측 0이며 나머지가 모두 1인 상태이다!!
  • 두 수를 and 연산자로 0이되는 지를 확인한다.

ex ) https://leetcode.com/problems/power-of-two/

from algorithm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.