Skip to content

Commit c4a7921

Browse files
committed
1. 新增部分题目
1 parent e091696 commit c4a7921

14 files changed

+454
-134
lines changed

java-note-algorithm/src/main/java/com/leosanqing/leetcode/easy/_70_ClimbingStairs.java

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.leosanqing.leetcode.easy.list;
2+
3+
import com.leosanqing.leetcode.medium.list.ListNode;
4+
5+
/**
6+
* @Author: rtliu
7+
* @Date: 2020/7/22 上午9:28
8+
* @Package: com.leosanqing.leetcode.easy.list
9+
* @Description: 1
10+
* Given a non-empty, singly linked list with head node head, return a middle node of linked list.
11+
* If there are two middle nodes, return the second middle node.
12+
* Example 1:
13+
* Input: [1,2,3,4,5]
14+
* Output: Node 3 from this list (Serialization: [3,4,5]) The returned node has value 3.
15+
* (The judge's serialization of this node is [3,4,5]).
16+
* Note that we returned a ListNode object ans, such that: ans.val = 3,
17+
* ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL.
18+
* Example 2:
19+
* Input: [1,2,3,4,5,6]
20+
* Output: Node 4 from this list (Serialization: [4,5,6])
21+
* Since the list has two middle nodes with values 3 and 4, we return the second one.
22+
* @Version: 1.0
23+
*/
24+
public class _876_middle_of_the_linked_list {
25+
public static void main(String[] args) {
26+
ListNode node1 = new ListNode(6);
27+
ListNode node2 = new ListNode(5, node1);
28+
ListNode node3 = new ListNode(4, node2);
29+
ListNode node4 = new ListNode(3,node3);
30+
ListNode node5 = new ListNode(2, node4);
31+
32+
middleNode(node5);
33+
}
34+
35+
public static ListNode middleNode(ListNode head) {
36+
37+
ListNode fakeHead = new ListNode();
38+
fakeHead.next = head;
39+
40+
ListNode fastNode = fakeHead;
41+
ListNode slowNode = fastNode;
42+
while (fastNode != null ) {
43+
slowNode =slowNode.next;
44+
fastNode = fastNode.next;
45+
if(fastNode == null){
46+
return slowNode;
47+
}
48+
fastNode = fastNode.next;
49+
}
50+
51+
return slowNode;
52+
53+
}
54+
}

java-note-algorithm/src/main/java/com/leosanqing/leetcode/easy/num/_7_reverseInteger.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,35 @@
44
* @Author: rtliu
55
* @Date: 2020/7/3 下午3:19
66
* @Package: com.leosanqing.leetcode.easy.num
7-
* @Description:
8-
* ` Given a 32-bit signed integer, reverse digits of an integer.
9-
* ` Example 1:
10-
* ` Input: 123
11-
* ` Output: 321
12-
* ` Example 2:
13-
* ` Input: -123
14-
* ` Output: -321
15-
* ` Example 3:
16-
* ` Input: 120
17-
* ` Output: 21
18-
* ` Note:
19-
* ` Assume we are dealing with an environment which could only store integers
20-
* ` within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem,
21-
* ` assume that your function returns 0 when the reversed integer overflows.
7+
* @Description: ` Given a 32-bit signed integer, reverse digits of an integer.
8+
* ` Example 1:
9+
* ` Input: 123
10+
* ` Output: 321
11+
* ` Example 2:
12+
* ` Input: -123
13+
* ` Output: -321
14+
* ` Example 3:
15+
* ` Input: 120
16+
* ` Output: 21
17+
* ` Note:
18+
* ` Assume we are dealing with an environment which could only store integers
19+
* ` within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem,
20+
* ` assume that your function returns 0 when the reversed integer overflows.
2221
* @Version: 1.0
2322
*/
2423
public class _7_reverseInteger {
2524
public static void main(String[] args) {
2625
_7_reverseInteger reverseInteger = new _7_reverseInteger();
2726
System.out.println(reverseInteger.reverse(-1200));
2827
}
28+
2929
public int reverse(int x) {
3030
long res = 0;
3131
while (x != 0) {
3232
res *= 10;
3333
res += x % 10;
3434
x /= 10;
3535
}
36-
return (int)res == res ? (int)res : 0;
36+
return (int) res == res ? (int) res : 0;
3737
}
3838
}

