[英]Find consecutive number of 1's
我試圖在二進制中找到連續1的數量。
示例:將十進制數轉換為二進制數並找到連續的1
static int count = 0;
static int max = 0;
static int index = 1;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
scan.close();
String b = Integer.toBinaryString(n);
char[] arr = b.toCharArray();
System.out.println(arr);
for (int i = 0; i < b.length(); i++) {
if (arr[i] == index) {
count++;
} else {
count = 0;
}
if (count > max) {
max = count;
}
}
System.out.println(max);
}
我總是得到0.似乎條件在我的代碼中不起作用。 你能否提出你對我在哪里出錯的建議?
你的問題不是很清楚,但是算法中的AFAIU,你試圖找到最重復的1的數量。 問題是當你進行比較if (arr[i] == index)
,比較是用char和整數完成的,因為arr的類型是char數組。 不是嗎? 要克服它,您可以將char array
轉換為整數或將整數index
值轉換為char
。 我這樣做是為了克服它。
if (arr[i] == index + '0')
這不是一個非常優雅的解決方案。 我假設你是一名學生,並希望你表明錯誤。 如果我想做這樣的事情,我會用,
private static int maxConsecutiveOnes(int x) {
// Initialize result
int count = 0;
// Count the number of iterations to
// reach x = 0.
while (x!=0) {
// This operation reduces length
// of every sequence of 1s by one.
x = (x & (x << 1));
count++;
}
return count;
}
它的訣竅是,
11101111 (x)
& 11011110 (x << 1)
----------
11001110 (x & (x << 1))
^ ^
| |
trailing 1 removed
正如我所理解的那樣,您希望在int
值的二進制表示中計算1
的組的最大長度。 例如,對於7917=0b1111011101101
結果將是4
(我們有以下組: 1
:1,2,3,4)。
您可以使用位操作(並避免字符串轉換)。 你有一個計數器(計算量1
當前組)和max
最大的全部金額的。 您只需將最低位檢查為1
然后將值向右旋轉直到它變為0
,就像getMaxConsecutiveSetBit1
一樣。
或者只是以一種非常簡單的方式進行 - 將其轉換為二進制字符串並計算其中1
字符的數量,如getMaxConsecutiveSetBit2
。 還有一個計數器+最大值。 不要忘記,Java中的char
是JVM級別的int
。 因此比較char
與int
值1
沒有編譯問題,但這是錯誤的。 要檢查字符是否為1
,您必須使用字符 - '1'
。
public static void main(String[] args) {
try (Scanner scan = new Scanner(System.in)) {
int val = scan.nextInt();
System.out.println(Integer.toBinaryString(val));
System.out.println(getMaxConsecutiveSetBit1(val));
System.out.println(getMaxConsecutiveSetBit2(val));
}
}
public static int getMaxConsecutiveSetBit1(int val) {
int max = 0;
int cur = 0;
while (val != 0) {
if ((val & 0x1) != 0)
cur++;
else {
max = Math.max(max, cur);
cur = 0;
}
val >>>= 1;
}
return Math.max(max, cur);
}
public static int getMaxConsecutiveSetBit2(int val) {
int max = 0;
int cur = 0;
for (char ch : Integer.toBinaryString(val).toCharArray()) {
if (ch == '1')
cur++;
else {
max = Math.max(max, cur);
cur = 0;
}
}
return Math.max(max, cur);
}
將index
變量的類型從int
更改為char
:
static char index = 1;
讓比較在這一行:
if (arr[i] == index)
做好自己的工作。 比較int 1
(在你的代碼中這是存儲在index
變量中的值)和char '1'
(在你的例子中它是arr[]
的當前檢查元素)檢查給定char
ASCII代碼是否等於int
值1。比較永遠不會是真的,因為char'1'具有ASCII碼49,這是與值1進行比較的值(49永遠不等於1)。
您可能希望查看Web中的ASCII代碼表,以查看其中的所有字符都已分配了相應的數值。 您需要注意,在將char
與int
與==
operaror進行比較時會考慮這些值。
當您將提到的index
類型更改為char
,比較工作正常,您的代碼似乎是固定的。
使用for循環結構並更改一些內容,同時添加一些其他統計信息以報告哪些內容可能很有用。 我計算數字中1的總數,連續1的數量(1的組)和連續1的最大數量。 你的for循環也是基於字符串長度循環而不是數組的長度,這只是一種挑剔。 這是代碼
int count = 0;
int max = 0;
char index = '1';
int consecutiveOnePairs = 0;
int numberOfOnes = 0;
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
String b = Integer.toBinaryString(n);
char[] arr = b.toCharArray();
System.out.println(arr);
for (int i = 0; i < arr.length; i++) {
if (arr[i] == index)
{
count++;
numberOfOnes++;
}
if((i + 1 == arr.length && count > 1) || arr[i] != index)
{
if(count > 1)
consecutiveOnePairs++;
if (count > max)
max = count;
count = 0;
}
}
System.out.println("Total Number of 1's in " + n + " is " + numberOfOnes);
System.out.println("Total Number of Consecutive 1's in " + n + " is " + consecutiveOnePairs);
System.out.println("Greatest Number of Consecutive 1's in " + n + " is " + max);
scan.close();
產量
13247
11001110111111
Total Number of 1's in 13247 is 11
Total Number of Consecutive 1's in 13247 is 3
Greatest Number of Consecutive 1's in 13247 is 6
511
111111111
Total Number of 1's in 511 is 9
Total Number of Consecutive 1's in 511 is 1
Greatest Number of Consecutive 1's in 511 is 9
887
1101110111
Total Number of 1's in 887 is 8
Total Number of Consecutive 1's in 887 is 3
Greatest Number of Consecutive 1's in 887 is 3
如果您使用Java 8,則可以嘗試以下代碼段:
public int maxOneConsecutive(int x)
{
String intAsBinaryStr = Integer.toBinaryString(x);
String[] split = intAsBinaryStr.split("0");
return Arrays.stream(split)
.filter(str -> !str.isEmpty())
.map(String::length)
.max(Comparator.comparingInt(a -> a)).orElse(0);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.