mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #79 from h2linlin/master
添加 0027, 0151, 0101, 0102, 0142, 0347 Java版本
This commit is contained in:
@ -123,6 +123,22 @@ public:
|
||||
|
||||
|
||||
Java:
|
||||
```java
|
||||
class Solution {
|
||||
public int removeElement(int[] nums, int val) {
|
||||
int p2 = 0;
|
||||
|
||||
for (int p1 = 0; p1 < nums.length; p1++) {
|
||||
if (nums[p1] != val) {
|
||||
nums[p2] = nums[p1];
|
||||
p2++;
|
||||
}
|
||||
}
|
||||
|
||||
return p2;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
@ -172,4 +188,4 @@ var removeElement = (nums, val) => {
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
|
@ -254,7 +254,94 @@ public:
|
||||
## 其他语言版本
|
||||
|
||||
Java:
|
||||
```java
|
||||
public class N0101 {
|
||||
/**
|
||||
* 解法1:DFS,递归。
|
||||
*/
|
||||
public boolean isSymmetric2(TreeNode root) {
|
||||
if (root == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return compare(root.left, root.right);
|
||||
}
|
||||
|
||||
private boolean compare(TreeNode left, TreeNode right) {
|
||||
if (left == null && right == null) {
|
||||
return true;
|
||||
}
|
||||
if (left != null && right == null) {
|
||||
return false;
|
||||
}
|
||||
if (left == null && right != null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (left.val == right.val) {
|
||||
return compare(left.left, right.right) && compare(left.right, right.left);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解法2:DFS,迭代
|
||||
*/
|
||||
public boolean isSymmetric3(TreeNode root) {
|
||||
if (root == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!equal(root.left, root.right)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Deque<TreeNode> st = new LinkedList<>();
|
||||
|
||||
st.push(root.right);
|
||||
st.push(root.left);
|
||||
|
||||
TreeNode curR = root.right;
|
||||
TreeNode curL = root.left;
|
||||
|
||||
while (!st.isEmpty()) {
|
||||
curL = st.pop();
|
||||
curR = st.pop();
|
||||
|
||||
// 前序,处理
|
||||
if (!equal(curL, curR)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (curR != null && curL != null) {
|
||||
st.push(curL.right);
|
||||
st.push(curR.left);
|
||||
st.push(curR.right);
|
||||
st.push(curL.left);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean equal(TreeNode l, TreeNode r) {
|
||||
if (l == null && r == null) {
|
||||
return true;
|
||||
}
|
||||
if (l != null && r == null) {
|
||||
return false;
|
||||
}
|
||||
if (l == null && r != null) {
|
||||
return false;
|
||||
}
|
||||
if (l.val == r.val) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
@ -283,4 +370,4 @@ const check = (leftPtr, rightPtr) => {
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
|
@ -420,54 +420,236 @@ public:
|
||||
Java:
|
||||
|
||||
```Java
|
||||
// 102.二叉树的层序遍历
|
||||
class Solution {
|
||||
public List<List<Integer>> resList=new ArrayList<List<Integer>>();
|
||||
public List<List<Integer>> resList = new ArrayList<List<Integer>>();
|
||||
|
||||
public List<List<Integer>> levelOrder(TreeNode root) {
|
||||
//checkFun01(root,0);
|
||||
checkFun02(root);
|
||||
|
||||
|
||||
return resList;
|
||||
}
|
||||
|
||||
//DFS--递归方式
|
||||
public void checkFun01(TreeNode node,Integer deep){
|
||||
if(node==null) return;
|
||||
//DFS--递归方式
|
||||
public void checkFun01(TreeNode node, Integer deep) {
|
||||
if (node == null) return;
|
||||
deep++;
|
||||
|
||||
if(resList.size()<deep){
|
||||
if (resList.size() < deep) {
|
||||
//当层级增加时,list的Item也增加,利用list的索引值进行层级界定
|
||||
List<Integer> item=new ArrayList<Integer>();
|
||||
List<Integer> item = new ArrayList<Integer>();
|
||||
resList.add(item);
|
||||
}
|
||||
resList.get(deep-1).add(node.val);
|
||||
|
||||
checkFun01(node.left,deep);
|
||||
checkFun01(node.right,deep);
|
||||
resList.get(deep - 1).add(node.val);
|
||||
|
||||
checkFun01(node.left, deep);
|
||||
checkFun01(node.right, deep);
|
||||
}
|
||||
|
||||
|
||||
//BFS--迭代方式--借助队列
|
||||
public void checkFun02(TreeNode node){
|
||||
if(node==null) return;
|
||||
Queue<TreeNode> que=new LinkedList<TreeNode>();
|
||||
public void checkFun02(TreeNode node) {
|
||||
if (node == null) return;
|
||||
Queue<TreeNode> que = new LinkedList<TreeNode>();
|
||||
que.offer(node);
|
||||
|
||||
while(!que.isEmpty()){
|
||||
List<Integer> itemList=new ArrayList<Integer>();
|
||||
int len=que.size();
|
||||
while (!que.isEmpty()) {
|
||||
List<Integer> itemList = new ArrayList<Integer>();
|
||||
int len = que.size();
|
||||
|
||||
while(len>0){
|
||||
TreeNode tmpNode=que.poll();
|
||||
while (len > 0) {
|
||||
TreeNode tmpNode = que.poll();
|
||||
itemList.add(tmpNode.val);
|
||||
|
||||
if(tmpNode.left!=null) que.offer(tmpNode.left);
|
||||
if(tmpNode.right!=null) que.offer(tmpNode.right);
|
||||
if (tmpNode.left != null) que.offer(tmpNode.left);
|
||||
if (tmpNode.right != null) que.offer(tmpNode.right);
|
||||
len--;
|
||||
}
|
||||
|
||||
|
||||
resList.add(itemList);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 107. 二叉树的层序遍历 II
|
||||
public class N0107 {
|
||||
|
||||
/**
|
||||
* 解法:队列,迭代。
|
||||
* 层序遍历,再翻转数组即可。
|
||||
*/
|
||||
public List<List<Integer>> solution1(TreeNode root) {
|
||||
List<List<Integer>> list = new ArrayList<>();
|
||||
Deque<TreeNode> que = new LinkedList<>();
|
||||
|
||||
if (root == null) {
|
||||
return list;
|
||||
}
|
||||
|
||||
que.offerLast(root);
|
||||
while (!que.isEmpty()) {
|
||||
List<Integer> levelList = new ArrayList<>();
|
||||
|
||||
int levelSize = que.size();
|
||||
for (int i = 0; i < levelSize; i++) {
|
||||
TreeNode peek = que.peekFirst();
|
||||
levelList.add(que.pollFirst().val);
|
||||
|
||||
if (peek.left != null) {
|
||||
que.offerLast(peek.left);
|
||||
}
|
||||
if (peek.right != null) {
|
||||
que.offerLast(peek.right);
|
||||
}
|
||||
}
|
||||
list.add(levelList);
|
||||
}
|
||||
|
||||
List<List<Integer>> result = new ArrayList<>();
|
||||
for (int i = list.size() - 1; i >= 0; i-- ) {
|
||||
result.add(list.get(i));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// 199.二叉树的右视图
|
||||
public class N0199 {
|
||||
/**
|
||||
* 解法:队列,迭代。
|
||||
* 每次返回每层的最后一个字段即可。
|
||||
*
|
||||
* 小优化:每层右孩子先入队。代码略。
|
||||
*/
|
||||
public List<Integer> rightSideView(TreeNode root) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
Deque<TreeNode> que = new LinkedList<>();
|
||||
|
||||
if (root == null) {
|
||||
return list;
|
||||
}
|
||||
|
||||
que.offerLast(root);
|
||||
while (!que.isEmpty()) {
|
||||
int levelSize = que.size();
|
||||
|
||||
for (int i = 0; i < levelSize; i++) {
|
||||
TreeNode poll = que.pollFirst();
|
||||
|
||||
if (poll.left != null) {
|
||||
que.addLast(poll.left);
|
||||
}
|
||||
if (poll.right != null) {
|
||||
que.addLast(poll.right);
|
||||
}
|
||||
|
||||
if (i == levelSize - 1) {
|
||||
list.add(poll.val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
// 637. 二叉树的层平均值
|
||||
public class N0637 {
|
||||
|
||||
/**
|
||||
* 解法:队列,迭代。
|
||||
* 每次返回每层的最后一个字段即可。
|
||||
*/
|
||||
public List<Double> averageOfLevels(TreeNode root) {
|
||||
List<Double> list = new ArrayList<>();
|
||||
Deque<TreeNode> que = new LinkedList<>();
|
||||
|
||||
if (root == null) {
|
||||
return list;
|
||||
}
|
||||
|
||||
que.offerLast(root);
|
||||
while (!que.isEmpty()) {
|
||||
TreeNode peek = que.peekFirst();
|
||||
|
||||
int levelSize = que.size();
|
||||
double levelSum = 0.0;
|
||||
for (int i = 0; i < levelSize; i++) {
|
||||
TreeNode poll = que.pollFirst();
|
||||
|
||||
levelSum += poll.val;
|
||||
|
||||
if (poll.left != null) {
|
||||
que.addLast(poll.left);
|
||||
}
|
||||
if (poll.right != null) {
|
||||
que.addLast(poll.right);
|
||||
}
|
||||
}
|
||||
list.add(levelSum / levelSize);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
// 429. N 叉树的层序遍历
|
||||
public class N0429 {
|
||||
/**
|
||||
* 解法1:队列,迭代。
|
||||
*/
|
||||
public List<List<Integer>> levelOrder(Node root) {
|
||||
List<List<Integer>> list = new ArrayList<>();
|
||||
Deque<Node> que = new LinkedList<>();
|
||||
|
||||
if (root == null) {
|
||||
return list;
|
||||
}
|
||||
|
||||
que.offerLast(root);
|
||||
while (!que.isEmpty()) {
|
||||
int levelSize = que.size();
|
||||
List<Integer> levelList = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < levelSize; i++) {
|
||||
Node poll = que.pollFirst();
|
||||
|
||||
levelList.add(poll.val);
|
||||
|
||||
List<Node> children = poll.children;
|
||||
if (children == null || children.size() == 0) {
|
||||
continue;
|
||||
}
|
||||
for (Node child : children) {
|
||||
if (child != null) {
|
||||
que.offerLast(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
list.add(levelList);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
class Node {
|
||||
public int val;
|
||||
public List<Node> children;
|
||||
|
||||
public Node() {}
|
||||
|
||||
public Node(int _val) {
|
||||
val = _val;
|
||||
}
|
||||
|
||||
public Node(int _val, List<Node> _children) {
|
||||
val = _val;
|
||||
children = _children;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
@ -186,7 +186,35 @@ public:
|
||||
|
||||
|
||||
Java:
|
||||
```java
|
||||
public class Solution {
|
||||
public ListNode detectCycle(ListNode head) {
|
||||
// 1.寻找相遇点
|
||||
ListNode fast = head;
|
||||
ListNode slow = head;
|
||||
|
||||
while (fast != null && fast.next != null) {
|
||||
fast = fast.next.next;
|
||||
slow = slow.next;
|
||||
if (fast != slow) {
|
||||
continue;
|
||||
}
|
||||
ListNode meet = fast;
|
||||
|
||||
// 2.寻找入口点
|
||||
slow = head;
|
||||
while (slow != fast) {
|
||||
slow = slow.next;
|
||||
fast = fast.next;
|
||||
}
|
||||
|
||||
return fast;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
@ -212,6 +212,77 @@ public:
|
||||
|
||||
|
||||
Java:
|
||||
```java
|
||||
class Solution {
|
||||
public String reverseWords(String s) {
|
||||
char[] chars = s.toCharArray();
|
||||
// 1.双指针去除空格
|
||||
// 去除头尾空格
|
||||
int head = 0;
|
||||
while (chars[head] == ' ') {
|
||||
head ++;
|
||||
}
|
||||
int tail = chars.length - 1;
|
||||
while (chars[tail] == ' ') {
|
||||
tail --;
|
||||
}
|
||||
|
||||
// 去除中间空格
|
||||
int p1 = head;
|
||||
int p2 = head + 1;
|
||||
while (p2 <= tail) {
|
||||
if (chars[p2 -1] == ' ' && chars[p2] == ' ') {
|
||||
p2 ++;
|
||||
continue;
|
||||
}
|
||||
chars[p1 + 1] = chars[p2];
|
||||
p1 ++;
|
||||
p2 ++;
|
||||
}
|
||||
|
||||
tail = p1;
|
||||
|
||||
// 2.双指针翻转整个字符串
|
||||
chars = reverse(chars, head, tail);
|
||||
|
||||
// 3.双指针翻转每个单词
|
||||
p1 = head;
|
||||
p2 = head;
|
||||
|
||||
for (int i = head; i <= tail;) {
|
||||
int add = 0;
|
||||
while (i + add <= tail && chars[i + add] != ' ') {
|
||||
add ++;
|
||||
}
|
||||
|
||||
if (i + add != tail) {
|
||||
reverse(chars, i, i + add - 1);
|
||||
i += add + 1;
|
||||
} else {
|
||||
reverse(chars, i, tail);
|
||||
i = tail + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return new String(chars, head, tail - head + 1);
|
||||
}
|
||||
|
||||
char[] reverse(char[] chars, int begin, int end) {
|
||||
int p1 = begin;
|
||||
int p2 = end;
|
||||
|
||||
while (p1 < p2) {
|
||||
chars[p1] ^= chars[p2];
|
||||
chars[p2] ^= chars[p1];
|
||||
chars[p1] ^= chars[p2];
|
||||
|
||||
p1 ++;
|
||||
p2 --;
|
||||
}
|
||||
return chars;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Python:
|
||||
|
@ -133,7 +133,59 @@ public:
|
||||
|
||||
|
||||
Java:
|
||||
```java
|
||||
public class N0347 {
|
||||
/**
|
||||
* 解法:
|
||||
* 1.统计频次。O(n)
|
||||
* 2.排序。O(nlogn)
|
||||
* 3.取前K个元素
|
||||
*
|
||||
* 注意到,排序的时候需要用小顶堆,而不是大顶堆。因为每次需要把最小的堆顶弹出去,最后才剩下最大的k个。
|
||||
* 时间复杂度:O(n + nlogk) = O(nlogk)
|
||||
*/
|
||||
public int[] topKFrequent(int[] nums, int k) {
|
||||
HashMap<Integer, Integer> map = statistic(nums);
|
||||
|
||||
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
|
||||
@Override
|
||||
public int compare(Integer o1, Integer o2) {
|
||||
return map.get(o1) - map.get(o2);
|
||||
}
|
||||
});
|
||||
|
||||
for (Integer key : map.keySet()) {
|
||||
if (pq.size() < k) {
|
||||
pq.offer(key);
|
||||
} else if (map.get(pq.peek()) < map.get(key)){
|
||||
pq.poll();
|
||||
pq.offer(key);
|
||||
}
|
||||
}
|
||||
|
||||
int[] result = new int[k];
|
||||
for (int i = 0; i < k; i++) {
|
||||
result[i] = pq.poll();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// key: 值,value:次数
|
||||
private HashMap<Integer, Integer> statistic(int[] nums) {
|
||||
HashMap<Integer, Integer> map = new HashMap<>();
|
||||
for (int num : nums) {
|
||||
if (map.get(num) == null) {
|
||||
map.put(num, 1);
|
||||
} else {
|
||||
map.put(num, map.get(num) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
|
Reference in New Issue
Block a user