Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
NoComments
leetcode
提交
5d8c4599
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,体验更适合开发者的 AI 搜索 >>
提交
5d8c4599
编写于
3月 11, 2019
作者:
L
liu13
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
20190311
上级
0bda8b63
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
70 addition
and
0 deletion
+70
-0
code/lc24.java
code/lc24.java
+33
-0
code/lc27.java
code/lc27.java
+37
-0
未找到文件。
code/lc24.java
0 → 100644
浏览文件 @
5d8c4599
package
code
;
/*
* 24. Swap Nodes in Pairs
* 题意:交换相邻的两个节点
* 难度:Medium
* 分类:Linked List
* 思路:递归的方法,递归交换后续节点以后,再交换现在的两个节点
* 非递归的方法,需要保存三个节点,加一个头节点指向head
* Tips:
*/
public
class
lc24
{
public
class
ListNode
{
int
val
;
ListNode
next
;
ListNode
(
int
x
)
{
val
=
x
;
}
}
public
ListNode
swapPairs
(
ListNode
head
)
{
if
(
head
==
null
||
head
.
next
==
null
)
return
head
;
return
helper
(
head
);
//用递归的方法,因为交换了两个以后,第二个节点的下一个节点必须等后边两个节点交换了以后才知道谁在前
}
public
ListNode
helper
(
ListNode
head
){
if
(
head
==
null
)
return
null
;
if
(
head
.
next
==
null
)
return
head
;
//节点数可能是奇数
ListNode
res
=
head
.
next
;
ListNode
ln1
=
head
,
ln2
=
head
.
next
,
ln3
=
ln2
.
next
;
ln2
.
next
=
ln1
;
ln1
.
next
=
helper
(
ln3
);
return
res
;
}
}
code/lc27.java
0 → 100644
浏览文件 @
5d8c4599
package
code
;
/*
* 27. Remove Element
* 题意:移除掉数组中指定的值,返回移除后数组的长度
* 难度:Easy
* 分类:Array, Two Pointers
* 思路:两个指针,分别O(n),指向要交换的位置和和他交换的数
* 答案中直接遍历一遍数组,放到位置上就行了,i++
* Tips:
*/
public
class
lc27
{
public
int
removeElement
(
int
[]
nums
,
int
val
)
{
int
p1
=
0
,
p2
=
0
;
while
(
p2
<
nums
.
length
){
while
(
p1
<
nums
.
length
&&
nums
[
p1
]!=
val
)
p1
++;
//p1指向要交换val的位置
p2
=
p1
;
while
(
p2
<
nums
.
length
&&
nums
[
p2
]==
val
)
p2
++;
if
(
p1
<
nums
.
length
&&
p2
<
nums
.
length
){
int
temp
=
nums
[
p1
];
nums
[
p1
]
=
nums
[
p2
];
nums
[
p2
]
=
temp
;
}
}
return
p1
;
}
public
int
removeElement2
(
int
[]
nums
,
int
val
)
{
int
i
=
0
;
for
(
int
j
=
0
;
j
<
nums
.
length
;
j
++)
{
if
(
nums
[
j
]
!=
val
)
{
nums
[
i
]
=
nums
[
j
];
i
++;
}
}
return
i
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录