Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
NoComments
leetcode
提交
0e078c55
L
leetcode
项目概览
NoComments
/
leetcode
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
L
leetcode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0e078c55
编写于
3月 05, 2019
作者:
L
liu13
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
20190305
上级
0765a89f
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
194 addition
and
2 deletion
+194
-2
code/lc344.java
code/lc344.java
+21
-0
code/lc378.java
code/lc378.java
+50
-2
code/lc380.java
code/lc380.java
+56
-0
code/lc384.java
code/lc384.java
+35
-0
code/lc454.java
code/lc454.java
+31
-0
code/lc76.java
code/lc76.java
+1
-0
未找到文件。
code/lc344.java
0 → 100644
浏览文件 @
0e078c55
package
code
;
/*
* 344. Reverse String
* 题意:反转字符串
* 难度:Easy
* 分类:Two Pointers, String
* 思路:
* Tips:
*/
public
class
lc344
{
public
void
reverseString
(
char
[]
s
)
{
int
begin
=
0
,
end
=
s
.
length
-
1
;
while
(
begin
<
end
){
char
ch
=
s
[
begin
];
s
[
begin
]
=
s
[
end
];
s
[
end
]
=
ch
;
begin
++;
end
--;
}
}
}
code/lc378.java
浏览文件 @
0e078c55
package
code
;
import
java.util.Comparator
;
import
java.util.PriorityQueue
;
/*
* 378. Kth Smallest Element in a Sorted Matrix
* 题意:在矩阵中搜索第k大的数,横轴和纵轴都是有序的
* 难度:Medium
* 分类:Binary Search, Heap
* 思路:
* 思路:两种思路。 1是类似多个有序链表合并的思路,优先队列。
* 2是二分,二分的是val,看比这个val小的数是不是k
* Tips:lc23方法很像
*/
public
class
lc378
{
class
Cell
{
int
val
,
row
,
col
;
Cell
(
int
v
,
int
r
,
int
c
){
val
=
v
;
row
=
r
;
col
=
c
;
}
}
public
int
kthSmallest
(
int
[][]
matrix
,
int
k
)
{
PriorityQueue
<
Cell
>
pq
=
new
PriorityQueue
(
new
Comparator
<
Cell
>()
{
@Override
public
int
compare
(
Cell
o1
,
Cell
o2
)
{
return
o1
.
val
-
o2
.
val
;
}
});
for
(
int
i
=
0
;
i
<
matrix
.
length
;
i
++)
pq
.
add
(
new
Cell
(
matrix
[
i
][
0
],
i
,
0
));
while
(
k
>
1
){
Cell
c
=
pq
.
remove
();
if
(
c
.
col
+
1
<
matrix
[
0
].
length
)
pq
.
add
(
new
Cell
(
matrix
[
c
.
row
][
c
.
col
+
1
],
c
.
row
,
c
.
col
+
1
));
k
--;
}
return
pq
.
remove
().
val
;
}
public
int
kthSmallest2
(
int
[][]
matrix
,
int
k
)
{
int
low
=
matrix
[
0
][
0
];
int
high
=
matrix
[
matrix
.
length
-
1
][
matrix
[
0
].
length
-
1
];
while
(
low
<=
high
){
//确保区间最后缩到0
int
mid
=
low
+(
high
-
low
)/
2
;
int
num
=
getLessNum
(
matrix
,
mid
);
if
(
num
<
k
)
low
=
mid
+
1
;
else
high
=
mid
-
1
;
}
return
low
-
1
;
}
public
int
getLessNum
(
int
[][]
matrix
,
int
val
){
//求矩阵中比这个数小的数的个数
int
res
=
0
;
int
row
=
0
;
while
(
row
<
matrix
.
length
){
int
col
=
0
;
while
(
col
<
matrix
[
0
].
length
&&
matrix
[
row
][
col
]<
val
)
col
++;
res
+=
col
;
row
++;
}
return
res
;
}
}
code/lc380.java
0 → 100644
浏览文件 @
0e078c55
package
code
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.List
;
/*
* 380. Insert Delete GetRandom O(1)
* 题意:设计一个数据结构,插入,删除,随机获得一个元素 这三个操作的复杂度都为O(1)
* 难度:Medium
* 分类:Array, Hash Table, Design
* 思路:List 的插入和删除都是O(1), 通过hashmap绑定来使得Get也为O(1)
* Tips:
*/
public
class
lc380
{
public
class
RandomizedSet
{
HashMap
<
Integer
,
Integer
>
valToInd
;
List
<
Integer
>
list
;
int
ind
=
0
;
/** Initialize your data structure here. */
public
RandomizedSet
()
{
valToInd
=
new
HashMap
<>();
list
=
new
ArrayList
<>();
}
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
public
boolean
insert
(
int
val
)
{
if
(
valToInd
.
containsKey
(
val
))
return
false
;
list
.
add
(
val
);
valToInd
.
put
(
val
,
list
.
size
()-
1
);
return
true
;
}
/** Removes a value from the set. Returns true if the set contained the specified element. */
public
boolean
remove
(
int
val
)
{
int
ind
=
valToInd
.
getOrDefault
(
val
,-
1
);
if
(
ind
==
-
1
)
return
false
;
Collections
.
swap
(
list
,
ind
,
list
.
size
()-
1
);
int
swappedWith
=
list
.
get
(
ind
);
valToInd
.
put
(
swappedWith
,
ind
);
list
.
remove
(
list
.
size
()-
1
);
valToInd
.
remove
(
val
);
return
true
;
}
/** Get a random element from the set. */
public
int
getRandom
()
{
int
max
=
list
.
size
();
int
min
=
0
;
int
ind
=
(
int
)(
Math
.
random
()
*
(
max
-
min
)
+
min
);
return
list
.
get
(
ind
);
}
}
}
code/lc384.java
0 → 100644
浏览文件 @
0e078c55
package
code
;
/*
* 384. Shuffle an Array
* 题意:重排列一个数组,每个值在每个位置的概率都是均匀的
* 难度:Medium
* 分类:
* 思路:
* Tips:
*/
public
class
lc384
{
public
class
Solution
{
private
int
[]
nums
;
public
Solution
(
int
[]
nums
)
{
this
.
nums
=
nums
;
}
/** Resets the array to its original configuration and return it. */
public
int
[]
reset
()
{
return
nums
;
}
/** Returns a random shuffling of the array. */
public
int
[]
shuffle
()
{
int
[]
rand
=
new
int
[
nums
.
length
];
for
(
int
i
=
0
;
i
<
nums
.
length
;
i
++){
int
r
=
(
int
)
(
Math
.
random
()
*
(
i
+
1
));
rand
[
i
]
=
rand
[
r
];
rand
[
r
]
=
nums
[
i
];
}
return
rand
;
}
}
}
code/lc454.java
0 → 100644
浏览文件 @
0e078c55
package
code
;
import
java.util.HashMap
;
/*
* 454. 4Sum II
* 题意:从4个数组中各挑一个数,使得和为0
* 难度:Medium
* 分类:Hash Table, Binary Search
* 思路:自己没想起来,看了答案后感觉很无聊
* 两个集合暴力求出所有和的可能,然后2Sum的思路,利用Hashmap即可
* 其实是利用了二分的思想,把4个数的和变为两个2个数的和
* Tips:
*/
public
class
lc454
{
public
int
fourSumCount
(
int
[]
A
,
int
[]
B
,
int
[]
C
,
int
[]
D
)
{
HashMap
<
Integer
,
Integer
>
hm
=
new
HashMap
();
int
res
=
0
;
for
(
int
i
=
0
;
i
<
A
.
length
;
i
++){
for
(
int
j
=
0
;
j
<
B
.
length
;
j
++){
//从0开始
hm
.
put
(
A
[
i
]+
B
[
j
],
hm
.
getOrDefault
(
A
[
i
]+
B
[
j
],
0
)+
1
);
}
}
for
(
int
i
=
0
;
i
<
C
.
length
;
i
++){
for
(
int
j
=
0
;
j
<
D
.
length
;
j
++){
res
+=
hm
.
getOrDefault
(-
C
[
i
]-
D
[
j
],
0
);
}
}
return
res
;
}
}
code/lc76.java
浏览文件 @
0e078c55
...
...
@@ -6,6 +6,7 @@ package code;
* 分类:Hash Table, Two Pointers, String
* 思路:两个指针,移动右指针使得满足条件,移动左指针缩短距离。用hashmap存储进行判断是否满足条件。
* Tips:很难的题,思路记一下。
* https://leetcode.com/problems/minimum-window-substring/discuss/26808/here-is-a-10-line-template-that-can-solve-most-substring-problems
*/
import
java.util.HashMap
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录