1 | # coding: utf-8 |

1 | # coding: utf-8 |

Difficulty: Medium
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
1 | /** |
Difficulty: Medium
Given a linked list and a value _x_, partition it such that all nodes less than _x_ come before nodes greater than or equal to _x_.
You should preserve the original relative order of the nodes in each of the two partitions.
For example,
Given 1->4->3->2->5->2 and _x_ = 3,
return 1->2->2->4->3->5.
用两个链表分别存储,最后合并
1 | /** |
Difficulty: Hard
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
For example,
Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
Your algorithm should run in O(_n_) complexity.
1 | class Solution { |
Difficulty: Medium
Follow up for “Remove Duplicates”:
What if duplicates are allowed at most twice?
For example,
Given sorted array nums = [1,1,1,2,2,3],
Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn’t matter what you leave beyond the new length.
因为是有序的,所以出现不递增的就是重复的1
2
3
4
5
6
7
8
9
10
11
12
13
14class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length<=2)return nums.length;
int index = 2;
for(int i=2;i<nums.length;i++){
if(nums[i]>nums[index-2]){
nums[index] = nums[i];
index++;
}
}
return index;
}
}
Difficulty: Medium
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
使用有向图遍历,超时1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42class Solution {
public boolean canJump(int[] nums) {
if(null == nums)return false;
int len = nums.length;
if(len==0)return false;
if(len==1)return true;
List<List<Integer>> graph = new ArrayList<>(len);
// 初始化有向图
for(int i=0;i<len-1;i++){
List<Integer> list = new ArrayList<>();
graph.add(list);
int n = nums[i];
for(int j=1;j<=n&&j<=len-1;j++){
list.add(i+j);
}
}
int[] visited = new int[len];
LinkedList<Integer> q = new LinkedList<>();
q.add(0);
while(!q.isEmpty()){
int pos = q.getFirst();
q.removeFirst();
if(visited[pos]==1)continue;
List<Integer> list = graph.get(pos);
//System.out.println(list);
System.out.println(pos);
for(int n:list){
//System.out.print(" "+n);
if(n==len-1)return true;
q.addLast(n);
}
visited[pos]=1;
}
return false;
}
}
直接拓展右边界1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23class Solution {
public boolean canJump(int[] nums) {
if(null == nums)return false;
int len = nums.length;
if(len==0)return false;
if(len==1)return true;
int maxR = 0;
for(int i=0;i<len-1;i++){
if(i<=maxR){
int r = i+nums[i];
if(maxR<r)maxR = r;
if(maxR>=len-1)return true;
}else{
return false;
}
}
return false;
}
}
Difficulty: Medium
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
According to the : “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
1 | _______3______ |
For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
递归解法,这里都是假定p和q是在树中存在的。
1 | /** |
tarjen 算法
Difficulty: Easy
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
According to the : “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
1 | _______6______ |
For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
1 | /** |
洗牌在生活中十分常见,现在需要写一个程序模拟洗牌的过程。 现在需要洗2n张牌,从上到下依次是第1张,第2张,第3张一直到第2n张。首先,我们把这2n张牌分成两堆,左手拿着第1张到第n张(上半堆),右手拿着第n+1张到第2n张(下半堆)。接着就开始洗牌的过程,先放下右手的最后一张牌,再放下左手的最后一张牌,接着放下右手的倒数第二张牌,再放下左手的倒数第二张牌,直到最后放下左手的第一张牌。接着把牌合并起来就可以了。 例如有6张牌,最开始牌的序列是1,2,3,4,5,6。首先分成两组,左手拿着1,2,3;右手拿着4,5,6。在洗牌过程中按顺序放下了6,3,5,2,4,1。把这六张牌再次合成一组牌之后,我们按照从上往下的顺序看这组牌,就变成了序列1,4,2,5,3,6。 现在给出一个原始牌组,请输出这副牌洗牌k次之后从上往下的序列。
第一行一个数T(T ≤ 100),表示数据组数。对于每组数据,第一行两个数n,k(1 ≤ n,k ≤ 100),接下来一行有2n个数a1,a2,…,a2n(1 ≤ ai ≤ 1000000000)。表示原始牌组从上到下的序列。
对于每组数据,输出一行,最终的序列。数字之间用空格隔开,不要在行末输出多余的空格。
3 3 1 1 2 3 4 5 6 3 2 1 2 3 4 5 6 2 2 1 1 1 1
1 4 2 5 3 6 1 5 4 3 2 6 1 1 1 1
1 | import java.util.*; |
1 | if(j<=n-1) |
一开始写反了
小明同学把1到n这n个数字按照一定的顺序放入了一个队列Q中。现在他对队列Q执行了如下程序:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17while(!Q.empty()) //队列不空,执行循环
{
int x=Q.front(); //取出当前队头的值x
Q.pop(); //弹出当前队头
Q.push(x); //把x放入队尾
x = Q.front(); //取出这时候队头的值
printf("%d\n",x); //输出x
Q.pop(); //弹出这时候的队头
}
做取出队头的值操作的时候,并不弹出当前队头。
小明同学发现,这段程序恰好按顺序输出了1,2,3,…,n。现在小明想让你构造出原始的队列,你能做到吗?
第一行一个整数T(T ≤ 100)表示数据组数,每组数据输入一个数n(1 ≤ n ≤ 100000),输入的所有n之和不超过200000。
对于每组数据,输出一行,表示原始的队列。数字之间用一个空格隔开,不要在行末输出多余的空格.
4
1
2
3
10
1
2 1
2 1 3
8 1 6 2 10 3 7 4 9 5
1 | import java.util.*; |
只能过80%,原因是因为get(i)方法每次都要查询遍历,会把时间复杂度变为O(n^2)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
int T;
T = scn.nextInt();
while(T-->0){
int n = scn.nextInt();
LinkedList<Integer> res = new LinkedList<Integer>();
for(int i=n;i>0;i--){
int x = i;
res.addFirst(x);
x = res.removeLast();
res.addFirst(x);
}
for(int i=0;i<n-1;i++){
System.out.print(res.removeFirst()+" ");
}
if(res.size()>0)System.out.println(res.removeFirst());
}
}
}
目前要在两台机器上做负载均衡,按网上的教程写的,一般都是两台机器按一样的配置,或者就是配好后只访问一台,这里总结一下。
以机器1的9000为负载均衡端口,其他机器的8000端口访问本机
1 | upstream myservers{ |
1 | server { |
这样访问 172.16.201.1:9000就可以按访问频率分发到两个机器上了
最好使用firefox,不要用chrome,不要打开开发者工具

tag:
缺失模块。
1、请确保node版本大于6.2
2、在博客根目录(注意不是yilia根目录)执行以下命令:
npm i hexo-generator-json-content --save
3、在根目录_config.yml里添加配置:
jsonContent:
meta: false
pages: false
posts:
title: true
date: true
path: true
text: false
raw: false
content: false
slug: false
updated: false
comments: false
link: false
permalink: false
excerpt: false
categories: false
tags: true