是否可以在一个解析中按顺序排列仅由1和0组成的数组而不使用辅助数组?
例如:假设您有一个数组a[]={1,0,0,0,1,0,1},为此预期的输出将是a[]={1,1,1,0,0,0,0}.
我编写了下面的C代码,但它找到了2个解析的解决方案.可以优化吗?
void arrange(int a[],int n) {
int i,count=0;
for(i=0;i<n;i++) {
if(a[i]==1)
count++;
a[i]=0;
}
for(i=0;i<count;i++) {
a[i]=1;
}
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码.
class Foo(object):
def __init__(self):
self.__baz = 40
def foo(self):
print self.__baz
class Bar(Foo):
def __init__(self):
#super(Bar, self).__init__()
self.__baz = 21
def bar(self):
print self.__baz
x = Bar()
x.foo()
x.bar()
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Traceback (most recent call last):
File "classes.py", line 15, in <module>
x.foo()
File "classes.py", line 5, in foo
print self.__baz
AttributeError: 'Bar' object has no attribute '_Foo__baz'
Run Code Online (Sandbox Code Playgroud)
为什么这个foo方法没有继承Bar.
编辑:它工作正常,如果你打电话给超级评论.
我有一个像这样定义的表:
CREATE TABLE `Message` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`user_id` integer NOT NULL,
`user_to` integer NOT NULL,
`top_num` integer NOT NULL,
`priority` smallint NOT NULL,
`error` varchar(120) NOT NULL,
UNIQUE (`user_id`, `user_to`, `top_num`)
);
Run Code Online (Sandbox Code Playgroud)
后来,我添加了另一个列,msg_type,如下所示:
ALTER TABLE Message ADD COLUMN msg_type SMALLINT(6) NOT NULL DEFAULT 0;
Run Code Online (Sandbox Code Playgroud)
但是,我已经意识到我需要将原始UNIQUE约束更改为包含msg_type.我试过跑步
ALTER TABLE Message
ADD UNIQUE INDEX (`user_id`, `user_to`, `top_num`, `msg_type`);
Run Code Online (Sandbox Code Playgroud)
但INSERT进入我的表仍然失败,错误消息表明这是因为旧的唯一性约束失败.
当我describe Messages在mysql中调用时,我看到以下内容:
+-----------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default …Run Code Online (Sandbox Code Playgroud) 在linux内核中,inlucde/linux/word_at_a_time.h有两个函数:
static inline long find_zero(unsigned long mask)
{
long byte = 0;
#ifdef CONFIG_64BIT
if (mask >> 32)
mask >>= 32;
else
byte = 4;
#endif
if (mask >> 16)
mask >>= 16;
else
byte += 2;
return (mask >> 8) ? byte : byte + 1;
}
static inline bool has_zero(unsigned long val,
unsigned long *data,
const struct word_at_a_time *c)
{
unsigned long rhs = val | c->low_bits;
*data = rhs;
return (val + c->high_bits) & ~rhs;
}
Run Code Online (Sandbox Code Playgroud)
它用在哈希函数中,在git …
我正在比较2段代码.第一
Integer i=3;
Integer j=3;
if(i==j)
System.out.println("i==j"); //prints i==j
Run Code Online (Sandbox Code Playgroud)
第二,
Integer i=3;
Integer j=new Integer(3);
if(i==j)
System.out.println("i==j"); // does not print
Run Code Online (Sandbox Code Playgroud)
我怀疑在第一个片段中为什么i==j要打印?引用不应该不同吗?
我刚刚开始在学校学习C,我正试图掌握基本概念.
我们的作业有一个问题,
为每一个 int x: x+1 > x
确定是真还是假,如果是真的则给出推理,如果为假则给出反例.
我很困惑,因为我们被告知int类型是32位,基本上这意味着整数是二进制格式.x + 1是否将1加1为十进制值?
给定一个二进制矩阵,我找出了所有1s 的最大尺寸方形子矩阵.
例如,考虑以下二进制矩阵:
0 1 1 0 1
1 1 0 1 0
0 1 1 1 0
1 1 1 1 0
1 1 1 1 1
0 0 0 0 0
Run Code Online (Sandbox Code Playgroud)
所有设置位的最大平方子矩阵是
1 1 1
1 1 1
1 1 1
Run Code Online (Sandbox Code Playgroud)
我在网上搜索解决方案,然后找到了构建辅助矩阵的关系:
If M[i][j] is 1 then
S[i][j] = min(S[i][j-1], S[i-1][j], S[i-1][j-1]) + 1
Else /*If M[i][j] is 0*/
S[i][j] = 0
Run Code Online (Sandbox Code Playgroud)
M[][]原始矩阵在哪里,s[][]是辅助矩阵? 如果我在if语句中定义一个数组,那么在编译期间会分配内存,例如.
if(1)
{
int a[1000];
}
else
{
float b[1000];
}
Run Code Online (Sandbox Code Playgroud)
然后2 * 1000为ints + 4 * 1000浮点数的内存分配?
我很确定我的代码是正确的,但它似乎没有返回预期的输出:
输入anti_vowel("Hey look words") - >输出:"Hey lk wrds".
显然它不起作用'e',任何人都可以解释为什么?
def anti_vowel(c):
newstr = ""
vowels = ('a', 'e', 'i', 'o', 'u')
for x in c.lower():
if x in vowels:
newstr = c.replace(x, "")
return newstr
Run Code Online (Sandbox Code Playgroud) c ×6
python ×2
algorithm ×1
arrays ×1
hash ×1
inheritance ×1
java ×1
linux-kernel ×1
matrix ×1
memory ×1
mysql ×1
oop ×1
optimization ×1
sorting ×1