Skip to content

Commit 364bfc1

Browse files
committed
提交部分easy题目
1 parent 15ae5e9 commit 364bfc1

10 files changed

+242
-2
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.mistray.array;
2+
3+
/**
4+
* @author ZJY(MistRay)
5+
* @Project algorithm-study
6+
* @Package com.mistray.array
7+
* @create 2020年05月21日 14:14
8+
* @Desc
9+
*/
10+
public class BestTimeToBuyAndSellStock122 {
11+
12+
// 思路,贪心算法,大于就加上,小于就不管,最后就可以算出最大利润
13+
public int maxProfit(int[] prices) {
14+
int max = 0;
15+
int pre = prices[0];
16+
for (int price : prices) {
17+
if (price - pre > 0){
18+
max = max + price - pre;
19+
}
20+
pre = price;
21+
}
22+
return max;
23+
}
24+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.mistray.array;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.HashSet;
6+
7+
/**
8+
* @author ZJY(MistRay)
9+
* @Project algorithm-study
10+
* @Package com.mistray.array
11+
* @create 2020年05月21日 15:08
12+
* @Desc
13+
*/
14+
public class ContainsDuplicate217 {
15+
// 思路 hashmap,hashset,排序
16+
// public boolean containsDuplicate(int[] nums) {
17+
// HashSet<Integer> set = new HashSet<>();
18+
// for (int num : nums) {
19+
// if (!set.contains(num)){
20+
// set.add(num);
21+
// }else{
22+
// return true;
23+
// }
24+
// }
25+
// return false;
26+
// }
27+
28+
public boolean containsDuplicate(int[] nums) {
29+
if (nums.length == 0) {
30+
return false;
31+
}
32+
Arrays.sort(nums);
33+
int pre = nums[0] + 1;
34+
for (int num : nums) {
35+
if (pre == num) {
36+
return true;
37+
}
38+
pre = num;
39+
}
40+
return false;
41+
}
42+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.mistray.array;
2+
3+
import java.lang.reflect.Array;
4+
import java.util.Arrays;
5+
6+
/**
7+
* @author ZJY(MistRay)
8+
* @Project algorithm-study
9+
* @Package com.mistray.array
10+
* @create 2020年05月21日 13:36
11+
* @Desc
12+
*/
13+
public class MajorityElement169 {
14+
15+
// 思路:由于,如果数组中必然存在大于[n/2]个数的元素的话
16+
// 可以先进行排序,取中间的数,必然是这个元素
17+
// 例: 1 1 1 2
18+
// 也可以使用hash的方式.
19+
public int majorityElement(int[] nums) {
20+
Arrays.sort(nums);
21+
return nums[nums.length/2];
22+
}
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.mistray.link;
2+
3+
/**
4+
* @author ZJY(MistRay)
5+
* @Project algorithm-study
6+
* @Package com.mistray.link
7+
* @create 2020年05月21日 10:44
8+
* @Desc
9+
*/
10+
public class DeleteNodeInALinkedList237 {
11+
12+
// 思路:因为无法访问上一个节点,所以要把当前节点变成下一个节点,然后把下一个节点干掉.
13+
public void deleteNode(ListNode node) {
14+
// 因为无法访问前一个结点,所以可以把要删除的结点的后一个结点的值前移
15+
node.val = node.next.val;
16+
// 然后删除掉后一个结点
17+
node.next = node.next.next;
18+
}
19+
20+
}

leetcode/src/main/java/com/mistray/link/MergeTwoSortedLists21.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
1818
if (l1 == null) {
1919
return l2;
2020
}
21+
2122
if (l2 == null) {
2223
return l1;
2324
}
24-
2525
if (l1.val > l2.val) {
2626
l2.next = mergeTwoLists(l2.next, l1);
2727
return l2;
2828
} else {
29-
l1.next = mergeTwoLists(l2, l1.next);
29+
l1.next = mergeTwoLists(l1.next, l2);
3030
return l1;
3131
}
3232
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.mistray.stack;
2+
3+
import java.util.Stack;
4+
5+
class MinStack {
6+
7+
private Stack<Integer> dataStack;
8+
private Stack<Integer> minStack;
9+
10+
// 思路:
11+
// 使用辅助栈的方式,让取最小值的时间复杂度保持在O(1)
12+
/** initialize your data structure here. */
13+
public MinStack() {
14+
dataStack = new Stack<>();
15+
minStack = new Stack<>();
16+
}
17+
18+
public void push(int x) {
19+
dataStack.push(x);
20+
if (minStack.isEmpty() || minStack.peek() >= x){
21+
minStack.push(x);
22+
}
23+
}
24+
25+
public void pop() {
26+
if (dataStack.pop().equals(minStack.peek()) && minStack.size() > 1){
27+
minStack.pop();
28+
}
29+
}
30+
31+
public int top() {
32+
return dataStack.peek();
33+
}
34+
35+
public int getMin() {
36+
return minStack.peek();
37+
}
38+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.mistray.stack;
2+
3+
/**
4+
* @author ZJY(MistRay)
5+
* @Project algorithm-study
6+
* @Package com.mistray.stack
7+
* @create 2020年05月21日 14:33
8+
* @Desc
9+
*/
10+
public class MinStack155 {
11+
12+
13+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.mistray.string;
2+
3+
/**
4+
* @author ZJY(MistRay)
5+
* @Project algorithm-study
6+
* @Package com.mistray.string
7+
* @create 2020年05月21日 11:09
8+
* @Desc
9+
*/
10+
public class ReverseString242 {
11+
12+
public static void main(String[] args) {
13+
char[] s= {'1','2','3'};
14+
ReverseString242 reverseString242 = new ReverseString242();
15+
reverseString242.reverseString(s);
16+
System.out.println(s);
17+
}
18+
19+
public void reverseString(char[] s) {
20+
for (int i = 0, j = s.length - 1; i < s.length/2; i++, j--) {
21+
char tmp = s[j];
22+
s[j] = s[i];
23+
s[i] = tmp;
24+
}
25+
}
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.mistray.string;
2+
3+
/**
4+
* @author ZJY(MistRay)
5+
* @Project algorithm-study
6+
* @Package com.mistray.string
7+
* @create 2020年05月21日 10:57
8+
* @Desc
9+
*/
10+
public class ReverseWordsInAString557 {
11+
12+
public static void main(String[] args) {
13+
ReverseWordsInAString557 reverseWordsInAString557 = new ReverseWordsInAString557();
14+
reverseWordsInAString557.reverseWords("12");
15+
}
16+
17+
public String reverseWords(String s) {
18+
String[] sp = s.split(" ");
19+
StringBuilder sb = new StringBuilder();
20+
for (String s1 : sp) {
21+
char[] chars = s1.toCharArray();
22+
for (int i = chars.length; i > 0; i--) {
23+
sb.append(chars[i - 1]);
24+
}
25+
sb.append(" ");
26+
}
27+
return sb.toString().trim();
28+
}
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.mistray.tree;
2+
3+
/**
4+
* @author ZJY(MistRay)
5+
* @Project algorithm-study
6+
* @Package com.mistray.tree
7+
* @create 2020年05月21日 13:22
8+
* @Desc
9+
*/
10+
public class LowestCommonAncestorOfABinarySearchTree235 {
11+
12+
// 思路,根节点比两个点都大,去左树查找
13+
// 根节点比两个都小,去右边查找
14+
// 否则,直接返回根节点
15+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
16+
if (root.val > p.val && root.val > q.val) {
17+
return lowestCommonAncestor(root.left,p,q);
18+
}
19+
if (root.val < p.val && root.val < q.val) {
20+
return lowestCommonAncestor(root.right,p,q);
21+
}
22+
return root;
23+
}
24+
25+
}

0 commit comments

Comments
 (0)