292. Nim Game

Source: Internet
Author: User

From today onwards, brush a leetcode every day.

Today's topic is simple: Given a non negative integer number num. For every numbers I in the range 0≤i≤num calculate the number of 1 ' s in their binary representation and return them as An array.

Initial thinking: double loop, outer loop number, inner layer statistics ' 1 ' occurrences.

New knowledge: ArrayList to int array mode;

Binary conversion.

Solve:

 Packagecom.wang.test;Importjava.util.ArrayList;Importjava.util.List;/*@Tittle * 338.  Counting Bits * Given a non negative integer number num. * For every numbers I in the range 0≤i≤num * Calculate the number of 1 ' s in their binary representation and return th EM as an array. * @auther: Wanglin * @time: 2016/04/11*/ Public classCountingbits { Public int[] Countbits (intnum) {List List=NewArrayList ();  for(intn = 0; n <= num; ++N) {//Binarynum receiving a number converted into binaryString Binarynum =integer.tobinarystring (n); Char[] Chararr =Binarynum.tochararray (); inti = 0;  for(intm = 0; M < chararr.length; ++m) {if(Chararr[m] = = ' 1 ') {i++;        }} list.add (i); }        int[] res =New int[List.size ()];  for(inti = 0; I < list.size (); i++) {Res[i]=(Integer) list.get (i); }        returnRes; }     Public Static voidMain (string[] args) {int[] res =NewCountingbits (). Countbits (5);  for(inti:res)        {System.out.print (i); }    }}

292. Nim Game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.