leecode更新
This commit is contained in:
parent
1f04a9b9c6
commit
2bb13486cd
|
|
@ -44,6 +44,11 @@
|
||||||
<artifactId>flume-kafka-source</artifactId>
|
<artifactId>flume-kafka-source</artifactId>
|
||||||
<version>1.9.0</version>
|
<version>1.9.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
<version>1.2.68</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,70 @@
|
||||||
|
package com.atguigu.kafka.consumer;
|
||||||
|
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
||||||
|
import org.apache.kafka.clients.consumer.ConsumerRecords;
|
||||||
|
import org.apache.kafka.clients.consumer.KafkaConsumer;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* kafka消费者:自动提交offset
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class KafkaConsumerDemoTest {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
//0.创建配置对象
|
||||||
|
Properties props =new Properties();
|
||||||
|
//kafka集群的位置
|
||||||
|
props.put("bootstrap.servers", "Ding202:9092");
|
||||||
|
//消费者组id
|
||||||
|
props.put("group.id", "suibian");
|
||||||
|
//自动提交offset
|
||||||
|
props.put("enable.auto.commit", "true");
|
||||||
|
//offset提交的间隔
|
||||||
|
props.put("auto.commit.interval.ms", "1000");
|
||||||
|
//key和value的反序列化器
|
||||||
|
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
|
||||||
|
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
|
||||||
|
|
||||||
|
|
||||||
|
//1.创建消费者对象
|
||||||
|
KafkaConsumer<String, String> kafkaConsumer = new KafkaConsumer<String, String>(props);
|
||||||
|
|
||||||
|
//2.订阅主题topic
|
||||||
|
//设置一个对象去封装topic,默认是用的collection,但是小的list现在就足够了
|
||||||
|
List<String> topic = new ArrayList<String>();
|
||||||
|
topic.add("pykafka_demo");
|
||||||
|
// topic.add("hello");
|
||||||
|
//如果没有这个主题,他会自动帮你创建一个,但是这个主题默认的是一个分区一个副本
|
||||||
|
//topic.add("second");
|
||||||
|
kafkaConsumer.subscribe(topic);
|
||||||
|
|
||||||
|
//3.持续消费数据
|
||||||
|
while (true){
|
||||||
|
//和kafka的consumer一样,会去主动拉去数据,所以需要设置超时拉取时间
|
||||||
|
//返回一个集合
|
||||||
|
ConsumerRecords<String, String> records = kafkaConsumer.poll(Duration.ofSeconds(2));
|
||||||
|
for (ConsumerRecord<String, String> record : records) {
|
||||||
|
//拿到每一个record,将他打印出来
|
||||||
|
System.out.println("消费到:"+record.topic()+
|
||||||
|
":"+record.partition()+
|
||||||
|
":"+record.offset()+
|
||||||
|
":"+record.key()+
|
||||||
|
":"+record.value());
|
||||||
|
System.out.println(JSON.toJSONString(JSON.parseObject(record.value())));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//关闭消费者对象
|
||||||
|
//kafkaConsumer.close();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -231,16 +231,4 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
//3d字符云
|
//3d字符云
|
||||||
{
|
{"status": 0,"msg": "", "data": [{"name": "海门", "value": 1}, {"name": "鄂尔多斯", "value": 1}]}
|
||||||
"status": 0,
|
|
||||||
"msg": "",
|
|
||||||
"data": [
|
|
||||||
{
|
|
||||||
"name": "海门",
|
|
||||||
"value": 1
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "鄂尔多斯",
|
|
||||||
"value": 1
|
|
||||||
}
|
|
||||||
]}
|
|
||||||
|
|
@ -0,0 +1,49 @@
|
||||||
|
package com.markilue.leecode.hot100.interviewHot.singlestack;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: Leecode
|
||||||
|
*@BelongsPackage: com.markilue.leecode.hot100.interviewHot.singlestack
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-05-17 10:50
|
||||||
|
*@Description: TODO 力扣42 接雨水
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class LC_42_Trap {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
|
||||||
|
int[] height = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
|
||||||
|
System.out.println(trap(height));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//单调栈解法:当前位置能积多少水本质上就是看看左右最高的位置的高度,取最小值
|
||||||
|
//当需要左右最高位置的高度时,单调栈来确定
|
||||||
|
public int trap(int[] height) {
|
||||||
|
|
||||||
|
LinkedList<Integer> stack = new LinkedList<>();
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < height.length; i++) {
|
||||||
|
while (!stack.isEmpty() && height[stack.peek()] < height[i]) {
|
||||||
|
Integer top = stack.pop();
|
||||||
|
if (stack.isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Integer left = stack.peek();//必须要有左边界
|
||||||
|
int curMin = Math.min(height[left] , height[i]);
|
||||||
|
result += (curMin - height[top])*(i-left-1);//注意这里要乘长度
|
||||||
|
}
|
||||||
|
stack.push(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -25,7 +25,7 @@ public class LC_76_MinWindow {
|
||||||
@Test
|
@Test
|
||||||
public void test1() {
|
public void test1() {
|
||||||
String s = "ADOBECODEBANC", t = "ABC";
|
String s = "ADOBECODEBANC", t = "ABC";
|
||||||
System.out.println(minWindow4(s, t));
|
System.out.println(minWindow5(s, t));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -278,4 +278,56 @@ public class LC_76_MinWindow {
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//四刷
|
||||||
|
public String minWindow5(String s, String t) {
|
||||||
|
HashMap<Character, Integer> need = new HashMap<>();
|
||||||
|
|
||||||
|
for (int i = 0; i < t.length(); i++) {
|
||||||
|
char c = t.charAt(i);
|
||||||
|
need.put(c, need.getOrDefault(c, 0) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
HashMap<Character, Integer> window = new HashMap<>();
|
||||||
|
int left = 0;
|
||||||
|
int right = 0;
|
||||||
|
int minLength = Integer.MAX_VALUE;
|
||||||
|
int start = 0;
|
||||||
|
int diff = need.size();
|
||||||
|
|
||||||
|
|
||||||
|
while (right < s.length()) {
|
||||||
|
char c = s.charAt(right++);
|
||||||
|
|
||||||
|
if (need.containsKey(c)) {
|
||||||
|
Integer count = window.getOrDefault(c, 0) + 1;
|
||||||
|
if (count.equals(need.get(c))) {
|
||||||
|
diff--;
|
||||||
|
}
|
||||||
|
window.put(c, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (diff == 0 && left < right) {
|
||||||
|
int cur = right - left;
|
||||||
|
if (cur < minLength) {
|
||||||
|
minLength = cur;
|
||||||
|
start = left;
|
||||||
|
}
|
||||||
|
|
||||||
|
char c1 = s.charAt(left++);
|
||||||
|
if (need.containsKey(c1)) {
|
||||||
|
Integer count = window.get(c1);
|
||||||
|
if (count.equals(need.get(c1))) {
|
||||||
|
diff++;
|
||||||
|
}
|
||||||
|
window.put(c1, count - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return minLength == Integer.MAX_VALUE ? "" : s.substring(start, start + minLength);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -207,4 +207,40 @@ public class T38_84_LargestRectangleArea {
|
||||||
return maxArea;
|
return maxArea;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//三刷:单调栈确定左右边界位置
|
||||||
|
public int largestRectangleArea5(int[] heights) {
|
||||||
|
|
||||||
|
int n = heights.length;
|
||||||
|
LinkedList<Integer> stack = new LinkedList<>();
|
||||||
|
int[] left = new int[n];
|
||||||
|
int[] right = new int[n];
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
while (!stack.isEmpty() && heights[stack.peek()] >= heights[i]) {
|
||||||
|
stack.pop();//寻找第一个比当前值小的数的位置
|
||||||
|
}
|
||||||
|
left[i] = stack.isEmpty() ? -1 : stack.peek();
|
||||||
|
stack.push(i);
|
||||||
|
}
|
||||||
|
stack.clear();
|
||||||
|
|
||||||
|
//反向遍历确定右边界
|
||||||
|
for (int i = n - 1; i >= 0; i--) {
|
||||||
|
while (!stack.isEmpty() && heights[stack.peek()] >= heights[i]) {
|
||||||
|
stack.pop();//寻找第一个比当前值小的数的位置
|
||||||
|
}
|
||||||
|
right[i] = stack.isEmpty() ? n : stack.peek();
|
||||||
|
stack.push(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
//确定每一个位置的最大矩形大小
|
||||||
|
int result = 0;
|
||||||
|
for (int i = 0; i < heights.length; i++) {
|
||||||
|
result = Math.max(result, (right[i] - left[i] - 1) * heights[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ public class T46_105_BuildTree {
|
||||||
public void test() {
|
public void test() {
|
||||||
int[] preorder = {3, 9, 20, 15, 7};
|
int[] preorder = {3, 9, 20, 15, 7};
|
||||||
int[] inorder = {9, 3, 15, 20, 7};
|
int[] inorder = {9, 3, 15, 20, 7};
|
||||||
TreeUtils.printTreeByLevel(buildTree1(preorder,inorder));
|
TreeUtils.printTreeByLevel(buildTree4(preorder, inorder));
|
||||||
}
|
}
|
||||||
|
|
||||||
int index = 0;
|
int index = 0;
|
||||||
|
|
@ -91,9 +91,6 @@ public class T46_105_BuildTree {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//官方最快
|
//官方最快
|
||||||
private int in = 0;
|
private int in = 0;
|
||||||
private int pre = 0;
|
private int pre = 0;
|
||||||
|
|
@ -115,4 +112,56 @@ public class T46_105_BuildTree {
|
||||||
node.right = build(preorder, inorder, stop);
|
node.right = build(preorder, inorder, stop);
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//二刷
|
||||||
|
public TreeNode buildTree4(int[] preorder, int[] inorder) {
|
||||||
|
index = 0;
|
||||||
|
return buildChild1(preorder, inorder, 0, inorder.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TreeNode buildChild1(int[] preorder, int[] inorder, int inStart, int inEnd) {
|
||||||
|
if (inStart >= inEnd || index >= inorder.length) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
int val = preorder[index++];
|
||||||
|
TreeNode root = new TreeNode(val);
|
||||||
|
int index = findIndex1(inorder, inStart, inEnd, val);//事实上,找index的过程本质上就是找一个边界
|
||||||
|
|
||||||
|
root.left = buildChild1(preorder, inorder, inStart, index);
|
||||||
|
root.right = buildChild1(preorder, inorder, index + 1, inEnd);
|
||||||
|
return root;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private int findIndex1(int[] inorder, int inStart, int inEnd, int val) {
|
||||||
|
while (inStart < inEnd) {
|
||||||
|
if (inorder[inStart] == val) {
|
||||||
|
return inStart;
|
||||||
|
}
|
||||||
|
inStart++;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//尝试官方最快:本质上就是逐步推进来确定边界,不用挨个寻找
|
||||||
|
public TreeNode buildTree5(int[] preorder, int[] inorder) {
|
||||||
|
return build1(preorder,inorder,Integer.MIN_VALUE);//找一个一定不会相等的即可
|
||||||
|
}
|
||||||
|
|
||||||
|
public TreeNode build1(int[] preorder, int[] inorder, int stop) {//只确定右边界
|
||||||
|
if (pre > preorder.length) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (inorder[in] == stop) {//遍历到了右边界
|
||||||
|
in++;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
TreeNode root = new TreeNode(preorder[pre++]);
|
||||||
|
root.left = build1(preorder, inorder, root.val);
|
||||||
|
root.right = build1(preorder, inorder, stop);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue