小编Ido*_*dos的帖子

在数组中查找两个非后续元素,其总和最小

简介:据我所知,此问题尚未在SO中提出.
这是一个面试问题.
我甚至没有专门寻找代码解决方案,任何算法/伪代码都可以工作.


问题:给定一个整数数组int[] A及其大小N,找到2个非后续(在数组中不能相邻)元素的总和最小.答案也必须不包含第一个或最后一个元素(索引0n-1).解决方案也应该是O(n)时间和空间的复杂性.

例如,当A = [5, 2, 4, 6, 3, 7]答案是5,从那以后2+3=5.
如果A = [1, 2, 3, 3, 2, 1]答案是4,因为2+2=4你不能选择使用的的1的,因为是在阵列的两端.


尝试:起初我认为解决方案中的一个数字必须是数组中最小的数字(除了第一个和最后一个),但这很快反驳了反例
A = [4, 2, 1, 2, 4] -> 4 (2+2)

然后我想如果我找到数组中的2个最小数字(除了第一个和最后一个),解决方案将是那两个.这显然很快就失败了,因为我不能选择2个相邻的数字,如果我必须选择不相邻的数字,那么这就是问题的定义:).

最后我想,好吧,我将在数组中找到3个最小的数字(除了第一个和最后一个),解决方案必须是其中的两个,因为其中两个必须不相互相邻.这失败了A = [2, 2, …

java arrays algorithm minimum time-complexity

26
推荐指数
6
解决办法
2891
查看次数

Go lang的多态性

我正在学习go lang,我想知道是否有办法做这样的事情:

type Foo struct {
   ...
}

type Bar struct {
   Foo
   ...
}

func getFoo() Foo {
   return Bar{...}
}
Run Code Online (Sandbox Code Playgroud)

在面向对象的语言中,这样的代码应该没有问题,但是在它中它会抛出一个错误,说getFoo()必须返回类Foo的实例.

有没有办法做多边形,类似于我在Go中所描述的?

oop polymorphism go

13
推荐指数
2
解决办法
6649
查看次数

PyVmomi使用未连接的dv添加NIC('config.distributedVirtualSwitch'未设置)

我使用下面的代码来添加配置DistributedVirtualSwitch到现有VM 的NIC (通过pyVmomi):

def __AddNIC(si, vmconf_dict, network_name):
    vm = __get_vm(si, vmconf_dict)
    print " Network label : " + network_name

    devices = []
    nicspec = vim.vm.device.VirtualDeviceSpec()
    nicspec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
    nicspec.device = vim.vm.device.VirtualVmxnet3()
    nicspec.device.wakeOnLanEnabled = True
    nicspec.device.deviceInfo = vim.Description()
    nicspec.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo()
    nicspec.device.connectable.startConnected = True
    nicspec.device.connectable.allowGuestControl = True

    network_objref = _get_mor_by_property(si, vim.dvs.DistributedVirtualPortgroup, network_name)
    dswitch_port_connection = vim.dvs.PortConnection(
        portgroupKey=network_objref.key,
        switchUuid=network_objref.config.distributedVirtualSwitch.uuid
    )
    nicspec.device.backing = vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo()
    nicspec.device.backing.port = dswitch_port_connection

    devices.append(nicspec)
    vmconf = vim.vm.ConfigSpec(deviceChange=devices)
    task = vm.ReconfigVM_Task(vmconf)
    tasks.wait_for_tasks(si, [task])
Run Code Online (Sandbox Code Playgroud)

我收到以下异常:

switchUuid = network_objref.config.distributedVirtualSwitch.uuid …

python vmware nic vcenter pyvmomi

13
推荐指数
1
解决办法
1592
查看次数

ImportError:无法导入名称泛型

我在django 1.9中使用eav-django(entity-attribute-value).每当我执行命令时./manage.py runserver,我都会收到错误:

Unhandled exception in thread started by <function wrapper at 0x10385b500>
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/Django-1.9-py2.7.egg/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/Django-1.9-py2.7.egg/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/Library/Python/2.7/site-packages/Django-1.9-py2.7.egg/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/Library/Python/2.7/site-packages/Django-1.9-py2.7.egg/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/Django-1.9-py2.7.egg/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Library/Python/2.7/site-packages/Django-1.9-py2.7.egg/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Library/Python/2.7/site-packages/Django-1.9-py2.7.egg/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name) …
Run Code Online (Sandbox Code Playgroud)

python generics django django-1.9

13
推荐指数
1
解决办法
1万
查看次数

REST:返回复杂的嵌套数据与多个调用

我有一个由NodeJS服务的Ang api到AngularJS前端.

我合作users:

GET  /api/users  #Returns all users
POST /api/users  #Create  new user

