[LeetCode]600. Non-negative Integers without Consecutive Ones
給一個數n,找出有多少個不大於n的正數,且這些正數的二進位制表示中不包含兩個連續的1
int轉2進位制string的API!!
先用dp找出在k位二進位制長度的數有多少滿足條件的。a[i]表示以0為結尾的長度為i + 1的滿足條件的數字個數,b[i]表示1為結尾的。
再找a[n - 1] + b[n - 1]裡面overcount了多少,只有當連續兩位為0時,b[i]是overcount的。11、01、10都能cover到a[i]和b[i]
public class Solution { public int findIntegers(int num) { StringBuilder sb = new StringBuilder(Integer.toBinaryString(num)).reverse(); int n = sb.length(); int[] a = new int[n]; int[] b = new int[n]; a[0] = 1; b[0] = 1; for (int i = 1; i < n; i++) { a[i] = a[i - 1] + b[i - 1]; b[i] = a[i - 1]; } int res = a[n - 1] + b[n - 1]; for (int i = n - 2; i >= 0; i--) { if (sb.charAt(i) == '0' && sb.charAt(i + 1) == '0') { res -= b[i]; } if (sb.charAt(i) == '1' && sb.charAt(i + 1) == '1') { break; } } return res; } }
相關推薦
Leetcode 600. Non-negative Integers without Consecutive Ones
Leetcode 600. Non-negative Integers without Consecutive Ones 題目: Given a positive integer n, find the number of non-negative integers less tha
[LeetCode]600. Non-negative Integers without Consecutive Ones
給一個數n,找出有多少個不大於n的正數,且這些正數的二進位制表示中不包含兩個連續的1 int轉2進位制string的API!! 先用dp找出在k位二進位制長度的數有多少滿足條件的。a[i]表示以0為結尾的長度為i + 1的滿足條件的數字個數,b[i]表示1為結尾
Leetcode 600 Non-negative Integers without Consecutive Ones
Given a positive integer n, find the number of non-negative integers less than or equal to n, whose binary representations do NOT
[LeetCode] Non-negative Integers without Consecutive Ones 非負整數不包括連續的1
Given a positive integer n, find the number of non-negative integers less than or equal to n, whose binary representations do NOT contain consecutive one
[leetcode-485-Max Consecutive Ones]
tput pla this ati exce cto return its plan Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: I
leetcode 485. Max Consecutive Ones
number digi int leetcode ati digits xpl exp not Given a binary array, find the maximum number of consecutive 1s in this array. Example 1
Leetcode刷題記錄[java]——485 Max Consecutive Ones
遍歷數組 刷題 color ati turn res positive pla str 一、前言 二、題485 Max Consecutive Ones Given a binary array, find the maximum number of consecut
Max Consecutive Ones——leetcode
== ati 直觀 his bin positive nes += ons Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1
[LeetCode] Max Consecutive Ones 最大連續1的個數
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two
[LeetCode] Max Consecutive Ones II 最大連續1的個數之二
Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at most one 0. Example 1: Input: [1,0,1,1,0] Output: 4
[LeetCode] 1004. Max Consecutive Ones III
case may pre get pointer 1-1 element etc lease Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the lengt
【leetcode】1004. Max Consecutive Ones III
int 2-2 block [] value 出現 change bsp 子數組 題目如下: Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the le
LeetCode 28 Divide Two Integers
範圍 long mod max article edi 優化 指數 故障 Divide two integers without using multiplication, division and mod operator. 思路:1.先將被除數和除數轉化為long的
485. Max Consecutive Ones (最大連續數) by Python
大連 put int statistic inpu one emp bin 簡單 485. Max Consecutive Ones 題目: Given a binary array, find the maximum number of consecutive 1s in
leetcode筆記:Longest Substring Without Repeating Characters
time addclass archive 使用 track hive function dsm views 一. 題目描寫敘述 Given a string, find the length of the longest substring with
leetcode 29. Divide Two Integers
log 線性 復雜度 viso -- iso 左移 最大 組合 數值處理的題目,有兩點要考慮的地方: 正負號的問題,對於正數除正數, 負數除正數。。。。 處理越界的問題。 解決方案:加減法,最簡單的方法是用被除數一直減去除數,直到為0, 但是復雜度高。 優化
[Coding Made Simple] Number without consecutive 1s in binary representation
repr res fib sent num urn ber log clas Given a number n, find the total number of numbers from 0 to 2^n - 1 which do not have consecutive
Max Consecutive Ones
desc posit 統計次數 計算 input public arr col amp Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: In
leetcode -- Algorithms -- 3_ Longest Substring Without Repeating Characters
style def mut int leet bsp bstr har erro from itertools import permutations class Solution(object): def lengthOfLongestSubstring(
Python 解leetcode:3. Longest Substring Without Repeating Characters
bject tco 存在 leetcode left str block 出現 subst 題目描述:求一個字符串的不含重復字符的最長連續子串的長度; 思路: 使用一個哈希表保存字符出現的位置; 使用left和right分別表示子串的最左和最右字符的下標; 遍歷字符串