java-note-algorithm/src/main/java/com/leosanqing/leetcode/easy/num/_9_palindromeNumber.java renamed to java-note-algorithm/src/main/java/com/leosanqing/leetcode/easy/num/_9_palindrome_number.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,43 @@
2525
* ` Reads 01 from right to left. Therefore it is not a palindrome.
2626
* @Version: 1.0
2727
*/
28-
public class _9_palindromeNumber {
28+
public class _9_palindrome_number {
2929

3030
public static void main(String[] args) {
3131
System.out.println(isPalindrome(12121));
3232
}
3333

3434
public static boolean isPalindrome(int x) {
3535
// 如果小于0 或者大于零 但是以0结尾的都不是
36-
if (x < 0 || (x !=0 && x % 10 == 0)) {
36+
if (x < 0 || (x != 0 && x % 10 == 0)) {
3737
return false;
3838
}
3939

4040
int rev = 0;
4141
// 只需要一半,就可以进行匹配
4242
while (x > rev) {
43-
rev = rev *10 + x%10;
44-
x /=10;
43+
rev = rev * 10 + x % 10;
44+
x /= 10;
4545
}
46-
return rev == x || x == rev/10;
46+
return rev == x || x == rev / 10;
4747
}
4848

4949
/**
5050
* 使用字符串
51+
*
5152
* @param x
5253
* @return
5354
*/
5455
public static boolean isPalindrome2(int x) {
5556
// 如果小于0 或者大于零 但是以0结尾的都不是
56-
if (x < 0 || (x !=0 && x % 10 == 0)) {
57+
if (x < 0 || (x != 0 && x % 10 == 0)) {
5758
return false;
5859
}
5960

6061
String s = String.valueOf(x);
6162

62-
for (int i = 0; i < s.length()/2; i++) {
63-
if (s.charAt(i) != s.charAt(s.length()-i-1)){
63+
for (int i = 0; i < s.length() / 2; i++) {
64+
if (s.charAt(i) != s.charAt(s.length() - i - 1)) {
6465
return false;
6566
}
6667
}

java-note-algorithm/src/main/java/com/leosanqing/leetcode/medium/_201_bitwiseANDOfNumbersRange.java

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,56 @@
44
* @Author: rtliu
55
* @Date: 2020/6/1 上午10:45
66
* @Package: medium
7-
* @Description:
8-
* Given a range [m, n] where 0 <= m <= n <= 2147483647,
9-
* return the bitwise AND of all numbers in this range, inclusive.
10-
*
11-
* 给定一个范围
12-
* 返回这个范围内的所有数字的 按位与 的 和
13-
*
14-
* Example 1:
15-
*
16-
* Input: [5,7]
17-
* Output: 4
18-
* Example 2:
19-
*
20-
* Input: [0,1]
21-
* Output: 0
22-
*
23-
*
7+
* @Description: `
8+
* ` Given a range [m, n] where 0 <= m <= n <= 2147483647,
9+
* ` return the bitwise AND of all numbers in this range, inclusive.
10+
* `
11+
* ` 给定一个范围
12+
* ` 返回这个范围内的所有数字的 按位与 的 和
13+
* `
14+
* ` Example 1:
15+
* `
16+
* ` Input: [5,7]
17+
* ` Output: 4
18+
* ` Example 2:
19+
* `
20+
* ` Input: [0,1]
21+
* ` Output: 0
22+
* `
23+
* `
2424
* @Version: 1.0
2525
*/
2626
public class _201_bitwiseANDOfNumbersRange {
2727

2828
public static void main(String[] args) {
2929

30-
System.out.println(solution(5,12));
30+
System.out.println(solution(5, 12));
3131

32-
System.out.println(solution(5,12));
32+
System.out.println(solution(5, 12));
3333
}
3434

3535

3636
/**
3737
* 思路很简单,只要从 m 到 n 的任意一个数字中的一位是0, 那么那一位 不管怎么 & 都是0
3838
* 比如 [5-12]
39-
* 5 101
40-
* 6 110
41-
* 7 111
42-
* 8 1000
43-
* 9 1001
44-
* 10 1010
45-
* 11 1011
46-
* 12 1010
47-
*
48-
* 我们看 只要我们找到他的最前面几位是一样的时候,再给他补完后面的位数,就找到了
49-
*
39+
* 5 101
40+
* 6 110
41+
* 7 111
42+
* 8 1000
43+
* 9 1001
44+
* 10 1010
45+
* 11 1011
46+
* 12 1010
47+
* <p>
48+
* 我们看 只要我们找到他的最前面几位是一样的时候,再给他补完后面的位数,就找到了
5049
*
5150
* @param m
5251
* @param n
5352
* @return
5453
*/
55-
private static int solution(int m ,int n){
54+
private static int solution(int m, int n) {
5655
int step = 0;
57-
while(m != n){
56+
while (m != n) {
5857
m >>= 1;
5958
n >>= 1;
6059
step++;

java-note-algorithm/src/main/java/com/leosanqing/leetcode/medium/array/TaskEvData.java

100755100644
File mode changed.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.leosanqing.leetcode.medium.array;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/**
8+
* @Author: rtliu
9+
* @Date: 2020/7/22 下午3:00
10+
* @Package: com.leosanqing.leetcode.medium.array
11+
* @Description: 1
12+
* ` Given a collection of integers that might contain duplicates, nums, return all possible subsets (the
13+
* power set).
14+
* ` Note: The solution set must not contain duplicate subsets.
15+
* ` Example:
16+
* ` Input: [1,2,2]
17+
* ` Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ]
18+
* @Version: 1.0
19+
*/
20+
public class _90_subsetsII {
21+
22+
public static void main(String[] args) {
23+
subsetsWithDup(new int[]{1, 2, 2});
24+
}
25+
26+
public static List<List<Integer>> subsetsWithDup(int[] nums) {
27+
if (nums == null) {
28+
return null;
29+
}
30+
List<List<Integer>> answer = new ArrayList<>();
31+
32+
// 先排序
33+
Arrays.sort(nums);
34+
for (int i = 0; i <= nums.length; i++) {
35+
backTrace(answer, new ArrayList<>(), nums, 0, i);
36+
}
37+
38+
return answer;
39+
}
40+
41+
private static void backTrace(List<List<Integer>> answer, List<Integer> list, int[] nums, int position, int max) {
42+
43+
if (list.size() == max) {
44+
45+
answer.add(new ArrayList<>(list));
46+
47+
// if(!answer.contains(new ArrayList<>(list))){
48+
// }
49+
50+
return;
51+
}
52+
53+
for (int i = position; i < nums.length; i++) {
54+
if (i > position && nums[i] == nums[i - 1]) {
55+
continue;
56+
}
57+
list.add(nums[i]);
58+
backTrace(answer, list, nums, i + 1, max);
59+
list.remove(list.size() - 1);
60+
}
61+
62+
}
63+
64+
65+
}

0 commit comments

Comments
 (0)