Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf9ae7f6

Browse files
committed
leetcode
1 parent2089348 commitf9ae7f6

12 files changed

+215
-193
lines changed

‎src/com/imood/msjava/HeapSort1.java

Lines changed: 0 additions & 71 deletions
This file was deleted.

‎src/com/imood/msjava/HeapSortMin.java

Lines changed: 0 additions & 67 deletions
This file was deleted.

‎src/com/imood/msjava/Test.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,5 @@ class ListNode {
4343
ListNode(intx) {val =x; }
4444
}
4545

46-
publicstaticvoidmain(String[]args) {
47-
Stringabc =newString("abc");
48-
49-
Stringabc1 =newString("abc");
50-
51-
52-
System.out.println(abc==abc1);
53-
System.out.println(abc.equals(abc1));
54-
55-
56-
}
57-
5846

5947
}

‎src/com/imood/msjava/TryTest.java

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
packagecom.imood.msjava.leetcode;
2+
3+
/**
4+
* @description:判断链表是否存在环
5+
* @author: 微信公众号:码上Java
6+
* @createDate: 2020/7/29/0029
7+
*/
8+
publicclassHasCycle_141 {
9+
/**
10+
* 使用双指针,一个指针每次移动一个节点,一个指针每次移动两个节点,如果存在环,那么这两个指针一定会相遇。
11+
* @param head
12+
* @return
13+
*/
14+
publicbooleanhasCycle(ListNodehead) {
15+
if(head==null){
16+
returnfalse;
17+
}
18+
19+
ListNodelow=head;
20+
ListNodehigh=head.next;
21+
22+
while (low!=null &&high!=null &&high.next!=null){
23+
if(low==high){
24+
returntrue;
25+
}
26+
low=low.next;
27+
high=high.next;
28+
}
29+
returnfalse;
30+
}
31+
}

‎src/com/imood/msjava/leetcode/MergeTwoLists.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@ public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
2222
if(l1==null) {
2323
returnl1;
2424
}
25-
2625
if(l2==null) {
2726
returnl2;
2827
}
29-
3028
if(l1.val<l2.val){
3129
l1.next=mergeTwoLists(l1.next,l2);
3230
returnl1;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
packagecom.imood.msjava.leetcode;
2+
3+
/**
4+
* @description:归并两个有序的链表
5+
* @author: 微信公众号:码上Java
6+
* @createDate: 2020/7/29/0029
7+
*/
8+
publicclassMergeTwoLists_21 {
9+
10+
/**
11+
* 归并两个有序的链表
12+
* @param l1
13+
* @param l2
14+
* @return
15+
*/
16+
publicListNodemergeTwoLists(ListNodel1,ListNodel2) {
17+
if (l1 ==null) {
18+
returnl2;
19+
}
20+
if (l2 ==null){
21+
returnl1;
22+
}
23+
24+
if (l1.val <l2.val) {
25+
l1.next =mergeTwoLists(l1.next,l2);
26+
returnl1;
27+
}else {
28+
l2.next =mergeTwoLists(l1,l2.next);
29+
returnl2;
30+
}
31+
}
32+
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
packagecom.imood.msjava.leetcode;
2+
3+
/**
4+
* @description:
5+
* @author: 微信公众号:码上Java
6+
* @createDate: 2020/7/29/0029
7+
*/
8+
publicclassRemoveNthFromEnd_19 {
9+
10+
/**
11+
* 删除链表的倒数第 n 个节点 双指针
12+
* @param head
13+
* @param n
14+
* @return
15+
*/
16+
publicListNoderemoveNthFromEnd(ListNodehead,intn) {
17+
ListNodefast =head;
18+
// 快指针先走n步
19+
while (n-- >0) {
20+
fast =fast.next;
21+
}
22+
if (fast ==null){
23+
returnhead.next;
24+
}
25+
ListNodeslow =head;
26+
// 快慢指针一起走
27+
while (fast.next !=null) {
28+
fast =fast.next;
29+
slow =slow.next;
30+
}
31+
//当fast.next == null 的时候,就到了
32+
slow.next =slow.next.next;
33+
returnhead;
34+
}
35+
36+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
packagecom.imood.msjava.leetcode;
2+
3+
/**
4+
* @description: 链表反转
5+
* @author: 微信公众号:码上Java
6+
* @createDate: 2020/7/29/0029
7+
*/
8+
publicclassReverseList_206 {
9+
10+
/**
11+
* 递归解决 —— 链表反转
12+
* @param head
13+
* @return
14+
*/
15+
publicListNodereverseList(ListNodehead) {
16+
17+
if (head ==null ||head.next ==null) {
18+
returnhead;
19+
}
20+
ListNodenext =head.next;
21+
ListNodenewHead =reverseList(next);
22+
next.next =head;
23+
head.next =null;
24+
returnnewHead;
25+
}
26+
27+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
packagecom.imood.msjava.leetcode;
2+
3+
/**
4+
* @description: 167. 两数之和 II - 输入有序数组
5+
* @author: 微信公众号:码上Java
6+
* @createDate: 2020/7/29/0029
7+
*/
8+
publicclassTwoSum_167 {
9+
10+
/**
11+
* 题目描述:在有序数组中找出两个数,使它们的和为 target。
12+
* 思路:
13+
* 使用双指针,一个指针指向值较小的元素,一个指针指向值较大的元素。指向较小元素的指针从头向尾遍历,指向较大元素的指针从尾向头遍历。
14+
* 如果两个指针指向元素的和 sum == target,那么得到要求的结果;
15+
* 如果 sum > target,移动较大的元素,使 sum 变小一些;
16+
* 如果 sum < target,移动较小的元素,使 sum 变大一些。
17+
* 数组中的元素最多遍历一次,时间复杂度为 O(N)。只使用了两个额外变量,空间复杂度为 O(1)。
18+
*
19+
*
20+
* @param numbers
21+
* @param target
22+
* @return
23+
*/
24+
publicint[]twoSum(int[]numbers,inttarget) {
25+
if(numbers==null){
26+
returnnull;
27+
}
28+
intlow=0;
29+
inthigh=numbers.length-1;
30+
31+
while (low<high){
32+
intsum=numbers[low]+numbers[high];
33+
if(sum==target){
34+
//返回数组下标+1
35+
returnnewint[]{low+1,high+1};
36+
}elseif(sum<target){
37+
low++;
38+
}else {
39+
high--;
40+
}
41+
}
42+
43+
returnnull;
44+
}
45+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp