From f835f672d5a919aad9e05a93401f9e5e007aff3a Mon Sep 17 00:00:00 2001 From: chenweijie Date: Wed, 16 Oct 2019 16:31:36 +0800 Subject: [PATCH 1/2] =?UTF-8?q?cherry-pick=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chen/algorithm/study/test13/Solution.java | 81 +++++++++++++++++++ .../chen/algorithm/study/test14/Solution.java | 64 +++++++++++++++ .../algorithm/study/test14/Solution2.java | 47 +++++++++++ .../chen/algorithm/study/test4/Solution.java | 63 +++++++++++++++ .../chen/algorithm/study/test5/Solution.java | 58 +++++++++++++ .../chen/algorithm/study/test7/Solution.java | 49 +++++++++++ .../chen/algorithm/study/test7/Solution2.java | 35 ++++++++ .../chen/algorithm/study/test9/Solution.java | 36 +++++++++ .../chen/algorithm/study/test9/Solution2.java | 45 +++++++++++ src/main/test/com/chen/test/TestDate.java | 1 + 10 files changed, 479 insertions(+) create mode 100644 src/main/java/com/chen/algorithm/study/test13/Solution.java create mode 100644 src/main/java/com/chen/algorithm/study/test14/Solution.java create mode 100644 src/main/java/com/chen/algorithm/study/test14/Solution2.java create mode 100644 src/main/java/com/chen/algorithm/study/test4/Solution.java create mode 100644 src/main/java/com/chen/algorithm/study/test5/Solution.java create mode 100644 src/main/java/com/chen/algorithm/study/test7/Solution.java create mode 100644 src/main/java/com/chen/algorithm/study/test7/Solution2.java create mode 100644 src/main/java/com/chen/algorithm/study/test9/Solution.java create mode 100644 src/main/java/com/chen/algorithm/study/test9/Solution2.java diff --git a/src/main/java/com/chen/algorithm/study/test13/Solution.java b/src/main/java/com/chen/algorithm/study/test13/Solution.java new file mode 100644 index 0000000..e51a04c --- /dev/null +++ b/src/main/java/com/chen/algorithm/study/test13/Solution.java @@ -0,0 +1,81 @@ +package com.chen.algorithm.study.test13; + +import org.junit.Test; + +/** + * @author : chen weijie + * @Date: 2019-09-04 23:18 + */ +public class Solution { + + + public int romanToInt(String s) { + + if (s == null || "".equals(s)) { + return 0; + } + + char[] chars = s.toCharArray(); + Integer[] integers = new Integer[chars.length]; + + for (int i = 0; i < chars.length; i++) { + + switch (chars[i]) { + case 'I': + integers[i] = 1; + continue; + case 'V': + integers[i] = 5; + continue; + case 'X': + integers[i] = 10; + continue; + case 'L': + integers[i] = 50; + continue; + case 'C': + integers[i] = 100; + continue; + case 'D': + integers[i] = 500; + continue; + case 'M': + integers[i] = 1000; + continue; + default: + } + } + + + int firstValue = 0; + int nextValue = 0; + int sum = 0; + + + for (int i = 0; i < integers.length; i++) { + firstValue = integers[i]; + + if (i == s.length() - 1) { + sum += firstValue; + } else { + nextValue = integers[i + 1]; + if (firstValue >= nextValue) { + sum += firstValue; + } else { + sum -= firstValue; + } + } + + + } + return sum; + } + + + @Test + public void testCase() { + System.out.println(romanToInt("IV")); + } + + +} diff --git a/src/main/java/com/chen/algorithm/study/test14/Solution.java b/src/main/java/com/chen/algorithm/study/test14/Solution.java new file mode 100644 index 0000000..2dfcaca --- /dev/null +++ b/src/main/java/com/chen/algorithm/study/test14/Solution.java @@ -0,0 +1,64 @@ +package com.chen.algorithm.study.test14; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * + * 错误 + * + * + * @author : chen weijie + * @Date: 2019-09-05 00:17 + */ +public class Solution { + + + public String longestCommonPrefix(String[] strs) { + + if (strs == null || strs.length == 0) { + + return ""; + } + + + String result; + Map map = new HashMap<>(); + + loop: + for (int j = 0; ; j++) { + for (int i = 0; i < strs.length; i++) { + if (strs[i] == null || "".equals(strs[i])) { + return ""; + } + + if (i == 0) { + map.put(strs[i].substring(0, j), strs[i].substring(0, j)); + continue; + } + if (!map.containsKey(strs[i].substring(0, j))) { + result = strs[i].substring(0, j - 1); + break loop; + } + } + } + return result; + + } + + @Test + public void testCase(){ + + + String [] strings = {""}; + + System.out.println(longestCommonPrefix(strings)); + + + + } + + +} diff --git a/src/main/java/com/chen/algorithm/study/test14/Solution2.java b/src/main/java/com/chen/algorithm/study/test14/Solution2.java new file mode 100644 index 0000000..7ebd8bc --- /dev/null +++ b/src/main/java/com/chen/algorithm/study/test14/Solution2.java @@ -0,0 +1,47 @@ +package com.chen.algorithm.study.test14; + +import org.junit.Test; + +/** + * @author : chen weijie + * @Date: 2019-09-05 00:55 + */ +public class Solution2 { + + + public String longestCommonPrefix(String[] strs) { + + if (strs.length == 0) { + return ""; + } + + String prefix = strs[0]; + for (int i = 1; i < strs.length; i++) { + while (!strs[i].startsWith(prefix)) { + prefix = prefix.substring(0, prefix.length() - 1); + if (prefix.isEmpty()) { + return ""; + } + } + } + return prefix; + } + + + @Test + public void testCase(){ + + + String [] strings = {"flower","flow","flight"}; + + System.out.println(longestCommonPrefix(strings)); + + + + } + + + + + +} diff --git a/src/main/java/com/chen/algorithm/study/test4/Solution.java b/src/main/java/com/chen/algorithm/study/test4/Solution.java new file mode 100644 index 0000000..13645cf --- /dev/null +++ b/src/main/java/com/chen/algorithm/study/test4/Solution.java @@ -0,0 +1,63 @@ +package com.chen.algorithm.study.test4; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author : chen weijie + * @Date: 2019-09-03 23:10 + */ +public class Solution { + + public double findMedianSortedArrays(int[] nums1, int[] nums2) { + + List resultList = new ArrayList<>(); + + + if (nums1 == null) { + for (int value : nums2) { + resultList.add(value); + } + } else if (nums2 == null) { + for (int value : nums1) { + resultList.add(value); + } + } else { + + + + + } + + + int n = resultList.size(); + + + double result = 0d; + + if (n % 2 == 0) { + result = resultList.get(resultList.size() / 2) + (resultList.get((resultList.size() / 2) - 1)) / 2; + } else { + double index = resultList.size() / 2; + result = resultList.get((int) Math.ceil(index)); + } + return result; + } + + + @Test + public void testCase() { + + int[] nums1 = {1, 2}; + int[] nums2 = {3, 4}; + + double d = findMedianSortedArrays(nums1, nums2); + System.out.println(d); + + + } + + +} diff --git a/src/main/java/com/chen/algorithm/study/test5/Solution.java b/src/main/java/com/chen/algorithm/study/test5/Solution.java new file mode 100644 index 0000000..7b5413b --- /dev/null +++ b/src/main/java/com/chen/algorithm/study/test5/Solution.java @@ -0,0 +1,58 @@ +package com.chen.algorithm.study.test5; + +import org.junit.Test; + +/** + * 中心扩展算法 + * + * @author : chen weijie + * @Date: 2019-09-03 23:58 + */ +public class Solution { + + + public String longestPalindrome(String s) { + + if (s == null || s.length() < 1) { + return ""; + } + + int start = 0, end = 0; + + + for (int i = 0; i < s.length(); i++) { + int len1 = expandArroundCenter(s, i, i); + int len2 = expandArroundCenter(s, i, i + 1); + + int len = Math.max(len1, len2); + + if (len > end - start) { + //中间值-长度的一半 + start = i - (len - 1) / 2; + //中间值+长度的一半 + end = i + len / 2; + } + } + return s.substring(start, end + 1); + } + + + private int expandArroundCenter(String s, int left, int right) { + + int L = left, R = right; + while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) { + L--; + R++; + } + return R - L - 1; + } + + + + @Test + public void testCase(){ + System.out.println(longestPalindrome("dcacdefd")); + } + + +} diff --git a/src/main/java/com/chen/algorithm/study/test7/Solution.java b/src/main/java/com/chen/algorithm/study/test7/Solution.java new file mode 100644 index 0000000..bb7a76a --- /dev/null +++ b/src/main/java/com/chen/algorithm/study/test7/Solution.java @@ -0,0 +1,49 @@ +package com.chen.algorithm.study.test7; + +import org.junit.Test; + +/** + * @author : chen weijie + * @Date: 2019-09-04 01:17 + */ +public class Solution { + + public int reverse(int x) { + + boolean belowZero = false; + Long l = Long.parseLong(x + ""); + if (l < 0) { + l = Math.abs(l); + belowZero = true; + } + + char[] chars = (l + "").toCharArray(); + + StringBuilder sb = new StringBuilder(); + for (int i = chars.length - 1; i >= 0; i--) { + sb.append(chars[i]); + } + + Long result = Long.parseLong(sb.toString()); + if (result > 2147483647 || result <-2147483648) { + return 0; + } + + if (!belowZero) { + return result.intValue(); + } else { + return -result.intValue(); + } + + } + + + @Test + public void testCase() { + // -2147483648~2147483647 + System.out.println(reverse(-2143847412)); + + } + + +} diff --git a/src/main/java/com/chen/algorithm/study/test7/Solution2.java b/src/main/java/com/chen/algorithm/study/test7/Solution2.java new file mode 100644 index 0000000..470d372 --- /dev/null +++ b/src/main/java/com/chen/algorithm/study/test7/Solution2.java @@ -0,0 +1,35 @@ +package com.chen.algorithm.study.test7; + +import org.junit.Test; + +/** + * @author : chen weijie + * @Date: 2019-09-04 01:51 + */ +public class Solution2 { + + public int reverse(int x) { + int rev = 0; + while (x != 0) { + int pop = x % 10; + x /= 10; + if (rev > Integer.MAX_VALUE / 10 || (rev == Integer.MAX_VALUE / 10 && pop > 7)) { + return 0; + } + if (rev < Integer.MIN_VALUE / 10 || (rev == Integer.MIN_VALUE / 10 && pop < -8)) { + return 0; + } + rev = rev * 10 + pop; + } + return rev; + } + + + @Test + public void testCase() { + + System.out.println(reverse(9083)); + + } + +} diff --git a/src/main/java/com/chen/algorithm/study/test9/Solution.java b/src/main/java/com/chen/algorithm/study/test9/Solution.java new file mode 100644 index 0000000..38ac78b --- /dev/null +++ b/src/main/java/com/chen/algorithm/study/test9/Solution.java @@ -0,0 +1,36 @@ +package com.chen.algorithm.study.test9; + +import org.junit.Test; + +/** + * @author : chen weijie + * @Date: 2019-09-04 22:54 + */ +public class Solution { + + + public boolean isPalindrome(int x) { + + if (x < 0) { + return false; + } + + char[] chars = String.valueOf(x).toCharArray(); + + StringBuilder sb = new StringBuilder(); + for (int i = chars.length - 1; i >= 0; i--) { + sb.append(chars[i]); + } + + return x == Long.parseLong(sb.toString()); + } + + + + @Test + public void testCase(){ + System.out.println(isPalindrome(2147483647)); + } + + +} diff --git a/src/main/java/com/chen/algorithm/study/test9/Solution2.java b/src/main/java/com/chen/algorithm/study/test9/Solution2.java new file mode 100644 index 0000000..a3473bf --- /dev/null +++ b/src/main/java/com/chen/algorithm/study/test9/Solution2.java @@ -0,0 +1,45 @@ +package com.chen.algorithm.study.test9; + +import org.junit.Test; + +/** + * @author : chen weijie + * @Date: 2019-09-04 23:09 + */ +public class Solution2 { + + +// 通过计算 1221 / 1000, 得首位1 +// 通过计算 1221 % 10, 可得末位 1 + + public boolean isPalindrome(int x) { + //边界判断 + if (x < 0) { + return false; + } + + int div = 1; + // + while (x / div >= 10) { + div *= 10; + } + + while (x > 0) { + int left = x / div; + int right = x % 10; + if (left != right) { + return false; + } + x = (x % div) / 10; + div /= 100; + } + return true; + } + + + @Test + public void testCase() { + System.out.println(isPalindrome(121)); + } + +} diff --git a/src/main/test/com/chen/test/TestDate.java b/src/main/test/com/chen/test/TestDate.java index a39cda1..58e8c75 100644 --- a/src/main/test/com/chen/test/TestDate.java +++ b/src/main/test/com/chen/test/TestDate.java @@ -21,6 +21,7 @@ public static void main(String[] args) { System.out.println("socre==="+score); + System.out.println(1<<31-1); From d3e51fa213b63cd46130af3fcc7eae036e0d59a2 Mon Sep 17 00:00:00 2001 From: chenweijie Date: Sun, 20 Oct 2019 17:19:51 +0800 Subject: [PATCH 2/2] netty simle demo --- pom.xml | 7 ++ src/main/java/com/chen/netty/TestMain.java | 11 ++ .../com/chen/netty/client/ClientTest.java | 56 ++++++++++ .../netty/client/SimpleClientHandler.java | 62 +++++++++++ .../com/chen/netty/server/ServerTest.java | 105 ++++++++++++++++++ .../netty/server/SimpleServerHandler.java | 73 ++++++++++++ 6 files changed, 314 insertions(+) create mode 100644 src/main/java/com/chen/netty/TestMain.java create mode 100644 src/main/java/com/chen/netty/client/ClientTest.java create mode 100644 src/main/java/com/chen/netty/client/SimpleClientHandler.java create mode 100644 src/main/java/com/chen/netty/server/ServerTest.java create mode 100644 src/main/java/com/chen/netty/server/SimpleServerHandler.java diff --git a/pom.xml b/pom.xml index cce003f..a431a73 100644 --- a/pom.xml +++ b/pom.xml @@ -125,6 +125,13 @@ + + io.netty + netty-all + 4.1.10.Final + + + diff --git a/src/main/java/com/chen/netty/TestMain.java b/src/main/java/com/chen/netty/TestMain.java new file mode 100644 index 0000000..a3b0eae --- /dev/null +++ b/src/main/java/com/chen/netty/TestMain.java @@ -0,0 +1,11 @@ +package com.chen.netty; + +/** + * + * https://blog.csdn.net/the_fool_/article/details/80611148 + * + * @author : chen weijie + * @Date: 2019-10-18 18:44 + */ +public class TestMain { +} diff --git a/src/main/java/com/chen/netty/client/ClientTest.java b/src/main/java/com/chen/netty/client/ClientTest.java new file mode 100644 index 0000000..397aaa7 --- /dev/null +++ b/src/main/java/com/chen/netty/client/ClientTest.java @@ -0,0 +1,56 @@ +package com.chen.netty.client; + +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; + +/** + * 客户端消费者对象 + * + * @author : chen weijie + * @Date: 2019-10-18 20:31 + */ +public class ClientTest { + + + public void connect(String host, int port) { + + EventLoopGroup worker = new NioEventLoopGroup(); + try { + + Bootstrap b = new Bootstrap(); + + b.group(worker); + + b.channel(NioSocketChannel.class); + + b.option(ChannelOption.SO_KEEPALIVE, true); + + b.handler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel socketChannel) throws Exception { + socketChannel.pipeline().addLast(new SimpleClientHandler()); + } + }); + + ChannelFuture f = b.connect(host, port).sync(); + f.channel().closeFuture().sync(); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + worker.shutdownGracefully(); + } + } + + public static void main(String[] args) { + ClientTest client = new ClientTest(); + client.connect("127.0.0.1", 9999); + } + + +} diff --git a/src/main/java/com/chen/netty/client/SimpleClientHandler.java b/src/main/java/com/chen/netty/client/SimpleClientHandler.java new file mode 100644 index 0000000..48e8fbc --- /dev/null +++ b/src/main/java/com/chen/netty/client/SimpleClientHandler.java @@ -0,0 +1,62 @@ +package com.chen.netty.client; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; + +/** + * @author : chen weijie + * @Date: 2019-10-18 20:37 + */ +public class SimpleClientHandler extends ChannelInboundHandlerAdapter { + + + /** + * 本方法用于接收服务端发送过来的消息 + * + * @param ctx + * @param msg + * @throws Exception + */ + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + System.out.println("SimpleClientHandler.channelRead"); + ByteBuf result = (ByteBuf) msg; + byte[] result1 = new byte[result.readableBytes()]; + result.readBytes(result1); + System.out.println("Server said:" + new String(result1)); + result.release(); + } + + /** + * 本方法用于处理异常 + * + * @param ctx + * @param cause + * @throws Exception + */ + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + // 当出现异常就关闭连接 + cause.printStackTrace(); + ctx.close(); + } + + + /** + * 本方法用于向服务端发送信息 + * + * @param ctx + * @throws Exception + */ + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + String msg = "hello Server!"; + ByteBuf encoded = ctx.alloc().buffer(4 * msg.length()); + encoded.writeBytes(msg.getBytes()); + ctx.write(encoded); + ctx.flush(); + } + + +} diff --git a/src/main/java/com/chen/netty/server/ServerTest.java b/src/main/java/com/chen/netty/server/ServerTest.java new file mode 100644 index 0000000..523b792 --- /dev/null +++ b/src/main/java/com/chen/netty/server/ServerTest.java @@ -0,0 +1,105 @@ +package com.chen.netty.server; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; + +/** + * 服务端生产者对象 + *

+ * Netty中,通讯的双方建立连接后,会把数据按照ByteBuf的方式进行传输, + * 例如http协议中,就是通过HttpRequestDecoder对ByteBuf数据流进行处理,转换成http的对象。 + * 深入学习: + * https://www.cnblogs.com/katsura/p/5991428.html + * https://www.cnblogs.com/stevenczp/p/7581940.html + * + * @author : chen weijie + * @Date: 2019-10-18 18:46 + */ +public class ServerTest { + + + /** + * 服务端口 + */ + private int port = 9999; + + + /** + * 开启服务 + */ + public void startNetty() { + + /** + * 创建两个EventLoop的组,EventLoop 这个相当于一个处理线程, + 是Netty接收请求和处理IO请求的线程。不理解的话可以百度NIO图解 + 相关资料:NioEventLoopGroup是一个处理I/O操作的多线程事件循环。 + Netty为不同类型的传输提供了各种EventLoopGroup实现。 + 在本例中,我们正在实现一个服务器端应用程序,因此将使用两个NioEventLoopGroup。 + 第一个,通常称为“boss”,接受传入的连接。 + 第二个,通常称为“worker”,当boss接受连接并注册被接受的连接到worker时,处理被接受连接的流量。 + 使用了多少线程以及如何将它们映射到创建的通道取决于EventLoopGroup实现,甚至可以通过构造函数进行配置。 + */ + + EventLoopGroup acceptor = new NioEventLoopGroup(); + EventLoopGroup worker = new NioEventLoopGroup(); + + // 创建启动类 + ServerBootstrap bootstrap = new ServerBootstrap(); + + //2、配置启动参数等 + /**设置循环线程组,前者用于处理客户端连接事件,后者用于处理网络IO(server使用两个参数这个) + *public ServerBootstrap group(EventLoopGroup group) + *public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) + */ + + bootstrap.group(acceptor, worker); + + + /**设置选项 + * 参数:Socket的标准参数(key,value),可自行百度 + * eg: + * bootstrap.option(ChannelOption.SO_BACKLOG, 1024); + *bootstrap.option(ChannelOption.SO_KEEPALIVE, true); + * */ + + bootstrap.option(ChannelOption.SO_BACKLOG,1024); + //用于构造socketchannel工厂 + bootstrap.channel(NioServerSocketChannel.class); + + /** + * 传入自定义客户端Handle(服务端在这里搞事情) + */ + bootstrap.childHandler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel socketChannel) throws Exception { + socketChannel.pipeline().addLast(new SimpleServerHandler()); + } + }); + + // 绑定端口,开始接收进来的连接 + ChannelFuture f; + try { + f = bootstrap.bind(port).sync(); + // 等待服务器 socket 关闭 。 + f.channel().closeFuture().sync(); + } catch (InterruptedException e) { + e.printStackTrace(); + }finally { + acceptor.shutdownGracefully(); + worker.shutdownGracefully(); + } + } + + public static void main(String[] args) { + new ServerTest().startNetty(); + + } + + +} diff --git a/src/main/java/com/chen/netty/server/SimpleServerHandler.java b/src/main/java/com/chen/netty/server/SimpleServerHandler.java new file mode 100644 index 0000000..8b7f373 --- /dev/null +++ b/src/main/java/com/chen/netty/server/SimpleServerHandler.java @@ -0,0 +1,73 @@ +package com.chen.netty.server; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; + +/** + * 服务端处理类 + * + * @author : chen weijie + * @Date: 2019-10-18 20:15 + */ +public class SimpleServerHandler extends ChannelInboundHandlerAdapter { + + + /** + * 本方法用于读取客户端发送的信息 + * + * @param ctx + * @param msg + * @throws Exception + */ + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + + System.out.println("simpleServerHandler.channelRead "); + ByteBuf result = (ByteBuf) msg; + byte[] results1 = new byte[result.readableBytes()]; + // msg中存储的是ByteBuf类型的数据,把数据读取到byte[]中 + result.readBytes(results1); + + String resultStr = new String(results1); + System.out.println("client said:" + resultStr); + // 释放资源,这行很关键 + result.release(); + + //向客户端发送信息 + String response = "hello client"; + // 在当前场景下,发送的数据必须转换成byteBuf数组 + ByteBuf encoded = ctx.alloc().buffer(4 * response.length()); + encoded.writeBytes(response.getBytes()); + ctx.write(encoded); + ctx.flush(); + } + + + /** + * 本方法用作处理异常 + * + * @param ctx + * @param cause + * @throws Exception + */ + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + // 当出现异常就关闭连接 + cause.printStackTrace(); + ctx.close(); + } + + /** + * 信息获取完毕后操作 + * + * @param ctx + * @throws Exception + */ + @Override + public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { + ctx.flush(); + } + + +}