tianyun 3 lat temu
rodzic
commit
51e7b7d7b5

+ 19 - 0
leetcode/pom.xml

@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>java-base-maven</artifactId>
+        <groupId>com.alvin</groupId>
+        <version>1.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>leetcode</artifactId>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+    </properties>
+
+</project>

+ 22 - 0
leetcode/src/main/java/Solution53.java

@@ -0,0 +1,22 @@
+import java.util.Arrays;
+
+public class Solution53 {
+    /**
+     * 53. 最大子数组和
+     *
+     * @param nums
+     * @return int
+     */
+    public int maxSubArray(int[] nums) {
+        int[] dp = new int[nums.length];
+        dp[0] = nums[0];
+        for (int i = 1; i < nums.length; i++) {
+            dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]);
+        }
+        return Arrays.stream(dp).max().getAsInt();
+    }
+
+    public static void main(String[] args) {
+        System.out.println(new Solution53().maxSubArray(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4}));
+    }
+}

+ 1 - 0
pom.xml

@@ -11,6 +11,7 @@
     <modules>
         <module>test1</module>
         <module>springboot-main</module>
+        <module>leetcode</module>
     </modules>
 
     <properties>

+ 12 - 0
springboot-main/pom.xml

@@ -37,6 +37,18 @@
             <artifactId>java-jwt</artifactId>
             <version>3.2.0</version>
         </dependency>
+
+        <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
+        <dependency>
+            <groupId>redis.clients</groupId>
+            <artifactId>jedis</artifactId>
+            <version>4.2.2</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-pool2</artifactId>
+            <version>2.11.1</version>
+        </dependency>
     </dependencies>
 
 </project>

+ 25 - 2
springboot-main/src/main/java/com/alvin/Application.java

@@ -1,10 +1,33 @@
 package com.alvin;
 
 
-import com.alvin.util.NumberUtil;
+import redis.clients.jedis.*;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
 
 public class Application {
-    public static void main(String[] args) {
+    public static void main(String[] args) throws Exception {
+
+        Set<String> hosts = new HashSet<>();
+        hosts.add("127.0.0.1:26379");
+        //hosts.add("127.0.0.1:36379"); 配置多个哨兵
+
+        JedisSentinelPool pool = new JedisSentinelPool("mymaster", hosts);
+        pool.setMaxTotal(10);
+        Jedis jedis = null;
 
+        for (int i = 0; i < 20; i++) {
+            Thread.sleep(2000);
+            try {
+                jedis = pool.getResource();
+                String v = "hi" + i;
+                jedis.set("hello", v);
+                System.out.println(v + "-->" + jedis.get("hello").equals(v));
+            } catch (Exception e) {
+                System.out.println(" [ exception happened]" + e);
+            }
+        }
     }
 }

+ 18 - 0
test1/src/main/java/tmp.java

@@ -1,9 +1,27 @@
 import java.nio.file.Files;
+import java.util.ArrayList;
+import java.util.Base64;
 import java.util.BitSet;
+import java.util.Stack;
 import java.util.concurrent.*;
 
 public class tmp {
     public static void main(String[] args) throws InterruptedException {
+        int[] ints = {2, 1, 2, 4, 3};
+        nextGreaterElement(ints);
+    }
 
+    public static int[] nextGreaterElement(int[] nums) {
+        int n = nums.length;
+        int[] res = new int[n];
+        Stack<Integer> stack = new Stack<>();
+        for (int i = n - 1; i >= 0; i--) {
+            while (!stack.isEmpty() && stack.peek() <= nums[i]) {
+                stack.pop();
+            }
+            res[i] = stack.isEmpty() ? -1 : stack.peek();
+            stack.push(nums[i]);
+        }
+        return res;
     }
 }