GET   /api/users/:id   #Return a user
PUT   /api/users/:id   #Edit a user
DELTE /api/users/:id   #Delete a user
Run Code Online (Sandbox Code Playgroud)

这是一个用户:

{
  login : "admin"
  email : "admin@admin.com"
  pass  : "hashedpassword"
  ...
}
Run Code Online (Sandbox Code Playgroud)

我的用户可以属于 groups

GET   /api/users/:id/groups   #Return the groups of a user
Run Code Online (Sandbox Code Playgroud)

他们也可以拥有constraints或可以继承constraints他们的团体

GET   /api/users/:id/constraints   #Return the constraints of a user
GET   /api/groups/:id/constraints   #Return the constraints of a group
Run Code Online (Sandbox Code Playgroud)

问题 :

我正在创建一个管理页面,显示所有用户,他们的组,他们的约束.

我是不是该 :

  1. 在javascript(Angular)前端的 …

javascript api rest node.js angularjs

11
推荐指数
4
解决办法
6056
查看次数

yum install mongodb 3.2失败

我正在尝试在CentOS 7机器上安装mongodb 3.2并面临查找软件包的问题.

我根据文档更新了repo文件:

[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
gpgcheck=0
enabled=1
Run Code Online (Sandbox Code Playgroud)

运行时sudo yum install mongodb-org我收到此错误:

[centos@ip-10-24-1-228 ~]$ sudo yum install mongodb-org
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.osuosl.org
 * epel: linux.mirrors.es.net
 * extras: mirror.lax.hugeserver.com
 * updates: mirror.hmc.edu
No package mongodb-org available.
Error: Nothing to do
Run Code Online (Sandbox Code Playgroud)

为什么我收到此消息?

centos yum mongodb

11
推荐指数
3
解决办法
2万
查看次数

相当于Python中R的source()

就像我们在R studio 中的另一个程序中source()执行R程序的功能一样R,如何在另一个python程序中执行python程序?

python execute

11
推荐指数
1
解决办法
3779
查看次数

assertEquals精度

我对delta/precision的困惑感到困惑assertEquals.我明白这0.034会给我分区代码的精度,如下所示:

public void testDivide() {
        assertEquals(3.0, Arithmetic.divide(12.0, 4.0), 0.0);
        assertEquals(3.3, Arithmetic.divide(10.0, 3.0), 0.034);

        //fail("Not yet implemented");
    }
Run Code Online (Sandbox Code Playgroud)

但是,我试图将其更改为0.03,测试失败.另一方面,当我将其更改为0.04成功时,或者即使我将其更改为0.034444等等,它也会成功.我可以知道数字是什么意思,我们如何使用它?

java precision junit assert

9
推荐指数
1
解决办法
1282
查看次数

Angular2打字稿 - 打印漂亮的XML

我从服务器获得以下XML字符串:

<find-item-command xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" find-method="Criteria" item-class="com" only-id="false" xsi:schemaLocation=""> <criteria> <criterion> <descriptors> <descriptor>DPSystemEventItem</descriptor> </descriptors> <value>cluster.manager.active</value> </criterion> </criteria> </find-item-command>
Run Code Online (Sandbox Code Playgroud)

但我想在我的模块中美化它:

<find-item-command
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" find-method="Criteria" item-class="com" only-id="false" xsi:schemaLocation="">
    <criteria>
        <criterion>
            <descriptors>
                <descriptor>DPSystemEventItem</descriptor>
            </descriptors>
            <value>cluster.manager.active</value>
        </criterion>
    </criteria>
</find-item-command>
Run Code Online (Sandbox Code Playgroud)

打印美化的最佳方式是什么?

javascript xml pretty-print typescript angular

9
推荐指数
1
解决办法
5533
查看次数

Java` this`在继承情况下实际指的是什么?

为什么以下Java代码会产生:

10
superclass
Run Code Online (Sandbox Code Playgroud)

有问题的代码是:

class SuperClass {
    int a;

    public SuperClass() {
        this.a = 10;
    }

    private void another_print() {
        System.out.println("superclass");
    }

    public void print() {
        System.out.println(this.a);
        this.another_print();
    }
}

class SubClass extends SuperClass {
    int a;

    public SubClass() {
        this.a = 20;
    }

    private void another_print() {
        System.out.println("subclass");
    }

    public void print() {
        super.print();
    }
}

public class Main {
    public static void main (String[] args) {
        SubClass c = new SubClass();
        c.print();
    }
}
Run Code Online (Sandbox Code Playgroud)

没有SuperClass创造过的实例,不存在吗?不仅Java开始寻找从中调用的方法SuperClass …

java oop inheritance constructor this

8
推荐指数
2
解决办法
371
查看次数