星空网 > 软件开发 > Java

292. Nim Game

  从今天开始,每天刷一道leetcode。

  今天的题目很简单: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.

  初步思路:双循环,外层循环数字,内层统计‘1’出现次数。

  新知识:ArrayList转int数组方式;

      进制转换。

  解决:

package com.wang.test;import java.util.ArrayList;import java.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 them as an array. * @auther:wanglin * @time:2016/04/11 */public class CountingBits {  public int[] countBits(int num) {    List list = new ArrayList();    for (int n = 0; n <= num; ++n) {      // binaryNum接收转为二进制的数字      String binaryNum = Integer.toBinaryString(n);      char[] charArr = binaryNum.toCharArray();      int i = 0;      for (int m = 0; m < charArr.length; ++m) {        if (charArr[m] == '1') {          i++;        }      }      list.add(i);    }    int[] res = new int[list.size()];    for (int i = 0; i < list.size(); i++) {      res[i] = (Integer) list.get(i);    }    return res;  }  public static void main(String[] args) {    int[] res = new CountingBits().countBits(5);    for (int i : res) {      System.out.print(i);    }  }}

 




原标题:292. Nim Game

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

tiktok海外版官网:https://www.goluckyvip.com/tag/81125.html
tiktok加速器永久免费版:https://www.goluckyvip.com/tag/81126.html
国际版tiktok下载:https://www.goluckyvip.com/tag/81129.html
沃尔玛售枪:https://www.goluckyvip.com/tag/8113.html
tiktok海外国际版:https://www.goluckyvip.com/tag/81130.html
tiktok成人:https://www.goluckyvip.com/tag/81131.html
不想长期打工,可做这10个暴利生意,竞争不大,赚钱容易点:https://www.kjdsnews.com/a/1836331.html
国旗LED灯(24-cv-02099)维权进度更新!内附专利查询懒人包!:https://www.kjdsnews.com/a/1836332.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流