基于该答案,这里有两个版本的merge函数用于mergesort.你能帮我理解为什么第二个更快.我已经测试了它的50000列表,第二个测试速度提高了8倍(Gist).
def merge1(left, right):
i = j = inv = 0
merged = []
while i < len(left) and j < len(right):
if left[i] <= right[j]:
merged.append(left[i])
i += 1
else:
merged.append(right[j])
j += 1
inv += len(left[i:])
merged += left[i:]
merged += right[j:]
return merged, inv
Run Code Online (Sandbox Code Playgroud)
.
def merge2(array1, array2):
inv = 0
merged_array = []
while array1 or array2:
if not array1:
merged_array.append(array2.pop())
elif (not array2) or array1[-1] > array2[-1]:
merged_array.append(array1.pop())
inv += len(array2) …Run Code Online (Sandbox Code Playgroud) 我已经订购了 LocalDateTime 列表,我需要在特定日期的午夜将其拆分。两个新列表中元素的顺序不应更改。
List<LocalDateTime> l = Arrays.asList(LocalDateTime.of(2017,10,24,7,0,0),LocalDateTime.of(2017,10,24,8,0,0),LocalDateTime.of(2017,10,25,7,0,0),LocalDateTime.of(2017,10,25,9,0,0));
Run Code Online (Sandbox Code Playgroud)
Collectors.partitioningBy 是否保证订单将被保留?
l.stream().collect(Collectors.partitioningBy(t-> t.isAfter(LocalDateTime.of(2017,10,25,0,0,0))))
Run Code Online (Sandbox Code Playgroud)
输出:
{false=[2017-10-24T07:00, 2017-10-24T08:00], true=[2017-10-25T07:00, 2017-10-25T09:00]}
Run Code Online (Sandbox Code Playgroud) 我正在使用Net :: SMPP包来处理SMPP协议.其中一个协议参数应为NULL终止字符串.当我发送它时,我做:
my $receipted_message_id = '111111'."\0";
Run Code Online (Sandbox Code Playgroud)
当我收到它时,我剥去了最后一个角色.
有没有更好的方法在Perl中使用"C String"?
我需要将字符串的前8个字符表示为以空格分隔的十六进制数字.
例如: "这是测试!" 转换为"54 68 69 73 20 69 73 20"
我使用以下代码来完成它.在Perl中有更好(更简单)的方法吗?
my $hex = unpack( "H16", $string );
my $hexOut = "";
for ( my $i = 0 ; $i < length($hex) ; $i += 2 )
{
$hexOut .= substr( $hex, $i, 2 ) . " ";
}
$hexOut = substr( $hexOut, 0, -1 );
Run Code Online (Sandbox Code Playgroud)