我正在做一个小项目来学习public class Median {
public static void main(String[] s) {
int[] test = {4,18,20,3,7,13,5,8,2,1,15,17,25,30,16};
System.out.println(selectK(test,8));
/*
int n = 100000000;
int[] test = new int[n];
for(int i=0; i<test.length; i++)
test[i] = (int)(Math.random()*test.length);
long start = System.currentTimeMillis();
random_selectK(test, test.length/2);
long end = System.currentTimeMillis();
System.out.println(end - start);
*/
}
public static int random_selectK(int[] a, int k) {
if(a.length <= 1)
return a[0];
int r = (int)(Math.random() * a.length);
int p = a[r];
int small = 0, equal = 0, big = 0;
for(int i=0; i<a.length; i++) {
if(a[i] < p) small++;
else if(a[i] == p) equal++;
else if(a[i] > p) big++;
}
if(k <= small) {
int[] temp = new int[small];
for(int i=0, j=0; i<a.length; i++)
if(a[i] < p)
temp[j++] = a[i];
return random_selectK(temp, k);
}
else if (k <= small+equal)
return p;
else {
int[] temp = new int[big];
for(int i=0, j=0; i<a.length; i++)
if(a[i] > p)
temp[j++] = a[i];
return random_selectK(temp,k-small-equal);
}
}
public static int selectK(int[] a, int k) {
if(a.length <= 5) {
Arrays.sort(a);
return a[k-1];
}
int p = median_of_medians(a);
int small = 0, equal = 0, big = 0;
for(int i=0; i<a.length; i++) {
if(a[i] < p) small++;
else if(a[i] == p) equal++;
else if(a[i] > p) big++;
}
if(k <= small) {
int[] temp = new int[small];
for(int i=0, j=0; i<a.length; i++)
if(a[i] < p)
temp[j++] = a[i];
return selectK(temp, k);
}
else if (k <= small+equal)
return p;
else {
int[] temp = new int[big];
for(int i=0, j=0; i<a.length; i++)
if(a[i] > p)
temp[j++] = a[i];
return selectK(temp,k-small-equal);
}
}
private static int median_of_medians(int[] a) {
int[] b = new int[a.length/5];
int[] temp = new int[5];
for(int i=0; i<b.length; i++) {
for(int j=0; j<5; j++)
temp[j] = a[5*i + j];
Arrays.sort(temp);
b[i] = temp[2];
}
return selectK(b, b.length/2 + 1);
}
}
,我在将Knockoutjs
转移到服务器时遇到了问题。
基本上我有以下代码: -
date
现在 self.loanBookSubmit = function (formElement) {
var loannee = {
BookId: this.loanedBook.Id,
Date: ko.observable(moment(self.newLoanBook.CurrentDate()).format('DD-MM-YYYY')),
Name: self.newLoanBook.Name(),
Surname: self.newLoanBook.Surname(),
Comments: self.newLoanBook.Comments()
};
console.log(loannee);
ajaxHelper(loansUri, 'POST', loannee).done(function (item) {
console.log(item);
self.books.push(item);
});
}
,日期是正确的,但是当我将其转移到我的api时,我得到一个空白日期console.log(loanee)
。
有人可以帮助我并告诉我我做错了吗?
答案 0 :(得分:0)
你能试试吗
var loannee = { BookId:this.loanedBook.Id, 日期:新日期(self.newLoanBook.CurrentDate()。getFullYear(),self.newLoanBook.CurrentDate()。getMonth()+ 1,self.newLoanBook.CurrentDate()。getDate()), 名称:self.newLoanBook.Name(), 姓:self.newLoanBook.Surname(), 评论:self.newLoanBook.Comments() };
答案 1 :(得分:0)
变量的名称必须与对象中参数的名称相同,并且还需要删除ko.observable()。
所以正确的语法应该是: -
DateLoaned: moment(self.newLoanBook.DateLoaned()).format('DD-MM-YYYY'),