我正在研究一个难题,包括分析所有大小的k子集并找出哪一个是最优的.我写了一个解决方案,当子集的数量很少时可以工作,但是对于更大的问题,它会耗尽内存.现在我正在尝试将用python编写的迭代函数转换为java,以便我可以在创建时分析每个子集,只获得表示它是如何优化的值,而不是整个集合,这样我就不会用完记忆.到目前为止,这是我所拥有的,即使是非常小的问题,它似乎也没有完成:
public static LinkedList<LinkedList<Integer>> getSets(int k, LinkedList<Integer> set)
{
int N = set.size();
int maxsets = nCr(N, k);
LinkedList<LinkedList<Integer>> toRet = new LinkedList<LinkedList<Integer>>();
int remains, thresh;
LinkedList<Integer> newset;
for (int i=0; i<maxsets; i++)
{
remains = k;
newset = new LinkedList<Integer>();
for (int val=1; val<=N; val++)
{
if (remains==0)
break;
thresh = nCr(N-val, remains-1);
if (i < thresh)
{
newset.add(set.get(val-1));
remains --;
}
else
{
i -= thresh;
}
}
toRet.add(newset);
}
return toRet;
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我调试这个函数或建议另一个迭代生成大小k子集的算法吗?
编辑:我终于使这个功能工作,我不得不创建一个新的变量,与我做的相同,我和thresh比较,因为python处理循环索引不同.
我正在尝试创建SQL Select,它根据字段返回某个字段的计数.所以,这就是我想要做的.
Select count(distinct id) as TotalCount, -- this will be the total of id
count(distinct id where type='A') as TotalA, -- this will be total when type='A'
count(distinct id where type='B') as TotalB -- This will be total when type = 'B'
from MyTable
Run Code Online (Sandbox Code Playgroud)
基本上,TotalCount = TotalA + TotalB.
如何在SQL Select语句中实现此目的?谢谢.
我正在尝试创建一个XML模式,可以捕获看起来像这样的XML:
<tagname description="simple string type attribute">
false <!-- simple boolean type -->
</tagname>
Run Code Online (Sandbox Code Playgroud)
但我遇到了困难.甚至可以定义一个模式来捕获它,还是我在进行狙击?
如果警报值未定义,如何阻止Javascript警报触发?换句话说,这样的事情:
if (alert(message) != 'undefined') {
alert(message);
}
Run Code Online (Sandbox Code Playgroud) 我有两个模型,Item和ShopSection.他们有多对多的关系.
@Entity(name = "item")
public class Item extends Model
{
@ManyToMany(cascade = CascadeType.PERSIST)
public Set<ShopSection> sections;
}
@Entity(name = "shop_section")
public class ShopSection extends Model
{
public List<Item> findActiveItems(int page, int length)
{
return Item.find("select distinct i from Item i join i.sections as s where s.id = ?", id).fetch(page, length);
}
}
Run Code Online (Sandbox Code Playgroud)
findActiveItems 是为了找到一个部分中的项目,但我收到此错误:
org.hibernate.hql.ast.QuerySyntaxException: Item is not mapped [select distinct i from Item i join i.sections as s where s.id = ?]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:111)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93)
at …Run Code Online (Sandbox Code Playgroud) 我的视图不是强类型视图,我需要在控制器操作中迭代Request Params来确定发布的值.
有没有更好的方法来遍历nameValueCollection AllKeys?
我目前正在循环请求参数并适当地设置值.
foreach (var key in Request.Params.AllKeys)
{
if (key.Equals("CustomerId"))
queryObject.CustomerId = Request.Params[key];
else if (key.Equals("OrderId"))
queryObject.OrderId= Request.Params[key];
//and so on
}
Run Code Online (Sandbox Code Playgroud)
我在这段代码中看到了相当多的重复.有没有更好的方法来处理这个?
我是一个新的程序员,我正在处理的代码有几个问题.
基本上代码所做的是从另一个JSP接收表单,读取字节,解析数据,并使用DataInputStream将结果提交给SalesForce.
//Getting the parameters from request
String contentType = request.getContentType();
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
//System.out.println(formDataLength);
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength)
{
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但只有代码处理正常字符.每当它试图处理特殊字符(如法语字符:àâäæçéèêëîïôùûü)时,我会得到以下乱码:
ÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃ
我知道它可能是DataInputStream的问题,以及它如何不返回UTF-8编码的文本.你们对如何解决这个问题提出任何建议吗?
所有.jsp文件都包含<%@ page pageEncoding ="UTF-8"contentType ="text/html; charset = UTF-8"%>并且Tomcat的设置很好(URI = UTF-8等).我尝试添加:
request.setCharacterEncoding("UTF-8");
和
response.setCharacterEncoding("UTF-8");
无济于事.
以下是解析数据的示例:
//Getting the notes for the Case
String notes = new String(dataBytes);
System.out.println(notes);
String savenotes …Run Code Online (Sandbox Code Playgroud) 我想从私有方法调用公共方法,但属性"this"指向窗口对象.
请注意我正在尝试应用模块模式.您可以在jsfiddle.net上找到一个有效的代码示例
// how can i access a public method from a private one?
// (in this example publicAlert from privateMethod)
// this refers to the window object.
$(function() {
var modulePattern = (function($)
{
var privateMethod = function()
{
appendText("called privateMethod()");
this.publicAlert();
};
var appendText = function(texToAppend)
{
var text = $('#output').text() + " | " + texToAppend;
$('#output').text(text);
};
return {
publicMethod : function()
{
appendText("called publicMethod()");
privateMethod();
},
publicAlert : function()
{
alert("publicAlert");
}
}; …Run Code Online (Sandbox Code Playgroud) 我正在为将用C++或C#编写的软件设计GUI.我的合作伙伴和我想首先在浏览器中测试GUI,然后导入到C++或C#.
是否存在将CSS/HTML/jQuery代码转换为C++或C#代码的C++或C#库?
只是想知道是否有可能以某种方式使CSS content属性插入html代码而不是字符串:before或:after类似的元素:
.header:before{
content: '<a href="#top">Back</a>';
}
Run Code Online (Sandbox Code Playgroud)
这将是非常方便的...它可以通过Javascript完成,但使用CSS这将真正让生活更轻松:)
java ×3
c# ×2
css ×2
html ×2
javascript ×2
jquery ×2
asp.net ×1
asp.net-mvc ×1
c++ ×1
css-content ×1
hibernate ×1
jpa ×1
many-to-many ×1
sql ×1
sql-server ×1
subset ×1
utf-8 ×1
xml ×1
xsd ×1