打印二进制排列的最快方法是什么?
如果n = 3和r = 2,则在下面打印
110
101
011
我使用以下代码,但需要很长时间。
public void printBinaryPermutation(int n, int r)
{
for(BigInteger i = new BigInteger("0"); i.compareTo(BigInteger.TWO.pow(n)) == -1; i = i.add(BigInteger.ONE))
{
String str = i.toString(2);
while(str.length() < n)
{
str = "0" + str;
}
BigInteger amount = new BigInteger("0");
for(char c : str.toCharArray())
{
if(c == '1')
{
amount = amount.add(BigInteger.ONE);
}
}
if(amount.compareTo(new BigInteger(String.valueOf(r))) == 0)
{
System.out.println(str);
}
}
}
打印二进制排列的最快方法是什么?
If n=3 and r=2, print below
110
101
011
I'm using below code, But it takes a long time.
public void printBinaryPermutation(int n, int r)
{
for(BigInteger i = new BigInteger("0"); i.compareTo(BigInteger.TWO.pow(n)) == -1; i = i.add(BigInteger.ONE))
{
String str = i.toString(2);
while(str.length() < n)
{
str = "0" + str;
}
BigInteger amount = new BigInteger("0");
for(char c : str.toCharArray())
{
if(c == '1')
{
amount = amount.add(BigInteger.ONE);
}
}
if(amount.compareTo(new BigInteger(String.valueOf(r))) == 0)
{
System.out.println(str);
}
}
}
What is the quickest way to print Binary permutation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论