You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: hello-algo/lagou-algo/leetcode-question/src/main/java/com/shuzijun/leetcode/editor/cn/Lc1206DesignSkiplist.java
+94-48Lines changed: 94 additions & 48 deletions
Original file line number
Diff line number
Diff line change
@@ -75,15 +75,40 @@ public static void main(String[] args) {
75
75
76
76
//leetcode submit region begin(Prohibit modification and deletion)
77
77
classSkiplist {
78
+
78
79
finalintHEAD_VALUE = -1; // 链表头节点的值
79
80
finalNodeHEAD = newNode(HEAD_VALUE);
80
81
81
82
Nodehead; // 最左上角的头节点,所有操作在开始位置
82
83
intlevels; // 当前层级,即 head 节点所在的最高层数
84
+
intlength; // 链表长度
83
85
84
86
publicSkiplist() {
85
87
head = HEAD;
86
88
levels = 1;
89
+
length = 1;
90
+
}
91
+
92
+
publicNodeget(inttarget) {
93
+
returnget(target, head);
94
+
}
95
+
96
+
publicNodeget(inttarget, Nodefrom) {
97
+
Noden = from;
98
+
while (n != null) {
99
+
// 1.在同一层级上向右查找,直到链接结尾,或者找到
100
+
while (n.right != null && n.right.val < target) {
101
+
n = n.right;
102
+
}
103
+
// 2.若找到,返回true
104
+
Noderight = n.right; // 要查找的节点
105
+
if (right != null && right.val == target) {
106
+
returnn; // 返回要查找的节点的前一个
107
+
}
108
+
// 3.若右侧数据较大,向下一层
109
+
n = n.down;
110
+
}
111
+
returnnull;
87
112
}
88
113
89
114
/**
@@ -94,27 +119,30 @@ public Skiplist() {
94
119
* 3.链表结尾,或大于,往下
95
120
*
96
121
* </pre>
122
+
*
97
123
* @param target
98
124
* @return
99
125
*/
100
126
publicbooleansearch(inttarget) {
101
-
Noden = head;
102
-
while (n != null) {
103
-
// 1.在同一层级向右查找,直到链表的结尾
104
-
while (n.right != null && n.right.val < target) {
105
-
n = n.right; // 向右
106
-
}
107
-
// 2.若找到,返回true
108
-
Noderight = n.right; // 要查找的节点
109
-
if(right != null && right.val == target) {
110
-
returntrue;
111
-
}
112
-
113
-
// 3.若右侧数据较大,向下一层
114
-
n = n.down; // 往下
115
-
}
116
-
117
-
returnfalse;
127
+
// Node n = head;
128
+
// while (n != null) {
129
+
// // 1.在同一层级向右查找,直到链表的结尾
130
+
// while (n.right != null && n.right.val < target) {
131
+
// n = n.right; // 向右
132
+
// }
133
+
// // 2.若找到,返回true
134
+
// Node right = n.right; // 要查找的节点
135
+
// if(right != null && right.val == target) {
136
+
// return true;
137
+
// }
138
+
//
139
+
// // 3.若右侧数据较大,向下一层
140
+
// n = n.down; // 往下
141
+
// }
142
+
//
143
+
// return false;
144
+
Nodenode = get(target);
145
+
returnnode != null;
118
146
}
119
147
120
148
/**
@@ -124,6 +152,7 @@ public boolean search(int target) {
0 commit comments