先上编程题
倒计时
解答:
自己的实现(有bug)
function second(second) {
var days = parseInt(second / 1000 / 60 / 60 / 24 - 1, 10); //计算剩余的天数
var hours = parseInt(second / 1000 / 60 / 60 % 24, 10); //计算剩余的小时
var minutes = parseInt(second / 1000 / 60 % 60, 10); //计算剩余的分钟
var seconds = parseInt(second / 1000 % 60, 10); //计算剩余的秒数
days = checkTime(days);
hours = checkTime(hours);
minutes = checkTime(minutes);
seconds = checkTime(seconds);
return {
"day": days,
"hour": hours,
"min": minutes,
"second": seconds
}
}
function checkTime(i) { //将0-9的数字前面加上0,例1变为01
if (i < 10) {
i = "0" + i;
i = parseInt(i);
}
return i;
}
second(200000);
同学的实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.hide {
display: none;
}
</style>
</head>
<body>
<div id="jsCountdown">
<span>01天</span>
<span>02:</span>
<span>03:</span>
<span>04</span>
</div>
<script>
function second(second) {
var now = new Date();
now.setTime(0);
second = second - 1000;
var future = new Date();
future.setTime(second);
var day1 = future.getDate();
var hour1 = future.getHours();
var min1 = future.getMinutes();
var sec1 = future.getSeconds();
var day2 = now.getDate();
var hour2 = now.getHours();
var min2 = now.getMinutes();
var sec2 = now.getSeconds();
var day = parseInt(day1 - day2);
var hour = parseInt(hour1 - hour2);
var min = parseInt(min1 - min2);
var second = parseInt(sec1 - sec2);
return {
day: day,
hour: hour,
min: min,
second: second
}
}
function render(data) {
var spanArr = document.querySelectorAll("#jsCountdown span");
console.log(spanArr)
var daySpan = spanArr[0];
var hourSpan = spanArr[1];
var minSpan = spanArr[2];
var secondSpan = spanArr[3];
function parseTime(num) {
if (num < 10) {
return '0' + num;
}
return num;
}
if (data.day == 0) {
var div = document.querySelector("#jsCountdown");
div.className = "hide";
return;
}
daySpan.innerHTML = parseTime(data.day) + '天';
hourSpan.innerHTML = parseTime(data.hour);
minSpan.innerHTML = parseTime(data.min);
secondSpan.innerHTML = parseTime(data.second);
}
render(second(3600))
</script>
</body>
</html>
字典序
java实现:
public class Main {
public static String getMaxString(String s, String t) {
if(t == null || t.length() <= 0) {
return "";
}
StringBuilder sb = new StringBuilder();
char[] sArray = s.toCharArray();
char[] tArray = t.toCharArray();
// 因为排序是升序的,所以 tArray 从后面开始
Arrays.sort(tArray);
int i = 0;
int j = tArray.length - 1;
for(; i < sArray.length && j >= 0; i++) {
if(sArray[i] >= tArray[j]) {
sb.append(sArray[i]);
} else {
sb.append(tArray[j--]);
}
}
for(; i < sArray.length; i++) {
sb.append(sArray[i]);
}
for(; j >= 0; j--) {
sb.append(tArray[j]);
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(getMaxString("fedcba", "ee"));
}
}
前端(有bug)
function getMaxString(s,t) {
if (t == null || t.length() <= 0) {
return "";
}
var sb;
var sArray = [];
var tArray = [];
for(var i = 0;i<s.length;i++){
sArray[i].push(s.charAt(i));
}
for(var i = 0;i<t.length;i++){
sArray[i].push(t.charAt(i));
}
// 因为排序是升序的,所以 tArray 从后面开始
Arrays.sort(tArray);
var i = 0;
var j = tArray.length - 1;
for (; i < sArray.length && j >= 0; i++) {
if (sArray[i] >= tArray[j]) {
sb.append(sArray[i]);
} else {
sb.append(tArray[j--]);
}
}
for (; i < sArray.length; i++) {
sb.append(sArray[i]);
}
for (; j >= 0; j--) {
sb.append(tArray[j]);
}
return sb.toString();
}
getMaxString("erfsdaf","asfasd");
问答题
JSONP和CORS都可以实现跨域访问,那服务器是否都可以解析到它们携带的cookie
浏览器发送第一次跨域请求时,如何知道服务器是否允许携带Cookie