leetcode 338. 比特位計數

給定一個非負整數 num。對于 0 ≤ i ≤ num 范圍中的每個數字 i ,計算其二進制數中的 1 的數目并將它們作為數組返回。

示例 1:

輸入: 2
輸出: [0,1,1]
示例 2:

輸入: 5
輸出: [0,1,1,2,1,2]

解題思路

偶數:和偶數除以2以后的數字,具有相同的1,例如110 和 1100,因為110乘2等于左移一位,只多出一個0,不影響結果
奇數:當前奇數=偶數+1 例如 1101 和 1100,因為任意偶數二進制的最后一位必為0,所以加一以后,就多出一個1

代碼

class Solution {public int[] countBits(int num) {int[] res=new int[num+1];int i=0;while (++i<=num) res[i]=((i&1)==1)?res[i-1]+1:res[i>>1];return res;}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391675.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391675.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391675.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

r語言調用數據集中的數據集_自然語言數據集中未解決的問題

r語言調用數據集中的數據集Garbage in, garbage out. You don’t have to be an ML expert to have heard this phrase. Models uncover patterns in the data, so when the data is broken, they develop broken behavior. This is why researchers allocate significant reso…

為什么不應該使用(長期存在的)功能分支

Isn’t the git history in the picture above nice to work with? There are probably many problems in that repository, but one of them is most definitely the use of feature branches. Let’s see how such a bad thing became such a common practice.上面圖片中的g…

(轉) Spring 3 報org.aopalliance.intercept.MethodInterceptor問題解決方法

http://blog.csdn.net/henuhaigang/article/details/13678023 轉自CSDN博客&#xff0c;因為一個jar包沒引入困擾我好長時間 &#xff0c;當時正在做spring AOP 的一個小測試&#xff0c;總在報錯&#xff0c;最后發現自己是問題3&#xff0c;引入一個jar包得到了解決 一 開發環…

數據特征分析-相關性分析

相關性分析是指對兩個或多個具備相關性的變量元素進行分析&#xff0c;從而衡量兩個變量的相關密切程度。 相關性的元素之間需要存在一定的聯系或者概率才可以進行相關性分析。 相關系數在[-1,1]之間。 一、圖示初判 通過pandas做散點矩陣圖進行初步判斷 df1 pd.DataFrame(np.…

leetcode 354. 俄羅斯套娃信封問題(dp+二分)

給定一些標記了寬度和高度的信封&#xff0c;寬度和高度以整數對形式 (w, h) 出現。當另一個信封的寬度和高度都比這個信封大的時候&#xff0c;這個信封就可以放進另一個信封里&#xff0c;如同俄羅斯套娃一樣。 請計算最多能有多少個信封能組成一組“俄羅斯套娃”信封&#…

fastlane use_legacy_build_api true

fastlane版本號&#xff1a;fastlane 1.108.0 Xcode版本號&#xff1a;8.1 MacOS版本號&#xff1a;10.12 使用fastlane打包 - Release / Ad-Hoc包時報錯: [13:36:59]: There was an error exporting your application [13:36:59]: Unfortunately the new Xcode export API is …

獲取所有權_住房所有權經濟學深入研究

獲取所有權Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works without seekin…

scala 單元測試_Scala中的法律測試簡介

scala 單元測試Property-based law testing is one of the most powerful tools in the scala ecosystem. In this post, I’ll explain how to use law testing and the value it’ll give you using in-depth code examples.基于財產的法律測試是scala生態系統中最強大的工具…

getBoundingClientRect說明

getBoundingClientRect用于獲取某個元素相對于視窗的位置集合。 1.語法&#xff1a;這個方法沒有參數。 rectObject object.getBoundingClientRect() 2.返回值類型&#xff1a;TextRectangle對象&#xff0c;每個矩形具有四個整數性質&#xff08; 上&#xff0c; 右 &#xf…

robot:接口入參為圖片時如何發送請求

https://www.cnblogs.com/changyou615/p/8776507.html 接口是上傳圖片&#xff0c;通過F12抓包獲得如下信息 由于使用的是RequestsLibrary&#xff0c;所以先看一下官網怎么傳遞二進制文件參數&#xff0c;https://2.python-requests.org//en/master/user/advanced/#post-multi…

開發小Tips-setValue

字典添加數據請用 [dict setValue:value forKey:key] 代替 [dict setObject:value forKey:key] 復制代碼這樣在一些網絡傳參時不用考慮nil的情況&#xff0c;?

瀏覽器快捷鍵指南_快速但完整的IndexedDB指南以及在瀏覽器中存儲數據

瀏覽器快捷鍵指南Interested in learning JavaScript? Get my JavaScript ebook at jshandbook.com有興趣學習JavaScript嗎&#xff1f; 在jshandbook.com上獲取我JavaScript電子書 IndexedDB簡介 (Introduction to IndexedDB) IndexedDB is one of the storage capabilities …

Java-Character String StringBuffer StringBuilder

Java Character 類 Character 類用于對單個字符進行操作character 類在對象包裝一個基本類型char的值 char ch "a";char uniChar \u039A;char[] charArray {a, b, c};使用Character的構造方法創建一個Character類對象 Character ch new Character(a);Charact…

已知兩點坐標拾取怎么操作_已知的操作員學習-第3部分

已知兩點坐標拾取怎么操作有關深層學習的FAU講義 (FAU LECTURE NOTES ON DEEP LEARNING) These are the lecture notes for FAU’s YouTube Lecture “Deep Learning”. This is a full transcript of the lecture video & matching slides. We hope, you enjoy this as mu…

缺失值和異常值處理

一、缺失值 1.空值判斷 isnull()空值為True&#xff0c;非空值為False notnull() 空值為False&#xff0c;非空值為True s pd.Series([1,2,3,np.nan,hello,np.nan]) df pd.DataFrame({a:[1,2,np.nan,3],b:[2,np.nan,3,hello]}) print(s.isnull()) print(s[s.isnull() False]…

leetcode 503. 下一個更大元素 II(單調棧)

給定一個循環數組&#xff08;最后一個元素的下一個元素是數組的第一個元素&#xff09;&#xff0c;輸出每個元素的下一個更大元素。數字 x 的下一個更大的元素是按數組遍歷順序&#xff0c;這個數字之后的第一個比它更大的數&#xff0c;這意味著你應該循環地搜索它的下一個更…

setNeedsDisplay看我就懂!

前言&#xff1a; setNeedsDisplay異步執行的。它會自動調用drawRect方法&#xff0c;這樣可以拿到 UIGraphicsGetCurrentContext&#xff0c;就可以繪制了。而setNeedsLayout會默認調用layoutSubViews&#xff0c;處理子視圖中的一些數據。 一、著手 我定義了一個UIView的子類…

如何使用ArchUnit測試Java項目的體系結構

by Emre Savc?由EmreSavc? 如何使用ArchUnit測試Java項目的體系結構 (How to test your Java project’s architecture with ArchUnit) In this post, I will show you an interesting library called ArchUnit that I met recently. It does not test your code flow or bu…

解決ionic3 android 運行出現Application Error - The connection to the server was unsuccessful

在真機上啟動ionic3打包成的android APK,啟動了很久結果彈出這個問題&#xff1a; Application Error - The connection to the server was unsuccessful 可能是我項目資源太多東西了&#xff0c;啟動的時間太久了&#xff0c;導致超時了。 解決方案是在項目目錄下的config.xml…

特征工程之特征選擇_特征工程與特征選擇

特征工程之特征選擇&#x1f4c8;Python金融系列 (&#x1f4c8;Python for finance series) Warning: There is no magical formula or Holy Grail here, though a new world might open the door for you.警告 &#xff1a; 這里沒有神奇的配方或圣杯&#xff0c;盡管新世界可…