From da6559ea46dbc932e4c1bcbdbd22020c37512e4d Mon Sep 17 00:00:00 2001
From: randian666 <553798608@qq.com>
Date: Mon, 23 Sep 2019 15:02:49 +0800
Subject: [PATCH 01/61] Update README.md
---
README.md | 2 --
1 file changed, 2 deletions(-)
diff --git a/README.md b/README.md
index 89432b9..a02d168 100644
--- a/README.md
+++ b/README.md
@@ -1,8 +1,6 @@
| 🍏 | 🍎 | 🍐 | 🍈 | 🥑 | 🥔| 🍠 | 🥝 | 🍱 | 🥞 |🌽| 🥦
| :--------: | :---------: | :---------: | :---------: | :---------: | :---------:| :---------: | :-------: | :-------:| :------:|:------:| :--------: |
| [JAVA基础](#JAVA基础) | [JVM知识](#JVM知识)|[开源框架知识](#开源框架知识) | [操作系统知识](#操作系统) |[多线程与并发](#多线程与并发)|[TCP与HTTP](#TCP与HTTP)| [架构设计与分布式](#架构设计与分布式) |[数据结构与算法](#数据结构与算法)|[数据库](#数据库知识)| [消息队列](#消息队列)|[缓存](#缓存) | [搜索](#搜索)
-### 求职、技术交流微信群
-
### JAVA基础
- [String,Stringbuffer,StringBuilder的区](http://www.cnblogs.com/su-feng/p/6659064.html)。
From f728b42faf930ff303615ea039356b9afccbdb03 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Fri, 18 Oct 2019 14:53:16 +0800
Subject: [PATCH 02/61] =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E5=8E=8B?=
=?UTF-8?q?=E7=BC=A9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 21 ++
.../java/com/algorithm/study/demo/Demo1.java | 31 +++
.../com/algorithm/study/demo/GZIPUtils.java | 223 ++++++++++++++++++
.../com/algorithm/study/demo/MainTest.java | 79 -------
.../algorithm/study/demo/OptionalTest.java | 22 ++
.../com/algorithm/study/demo/TestBase64.java | 65 +++++
.../com/algorithm/study/demo/ZipUtil.java | 55 +++++
.../algorithm/study/demo/guava/MainTest.java | 40 ++++
.../algorithm/study/demo/util/DateUtils.java | 44 ++++
9 files changed, 501 insertions(+), 79 deletions(-)
create mode 100644 src/main/java/com/algorithm/study/demo/Demo1.java
create mode 100644 src/main/java/com/algorithm/study/demo/GZIPUtils.java
delete mode 100644 src/main/java/com/algorithm/study/demo/MainTest.java
create mode 100644 src/main/java/com/algorithm/study/demo/OptionalTest.java
create mode 100644 src/main/java/com/algorithm/study/demo/TestBase64.java
create mode 100644 src/main/java/com/algorithm/study/demo/ZipUtil.java
create mode 100644 src/main/java/com/algorithm/study/demo/guava/MainTest.java
create mode 100644 src/main/java/com/algorithm/study/demo/util/DateUtils.java
diff --git a/pom.xml b/pom.xml
index 4594050..7eca3bf 100644
--- a/pom.xml
+++ b/pom.xml
@@ -26,6 +26,27 @@
zookeeper
3.4.6
+
+
+ com.github.rholder
+ guava-retrying
+ 2.0.0
+
+
+
+ com.wuyushuo
+ spring-mould-beyond
+ 1.2.6
+
+
+
+
+
+
+ com.ly.isbase
+ isbase
+ 1.0.14
+
diff --git a/src/main/java/com/algorithm/study/demo/Demo1.java b/src/main/java/com/algorithm/study/demo/Demo1.java
new file mode 100644
index 0000000..be8e8a6
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/Demo1.java
@@ -0,0 +1,31 @@
+package com.algorithm.study.demo;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.TreeMap;
+
+/**
+ * @author xun2.liu
+ * @title: Demo1
+ * @projectName algorithm-study
+ * @description: TODO
+ * @date 2019/9/24 15:59
+ */
+public class Demo1 {
+ public static void main(String[] args) {
+ TreeMap pairs = new TreeMap<>(Comparator.reverseOrder());
+ pairs.put(new BigDecimal("10000000000000"),"c");
+
+ pairs.put(new BigDecimal("1000"),"a");
+ pairs.put(new BigDecimal("1000"),"d");
+ pairs.put(new BigDecimal(String.valueOf(Integer.MAX_VALUE)),"b");
+
+ Iterator iterator = pairs.keySet().iterator();
+ while(iterator.hasNext()) {
+ BigDecimal key = iterator.next();
+ System.out.println("Key: " + key + ", Value: " + pairs.get(key));
+ }
+ }
+}
diff --git a/src/main/java/com/algorithm/study/demo/GZIPUtils.java b/src/main/java/com/algorithm/study/demo/GZIPUtils.java
new file mode 100644
index 0000000..29b2f75
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/GZIPUtils.java
@@ -0,0 +1,223 @@
+package com.algorithm.study.demo;
+
+
+import org.apache.commons.io.FileUtils;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.util.zip.*;
+
+/**
+ * @author xun2.liu
+ * @title: GZIPUtils
+ * @projectName algorithm-study
+ * @description: TODO
+ * @date 2019/10/17 20:50
+ */
+public class GZIPUtils {
+
+ /**
+ * 使用gzip进行压缩
+ */
+ public static String gzip(String primStr) {
+ if (primStr == null || primStr.length() == 0) {
+ return primStr;
+ }
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+ GZIPOutputStream gzip = null;
+ try {
+ gzip = new GZIPOutputStream(out);
+ gzip.write(primStr.getBytes());
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ if (gzip != null) {
+ try {
+ gzip.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+
+ return new sun.misc.BASE64Encoder().encode(out.toByteArray());
+ }
+
+ /**
+ * Description:使用gzip进行解压缩
+ *
+ * @param compressedStr
+ * @return
+ */
+ public static String gunzip(String compressedStr) {
+ if (compressedStr == null) {
+ return null;
+ }
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ ByteArrayInputStream in = null;
+ GZIPInputStream ginzip = null;
+ byte[] compressed = null;
+ String decompressed = null;
+ try {
+ compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
+ in = new ByteArrayInputStream(compressed);
+ ginzip = new GZIPInputStream(in);
+
+ byte[] buffer = new byte[1024];
+ int offset = -1;
+ while ((offset = ginzip.read(buffer)) != -1) {
+ out.write(buffer, 0, offset);
+ }
+ decompressed = out.toString();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ if (ginzip != null) {
+ try {
+ ginzip.close();
+ } catch (IOException e) {
+ }
+ }
+ if (in != null) {
+ try {
+ in.close();
+ } catch (IOException e) {
+ }
+ }
+ if (out != null) {
+ try {
+ out.close();
+ } catch (IOException e) {
+ }
+ }
+ }
+
+ return decompressed;
+ }
+
+ /**
+ * 使用zip进行压缩
+ *
+ * @param str 压缩前的文本
+ * @return 返回压缩后的文本
+ */
+ public static final String zip(String str) {
+ if (str == null) {
+ return null;
+ }
+ byte[] compressed;
+ ByteArrayOutputStream out = null;
+ ZipOutputStream zout = null;
+ String compressedStr = null;
+ try {
+ out = new ByteArrayOutputStream();
+ zout = new ZipOutputStream(out);
+ zout.putNextEntry(new ZipEntry("0"));
+ zout.write(str.getBytes());
+ zout.closeEntry();
+ compressed = out.toByteArray();
+ compressedStr = new sun.misc.BASE64Encoder().encodeBuffer(compressed);
+ } catch (IOException e) {
+ compressed = null;
+ } finally {
+ if (zout != null) {
+ try {
+ zout.close();
+ } catch (IOException e) {
+ }
+ }
+ if (out != null) {
+ try {
+ out.close();
+ } catch (IOException e) {
+ }
+ }
+ }
+ return compressedStr;
+ }
+
+ /**
+ * 使用zip进行解压缩
+ *
+ * @param compressedStr 压缩后的文本
+ * @return 解压后的字符串
+ */
+ public static final String unzip(String compressedStr) {
+ if (compressedStr == null) {
+ return null;
+ }
+ ByteArrayOutputStream out = null;
+ ByteArrayInputStream in = null;
+ ZipInputStream zin = null;
+ String decompressed = null;
+ try {
+ byte[] compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
+ out = new ByteArrayOutputStream();
+ in = new ByteArrayInputStream(compressed);
+ zin = new ZipInputStream(in);
+ zin.getNextEntry();
+ byte[] buffer = new byte[1024];
+ int offset = -1;
+ while ((offset = zin.read(buffer)) != -1) {
+ out.write(buffer, 0, offset);
+ }
+ decompressed = out.toString();
+ } catch (IOException e) {
+ decompressed = null;
+ } finally {
+ if (zin != null) {
+ try {
+ zin.close();
+ } catch (IOException e) {
+ }
+ }
+ if (in != null) {
+ try {
+ in.close();
+ } catch (IOException e) {
+ }
+ }
+ if (out != null) {
+ try {
+ out.close();
+ } catch (IOException e) {
+ }
+ }
+ }
+ return decompressed;
+ }
+
+ public static void main(String[] args) throws IOException {
+ String strOld = FileUtils.readFileToString(new File("D:/123456.txt"), "utf-8");
+ System.out.println("压缩前长度:"+strOld.length());
+ String gzip = zip(strOld);
+ System.out.println("压缩后长度:"+gzip.length());
+ String unzip = unzip(gzip);
+ FileUtils.write(new File("D:/2234567.txt"),unzip,"UTF-8");
+
+ int num=10000;
+
+ long beginTime = System.currentTimeMillis();
+ for (int i = 0; i < num; i++) {
+ zip(strOld);
+ }
+ long endTime = System.currentTimeMillis();
+ System.out.println("压缩总耗时"+(endTime - beginTime));
+ System.out.println("压缩平均耗时"+(endTime - beginTime) / 10000);
+
+ long currentTimeMillis = System.currentTimeMillis();
+ for (int i = 0; i < 10000; i++) {
+ unzip(gzip);
+ }
+ long endTimeMillis = System.currentTimeMillis();
+ System.out.println("解压总耗时"+(endTimeMillis - currentTimeMillis));
+ System.out.println("解压平均耗时"+(endTimeMillis - currentTimeMillis) / 10000);
+
+ }
+}
diff --git a/src/main/java/com/algorithm/study/demo/MainTest.java b/src/main/java/com/algorithm/study/demo/MainTest.java
deleted file mode 100644
index 619c3e9..0000000
--- a/src/main/java/com/algorithm/study/demo/MainTest.java
+++ /dev/null
@@ -1,79 +0,0 @@
-package com.algorithm.study.demo;
-
-import java.io.*;
-import java.text.SimpleDateFormat;
-import java.time.LocalDateTime;
-import java.time.format.DateTimeFormatter;
-
-/**
- * @Author: liuxun
- * @CreateDate: 2019/1/2 上午11:29
- * @Version: 1.0
- */
-public class MainTest {
- public static void main(String[] args) {
- try {
- String filePath="/Users/liuxun/csv.txt";
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
-
- File file=new File(filePath);
- File out=new File("/Users/liuxun/out.txt");
- createFile(out);
-
- BufferedReader reader=new BufferedReader(new FileReader(file));
- String temp=null;
- while ((temp=reader.readLine())!=null){
- StringBuffer sb=new StringBuffer();
- //双引号内的逗号不分割 双引号外的逗号进行分割
- String[] strArr = temp.trim().split(",(?=([^\\\"]*\\\"[^\\\"]*\\\")*[^\\\"]*$)",-1);
- if (strArr.length<=5){
- System.out.println("数据格式不准确");
- continue;
- }
- sb.append(Integer.valueOf(strArr[0])).append("\t");
- sb.append("'"+strArr[1]+"'").append("\t");
- sb.append("'"+strArr[2]+"'").append("\t");
- sb.append(Float.valueOf(strArr[3])).append("\t").append("\t");
- sb.append(sdf1.format(sdf.parse(strArr[4])));
- System.out.println(sb.toString());
- fileChaseFW("/Users/liuxun/out.txt",sb.toString());
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- /**
- * 写入TXT,追加写入
- * @param filePath
- * @param content
- */
- public static void fileChaseFW(String filePath, String content) {
- try {
- //构造函数中的第二个参数true表示以追加形式写文件
- FileWriter fw = new FileWriter(filePath,true);
- fw.write(content+"\n");
- fw.close();
- } catch (Exception e) {
- System.out.println("文件写入失败!" + e);
- }
- }
- /**
- * 创建文件
- * @param fileName
- * @return
- */
- public static boolean createFile(File fileName)throws Exception{
- try{
- if(!fileName.exists()){
- fileName.createNewFile();
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- return true;
- }
-
-
-}
diff --git a/src/main/java/com/algorithm/study/demo/OptionalTest.java b/src/main/java/com/algorithm/study/demo/OptionalTest.java
new file mode 100644
index 0000000..cfb1cac
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/OptionalTest.java
@@ -0,0 +1,22 @@
+package com.algorithm.study.demo;
+
+import com.algorithm.study.demo.string.TestString;
+
+import java.math.BigDecimal;
+import java.text.NumberFormat;
+import java.util.Optional;
+
+/**
+ * @author xun2.liu
+ * @title: OptionalTest
+ * @projectName algorithm-study
+ * @description: TODO
+ * @date 2019/8/21 10:10
+ */
+public class OptionalTest {
+ public static void main(String[] args) {
+ for (int i = 5; i > 0; i--) {
+ System.out.println(i);
+ }
+ }
+}
diff --git a/src/main/java/com/algorithm/study/demo/TestBase64.java b/src/main/java/com/algorithm/study/demo/TestBase64.java
new file mode 100644
index 0000000..de531ed
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/TestBase64.java
@@ -0,0 +1,65 @@
+package com.algorithm.study.demo;
+
+import com.alibaba.fastjson.JSON;
+import com.ly.ie.common.utils.HessianUtils;
+import com.ly.isbase.util.Base64Util;
+import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.io.FileUtils;
+
+import java.io.File;
+
+/**
+ * @author xun2.liu
+ * @title: TestBase64
+ * @projectName algorithm-study
+ * @description: TODO
+ * @date 2019/10/18 11:37
+ */
+public class TestBase64 {
+
+ /**
+ * BASE64解密
+ * @throws Exception
+ */
+ public static Object decryptBASE64(String key) throws Exception {
+ return HessianUtils.fromBytes(Base64Util.decode(key));
+ }
+ /**
+ * BASE64加密
+ */
+ public static String encryptBASE64(String key) throws Exception {
+ return Base64Util.encodeToString(HessianUtils.toBytes(key));
+ }
+ /**
+ * BASE64加密解密
+ */
+ public static void enAndDeCode(String str) throws Exception {
+ System.out.println("压缩前长度:"+str.length());
+ String data = encryptBASE64(str);
+ System.out.println("压缩后长度:"+data.length());
+ Object byteArray = decryptBASE64(data);
+ FileUtils.write(new File("D:/78910.txt"),byteArray.toString(),"UTF-8");
+
+ int num=10000;
+
+ long beginTime = System.currentTimeMillis();
+ for (int i = 0; i < num; i++) {
+ encryptBASE64(str);
+ }
+ long endTime = System.currentTimeMillis();
+ System.out.println("压缩总耗时"+(endTime - beginTime));
+ System.out.println("压缩平均耗时"+(endTime - beginTime) / 10000);
+
+ long currentTimeMillis = System.currentTimeMillis();
+ for (int i = 0; i < 10000; i++) {
+ decryptBASE64(data);
+ }
+ long endTimeMillis = System.currentTimeMillis();
+ System.out.println("解压总耗时"+(endTimeMillis - currentTimeMillis));
+ System.out.println("解压平均耗时"+(endTimeMillis - currentTimeMillis) / 10000);
+ }
+ public static void main(String[] args) throws Exception {
+ String strOld = FileUtils.readFileToString(new File("D:/123456.txt"), "utf-8");
+ enAndDeCode(strOld);
+ }
+}
diff --git a/src/main/java/com/algorithm/study/demo/ZipUtil.java b/src/main/java/com/algorithm/study/demo/ZipUtil.java
new file mode 100644
index 0000000..0028244
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/ZipUtil.java
@@ -0,0 +1,55 @@
+package com.algorithm.study.demo;
+
+/**
+ * @author xun2.liu
+ * @title: ZipUtil
+ * @projectName algorithm-study
+ * @description: TODO
+ * @date 2019/10/18 13:39
+ */
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.GZIPOutputStream;
+
+// 将一个字符串按照zip方式压缩和解压缩
+public class ZipUtil {
+
+ // 压缩
+ public static String compress(String str) throws IOException {
+ if (str == null || str.length() == 0) {
+ return str;
+ }
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ GZIPOutputStream gzip = new GZIPOutputStream(out);
+ gzip.write(str.getBytes());
+ gzip.close();
+ return out.toString("ISO-8859-1");
+ }
+
+ // 解压缩
+ public static String uncompress(String str) throws IOException {
+ if (str == null || str.length() == 0) {
+ return str;
+ }
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ ByteArrayInputStream in = new ByteArrayInputStream(str
+ .getBytes("ISO-8859-1"));
+ GZIPInputStream gunzip = new GZIPInputStream(in);
+ byte[] buffer = new byte[256];
+ int n;
+ while ((n = gunzip.read(buffer)) >= 0) {
+ out.write(buffer, 0, n);
+ }
+ // toString()使用平台默认编码,也可以显式的指定如toString("GBK")
+ return out.toString();
+ }
+
+ // 测试方法
+ public static void main(String[] args) throws IOException {
+ System.out.println(ZipUtil.compress("中国China"));
+ System.out.println(ZipUtil.uncompress(ZipUtil.compress("中国China")));
+ }
+
+}
diff --git a/src/main/java/com/algorithm/study/demo/guava/MainTest.java b/src/main/java/com/algorithm/study/demo/guava/MainTest.java
new file mode 100644
index 0000000..00452d6
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/guava/MainTest.java
@@ -0,0 +1,40 @@
+package com.algorithm.study.demo.guava;
+
+import com.github.rholder.retry.*;
+import com.google.common.base.Predicates;
+
+import java.io.*;
+import java.text.SimpleDateFormat;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * guava 重试机制
+ * @Author: liuxun
+ * @CreateDate: 2019/1/2 上午11:29
+ * @Version: 1.0
+ */
+public class MainTest {
+ public static void main(String[] args) {
+ //定义重试机制
+ Retryer retryer = RetryerBuilder.newBuilder()
+ .retryIfException() //设置异常重试
+ .retryIfResult(Predicates.equalTo(true)) //call方法返回true重试
+ .withWaitStrategy(WaitStrategies.fixedWait(10, TimeUnit.SECONDS)) //设置10秒后重试
+ .withStopStrategy(StopStrategies.stopAfterAttempt(3)).build(); //设置重试次数 超过将出异常
+ try {
+ retryer.call(() -> {
+ //这里写你的业务逻辑代码
+ System.out.println("11111111111111111122222");
+ return true; //需要重试返回true
+ });
+ } catch (ExecutionException e) {
+ e.printStackTrace();
+ } catch (RetryException e) {
+ e.printStackTrace();
+ }
+ }
+}
+
diff --git a/src/main/java/com/algorithm/study/demo/util/DateUtils.java b/src/main/java/com/algorithm/study/demo/util/DateUtils.java
new file mode 100644
index 0000000..f4a2efd
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/util/DateUtils.java
@@ -0,0 +1,44 @@
+package com.algorithm.study.demo.util;
+
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/**
+ * @author xun2.liu
+ * @title: DateUtils
+ * @projectName algorithm-study
+ * @description: TODO
+ * @date 2019/8/30 11:14
+ */
+public class DateUtils {
+ private static final String formatStr = "HH:mm";
+ public static void main(String args[]) throws ParseException {
+ String tS = "11:00";
+ String tE = "12:10";
+ System.out.println(getLong(tS));
+ System.out.println(getLong(tE));
+ System.out.println(getCurrentTime());
+ System.out.println(isInZone(getLong(tS),getLong(tE),getCurrentTime()));
+ if(isInZone(getLong(tS),getLong(tE),getCurrentTime())){
+ System.out.println("当前时间在范围中");
+ }else{
+ System.out.println("当前时间不在范围中");
+ }
+ }
+
+ private static boolean isInZone(long tStart,long tEnd,long t) throws ParseException {
+ return t>=tStart && t<=tEnd;
+ }
+
+ private static long getLong(String timeStr) throws ParseException {
+ DateFormat dateFormat = new SimpleDateFormat(formatStr);
+ return dateFormat.parse(timeStr).getTime();
+ }
+
+ private static long getCurrentTime() throws ParseException {
+ DateFormat dateFormat = new SimpleDateFormat(formatStr);
+ return getLong(dateFormat.format(new Date()));
+ }
+}
From 69fc53f98125b0626d80327d0c0bce19fe418016 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Thu, 24 Oct 2019 14:02:42 +0800
Subject: [PATCH 03/61] =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E5=8E=8B?=
=?UTF-8?q?=E7=BC=A9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../com/algorithm/study/demo/GZIPUtils.java | 34 +++---
.../algorithm/study/demo/JodaTimeUtil.java | 107 ++++++++++++++++++
.../com/algorithm/study/demo/TestDemo.java | 39 +++++++
.../com/algorithm/study/demo/util/Paging.java | 3 +
4 files changed, 166 insertions(+), 17 deletions(-)
create mode 100644 src/main/java/com/algorithm/study/demo/JodaTimeUtil.java
create mode 100644 src/main/java/com/algorithm/study/demo/TestDemo.java
diff --git a/src/main/java/com/algorithm/study/demo/GZIPUtils.java b/src/main/java/com/algorithm/study/demo/GZIPUtils.java
index 29b2f75..4438ac7 100644
--- a/src/main/java/com/algorithm/study/demo/GZIPUtils.java
+++ b/src/main/java/com/algorithm/study/demo/GZIPUtils.java
@@ -201,23 +201,23 @@ public static void main(String[] args) throws IOException {
String unzip = unzip(gzip);
FileUtils.write(new File("D:/2234567.txt"),unzip,"UTF-8");
- int num=10000;
-
- long beginTime = System.currentTimeMillis();
- for (int i = 0; i < num; i++) {
- zip(strOld);
- }
- long endTime = System.currentTimeMillis();
- System.out.println("压缩总耗时"+(endTime - beginTime));
- System.out.println("压缩平均耗时"+(endTime - beginTime) / 10000);
-
- long currentTimeMillis = System.currentTimeMillis();
- for (int i = 0; i < 10000; i++) {
- unzip(gzip);
- }
- long endTimeMillis = System.currentTimeMillis();
- System.out.println("解压总耗时"+(endTimeMillis - currentTimeMillis));
- System.out.println("解压平均耗时"+(endTimeMillis - currentTimeMillis) / 10000);
+// int num=10000;
+//
+// long beginTime = System.currentTimeMillis();
+// for (int i = 0; i < num; i++) {
+// zip(strOld);
+// }
+// long endTime = System.currentTimeMillis();
+// System.out.println("压缩总耗时"+(endTime - beginTime));
+// System.out.println("压缩平均耗时"+(endTime - beginTime) / 10000);
+//
+// long currentTimeMillis = System.currentTimeMillis();
+// for (int i = 0; i < 10000; i++) {
+// unzip(gzip);
+// }
+// long endTimeMillis = System.currentTimeMillis();
+// System.out.println("解压总耗时"+(endTimeMillis - currentTimeMillis));
+// System.out.println("解压平均耗时"+(endTimeMillis - currentTimeMillis) / 10000);
}
}
diff --git a/src/main/java/com/algorithm/study/demo/JodaTimeUtil.java b/src/main/java/com/algorithm/study/demo/JodaTimeUtil.java
new file mode 100644
index 0000000..a5319f4
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/JodaTimeUtil.java
@@ -0,0 +1,107 @@
+package com.algorithm.study.demo;
+
+import org.apache.commons.lang3.StringUtils;
+import org.joda.time.DateTime;
+import org.joda.time.format.DateTimeFormat;
+import org.joda.time.format.DateTimeFormatter;
+
+import java.util.Date;
+
+/**
+ * @description: JodaTime工具类
+ * @author xun2.liu
+ * @date 2019/5/31 11:24
+ **/
+public class JodaTimeUtil {
+ public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
+ public static final String STANDARD_DAY_FORMAT = "yyyy-MM-dd";
+ public static final String STANDARD_DAY_FORMAT_1 = "yyyyMMdd";
+ public static final String STANDARD_MILLIS_FORMAT="yyyy-MM-dd HH:mm:ss.SSS";
+ public static final String STANDARD_MINUTE_FORMAT="yyyyMMddHHmm";
+
+ public static Date strToDate(String dateTimeStr,String formatStr){
+ DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
+ DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
+ return dateTime.toDate();
+ }
+ public static DateTime dateToDateTime(Date date){
+ DateTime dateTime = new DateTime(date);
+ return dateTime;
+ }
+ public static Date strToDate(String dateTimeStr){
+ DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
+ DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
+ return dateTime.toDate();
+ }
+ public static Date getNow(){
+ return DateTime.now().toDate();
+ }
+ public static String getNowFormat(String formatStr){
+ return DateTime.now().toString(formatStr);
+ }
+ public static String getFormatNow(){
+ return DateTime.now().toString(STANDARD_DAY_FORMAT_1);
+ }
+ public static String getFormatNowDay(){
+ return DateTime.now().toString(STANDARD_DAY_FORMAT);
+ }
+
+ public static Long strToMillis(String dateTimeStr,String formatStr){
+ DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
+ long dateTime = dateTimeFormatter.parseDateTime(dateTimeStr).getMillis();
+ return dateTime;
+ }
+
+ public static String dateToStr(Date date,String formatStr){
+ if(date == null){
+ return StringUtils.EMPTY;
+ }
+ DateTime dateTime = new DateTime(date);
+ return dateTime.toString(formatStr);
+ }
+ public static String dateStrToMinuteStr(String dateTimeStr,String formatStr1,String formatStr2){
+ DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr1);
+ DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
+ return dateTime.toString(formatStr2);
+ }
+
+
+
+ public static String dateToStr(Date date){
+ if(date == null){
+ return StringUtils.EMPTY;
+ }
+ DateTime dateTime = new DateTime(date);
+ return dateTime.toString(STANDARD_FORMAT);
+ }
+
+ /**
+ * 解析日期 yyyy-MM-dd HH:mm:ss
+ *
+ * @param timestamp
+ * @return
+ */
+ public static String format(Long timestamp, String pattern) {
+ String dateStr = "";
+ if (null == timestamp || timestamp.longValue() < 0) {
+ return dateStr;
+ }
+ try {
+ Date date = new Date(timestamp);
+ dateStr=dateToStr(date,pattern);
+ } catch (Exception e) {
+ // ignore
+ }
+ return dateStr;
+ }
+ public static DateTime strToDateTime(String dateTimeStr,String formatStr){
+ Date date = strToDate(dateTimeStr, formatStr);
+ return dateToDateTime(date);
+ }
+
+ public static void main(String[] args) {
+ Date createTime = JodaTimeUtil.strToDate("2019-10-24 02:04:41.921", JodaTimeUtil.STANDARD_MILLIS_FORMAT);
+ DateTime dateTime = dateToDateTime(createTime);
+ System.out.println(dateTime.getHourOfDay());
+ }
+}
diff --git a/src/main/java/com/algorithm/study/demo/TestDemo.java b/src/main/java/com/algorithm/study/demo/TestDemo.java
new file mode 100644
index 0000000..74f4b59
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/TestDemo.java
@@ -0,0 +1,39 @@
+package com.algorithm.study.demo;
+
+
+import com.google.common.collect.Lists;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author xun2.liu
+ * @title: TestDemo
+ * @projectName algorithm-study
+ * @description: TODO
+ * @date 2019/10/21 17:16
+ */
+public class TestDemo {
+ public static void main(String[] args) {
+// List integers = Lists.newArrayList(1, 2, 3, 4, 5,6,7,8,9,10);
+// List> partition = Lists.partition(integers, 3);
+// for (List integerList : partition) {
+// for (int i = 0; i < integerList.size(); i++) {
+// System.out.println(integerList.get(i));
+// integerList.set(i,null);
+// }
+// }
+
+ int totalNum=2000;
+ int pageSize=1000;
+ int pageNo=(totalNum+pageSize-1)/pageSize;
+ System.out.println(getFrom(3,1000));
+ }
+
+ public static int getFrom(int pageNo,int pageSize) {
+ if (pageNo<=1){
+ return 0;
+ }
+ return (pageNo - 1) * pageSize;
+ }
+}
diff --git a/src/main/java/com/algorithm/study/demo/util/Paging.java b/src/main/java/com/algorithm/study/demo/util/Paging.java
index c1c6427..0c96ffe 100644
--- a/src/main/java/com/algorithm/study/demo/util/Paging.java
+++ b/src/main/java/com/algorithm/study/demo/util/Paging.java
@@ -72,4 +72,7 @@ public int getStart() {
return Math.max((page - 1) * pageSize, 0);
}
+ public static void main(String[] args) {
+
+ }
}
From 1c7830ad24d6a995c230a7319051fa3910111c5d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Thu, 14 Nov 2019 17:27:09 +0800
Subject: [PATCH 04/61] =?UTF-8?q?=E9=9B=B6=E9=92=B1=E6=94=AF=E4=BB=98?=
=?UTF-8?q?=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../com/algorithm/study/demo/GZIPUtils.java | 6 ++
.../com/algorithm/study/demo/TestDemo.java | 10 +-
.../algorithm/greedyalgorithm/MoneyBusi.java | 26 +++++
.../algorithm/greedyalgorithm/Solutions.java | 99 +++++++++++++++++++
4 files changed, 137 insertions(+), 4 deletions(-)
create mode 100644 src/main/java/com/algorithm/study/demo/algorithm/greedyalgorithm/MoneyBusi.java
create mode 100644 src/main/java/com/algorithm/study/demo/algorithm/greedyalgorithm/Solutions.java
diff --git a/src/main/java/com/algorithm/study/demo/GZIPUtils.java b/src/main/java/com/algorithm/study/demo/GZIPUtils.java
index 4438ac7..a8fe8df 100644
--- a/src/main/java/com/algorithm/study/demo/GZIPUtils.java
+++ b/src/main/java/com/algorithm/study/demo/GZIPUtils.java
@@ -1,12 +1,15 @@
package com.algorithm.study.demo;
+import com.alibaba.fastjson.JSON;
+import com.google.common.collect.Maps;
import org.apache.commons.io.FileUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
+import java.util.Map;
import java.util.zip.*;
/**
@@ -201,6 +204,9 @@ public static void main(String[] args) throws IOException {
String unzip = unzip(gzip);
FileUtils.write(new File("D:/2234567.txt"),unzip,"UTF-8");
+ Map map= Maps.newHashMap();
+ map.put("iflight.java.dsf.itradecore",100);
+ System.out.println(JSON.toJSONString(map));
// int num=10000;
//
// long beginTime = System.currentTimeMillis();
diff --git a/src/main/java/com/algorithm/study/demo/TestDemo.java b/src/main/java/com/algorithm/study/demo/TestDemo.java
index 74f4b59..a1593f9 100644
--- a/src/main/java/com/algorithm/study/demo/TestDemo.java
+++ b/src/main/java/com/algorithm/study/demo/TestDemo.java
@@ -3,6 +3,7 @@
import com.google.common.collect.Lists;
+import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@@ -24,10 +25,11 @@ public static void main(String[] args) {
// }
// }
- int totalNum=2000;
- int pageSize=1000;
- int pageNo=(totalNum+pageSize-1)/pageSize;
- System.out.println(getFrom(3,1000));
+// int totalNum=2000;
+// int pageSize=1000;
+// int pageNo=(totalNum+pageSize-1)/pageSize;
+// System.out.println(getFrom(3,1000));
+ System.out.println(".17usoft.com".length());
}
public static int getFrom(int pageNo,int pageSize) {
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/greedyalgorithm/MoneyBusi.java b/src/main/java/com/algorithm/study/demo/algorithm/greedyalgorithm/MoneyBusi.java
new file mode 100644
index 0000000..2bd261e
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/algorithm/greedyalgorithm/MoneyBusi.java
@@ -0,0 +1,26 @@
+package com.algorithm.study.demo.algorithm.greedyalgorithm;
+
+import lombok.*;
+
+/**
+ * @author xun2.liu
+ * @title: MoneyBusi
+ * @projectName algorithm-study
+ * @description: 零钱支付
+ * @date 2019/11/14 17:19
+ */
+@Getter
+@Setter
+@ToString
+@AllArgsConstructor
+@NoArgsConstructor
+public class MoneyBusi {
+ /** 面值 */
+ private String value;
+
+ /** 张数 */
+ private int num;
+
+ /** 金额 */
+ private int memory;
+}
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/greedyalgorithm/Solutions.java b/src/main/java/com/algorithm/study/demo/algorithm/greedyalgorithm/Solutions.java
new file mode 100644
index 0000000..f654d10
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/algorithm/greedyalgorithm/Solutions.java
@@ -0,0 +1,99 @@
+package com.algorithm.study.demo.algorithm.greedyalgorithm;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.PriorityQueue;
+
+/**
+ * @author xun2.liu
+ * @title: Solutions
+ * @projectName algorithm-study
+ * @description: 零钱支付问题
+ * 假设我们有 1 元、2 元、5 元、10 元、20 元、50 元、100 元这些面额的纸币,
+ * 它们的张数分别是 c1、c2、c5、c10、c20、c50、c100。
+ * 我们现在要用这些钱来支付 K 元,最少要用多少张纸币呢?
+ * 先用面值最大的来支付,如果不够,就继续用更小一点面值的,以此类推,最后剩下的用 1 元来补齐。
+ * @date 2019/11/14 17:20
+ */
+public class Solutions {
+ /**
+ * 用于存储金额的信息
+ */
+ private PriorityQueue moneyQueue =
+ new PriorityQueue<>(
+ (o1, o2) -> {
+ if (o1.getMemory() < o2.getMemory()) {
+ return 1;
+ } else if (o1.getMemory() > o2.getMemory()) {
+ return -1;
+ }
+ return 0;
+ });
+
+ /**
+ * 添加金额信息
+ * @param value 面值信息
+ * @param num 张数
+ * @param memory 金额值
+ */
+ public void addMemoryInfo(String value, int num, int memory) {
+ moneyQueue.offer(new MoneyBusi(value, num, memory));
+ }
+
+ /**
+ * 计算找零钱的问题
+ *
+ * @param money 找零的金额信息
+ * @return 找零钱的信息
+ */
+ public List looseChange(int money) {
+
+ List resultMemory = new ArrayList<>();
+
+ List moreMemory = new ArrayList<>();
+
+ int surplus = money;
+
+ while (surplus > 0) {
+ MoneyBusi busi = moneyQueue.peek();
+
+ if (null != busi) {
+ if (busi.getMemory() <= surplus) {
+ busi = moneyQueue.poll();
+ surplus = surplus - busi.getMemory();
+
+ MoneyBusi busiNew = new MoneyBusi(busi.getValue(), 1, busi.getMemory());
+ resultMemory.add(busiNew);
+
+ busi.setNum(busi.getNum() - 1);
+
+ if (busi.getNum() > 0) {
+ moneyQueue.offer(busi);
+ }
+ } else {
+ moreMemory.add(moneyQueue.poll());
+ }
+ } else {
+ break;
+ }
+ }
+ moneyQueue.addAll(moreMemory);
+ return resultMemory;
+ }
+
+ public static void main(String[] args) {
+ Solutions instance = new Solutions();
+ instance.addMemoryInfo("100元", 2, 100);
+ instance.addMemoryInfo("50元", 2, 50);
+ instance.addMemoryInfo("20元", 2, 20);
+ instance.addMemoryInfo("10元", 2, 10);
+ instance.addMemoryInfo("5元", 2, 5);
+ instance.addMemoryInfo("2元", 2, 2);
+ instance.addMemoryInfo("1元", 5, 1);
+
+ List list = instance.looseChange(332);
+ for (MoneyBusi busi : list) {
+ System.out.println(busi);
+ }
+ }
+}
From 5be4cb75708bf47e0ef666e30ec9df2cbce31ada Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Thu, 14 Nov 2019 20:16:04 +0800
Subject: [PATCH 05/61] =?UTF-8?q?=E9=9B=B6=E9=92=B1=E6=94=AF=E4=BB=98?=
=?UTF-8?q?=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../demo/algorithm/greedyalgorithm/Solutions.java | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/greedyalgorithm/Solutions.java b/src/main/java/com/algorithm/study/demo/algorithm/greedyalgorithm/Solutions.java
index f654d10..ecf31a5 100644
--- a/src/main/java/com/algorithm/study/demo/algorithm/greedyalgorithm/Solutions.java
+++ b/src/main/java/com/algorithm/study/demo/algorithm/greedyalgorithm/Solutions.java
@@ -5,7 +5,6 @@
import java.util.PriorityQueue;
/**
- * @author xun2.liu
* @title: Solutions
* @projectName algorithm-study
* @description: 零钱支付问题
@@ -17,9 +16,9 @@
*/
public class Solutions {
/**
- * 用于存储金额的信息
+ * 用于存储金额的信息,根据金额从大到小排序
*/
- private PriorityQueue moneyQueue =
+ public PriorityQueue moneyQueue =
new PriorityQueue<>(
(o1, o2) -> {
if (o1.getMemory() < o2.getMemory()) {
@@ -55,9 +54,10 @@ public List looseChange(int money) {
int surplus = money;
while (surplus > 0) {
+ //返回队列头部元素
MoneyBusi busi = moneyQueue.peek();
-
if (null != busi) {
+ System.out.println("当前金额:"+busi.getMemory());
if (busi.getMemory() <= surplus) {
busi = moneyQueue.poll();
surplus = surplus - busi.getMemory();
@@ -90,7 +90,7 @@ public static void main(String[] args) {
instance.addMemoryInfo("5元", 2, 5);
instance.addMemoryInfo("2元", 2, 2);
instance.addMemoryInfo("1元", 5, 1);
-
+ System.out.println(instance.moneyQueue);
List list = instance.looseChange(332);
for (MoneyBusi busi : list) {
System.out.println(busi);
From 7dbbb6fdb3b797ad52b80136a8e7be8894dffa00 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Thu, 14 Nov 2019 20:17:11 +0800
Subject: [PATCH 06/61] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B3=A8=E9=87=8A?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/algorithm/study/demo/Demo1.java | 1 -
src/main/java/com/algorithm/study/demo/GZIPUtils.java | 1 -
src/main/java/com/algorithm/study/demo/JodaTimeUtil.java | 1 -
src/main/java/com/algorithm/study/demo/OptionalTest.java | 1 -
src/main/java/com/algorithm/study/demo/TestBase64.java | 1 -
src/main/java/com/algorithm/study/demo/TestDemo.java | 7 -------
src/main/java/com/algorithm/study/demo/ZipUtil.java | 1 -
.../study/demo/algorithm/greedyalgorithm/MoneyBusi.java | 1 -
src/main/java/com/algorithm/study/demo/util/DateUtils.java | 1 -
9 files changed, 15 deletions(-)
diff --git a/src/main/java/com/algorithm/study/demo/Demo1.java b/src/main/java/com/algorithm/study/demo/Demo1.java
index be8e8a6..9072486 100644
--- a/src/main/java/com/algorithm/study/demo/Demo1.java
+++ b/src/main/java/com/algorithm/study/demo/Demo1.java
@@ -7,7 +7,6 @@
import java.util.TreeMap;
/**
- * @author xun2.liu
* @title: Demo1
* @projectName algorithm-study
* @description: TODO
diff --git a/src/main/java/com/algorithm/study/demo/GZIPUtils.java b/src/main/java/com/algorithm/study/demo/GZIPUtils.java
index a8fe8df..6d6ad8a 100644
--- a/src/main/java/com/algorithm/study/demo/GZIPUtils.java
+++ b/src/main/java/com/algorithm/study/demo/GZIPUtils.java
@@ -13,7 +13,6 @@
import java.util.zip.*;
/**
- * @author xun2.liu
* @title: GZIPUtils
* @projectName algorithm-study
* @description: TODO
diff --git a/src/main/java/com/algorithm/study/demo/JodaTimeUtil.java b/src/main/java/com/algorithm/study/demo/JodaTimeUtil.java
index a5319f4..94ff6bf 100644
--- a/src/main/java/com/algorithm/study/demo/JodaTimeUtil.java
+++ b/src/main/java/com/algorithm/study/demo/JodaTimeUtil.java
@@ -9,7 +9,6 @@
/**
* @description: JodaTime工具类
- * @author xun2.liu
* @date 2019/5/31 11:24
**/
public class JodaTimeUtil {
diff --git a/src/main/java/com/algorithm/study/demo/OptionalTest.java b/src/main/java/com/algorithm/study/demo/OptionalTest.java
index cfb1cac..7081aba 100644
--- a/src/main/java/com/algorithm/study/demo/OptionalTest.java
+++ b/src/main/java/com/algorithm/study/demo/OptionalTest.java
@@ -7,7 +7,6 @@
import java.util.Optional;
/**
- * @author xun2.liu
* @title: OptionalTest
* @projectName algorithm-study
* @description: TODO
diff --git a/src/main/java/com/algorithm/study/demo/TestBase64.java b/src/main/java/com/algorithm/study/demo/TestBase64.java
index de531ed..4af2a70 100644
--- a/src/main/java/com/algorithm/study/demo/TestBase64.java
+++ b/src/main/java/com/algorithm/study/demo/TestBase64.java
@@ -9,7 +9,6 @@
import java.io.File;
/**
- * @author xun2.liu
* @title: TestBase64
* @projectName algorithm-study
* @description: TODO
diff --git a/src/main/java/com/algorithm/study/demo/TestDemo.java b/src/main/java/com/algorithm/study/demo/TestDemo.java
index a1593f9..67ab24e 100644
--- a/src/main/java/com/algorithm/study/demo/TestDemo.java
+++ b/src/main/java/com/algorithm/study/demo/TestDemo.java
@@ -1,14 +1,7 @@
package com.algorithm.study.demo;
-import com.google.common.collect.Lists;
-
-import java.math.BigDecimal;
-import java.util.ArrayList;
-import java.util.List;
-
/**
- * @author xun2.liu
* @title: TestDemo
* @projectName algorithm-study
* @description: TODO
diff --git a/src/main/java/com/algorithm/study/demo/ZipUtil.java b/src/main/java/com/algorithm/study/demo/ZipUtil.java
index 0028244..851c4c4 100644
--- a/src/main/java/com/algorithm/study/demo/ZipUtil.java
+++ b/src/main/java/com/algorithm/study/demo/ZipUtil.java
@@ -1,7 +1,6 @@
package com.algorithm.study.demo;
/**
- * @author xun2.liu
* @title: ZipUtil
* @projectName algorithm-study
* @description: TODO
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/greedyalgorithm/MoneyBusi.java b/src/main/java/com/algorithm/study/demo/algorithm/greedyalgorithm/MoneyBusi.java
index 2bd261e..e402baa 100644
--- a/src/main/java/com/algorithm/study/demo/algorithm/greedyalgorithm/MoneyBusi.java
+++ b/src/main/java/com/algorithm/study/demo/algorithm/greedyalgorithm/MoneyBusi.java
@@ -3,7 +3,6 @@
import lombok.*;
/**
- * @author xun2.liu
* @title: MoneyBusi
* @projectName algorithm-study
* @description: 零钱支付
diff --git a/src/main/java/com/algorithm/study/demo/util/DateUtils.java b/src/main/java/com/algorithm/study/demo/util/DateUtils.java
index f4a2efd..dbca451 100644
--- a/src/main/java/com/algorithm/study/demo/util/DateUtils.java
+++ b/src/main/java/com/algorithm/study/demo/util/DateUtils.java
@@ -6,7 +6,6 @@
import java.util.Date;
/**
- * @author xun2.liu
* @title: DateUtils
* @projectName algorithm-study
* @description: TODO
From 95e84e1e02ba68cf63317722890de0b90239e37b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Thu, 14 Nov 2019 20:21:09 +0800
Subject: [PATCH 07/61] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B3=A8=E9=87=8A?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../com/algorithm/study/demo/TestDemo.java | 34 -------------------
1 file changed, 34 deletions(-)
delete mode 100644 src/main/java/com/algorithm/study/demo/TestDemo.java
diff --git a/src/main/java/com/algorithm/study/demo/TestDemo.java b/src/main/java/com/algorithm/study/demo/TestDemo.java
deleted file mode 100644
index 67ab24e..0000000
--- a/src/main/java/com/algorithm/study/demo/TestDemo.java
+++ /dev/null
@@ -1,34 +0,0 @@
-package com.algorithm.study.demo;
-
-
-/**
- * @title: TestDemo
- * @projectName algorithm-study
- * @description: TODO
- * @date 2019/10/21 17:16
- */
-public class TestDemo {
- public static void main(String[] args) {
-// List integers = Lists.newArrayList(1, 2, 3, 4, 5,6,7,8,9,10);
-// List> partition = Lists.partition(integers, 3);
-// for (List integerList : partition) {
-// for (int i = 0; i < integerList.size(); i++) {
-// System.out.println(integerList.get(i));
-// integerList.set(i,null);
-// }
-// }
-
-// int totalNum=2000;
-// int pageSize=1000;
-// int pageNo=(totalNum+pageSize-1)/pageSize;
-// System.out.println(getFrom(3,1000));
- System.out.println(".17usoft.com".length());
- }
-
- public static int getFrom(int pageNo,int pageSize) {
- if (pageNo<=1){
- return 0;
- }
- return (pageNo - 1) * pageSize;
- }
-}
From 551145f9ef4f7a99b2c1f41b0ca69eac58da841c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Fri, 15 Nov 2019 16:23:57 +0800
Subject: [PATCH 08/61] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B3=A8=E9=87=8A?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../study/demo/algorithm/greedyalgorithm/Solutions.java | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/greedyalgorithm/Solutions.java b/src/main/java/com/algorithm/study/demo/algorithm/greedyalgorithm/Solutions.java
index ecf31a5..761a235 100644
--- a/src/main/java/com/algorithm/study/demo/algorithm/greedyalgorithm/Solutions.java
+++ b/src/main/java/com/algorithm/study/demo/algorithm/greedyalgorithm/Solutions.java
@@ -5,6 +5,7 @@
import java.util.PriorityQueue;
/**
+ * @author liuxun
* @title: Solutions
* @projectName algorithm-study
* @description: 零钱支付问题
From 29deef8a6436507fda025055efab9a02b6907ad8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Tue, 19 Nov 2019 17:06:27 +0800
Subject: [PATCH 09/61] =?UTF-8?q?huffman=E7=BC=96=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../huffman/DataOutputStreamHuffman.java | 59 ++++++
.../demo/algorithm/huffman/HuffmanCode.java | 193 ++++++++++++++++++
2 files changed, 252 insertions(+)
create mode 100644 src/main/java/com/algorithm/study/demo/algorithm/huffman/DataOutputStreamHuffman.java
create mode 100644 src/main/java/com/algorithm/study/demo/algorithm/huffman/HuffmanCode.java
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/huffman/DataOutputStreamHuffman.java b/src/main/java/com/algorithm/study/demo/algorithm/huffman/DataOutputStreamHuffman.java
new file mode 100644
index 0000000..1d622e4
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/algorithm/huffman/DataOutputStreamHuffman.java
@@ -0,0 +1,59 @@
+package com.algorithm.study.demo.algorithm.huffman;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+/**
+ * @author xun2.liu
+ * @title: DataOutputStreamHuffman
+ * @projectName algorithm-study
+ * @description: TODO
+ * @date 2019/11/19 17:03
+ */
+public class DataOutputStreamHuffman {
+
+ public static final DataOutputStreamHuffman OUTPUT = new DataOutputStreamHuffman();
+
+ public static final String path = "D:\\java\\test\\datastruct\\hoffman\\";
+
+ public void outtoFile(byte[] value) {
+ FileOutputStream output = null;
+ try {
+ output = new FileOutputStream(path + "src.file");
+ output.write(value);
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ if (null != output) {
+ try {
+ output.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+
+ public void outHuffmantoFile(byte[] value) {
+ FileOutputStream output = null;
+ try {
+ output = new FileOutputStream(path + "out.huff");
+ output.write(value);
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ if (null != output) {
+ try {
+ output.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+
+}
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/huffman/HuffmanCode.java b/src/main/java/com/algorithm/study/demo/algorithm/huffman/HuffmanCode.java
new file mode 100644
index 0000000..9d72631
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/algorithm/huffman/HuffmanCode.java
@@ -0,0 +1,193 @@
+package com.algorithm.study.demo.algorithm.huffman;
+
+import java.util.*;
+/**
+ * @author xun2.liu
+ * @title: HuffmanCode
+ * @projectName algorithm-study
+ * @description: TODO
+ * @date 2019/11/19 17:03
+ */
+public class HuffmanCode {
+ /** 根节点信息,根据编码后设置根节点信息 */
+ public CodeNode root;
+
+ /** 节点信息 */
+ public class CodeNode {
+ /** 编码存储的数据信息 */
+ public char data;
+
+ /** 字符出现的频率 */
+ public int frequence;
+
+ /** 霍夫漫编码左节点 */
+ public CodeNode left;
+
+ /** 霍夫漫编码右节点 */
+ public CodeNode right;
+
+ /** 父节点信息 */
+ public CodeNode parent;
+
+ /** 标识是否为计算添加节点 */
+ public boolean addNode;
+
+ @Override
+ public String toString() {
+ final StringBuilder sb = new StringBuilder("CodeNode{");
+ sb.append("data=").append(data);
+ sb.append(", frequence=").append(frequence);
+ sb.append('}');
+ return sb.toString();
+ }
+ }
+ /**
+ * 编码,形成每个字符的霍夫漫编码
+ *
+ * @param map 统计信息
+ * @return 结果编码后的信息
+ */
+ public Map getHuffManCode(Map map) {
+ if (null != map && !map.isEmpty()) {
+ // 使用小顶堆来进行数据的存储
+ PriorityQueue nodePriQueue =
+ new PriorityQueue<>(
+ map.size(),
+ (o1, o2) -> {
+ if (o1.frequence > o2.frequence) {
+ return 1;
+ } else if (o1.frequence < o2.frequence) {
+ return -1;
+ }
+ return 0;
+ });
+
+ CodeNode nodeTmp = null;
+
+ // 1,将数据放入小顶堆中
+ for (Map.Entry item : map.entrySet()) {
+
+ nodeTmp = new CodeNode();
+ nodeTmp.data = item.getKey();
+ nodeTmp.frequence = item.getValue();
+
+ nodePriQueue.offer(nodeTmp);
+ }
+
+ int queueSize = nodePriQueue.size();
+
+ // 将统计数据编译成霍夫漫编码树信息
+ for (int i = 1; i < queueSize; i++) {
+ CodeNode left = nodePriQueue.poll();
+ CodeNode right = nodePriQueue.poll();
+
+ CodeNode sumNode = new CodeNode();
+ sumNode.frequence = left.frequence + right.frequence;
+ sumNode.data = (char) ((int) left.data + (int) right.data);
+ sumNode.addNode = true;
+
+ sumNode.left = left;
+ sumNode.right = right;
+
+ left.parent = sumNode;
+ right.parent = sumNode;
+
+ nodePriQueue.offer(sumNode);
+ }
+
+ // 构建完成
+ root = nodePriQueue.poll();
+
+ // 构建霍夫漫编码
+ Map result = this.builderCode(root);
+
+ return result;
+ }
+
+ return null;
+ }
+
+ public Map builderCode(CodeNode root) {
+
+ Map result = new HashMap<>();
+
+ StringBuilder code = new StringBuilder();
+
+ this.buildCode(code, root, result);
+
+ return result;
+ }
+
+ /**
+ * 进行霍夫温编码的操作,此处使用递归来实现
+ *
+ * @param code 主串信息
+ * @param node 霍夫漫编码树信息
+ * @param result 存储的结果节点信息
+ */
+ private void buildCode(StringBuilder code, CodeNode node, Map result) {
+ if (null == node) {
+ return;
+ }
+
+ if (!node.addNode) {
+ result.put(node.data, code.toString());
+ }
+
+ if (node.left != null) {
+ code.append("0");
+ this.buildCode(code, node.left, result);
+ code.deleteCharAt(code.length() - 1);
+ }
+
+ if (node.right != null) {
+ code.append("1");
+ this.buildCode(code, node.right, result);
+ code.deleteCharAt(code.length() - 1);
+ }
+ }
+
+ public String parseHuffman2(String src, Map huffman) {
+ StringBuilder out = new StringBuilder();
+
+ char[] hufSrcs = src.toCharArray();
+
+ for (char hufs : hufSrcs) {
+ out.append(huffman.get(hufs));
+ }
+
+ return out.toString();
+ }
+
+ /**
+ * 进行霍夫漫的解码操作
+ *
+ * @param hufStr
+ * @param root
+ * @return
+ */
+ public String decodeHuffman(String hufStr, CodeNode root) {
+ char[] hubs = hufStr.toCharArray();
+
+ int index = 0;
+
+ StringBuilder resultMsg = new StringBuilder();
+
+ while (index < hubs.length) {
+ CodeNode node = root;
+
+ do {
+ if (hubs[index] == '0') {
+ node = node.left;
+ } else if (hubs[index] == '1') {
+ node = node.right;
+ }
+ index++;
+ } while (index < hubs.length && (node.left != null || node.right != null));
+
+ resultMsg.append(node.data);
+ }
+
+ return resultMsg.toString();
+ }
+}
From 6c28c4ba8e77be9a753202eff9fcfc8aa99dfbc1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Tue, 19 Nov 2019 17:08:21 +0800
Subject: [PATCH 10/61] =?UTF-8?q?huffman=E7=BC=96=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../study/demo/algorithm/huffman/StrProc.java | 42 +++++++++++++++++++
1 file changed, 42 insertions(+)
create mode 100644 src/main/java/com/algorithm/study/demo/algorithm/huffman/StrProc.java
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/huffman/StrProc.java b/src/main/java/com/algorithm/study/demo/algorithm/huffman/StrProc.java
new file mode 100644
index 0000000..cf753bd
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/algorithm/huffman/StrProc.java
@@ -0,0 +1,42 @@
+package com.algorithm.study.demo.algorithm.huffman;
+
+import java.util.HashMap;
+import java.util.Map;
+/**
+ * @author xun2.liu
+ * @title: StrProc
+ * @projectName algorithm-study
+ * @description: 对字符进行统计
+ * @date 2019/11/19 17:07
+ */
+public class StrProc {
+ /**
+ * 对字符进行统计操作
+ *
+ * @param str
+ * @return
+ */
+ public static Map countCharset(String str) {
+
+ if (null != str && str.length() > 0) {
+
+ Map result = new HashMap<>();
+
+ char[] strChars = str.toCharArray();
+
+ Integer value = null;
+ for (int i = 0; i < strChars.length; i++) {
+ value = result.get(strChars[i]);
+
+ if (value == null) {
+ result.put(strChars[i], 1);
+ } else {
+ result.put(strChars[i], value + 1);
+ }
+ }
+
+ return result;
+ }
+ return null;
+ }
+}
From de24d1dc6c5cc916b775ed96f3cb6866c36f0d84 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Tue, 19 Nov 2019 17:09:36 +0800
Subject: [PATCH 11/61] =?UTF-8?q?huffman=E7=BC=96=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../algorithm/huffman/TestHuffmanCode.java | 55 +++++++++++++++++++
1 file changed, 55 insertions(+)
create mode 100644 src/main/java/com/algorithm/study/demo/algorithm/huffman/TestHuffmanCode.java
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/huffman/TestHuffmanCode.java b/src/main/java/com/algorithm/study/demo/algorithm/huffman/TestHuffmanCode.java
new file mode 100644
index 0000000..fbe1fc3
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/algorithm/huffman/TestHuffmanCode.java
@@ -0,0 +1,55 @@
+package com.algorithm.study.demo.algorithm.huffman;
+
+
+import java.util.Map;
+import java.util.concurrent.ThreadLocalRandom;
+/**
+ * @author xun2.liu
+ * @title: TestHuffmanCode
+ * @projectName algorithm-study
+ * @description: huffman编码测试
+ * @date 2019/11/19 17:08
+ */
+public class TestHuffmanCode {
+ public static void main(String[] args) {
+ for (int i = 0; i < 5; i++) {
+ int valueRand = ThreadLocalRandom.current().nextInt(1, 50);
+
+ StringBuilder msg = new StringBuilder();
+
+ for (int j = 0; j < valueRand; j++) {
+ msg.append((char) ThreadLocalRandom.current().nextInt(65, 122));
+ }
+
+ String src = "我我我我我我我我我我我我是是是是是是小小小小小小thisis" + msg.toString();
+
+ Map conMap = StrProc.countCharset(src);
+
+ System.out.println(conMap);
+
+ HuffmanCode instance = new HuffmanCode();
+ Map huffCode = instance.getHuffManCode(conMap);
+ System.out.println(huffCode);
+
+ Integer value = Integer.parseInt("1010", 2);
+ System.out.println(value);
+
+ // Map parseTwo = instance.encodeHuf(huffCode);
+
+ // String hufOutValue = instance.parseHuffman(src, parseTwo);
+ String hufOutValue = instance.parseHuffman2(src, huffCode);
+
+ // DataOutputStreamHuffman.OUTPUT.outtoFile(src.getBytes(StandardCharsets.UTF_8));
+ // DataOutputStreamHuffman.OUTPUT.outHuffmantoFile(hufOutValue.getBytes());
+
+ String deValue = instance.decodeHuffman(hufOutValue, instance.root);
+ System.out.println("原始" + src);
+ System.out.println("结果" + deValue);
+
+// Assert.assertEquals(src, deValue);
+
+ System.out.println(
+ "--------------------------------------------------------------------------------");
+ }
+ }
+}
From 555390c49e12d7bb9d1389536d59403f08c2695c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Tue, 19 Nov 2019 17:12:52 +0800
Subject: [PATCH 12/61] =?UTF-8?q?huffman=E7=BC=96=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../study/demo/algorithm/huffman/TestHuffmanCode.java | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/huffman/TestHuffmanCode.java b/src/main/java/com/algorithm/study/demo/algorithm/huffman/TestHuffmanCode.java
index fbe1fc3..58bae54 100644
--- a/src/main/java/com/algorithm/study/demo/algorithm/huffman/TestHuffmanCode.java
+++ b/src/main/java/com/algorithm/study/demo/algorithm/huffman/TestHuffmanCode.java
@@ -12,7 +12,7 @@
*/
public class TestHuffmanCode {
public static void main(String[] args) {
- for (int i = 0; i < 5; i++) {
+// for (int i = 0; i < 5; i++) {
int valueRand = ThreadLocalRandom.current().nextInt(1, 50);
StringBuilder msg = new StringBuilder();
@@ -43,13 +43,13 @@ public static void main(String[] args) {
// DataOutputStreamHuffman.OUTPUT.outHuffmantoFile(hufOutValue.getBytes());
String deValue = instance.decodeHuffman(hufOutValue, instance.root);
- System.out.println("原始" + src);
- System.out.println("结果" + deValue);
+ System.out.println("原始:" + src);
+ System.out.println("结果:" + deValue);
// Assert.assertEquals(src, deValue);
System.out.println(
"--------------------------------------------------------------------------------");
}
- }
+// }
}
From f8b863e39fe1b518f5c67810c2801bc89924336e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Mon, 25 Nov 2019 00:57:38 +0800
Subject: [PATCH 13/61] =?UTF-8?q?LZ77=E7=AE=97=E6=B3=95=E7=AE=80=E5=8D=95?=
=?UTF-8?q?=E5=AE=9E=E7=8E=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../java/com/algorithm/study/demo/Demo1.java | 27 ++--
.../algorithm/huffman/TestHuffmanCode.java | 4 +-
.../study/demo/algorithm/lz77/LZ77Codec.java | 123 ++++++++++++++++++
.../com/algorithm/study/demo/model/User.java | 3 +-
4 files changed, 142 insertions(+), 15 deletions(-)
create mode 100644 src/main/java/com/algorithm/study/demo/algorithm/lz77/LZ77Codec.java
diff --git a/src/main/java/com/algorithm/study/demo/Demo1.java b/src/main/java/com/algorithm/study/demo/Demo1.java
index 9072486..1a9c4c4 100644
--- a/src/main/java/com/algorithm/study/demo/Demo1.java
+++ b/src/main/java/com/algorithm/study/demo/Demo1.java
@@ -1,5 +1,10 @@
package com.algorithm.study.demo;
+import com.algorithm.study.demo.model.User;
+import com.alibaba.fastjson.JSON;
+import org.apache.commons.lang3.StringUtils;
+
+import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Comparator;
@@ -13,18 +18,16 @@
* @date 2019/9/24 15:59
*/
public class Demo1 {
- public static void main(String[] args) {
- TreeMap pairs = new TreeMap<>(Comparator.reverseOrder());
- pairs.put(new BigDecimal("10000000000000"),"c");
-
- pairs.put(new BigDecimal("1000"),"a");
- pairs.put(new BigDecimal("1000"),"d");
- pairs.put(new BigDecimal(String.valueOf(Integer.MAX_VALUE)),"b");
+ public static void main(String[] args) throws Exception {
+ User user=new User();
+ user.setId(1);
+ setUserName(user,"name","刘勋");
+ System.out.println(JSON.toJSONString(user));
+ }
- Iterator iterator = pairs.keySet().iterator();
- while(iterator.hasNext()) {
- BigDecimal key = iterator.next();
- System.out.println("Key: " + key + ", Value: " + pairs.get(key));
- }
+ private static void setUserName(User t, String column, String name) throws Exception {
+ Field f = t.getClass().getDeclaredField(column);
+ f.setAccessible(true);
+ f.set(t, name);
}
}
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/huffman/TestHuffmanCode.java b/src/main/java/com/algorithm/study/demo/algorithm/huffman/TestHuffmanCode.java
index 58bae54..a069bf4 100644
--- a/src/main/java/com/algorithm/study/demo/algorithm/huffman/TestHuffmanCode.java
+++ b/src/main/java/com/algorithm/study/demo/algorithm/huffman/TestHuffmanCode.java
@@ -21,7 +21,7 @@ public static void main(String[] args) {
msg.append((char) ThreadLocalRandom.current().nextInt(65, 122));
}
- String src = "我我我我我我我我我我我我是是是是是是小小小小小小thisis" + msg.toString();
+ String src = "我我是是一只小小鸟" + msg.toString();
Map conMap = StrProc.countCharset(src);
@@ -38,7 +38,7 @@ public static void main(String[] args) {
// String hufOutValue = instance.parseHuffman(src, parseTwo);
String hufOutValue = instance.parseHuffman2(src, huffCode);
-
+ System.out.println(hufOutValue);
// DataOutputStreamHuffman.OUTPUT.outtoFile(src.getBytes(StandardCharsets.UTF_8));
// DataOutputStreamHuffman.OUTPUT.outHuffmantoFile(hufOutValue.getBytes());
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/lz77/LZ77Codec.java b/src/main/java/com/algorithm/study/demo/algorithm/lz77/LZ77Codec.java
new file mode 100644
index 0000000..3a03fc4
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/algorithm/lz77/LZ77Codec.java
@@ -0,0 +1,123 @@
+package com.algorithm.study.demo.algorithm.lz77;
+
+/**
+ * @author xun2.liu
+ * @title: LZ77Codec
+ * @projectName algorithm-study
+ * @description: LZ77算法实现
+ * @date 2019/11/25 0:54
+ */
+public class LZ77Codec {
+ /**
+ * 搜索最大相同字符串的辅助类
+ */
+ private class SearchResult{
+ public int off = 0;
+ public int length = 0;
+ }
+
+ /**
+ * search the max matched string in the slide window;
+ * 可以使用KMP算法提高效率
+ * @param s 需要编码的字符串
+ * @param currentPosition 当前位置
+ * @return
+ */
+ private SearchResult search(String s,int currentPosition){
+ SearchResult result = new SearchResult();
+ for (int i = 0; i < currentPosition; i++) {
+ SearchResult t = new SearchResult();
+ for (int j = i; j < currentPosition; j++) {
+ // 区别已匹配和没有匹配的情况
+ if(s.charAt(currentPosition+j-i)==s.charAt(j)){
+ // 已经匹配的话 length+1
+ // 没有匹配的话 length=1 并计算偏移量
+ if(t.length >0){
+ t.length++;
+ }else{
+ t.length=1;
+ // 计算偏移量
+ t.off = currentPosition - j;
+ }
+ }else {
+ break;
+ }
+ }
+ if(t.length>result.length){
+ result=t;
+ }
+ }
+ return result;
+ }
+ /**
+ * 编码
+ *
+ * 目前发现的问题
+ * 1. 从左往右扫描和从右往左扫描off可能不一样
+ * @param s 需要编码的字符串
+ * @return 已经编码的字符串
+ */
+ String encoding(String s) {
+ StringBuilder builder = new StringBuilder();
+ // set current coding position pointer to the start of message
+ int currentPosition = 0;
+ while (true){
+ // search the max matched string in the slide window;
+ SearchResult result = search(s,currentPosition);
+ System.out.println("result:"+result.off+" "+result.length+" "+s.substring(currentPosition,currentPosition+result.length+1));
+ Character nextChar = s.charAt(currentPosition+result.length);
+ if(result.length!=0){
+ builder.append("(").append(result.off).append(",").append(result.length).append(",").append(nextChar).append(")");
+ currentPosition+=result.length+1;
+ }else {
+ builder.append("(0,0,").append(nextChar).append(")");
+ currentPosition++;
+ }
+ if(currentPosition>=s.length()){
+ break;
+ }
+ }
+ return builder.toString();
+ }
+
+ /**
+ * 解码
+ * @param s 已经编码的字符串
+ * @return 已经解码的字符串
+ */
+ String decoding(String s) {
+ StringBuilder builder = new StringBuilder();
+ // 提取(off,length,next_char)
+ String[] arr = s.split("\\)\\(");
+ if (arr.length==0){
+ return "";
+ }
+ arr[0]=arr[0].substring(1,arr[0].length());
+ if(arr.length>1){
+ arr[arr.length-1]=arr[arr.length-1].substring(0,arr[arr.length-1].length()-1);
+ }
+ for(String it : arr){
+ String[] data = it.split(",");
+ Integer off = Integer.valueOf(data[0]);
+ Integer length = Integer.valueOf(data[1]);
+ String nextChar = data[2];
+ Integer iv = builder.length()-off;
+ for (int i = 0; i < length; i++) {
+ builder.append(builder.charAt(iv+i));
+ }
+ builder.append(nextChar);
+ }
+ return builder.toString();
+ }
+
+ public static void main(String[] args) {
+ LZ77Codec codec = new LZ77Codec();
+ String input = "AABCAABCCAABCE";
+// String output = "(0,0,A)(1,1,B)(0,0,C)(4,4,C)(5,4,E)";
+ String code = codec.encoding(input);
+ System.out.println(code);
+
+ String message = codec.decoding(code);
+ System.out.println(message);
+ }
+}
diff --git a/src/main/java/com/algorithm/study/demo/model/User.java b/src/main/java/com/algorithm/study/demo/model/User.java
index ae11880..99395ee 100644
--- a/src/main/java/com/algorithm/study/demo/model/User.java
+++ b/src/main/java/com/algorithm/study/demo/model/User.java
@@ -6,7 +6,8 @@
public class User {
private int id;
private String name;
-
+ public User(){
+ }
public User(int id,String name){
this.id=id;
this.name=name;
From 347c22a0db9fa09443694187cedf27e6110f62ca Mon Sep 17 00:00:00 2001
From: liuxun <553798608@qq.com>
Date: Tue, 26 Nov 2019 22:23:50 +0800
Subject: [PATCH 14/61] Merge branch 'master' of
/Users/lolita/gitwork/algorithm-study with conflicts.
---
pom.xml | 28 +++----------------
src/main/java/com/algorithm/study/demo/A.java | 4 +++
.../study/demo/algorithm/FindProject.java | 7 -----
.../study/demo/base/AppleMobile.java | 5 ++++
.../study/demo/base/HuaweiMobile.java | 13 +++++++++
.../algorithm/study/demo/base/IPerson.java | 5 ++++
.../study/demo/base/IphoneXMobile.java | 8 ++++++
.../algorithm/study/demo/base/MainTest.java | 22 +++++++++++++++
.../com/algorithm/study/demo/base/Mobile.java | 9 ++++++
.../study/demo/base/YellowPerson.java | 8 ++++++
.../com/algorithm/study/demo/testng/Test.java | 11 ++++++++
.../study/demo/thread/ThreadTest.java | 7 +++++
12 files changed, 96 insertions(+), 31 deletions(-)
create mode 100644 src/main/java/com/algorithm/study/demo/A.java
create mode 100644 src/main/java/com/algorithm/study/demo/base/AppleMobile.java
create mode 100644 src/main/java/com/algorithm/study/demo/base/HuaweiMobile.java
create mode 100644 src/main/java/com/algorithm/study/demo/base/IPerson.java
create mode 100644 src/main/java/com/algorithm/study/demo/base/IphoneXMobile.java
create mode 100644 src/main/java/com/algorithm/study/demo/base/MainTest.java
create mode 100644 src/main/java/com/algorithm/study/demo/base/Mobile.java
create mode 100644 src/main/java/com/algorithm/study/demo/base/YellowPerson.java
create mode 100644 src/main/java/com/algorithm/study/demo/testng/Test.java
create mode 100644 src/main/java/com/algorithm/study/demo/thread/ThreadTest.java
diff --git a/pom.xml b/pom.xml
index 7eca3bf..aff5910 100644
--- a/pom.xml
+++ b/pom.xml
@@ -20,32 +20,12 @@
cglib
2.2.2
-
-
- org.apache.zookeeper
- zookeeper
- 3.4.6
-
-
-
- com.github.rholder
- guava-retrying
- 2.0.0
-
-
-
- com.wuyushuo
- spring-mould-beyond
- 1.2.6
-
-
-
-
+
- com.ly.isbase
- isbase
- 1.0.14
+ org.testng
+ testng
+ 6.14.3
diff --git a/src/main/java/com/algorithm/study/demo/A.java b/src/main/java/com/algorithm/study/demo/A.java
new file mode 100644
index 0000000..a767f73
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/A.java
@@ -0,0 +1,4 @@
+package com.algorithm.study.demo;
+
+public class A {
+}
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/FindProject.java b/src/main/java/com/algorithm/study/demo/algorithm/FindProject.java
index b46445d..90ca547 100644
--- a/src/main/java/com/algorithm/study/demo/algorithm/FindProject.java
+++ b/src/main/java/com/algorithm/study/demo/algorithm/FindProject.java
@@ -1,12 +1,5 @@
package com.algorithm.study.demo.algorithm;
-import com.alibaba.fastjson.JSON;
-import com.sun.deploy.util.StringUtils;
-
-import java.util.*;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
/**
* 查找算法
* Created by liuxun on 2017/4/25.
diff --git a/src/main/java/com/algorithm/study/demo/base/AppleMobile.java b/src/main/java/com/algorithm/study/demo/base/AppleMobile.java
new file mode 100644
index 0000000..47dd67b
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/base/AppleMobile.java
@@ -0,0 +1,5 @@
+package com.algorithm.study.demo.base;
+
+public abstract class AppleMobile extends Mobile{
+
+}
diff --git a/src/main/java/com/algorithm/study/demo/base/HuaweiMobile.java b/src/main/java/com/algorithm/study/demo/base/HuaweiMobile.java
new file mode 100644
index 0000000..c472308
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/base/HuaweiMobile.java
@@ -0,0 +1,13 @@
+package com.algorithm.study.demo.base;
+
+public class HuaweiMobile extends Mobile{
+
+ @Override
+ public void call() {
+ System.out.println("huawei call");
+ }
+ @Override
+ public void show(){
+ System.out.println("niubi show");
+ }
+}
diff --git a/src/main/java/com/algorithm/study/demo/base/IPerson.java b/src/main/java/com/algorithm/study/demo/base/IPerson.java
new file mode 100644
index 0000000..34d7545
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/base/IPerson.java
@@ -0,0 +1,5 @@
+package com.algorithm.study.demo.base;
+
+public interface IPerson {
+ void eat();
+}
diff --git a/src/main/java/com/algorithm/study/demo/base/IphoneXMobile.java b/src/main/java/com/algorithm/study/demo/base/IphoneXMobile.java
new file mode 100644
index 0000000..96d35fc
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/base/IphoneXMobile.java
@@ -0,0 +1,8 @@
+package com.algorithm.study.demo.base;
+
+public class IphoneXMobile extends AppleMobile {
+ @Override
+ public void call() {
+ System.out.println("iphonex call");
+ }
+}
diff --git a/src/main/java/com/algorithm/study/demo/base/MainTest.java b/src/main/java/com/algorithm/study/demo/base/MainTest.java
new file mode 100644
index 0000000..e5798a7
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/base/MainTest.java
@@ -0,0 +1,22 @@
+package com.algorithm.study.demo.base;
+
+public class MainTest {
+ private static final String msgg="123";
+ public static void main(String[] args) {
+ MainTest mo=new MainTest();
+ Mobile mobile=new IphoneXMobile();
+ mobile.call();
+ mobile.show();
+
+ System.out.println(mobile);
+ Mobile mobile2=new HuaweiMobile();
+ mobile2.call();
+ mobile2.show();
+
+ System.out.println(mobile);
+
+
+ IPerson per = new YellowPerson();
+ per.eat();
+ }
+}
diff --git a/src/main/java/com/algorithm/study/demo/base/Mobile.java b/src/main/java/com/algorithm/study/demo/base/Mobile.java
new file mode 100644
index 0000000..e77aebf
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/base/Mobile.java
@@ -0,0 +1,9 @@
+package com.algorithm.study.demo.base;
+
+public abstract class Mobile {
+ public abstract void call();
+
+ void show(){
+ System.out.println("show");
+ }
+}
diff --git a/src/main/java/com/algorithm/study/demo/base/YellowPerson.java b/src/main/java/com/algorithm/study/demo/base/YellowPerson.java
new file mode 100644
index 0000000..4d6945a
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/base/YellowPerson.java
@@ -0,0 +1,8 @@
+package com.algorithm.study.demo.base;
+
+public class YellowPerson implements IPerson {
+ @Override
+ public void eat() {
+ System.out.println("kuaizi eat");
+ }
+}
diff --git a/src/main/java/com/algorithm/study/demo/testng/Test.java b/src/main/java/com/algorithm/study/demo/testng/Test.java
new file mode 100644
index 0000000..2f1f84a
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/testng/Test.java
@@ -0,0 +1,11 @@
+package com.algorithm.study.demo.testng;
+
+import com.algorithm.study.demo.thread.SleepUtils;
+
+public class Test {
+
+ @org.testng.annotations.Test(threadPoolSize = 10, invocationCount = 10000)
+ public void testJsf(){
+ System.out.println("asdklfjalskfdj"+Thread.currentThread().getName());
+ }
+}
diff --git a/src/main/java/com/algorithm/study/demo/thread/ThreadTest.java b/src/main/java/com/algorithm/study/demo/thread/ThreadTest.java
new file mode 100644
index 0000000..29aeda9
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/thread/ThreadTest.java
@@ -0,0 +1,7 @@
+package com.algorithm.study.demo.thread;
+
+public class ThreadTest {
+ public static void main(String[] args) {
+
+ }
+}
From e6bdc2b455c69476f4e290fb20a19d0d6df2ed9d Mon Sep 17 00:00:00 2001
From: liuxun <553798608@qq.com>
Date: Tue, 26 Nov 2019 22:34:04 +0800
Subject: [PATCH 15/61] =?UTF-8?q?maven=E5=8C=85=E5=8D=87=E7=BA=A7?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 33 ++++++++
src/main/java/com/algorithm/study/demo/A.java | 4 -
.../java/com/algorithm/study/demo/Demo1.java | 29 +++----
.../com/algorithm/study/demo/TestBase64.java | 64 ---------------
.../algorithm/study/demo/guava/MainTest.java | 80 +++++++++----------
5 files changed, 82 insertions(+), 128 deletions(-)
delete mode 100644 src/main/java/com/algorithm/study/demo/A.java
delete mode 100644 src/main/java/com/algorithm/study/demo/TestBase64.java
diff --git a/pom.xml b/pom.xml
index aff5910..5496700 100644
--- a/pom.xml
+++ b/pom.xml
@@ -27,6 +27,39 @@
testng
6.14.3
+
+
+
+ com.google.guava
+ guava
+ 23.0
+
+
+
+ commons-io
+ commons-io
+ 2.4
+
+
+
+ org.projectlombok
+ lombok
+ 1.18.8
+ provided
+
+
+
+ joda-time
+ joda-time
+ 2.10.1
+
+
+
+ org.apache.commons
+ commons-lang3
+ 3.9
+
+
diff --git a/src/main/java/com/algorithm/study/demo/A.java b/src/main/java/com/algorithm/study/demo/A.java
deleted file mode 100644
index a767f73..0000000
--- a/src/main/java/com/algorithm/study/demo/A.java
+++ /dev/null
@@ -1,4 +0,0 @@
-package com.algorithm.study.demo;
-
-public class A {
-}
diff --git a/src/main/java/com/algorithm/study/demo/Demo1.java b/src/main/java/com/algorithm/study/demo/Demo1.java
index 1a9c4c4..4737374 100644
--- a/src/main/java/com/algorithm/study/demo/Demo1.java
+++ b/src/main/java/com/algorithm/study/demo/Demo1.java
@@ -1,16 +1,5 @@
package com.algorithm.study.demo;
-import com.algorithm.study.demo.model.User;
-import com.alibaba.fastjson.JSON;
-import org.apache.commons.lang3.StringUtils;
-
-import java.lang.reflect.Field;
-import java.math.BigDecimal;
-import java.math.BigInteger;
-import java.util.Comparator;
-import java.util.Iterator;
-import java.util.TreeMap;
-
/**
* @title: Demo1
* @projectName algorithm-study
@@ -19,15 +8,15 @@
*/
public class Demo1 {
public static void main(String[] args) throws Exception {
- User user=new User();
- user.setId(1);
- setUserName(user,"name","刘勋");
- System.out.println(JSON.toJSONString(user));
+ System.out.println(StrToBinstr("a"));
}
-
- private static void setUserName(User t, String column, String name) throws Exception {
- Field f = t.getClass().getDeclaredField(column);
- f.setAccessible(true);
- f.set(t, name);
+ // 将字符串转换成二进制字符串,以空格相隔
+ private static String StrToBinstr(String str) {
+ char[] strChar = str.toCharArray();
+ String result = "";
+ for (int i = 0; i < strChar.length; i++) {
+ result += Integer.toBinaryString(strChar[i]) + " ";
+ }
+ return result;
}
}
diff --git a/src/main/java/com/algorithm/study/demo/TestBase64.java b/src/main/java/com/algorithm/study/demo/TestBase64.java
deleted file mode 100644
index 4af2a70..0000000
--- a/src/main/java/com/algorithm/study/demo/TestBase64.java
+++ /dev/null
@@ -1,64 +0,0 @@
-package com.algorithm.study.demo;
-
-import com.alibaba.fastjson.JSON;
-import com.ly.ie.common.utils.HessianUtils;
-import com.ly.isbase.util.Base64Util;
-import org.apache.commons.codec.binary.Base64;
-import org.apache.commons.io.FileUtils;
-
-import java.io.File;
-
-/**
- * @title: TestBase64
- * @projectName algorithm-study
- * @description: TODO
- * @date 2019/10/18 11:37
- */
-public class TestBase64 {
-
- /**
- * BASE64解密
- * @throws Exception
- */
- public static Object decryptBASE64(String key) throws Exception {
- return HessianUtils.fromBytes(Base64Util.decode(key));
- }
- /**
- * BASE64加密
- */
- public static String encryptBASE64(String key) throws Exception {
- return Base64Util.encodeToString(HessianUtils.toBytes(key));
- }
- /**
- * BASE64加密解密
- */
- public static void enAndDeCode(String str) throws Exception {
- System.out.println("压缩前长度:"+str.length());
- String data = encryptBASE64(str);
- System.out.println("压缩后长度:"+data.length());
- Object byteArray = decryptBASE64(data);
- FileUtils.write(new File("D:/78910.txt"),byteArray.toString(),"UTF-8");
-
- int num=10000;
-
- long beginTime = System.currentTimeMillis();
- for (int i = 0; i < num; i++) {
- encryptBASE64(str);
- }
- long endTime = System.currentTimeMillis();
- System.out.println("压缩总耗时"+(endTime - beginTime));
- System.out.println("压缩平均耗时"+(endTime - beginTime) / 10000);
-
- long currentTimeMillis = System.currentTimeMillis();
- for (int i = 0; i < 10000; i++) {
- decryptBASE64(data);
- }
- long endTimeMillis = System.currentTimeMillis();
- System.out.println("解压总耗时"+(endTimeMillis - currentTimeMillis));
- System.out.println("解压平均耗时"+(endTimeMillis - currentTimeMillis) / 10000);
- }
- public static void main(String[] args) throws Exception {
- String strOld = FileUtils.readFileToString(new File("D:/123456.txt"), "utf-8");
- enAndDeCode(strOld);
- }
-}
diff --git a/src/main/java/com/algorithm/study/demo/guava/MainTest.java b/src/main/java/com/algorithm/study/demo/guava/MainTest.java
index 00452d6..445274e 100644
--- a/src/main/java/com/algorithm/study/demo/guava/MainTest.java
+++ b/src/main/java/com/algorithm/study/demo/guava/MainTest.java
@@ -1,40 +1,40 @@
-package com.algorithm.study.demo.guava;
-
-import com.github.rholder.retry.*;
-import com.google.common.base.Predicates;
-
-import java.io.*;
-import java.text.SimpleDateFormat;
-import java.time.LocalDateTime;
-import java.time.format.DateTimeFormatter;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeUnit;
-
-/**
- * guava 重试机制
- * @Author: liuxun
- * @CreateDate: 2019/1/2 上午11:29
- * @Version: 1.0
- */
-public class MainTest {
- public static void main(String[] args) {
- //定义重试机制
- Retryer retryer = RetryerBuilder.newBuilder()
- .retryIfException() //设置异常重试
- .retryIfResult(Predicates.equalTo(true)) //call方法返回true重试
- .withWaitStrategy(WaitStrategies.fixedWait(10, TimeUnit.SECONDS)) //设置10秒后重试
- .withStopStrategy(StopStrategies.stopAfterAttempt(3)).build(); //设置重试次数 超过将出异常
- try {
- retryer.call(() -> {
- //这里写你的业务逻辑代码
- System.out.println("11111111111111111122222");
- return true; //需要重试返回true
- });
- } catch (ExecutionException e) {
- e.printStackTrace();
- } catch (RetryException e) {
- e.printStackTrace();
- }
- }
-}
-
+//package com.algorithm.study.demo.guava;
+//
+//import com.github.rholder.retry.*;
+//import com.google.common.base.Predicates;
+//
+//import java.io.*;
+//import java.text.SimpleDateFormat;
+//import java.time.LocalDateTime;
+//import java.time.format.DateTimeFormatter;
+//import java.util.concurrent.ExecutionException;
+//import java.util.concurrent.TimeUnit;
+//
+///**
+// * guava 重试机制
+// * @Author: liuxun
+// * @CreateDate: 2019/1/2 上午11:29
+// * @Version: 1.0
+// */
+//public class MainTest {
+// public static void main(String[] args) {
+// //定义重试机制
+// Retryer retryer = RetryerBuilder.newBuilder()
+// .retryIfException() //设置异常重试
+// .retryIfResult(Predicates.equalTo(true)) //call方法返回true重试
+// .withWaitStrategy(WaitStrategies.fixedWait(10, TimeUnit.SECONDS)) //设置10秒后重试
+// .withStopStrategy(StopStrategies.stopAfterAttempt(3)).build(); //设置重试次数 超过将出异常
+// try {
+// retryer.call(() -> {
+// //这里写你的业务逻辑代码
+// System.out.println("11111111111111111122222");
+// return true; //需要重试返回true
+// });
+// } catch (ExecutionException e) {
+// e.printStackTrace();
+// } catch (RetryException e) {
+// e.printStackTrace();
+// }
+// }
+//}
+//
From 928e45cc6359167357dc039ce0ff8c6dda8e3025 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Wed, 27 Nov 2019 12:15:56 +0800
Subject: [PATCH 16/61] =?UTF-8?q?LZ77=E7=AE=97=E6=B3=95=E7=AE=80=E5=8D=95?=
=?UTF-8?q?=E5=AE=9E=E7=8E=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../algorithm/huffman/TestHuffmanCode.java | 28 ++++------------
.../study/demo/algorithm/lz77/LZ77Codec.java | 1 +
.../study/demo/algorithm/string/Solution.java | 32 +++++++++++++++++++
3 files changed, 40 insertions(+), 21 deletions(-)
create mode 100644 src/main/java/com/algorithm/study/demo/algorithm/string/Solution.java
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/huffman/TestHuffmanCode.java b/src/main/java/com/algorithm/study/demo/algorithm/huffman/TestHuffmanCode.java
index a069bf4..8fe436f 100644
--- a/src/main/java/com/algorithm/study/demo/algorithm/huffman/TestHuffmanCode.java
+++ b/src/main/java/com/algorithm/study/demo/algorithm/huffman/TestHuffmanCode.java
@@ -14,42 +14,28 @@ public class TestHuffmanCode {
public static void main(String[] args) {
// for (int i = 0; i < 5; i++) {
int valueRand = ThreadLocalRandom.current().nextInt(1, 50);
-
StringBuilder msg = new StringBuilder();
-
for (int j = 0; j < valueRand; j++) {
msg.append((char) ThreadLocalRandom.current().nextInt(65, 122));
}
- String src = "我我是是一只小小鸟" + msg.toString();
+ String src = "aabbcceed" + msg.toString();
+ System.out.println("原始:" + src);
Map conMap = StrProc.countCharset(src);
- System.out.println(conMap);
+ System.out.println("字符频率统计:"+conMap);
HuffmanCode instance = new HuffmanCode();
Map huffCode = instance.getHuffManCode(conMap);
- System.out.println(huffCode);
-
- Integer value = Integer.parseInt("1010", 2);
- System.out.println(value);
+ System.out.println("huffcode字符编码映射:"+huffCode);
- // Map parseTwo = instance.encodeHuf(huffCode);
-
- // String hufOutValue = instance.parseHuffman(src, parseTwo);
String hufOutValue = instance.parseHuffman2(src, huffCode);
- System.out.println(hufOutValue);
- // DataOutputStreamHuffman.OUTPUT.outtoFile(src.getBytes(StandardCharsets.UTF_8));
- // DataOutputStreamHuffman.OUTPUT.outHuffmantoFile(hufOutValue.getBytes());
+ System.out.println("最终编码:"+hufOutValue);
String deValue = instance.decodeHuffman(hufOutValue, instance.root);
- System.out.println("原始:" + src);
- System.out.println("结果:" + deValue);
-
-// Assert.assertEquals(src, deValue);
+ System.out.println("解压结果:" + deValue);
- System.out.println(
- "--------------------------------------------------------------------------------");
+ System.out.println("--------------------------------------------------------------------------------");
}
-// }
}
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/lz77/LZ77Codec.java b/src/main/java/com/algorithm/study/demo/algorithm/lz77/LZ77Codec.java
index 3a03fc4..7c0c777 100644
--- a/src/main/java/com/algorithm/study/demo/algorithm/lz77/LZ77Codec.java
+++ b/src/main/java/com/algorithm/study/demo/algorithm/lz77/LZ77Codec.java
@@ -106,6 +106,7 @@ String decoding(String s) {
builder.append(builder.charAt(iv+i));
}
builder.append(nextChar);
+ System.out.println("decoding:"+iv+" "+ builder.toString());
}
return builder.toString();
}
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/string/Solution.java b/src/main/java/com/algorithm/study/demo/algorithm/string/Solution.java
new file mode 100644
index 0000000..ddf6ed5
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/algorithm/string/Solution.java
@@ -0,0 +1,32 @@
+package com.algorithm.study.demo.algorithm.string;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @author xun2.liu
+ * @title: Solution
+ * @projectName algorithm-study
+ * @description: 给定一个字符串,查找不重复字符的最长子字符串的长度。
+ * @date 2019/11/27 11:15
+ */
+public class Solution {
+ int lengthOfLongestSubstring(String s) {
+ int n = s.length();
+ Set set = new HashSet();
+ int ans = 0, i = 0, j = 0;
+ while (i < n && j < n) {
+ if (!set.contains(s.charAt(j))) {
+ set.add(s.charAt(j++));//如果不包含,j就自增
+ ans = Math.max(ans, j - i);//j - i = 最大的不重复的长度。
+ } else {
+ set.remove(s.charAt(i++));//如果包含,i就增,并把窗口后滑
+ }
+ }
+ return ans;
+ }
+ public static void main(String[] args) {
+ String s = "jkklmmds";
+ System.out.println(new Solution().lengthOfLongestSubstring(s));
+ }
+}
From 5852cb40a050cf5d3f074f29c7c4f05d4a118443 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Wed, 27 Nov 2019 13:47:19 +0800
Subject: [PATCH 17/61] =?UTF-8?q?LZ77=E7=AE=97=E6=B3=95=E7=AE=80=E5=8D=95?=
=?UTF-8?q?=E5=AE=9E=E7=8E=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/algorithm/study/demo/GZIPUtils.java | 3 ---
.../algorithm/study/demo/algorithm/huffman/HuffmanCode.java | 2 +-
2 files changed, 1 insertion(+), 4 deletions(-)
diff --git a/src/main/java/com/algorithm/study/demo/GZIPUtils.java b/src/main/java/com/algorithm/study/demo/GZIPUtils.java
index 6d6ad8a..dab43ce 100644
--- a/src/main/java/com/algorithm/study/demo/GZIPUtils.java
+++ b/src/main/java/com/algorithm/study/demo/GZIPUtils.java
@@ -203,9 +203,6 @@ public static void main(String[] args) throws IOException {
String unzip = unzip(gzip);
FileUtils.write(new File("D:/2234567.txt"),unzip,"UTF-8");
- Map map= Maps.newHashMap();
- map.put("iflight.java.dsf.itradecore",100);
- System.out.println(JSON.toJSONString(map));
// int num=10000;
//
// long beginTime = System.currentTimeMillis();
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/huffman/HuffmanCode.java b/src/main/java/com/algorithm/study/demo/algorithm/huffman/HuffmanCode.java
index 9d72631..6de61a1 100644
--- a/src/main/java/com/algorithm/study/demo/algorithm/huffman/HuffmanCode.java
+++ b/src/main/java/com/algorithm/study/demo/algorithm/huffman/HuffmanCode.java
@@ -5,7 +5,7 @@
* @author xun2.liu
* @title: HuffmanCode
* @projectName algorithm-study
- * @description: TODO
+ * @description: Huffman编码
* @date 2019/11/19 17:03
*/
public class HuffmanCode {
From ab170a8fa20374c7a9409868320f9b995389fda3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Wed, 27 Nov 2019 18:48:14 +0800
Subject: [PATCH 18/61] =?UTF-8?q?huffman=E7=BC=96=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/algorithm/study/demo/Demo1.java | 6 +++++-
src/main/java/com/algorithm/study/demo/GZIPUtils.java | 4 ++--
.../study/demo/algorithm/huffman/TestHuffmanCode.java | 2 +-
3 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/src/main/java/com/algorithm/study/demo/Demo1.java b/src/main/java/com/algorithm/study/demo/Demo1.java
index 4737374..420a65d 100644
--- a/src/main/java/com/algorithm/study/demo/Demo1.java
+++ b/src/main/java/com/algorithm/study/demo/Demo1.java
@@ -1,5 +1,7 @@
package com.algorithm.study.demo;
+import java.math.BigDecimal;
+
/**
* @title: Demo1
* @projectName algorithm-study
@@ -8,7 +10,9 @@
*/
public class Demo1 {
public static void main(String[] args) throws Exception {
- System.out.println(StrToBinstr("a"));
+ BigDecimal b = new BigDecimal(0.115);
+ double d = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
+ System.out.println(d);
}
// 将字符串转换成二进制字符串,以空格相隔
private static String StrToBinstr(String str) {
diff --git a/src/main/java/com/algorithm/study/demo/GZIPUtils.java b/src/main/java/com/algorithm/study/demo/GZIPUtils.java
index dab43ce..299f705 100644
--- a/src/main/java/com/algorithm/study/demo/GZIPUtils.java
+++ b/src/main/java/com/algorithm/study/demo/GZIPUtils.java
@@ -196,13 +196,13 @@ public static final String unzip(String compressedStr) {
}
public static void main(String[] args) throws IOException {
- String strOld = FileUtils.readFileToString(new File("D:/123456.txt"), "utf-8");
+ //78910.txt 123456.txt
+ String strOld = FileUtils.readFileToString(new File("D:/78910.txt"), "utf-8");
System.out.println("压缩前长度:"+strOld.length());
String gzip = zip(strOld);
System.out.println("压缩后长度:"+gzip.length());
String unzip = unzip(gzip);
FileUtils.write(new File("D:/2234567.txt"),unzip,"UTF-8");
-
// int num=10000;
//
// long beginTime = System.currentTimeMillis();
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/huffman/TestHuffmanCode.java b/src/main/java/com/algorithm/study/demo/algorithm/huffman/TestHuffmanCode.java
index 8fe436f..af4f299 100644
--- a/src/main/java/com/algorithm/study/demo/algorithm/huffman/TestHuffmanCode.java
+++ b/src/main/java/com/algorithm/study/demo/algorithm/huffman/TestHuffmanCode.java
@@ -19,7 +19,7 @@ public static void main(String[] args) {
msg.append((char) ThreadLocalRandom.current().nextInt(65, 122));
}
- String src = "aabbcceed" + msg.toString();
+ String src = "aaaaaaabbcceed" + msg.toString();
System.out.println("原始:" + src);
Map conMap = StrProc.countCharset(src);
From 877771fea7d8d71c2804b893dfe1eae7380e0ca6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Tue, 7 Jan 2020 14:53:55 +0800
Subject: [PATCH 19/61] =?UTF-8?q?=E6=9E=9A=E4=B8=BE=E7=B1=BB=E8=A7=A3?=
=?UTF-8?q?=E5=86=B3IF=20ESLE=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../study/demo/enums/Calculator.java | 15 +++
.../algorithm/study/demo/enums/MainTest.java | 25 +++++
.../algorithm/study/demo/enums/Operator.java | 48 ++++++++++
.../study/demo/java8/FunctionTest.java | 95 +++++++++++++++++++
4 files changed, 183 insertions(+)
create mode 100644 src/main/java/com/algorithm/study/demo/enums/Calculator.java
create mode 100644 src/main/java/com/algorithm/study/demo/enums/MainTest.java
create mode 100644 src/main/java/com/algorithm/study/demo/enums/Operator.java
create mode 100644 src/main/java/com/algorithm/study/demo/java8/FunctionTest.java
diff --git a/src/main/java/com/algorithm/study/demo/enums/Calculator.java b/src/main/java/com/algorithm/study/demo/enums/Calculator.java
new file mode 100644
index 0000000..2e4a0c2
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/enums/Calculator.java
@@ -0,0 +1,15 @@
+package com.algorithm.study.demo.enums;
+
+/**
+ * @author xun2.liu
+ * @title: Calculator
+ * @projectName algorithm-study
+ * @description: TODO
+ * @date 2020/1/7 14:51
+ */
+public class Calculator{
+
+ public int apply(int a, int b,Operator operator) {
+ return operator.apply(a,b);
+ }
+}
diff --git a/src/main/java/com/algorithm/study/demo/enums/MainTest.java b/src/main/java/com/algorithm/study/demo/enums/MainTest.java
new file mode 100644
index 0000000..3bbd4ef
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/enums/MainTest.java
@@ -0,0 +1,25 @@
+package com.algorithm.study.demo.enums;
+
+import com.algorithm.study.demo.JodaTimeUtil;
+import com.alibaba.fastjson.JSON;
+import com.google.common.collect.Maps;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.*;
+
+/**
+ * @author xun2.liu
+ * @title: MainTest
+ * @projectName algorithm-study
+ * @description: 枚举类解决IF ESLE问题
+ * @date 2019/12/13 16:32
+ */
+public class MainTest{
+ public static void main(String[] args) throws ParseException {
+ Calculator calculator=new Calculator();
+ int result=calculator.apply(2,4,Operator.ADD);
+ System.out.println(result);
+ }
+
+}
diff --git a/src/main/java/com/algorithm/study/demo/enums/Operator.java b/src/main/java/com/algorithm/study/demo/enums/Operator.java
new file mode 100644
index 0000000..54201b9
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/enums/Operator.java
@@ -0,0 +1,48 @@
+package com.algorithm.study.demo.enums;
+
+/**
+ * @author xun2.liu
+ * @title: Operator
+ * @projectName algorithm-study
+ * @description: TODO
+ * @date 2019/12/13 16:31
+ */
+public enum Operator {
+
+ ADD {
+ @Override
+ public int apply(int a, int b) {
+ return a + b;
+ }
+ },
+
+ MULTIPLY {
+ @Override
+ public int apply(int a, int b) {
+ return a * b;
+ }
+ },
+
+ SUBTRACT {
+ @Override
+ public int apply(int a, int b) {
+ return a - b;
+ }
+ },
+
+ DIVIDE {
+ @Override
+ public int apply(int a, int b) {
+ return a / b;
+ }
+ },
+
+ MODULO {
+ @Override
+ public int apply(int a, int b) {
+ return a % b;
+ }
+ };
+
+ public abstract int apply(int a, int b);
+}
\ No newline at end of file
diff --git a/src/main/java/com/algorithm/study/demo/java8/FunctionTest.java b/src/main/java/com/algorithm/study/demo/java8/FunctionTest.java
new file mode 100644
index 0000000..1706fae
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/java8/FunctionTest.java
@@ -0,0 +1,95 @@
+package com.algorithm.study.demo.java8;
+
+import org.testng.Assert;
+
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Predicate;
+
+/**
+ * @author xun2.liu
+ * @title: FunctionTest
+ * @projectName algorithm-study
+ * @description: Consumer实例
+ * @date 2019/12/20 15:19
+ */
+public class FunctionTest {
+ public static void main(String[] args) {
+// consumerTest();
+ functionTest();
+ }
+
+ /***
+ * Consumer是一个函数式编程接口; 顾名思义,Consumer的意思就是消费,即针对某个东西我们来使用它,因此它包含有一个有输入而无输出的accept接口方法;
+ * 除accept方法,它还包含有andThen这个方法;
+ */
+ public static void consumerTest() {
+ Consumer f = System.out::println;
+ Consumer f2 = n -> System.out.println(n + "-F2");
+
+ //执行完F后再执行F2的Accept方法
+ f.andThen(f2).accept("test");
+
+ //连续执行F的Accept方法
+// f.andThen(f).andThen(f).andThen(f).accept("test1");
+ }
+
+ /**
+ * Function测试
+ * Function也是一个函数式编程接口;它代表的含义是“函数”,而函数经常是有输入输出的,因此它含有一个apply方法,
+ * 包含一个输入与一个输出;除apply方法外,它还有compose与andThen及indentity三个方法
+ */
+ public static void functionTest() {
+ Function f = s -> s++;
+ Function g = s -> s * 2;
+
+ /**
+ * 下面表示在执行F时,先执行G,并且执行F时使用G的输出当作输入。
+ * 相当于以下代码:
+ * Integer a = g.apply(1);
+ * System.out.println(f.apply(a));
+ */
+ System.out.println(f.compose(g).apply(1));
+
+ /**
+ * 表示执行F的Apply后使用其返回的值当作输入再执行G的Apply;
+ * 相当于以下代码
+ * Integer a = f.apply(1);
+ * System.out.println(g.apply(a));
+ */
+ System.out.println(f.andThen(g).apply(1));
+
+ /**
+ * identity方法会返回一个不进行任何处理的Function,即输出与输入值相等;
+ */
+ System.out.println(Function.identity().apply("a"));
+ }
+
+
+
+ /**
+ * Predicate测试
+ * Predicate为函数式接口,predicate的中文意思是“断定”,即判断的意思,判断某个东西是否满足某种条件;
+ * 因此它包含test方法,根据输入值来做逻辑判断,其结果为True或者False。
+ */
+ private static void predicateTest() {
+ Predicate p = o -> o.equals("test");
+ Predicate g = o -> o.startsWith("t");
+
+ /**
+ * negate: 用于对原来的Predicate做取反处理;
+ * 如当调用p.test("test")为True时,调用p.negate().test("test")就会是False;
+ */
+ Assert.assertFalse(p.negate().test("test"));
+
+ /**
+ * and: 针对同一输入值,多个Predicate均返回True时返回True,否则返回False;
+ */
+ Assert.assertTrue(p.and(g).test("test"));
+
+ /**
+ * or: 针对同一输入值,多个Predicate只要有一个返回True则返回True,否则返回False
+ */
+ Assert.assertTrue(p.or(g).test("ta"));
+ }
+}
From 283a3cf30c096c6c3f5b497f6026190e7c94e843 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Fri, 20 Mar 2020 15:50:09 +0800
Subject: [PATCH 20/61] =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../java/com/algorithm/study/demo/Demo1.java | 25 +++++-----
.../algorithm/dynamicprogramming/demo1.java | 50 +++++++++++++++++++
.../study/demo/algorithm/leetcode/Day1.java | 24 +++++++++
3 files changed, 87 insertions(+), 12 deletions(-)
create mode 100644 src/main/java/com/algorithm/study/demo/algorithm/dynamicprogramming/demo1.java
create mode 100644 src/main/java/com/algorithm/study/demo/algorithm/leetcode/Day1.java
diff --git a/src/main/java/com/algorithm/study/demo/Demo1.java b/src/main/java/com/algorithm/study/demo/Demo1.java
index 420a65d..dcbb121 100644
--- a/src/main/java/com/algorithm/study/demo/Demo1.java
+++ b/src/main/java/com/algorithm/study/demo/Demo1.java
@@ -1,5 +1,7 @@
package com.algorithm.study.demo;
+import org.apache.commons.lang3.StringUtils;
+
import java.math.BigDecimal;
/**
@@ -10,17 +12,16 @@
*/
public class Demo1 {
public static void main(String[] args) throws Exception {
- BigDecimal b = new BigDecimal(0.115);
- double d = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
- System.out.println(d);
- }
- // 将字符串转换成二进制字符串,以空格相隔
- private static String StrToBinstr(String str) {
- char[] strChar = str.toCharArray();
- String result = "";
- for (int i = 0; i < strChar.length; i++) {
- result += Integer.toBinaryString(strChar[i]) + " ";
- }
- return result;
+// String str="2020-01-07 19:42:06.386 INFO [BUS_VCC_ORDER_DETAIL][common][][BJ2001071650476613943652869165056][VCC20200107173708DDPSYV]PsiDAOProxy 类 listBySerialNo 方法入参:BJ2001071650476613943652869165056";
+// String traceId="VCC20200107173708DDPSYV";
+// String p="["+traceId+"]";
+// if (str.indexOf(p)>0){
+// str=StringUtils.substring(str,str.indexOf("["+traceId+"]")+p.length());
+// }
+// System.out.println(str);
+ String msg="asdfasdf";
+ String throwable="asdfasdfasdfadaskdjfaskdjf";
+ msg+=throwable;
+ System.out.println(msg);
}
}
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/dynamicprogramming/demo1.java b/src/main/java/com/algorithm/study/demo/algorithm/dynamicprogramming/demo1.java
new file mode 100644
index 0000000..9fcb5f0
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/algorithm/dynamicprogramming/demo1.java
@@ -0,0 +1,50 @@
+package com.algorithm.study.demo.algorithm.dynamicprogramming;
+
+/**
+ * @author xun2.liu
+ * @title: demo1
+ * @projectName algorithm-study
+ * @description: 动态规划
+ * @date 2020/3/18 14:04
+ */
+public class demo1 {
+ public static void main(String[] args) {
+ System.out.println(fib(10));
+ System.out.println(fib2(10));
+ }
+
+ /**
+ * 重叠子问题,子问题个数为 O(2^n)。性能非常低。
+ * @param n
+ * @return
+ */
+ private static int fib(int n){
+ if(n==1 || n==2){
+ return 1;
+ }
+ return fib(n-1)+fib(n-2);
+ }
+
+ /**
+ * 带备忘录的递归解法,解决重叠子问题。
+ * 子问题个数为 O(n),时间复杂度是 O(n)
+ * @param n
+ * @return
+ */
+ private static int fib2(int n){
+ if(n==1 || n==2){
+ return 1;
+ }
+ int[] memo=new int[n+1];
+ return helper(memo,n);
+ }
+ private static int helper(int[] memo,int n){
+ if(n==1 || n==2){
+ return 1;
+ }
+ if (memo[n]!=0) {
+ return memo[n];
+ }
+ return memo[n]=helper(memo,n-1)+helper(memo,n-2);
+ }
+}
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/leetcode/Day1.java b/src/main/java/com/algorithm/study/demo/algorithm/leetcode/Day1.java
new file mode 100644
index 0000000..4b154aa
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/algorithm/leetcode/Day1.java
@@ -0,0 +1,24 @@
+package com.algorithm.study.demo.algorithm.leetcode;
+
+/**
+ * @author xun2.liu
+ * @title: Day1
+ * @projectName algorithm-study
+ * @description: TODO
+ * @date 2020/1/7 15:05
+ */
+public class Day1 {
+ public static void main(String[] args) {
+ System.out.println(Integer.MAX_VALUE);
+ }
+
+ /***
+ * 给出一个32位的有符号的整数,你需要将这个整数中每位上的数字进行反转
+ * https://leetcode-cn.com/problems/reverse-integer/
+ * @param x
+ * @return
+ */
+ public static int reverse(int x){
+ return 0;
+ }
+}
From b4e847701d348d7a313f3ab0befe4ea7a37b137a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Wed, 25 Mar 2020 11:33:27 +0800
Subject: [PATCH 21/61] =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92-?=
=?UTF-8?q?=E6=96=90=E6=B3=A2=E9=82=A3=E5=A5=91=E6=95=B0=E5=88=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../algorithm/dynamicprogramming/demo1.java | 40 ++++++++++++++++++-
1 file changed, 39 insertions(+), 1 deletion(-)
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/dynamicprogramming/demo1.java b/src/main/java/com/algorithm/study/demo/algorithm/dynamicprogramming/demo1.java
index 9fcb5f0..4488b09 100644
--- a/src/main/java/com/algorithm/study/demo/algorithm/dynamicprogramming/demo1.java
+++ b/src/main/java/com/algorithm/study/demo/algorithm/dynamicprogramming/demo1.java
@@ -4,13 +4,15 @@
* @author xun2.liu
* @title: demo1
* @projectName algorithm-study
- * @description: 动态规划
+ * @description: 动态规划-斐波那契数列
* @date 2020/3/18 14:04
*/
public class demo1 {
public static void main(String[] args) {
System.out.println(fib(10));
System.out.println(fib2(10));
+ System.out.println(fib3(10));
+ System.out.println(fib4(10));
}
/**
@@ -47,4 +49,40 @@ private static int helper(int[] memo,int n){
}
return memo[n]=helper(memo,n-1)+helper(memo,n-2);
}
+
+ /**
+ * DP table 数组的迭代解法
+ * 空间复杂度降为 O(N)
+ *自底向上计算斐波那契数列
+ * @param n
+ * @return
+ */
+ private static int fib3(int n){
+ int[] temp=new int[n+1];
+ temp[1]=temp[2]=1;
+ for (int i=3;i<=n;i++){
+ temp[i]=temp[i-1]+temp[i-2];
+ }
+ return temp[n];
+ }
+
+ /**
+ * DP table 数组的迭代解法优化
+ * 空间复杂度降为 O(1)
+ *自底向上计算斐波那契数列
+ * @param n
+ * @return
+ */
+ private static int fib4(int n){
+ if (n==1 || n==2){
+ return 1;
+ }
+ int prev=1;int curr=1;
+ for (int i=3;i<=n;i++){
+ int temp=prev+curr;
+ prev=curr;
+ curr=temp;
+ }
+ return curr;
+ }
}
From c548c9f756e40fb76c469f6af496fb7f7c188882 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Mon, 11 May 2020 15:20:29 +0800
Subject: [PATCH 22/61] LRUCache
---
.../demo/algorithm/leetcode/Solution.java | 43 +++++++++++++++++++
1 file changed, 43 insertions(+)
create mode 100644 src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution.java
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution.java b/src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution.java
new file mode 100644
index 0000000..cb825b6
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution.java
@@ -0,0 +1,43 @@
+package com.algorithm.study.demo.algorithm.leetcode;
+
+/**
+ * @author xun2.liu
+ * @title: Solution
+ * @projectName algorithm-study
+ * @description: TODO
+ * @date 2020/5/9 14:42
+ */
+public class Solution {
+
+ /**
+ * 实现 int sqrt(int x) 函数。
+ * 计算并返回 x 的平方根,其中 x 是非负整数。
+ *
+ * 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。
+ *
+ * 示例 1:
+ *
+ * 输入: 4
+ * 输出: 2
+ * 示例 2:
+ *
+ * 输入: 8
+ * 输出: 2
+ * 说明: 8 的平方根是 2.82842...,由于返回类型是整数,小数部分将被舍去。
+ * @param x
+ * @return
+ */
+ public static int mySqrt(int x) {
+ if (x==0){
+ return 0;
+ }
+ if (x==1){
+ return 1;
+ }
+ return x/2;
+ }
+
+ public static void main(String[] args) {
+ System.out.println(mySqrt(8));
+ }
+}
From 183bcafbf86ce8acb427774f0320bdbda623ce75 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Mon, 11 May 2020 15:21:09 +0800
Subject: [PATCH 23/61] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../demo/datastructure/tree/LinkBinTree.java | 74 ++++++++++++-------
1 file changed, 48 insertions(+), 26 deletions(-)
diff --git a/src/main/java/com/algorithm/study/demo/datastructure/tree/LinkBinTree.java b/src/main/java/com/algorithm/study/demo/datastructure/tree/LinkBinTree.java
index 805abcc..35e2c95 100644
--- a/src/main/java/com/algorithm/study/demo/datastructure/tree/LinkBinTree.java
+++ b/src/main/java/com/algorithm/study/demo/datastructure/tree/LinkBinTree.java
@@ -1,9 +1,9 @@
package com.algorithm.study.demo.datastructure.tree;
-import java.util.ArrayList;
-import java.util.LinkedList;
-import java.util.Queue;
-import java.util.Stack;
+import com.alibaba.fastjson.JSON;
+import org.testng.collections.Lists;
+
+import java.util.*;
/**
*
@@ -114,9 +114,13 @@ private void add(TreeNode t,int value){
}
}
private void add2(TreeNode t,int value){
+ if(null==t.data){
+ t.data=value;
+ return;
+ }
TreeNode node=new TreeNode(value);
TreeNode current=t;
- while(current!=null){
+ while(true){
TreeNode parentNode=current;
if (current.data>value){
current=current.left;
@@ -280,26 +284,43 @@ public void postOrderTraverse2(){
* 层级遍历
* @param t
*/
- public void divOrderTraverse(TreeNode t){
+ public List> divOrderTraverse(TreeNode t){
if (t==null) {
- return;
+ return new ArrayList>();
}
+ //初始化队列只包含一个节点 root 和层次编号 0 : level = 0。
+ List> levels= new ArrayList<>();
Queue queue = new LinkedList() ;
queue.add(root);
+ //树的层数
+ int level=0;
while(queue.size() != 0)
{
+ //插入一个空列表,开始当前层的算法。
+ levels.add(new ArrayList<>());
int len = queue.size();
+ //计算当前层有多少个元素:等于队列的长度。
for(int i=0;i > lists = divOrderTraverse(root);
+ System.out.println(JSON.toJSONString(lists));
}
/**区间搜索**/
private void searchSection(TreeNode t,int k1,int k2,ArrayList result){
@@ -364,9 +385,9 @@ public TreeNode findMax(){
return maxNode;
}
public static void main(String[] args) {
- int[] ls=new int[]{30,9,8};
- LinkBinTree linkBinTree=new LinkBinTree(ls[0]);
- for (int i=1;i list=new ArrayList();
// linkBinTree.searchSection(linkBinTree.getRoot(),10,20,list);
// System.out.println("区间查询"+list.toString());
-// System.out.println("-------------递归遍历----------------");
-// linkBinTree.preOrderTraverse();//前序遍历 从根节点开始遍历
-// System.out.println("-----------------------------");
-// linkBinTree.inOrderTraverse();//中序遍历 从根节点开始
-// System.out.println("-----------------------------");
-// linkBinTree.postOrderTraverse();//后序遍历
-// System.out.println("-----------------------------");
-// linkBinTree.divOrderTraverse();//层次遍历
+ System.out.println("-------------递归遍历----------------");
+ linkBinTree.preOrderTraverse();//前序遍历 从根节点开始遍历
+ System.out.println("-----------------------------");
+ linkBinTree.inOrderTraverse();//中序遍历 从根节点开始
+ System.out.println("-----------------------------");
+ linkBinTree.postOrderTraverse();//后序遍历
+ System.out.println("-----------------------------");
+ linkBinTree.divOrderTraverse();//层次遍历
+
// //前序遍历:根节点->左子树->右子树
// //中序遍历:左子树->根节点->右子树
// //后序遍历:左子树->右子树->根节点
// System.out.println();
// System.out.println("-------------非递归遍历----------------");
- linkBinTree.preOrderTraverse2();//前序遍历
+// linkBinTree.preOrderTraverse2();//前序遍历
// System.out.println("-----------------------------");
// linkBinTree.inOrderTraverse2();//中序遍历
// System.out.println("-----------------------------");
// linkBinTree.postOrderTraverse2();//后序遍历
//二叉查找树搜索
- TreeNode node = linkBinTree.find(9);
- System.out.println(node.data);
- System.out.println("最小值为:"+linkBinTree.findMin().data);
+// TreeNode node = linkBinTree.find(9);
+// System.out.println(node.data);
+// System.out.println("最小值为:"+linkBinTree.findMin().data);
}
}
From 914da66bedbbceab331d8f0cf46f0eabe82643ef Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Mon, 11 May 2020 15:21:43 +0800
Subject: [PATCH 24/61] LRU
---
.../study/demo/LRUCache/LRUCache.java | 158 +++++++++++++---
.../study/demo/LRUCache/LRULinkedMap.java | 1 +
.../algorithm/study/demo/LRUCache/LRUMap.java | 168 ------------------
.../study/demo/algorithm/leetcode/Day1.java | 24 ---
4 files changed, 132 insertions(+), 219 deletions(-)
delete mode 100644 src/main/java/com/algorithm/study/demo/LRUCache/LRUMap.java
delete mode 100644 src/main/java/com/algorithm/study/demo/algorithm/leetcode/Day1.java
diff --git a/src/main/java/com/algorithm/study/demo/LRUCache/LRUCache.java b/src/main/java/com/algorithm/study/demo/LRUCache/LRUCache.java
index 6234068..0fcbb70 100644
--- a/src/main/java/com/algorithm/study/demo/LRUCache/LRUCache.java
+++ b/src/main/java/com/algorithm/study/demo/LRUCache/LRUCache.java
@@ -1,48 +1,152 @@
package com.algorithm.study.demo.LRUCache;
-import java.util.*;
+import java.util.HashMap;
+import java.util.Map;
/**
- * LinkedHashMap实现LRU缓存
+ * LRU缓存链表实现思路
+ * 每次写入数据时将数据放入链表头结点。
+ * 使用数据时候将数据移动到头结点。
+ * 缓存数量超过阈值时移除链表尾部数据。
* @Author: liuxun
- * @CreateDate: 2019/2/13 上午10:24
+ * @CreateDate: 2018/7/12 下午6:05
* @Version: 1.0
*/
public class LRUCache {
- MapCache cache;
- public LRUCache(int capacity) {
- this.cache = new MapCache(capacity);
+ class Node{
+ private int key;
+ private int value;
+ private Node prev;
+ private Node next;
+
+ public Node(int key,int value){
+ this.key=key;
+ this.value=value;
+ }
+ public Node(){}
+
+ public int getKey() {
+ return key;
+ }
+
+ public void setKey(int key) {
+ this.key = key;
+ }
+
+ public int getValue() {
+ return value;
+ }
+
+ public void setValue(int value) {
+ this.value = value;
+ }
+ }
+ private void moveToHead(Node node){
+ remove(node);
+ addNode(node);
}
+ //删除尾节点
+ private Node popTail(){
+ Node prevNode= tail.prev;
+ tail.prev=prevNode.prev;
+ prevNode.prev.next=tail;
- public int get(int key) {
- return cache.getOrDefault(key, -1);
+ prevNode.next=null;
+ prevNode.prev=null;
+
+ size--;
+ return prevNode;
}
+ //删除中间节点
+ private void remove(Node node){
+ Node prevNode=node.prev;
+ Node nextNode=node.next;
- public void put(int key, int value) {
- cache.put(key, value);
+ prevNode.next=nextNode;
+ nextNode.prev=prevNode;
+
+ node.next=null;
+ node.prev=null;
+
+ size--;
}
- public Collection> getAll() {
- return new ArrayList>(cache.entrySet());
+ //添加节点
+ private void addNode(Node node){
+ node.next=head.next;
+ node.prev=head;
+ node.next.prev=node;
+ head.next=node;
+ size++;
+ }
+ private Map cache=new HashMap();
+ private int size=0;
+ private int capacity=0;
+ //头结点
+ private Node head;
+ //尾结点
+ private Node tail;
+ public LRUCache(int capacity) {
+ this.capacity=capacity;
+ //初始化头尾节点
+ this.head=new Node();
+ this.tail=new Node();
+ head.next=tail;
+ tail.prev=head;
}
- class MapCache extends LinkedHashMap {
- public int max;
- public MapCache(int max) {
- super(max, 0.75f, true);
- this.max = max;
+
+ public int get(int key) {
+ //从缓存获取
+ Node node=cache.get(key);
+ if(null==node){
+ return -1;
}
- protected boolean removeEldestEntry(Map.Entry eldest) {
- return size() > max;
+ //数据移到头结点
+ moveToHead(node);
+ return node.value;
+ }
+
+ public void put(int key, int value) {
+ Node node=cache.get(key);
+ if(null==node){
+ node=new Node(key,value);
+ //写入新节点至头节点
+ addNode(node);
+ cache.put(key,node);
+ //如果容量已满,删除尾节点
+ if(size>capacity){
+ //删除尾节点
+ Node delNode=popTail();
+ cache.remove(delNode.key);
+ }
+ }else{
+ //数据更新并移到头结点
+ node.value=value;
+ moveToHead(node);
}
+ }
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder() ;
+ for (Node node = head;node!=null;node=node.next){
+ sb.append(node.getKey()).append(":")
+ .append(node.getValue())
+ .append("-->");
+ }
+ return sb.toString();
}
public static void main(String[] args) {
- LRUCache map = new LRUCache(2) ;
- map.put(1,1);
- System.out.println(map.get(4));
- for (Map.Entry e : map.getAll()){
- System.out.print(e.getKey() + " : " + e.getValue() + "\t");
- }
+ LRUCache lruMap=new LRUCache(2);
+ lruMap.put(1,1);
+ lruMap.put(2,2);
+ lruMap.get(1);
+ lruMap.put(3,3);
+ lruMap.get(2);
+ lruMap.put(4,4);
+ lruMap.get(1);
+ lruMap.get(3);
+ lruMap.get(4);
+ System.out.println(lruMap.toString());
}
-}
-
+}
diff --git a/src/main/java/com/algorithm/study/demo/LRUCache/LRULinkedMap.java b/src/main/java/com/algorithm/study/demo/LRUCache/LRULinkedMap.java
index fab7c86..7f84245 100644
--- a/src/main/java/com/algorithm/study/demo/LRUCache/LRULinkedMap.java
+++ b/src/main/java/com/algorithm/study/demo/LRUCache/LRULinkedMap.java
@@ -25,6 +25,7 @@ public LRULinkedMap(int cacheSize){
* @param eldest
* @return
*/
+ @Override
protected boolean removeEldestEntry(Map.Entry eldest){
return size()>CACHESIZE;
}
diff --git a/src/main/java/com/algorithm/study/demo/LRUCache/LRUMap.java b/src/main/java/com/algorithm/study/demo/LRUCache/LRUMap.java
deleted file mode 100644
index f0363a3..0000000
--- a/src/main/java/com/algorithm/study/demo/LRUCache/LRUMap.java
+++ /dev/null
@@ -1,168 +0,0 @@
-package com.algorithm.study.demo.LRUCache;
-
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * LRU缓存链表实现思路
- * 每次写入数据时将数据放入链表头结点。
- * 使用数据时候将数据移动到头结点。
- * 缓存数量超过阈值时移除链表尾部数据。
- * @Author: liuxun
- * @CreateDate: 2018/7/12 下午6:05
- * @Version: 1.0
- */
-public class LRUMap {
- private final Map cacheMap = new HashMap<>();
- /**
- * 最大缓存大小
- */
- private int cacheSize;
- /**
- * 节点大小
- */
- private int nodeCount;
- /**
- * 头结点
- */
- private Node header;
- /**
- * 尾结点
- */
- private Node tailer;
- public LRUMap(int cacheSize) {
- this.cacheSize = cacheSize;
- this.header=null;
- this.tailer=null;
- }
- public void put(K key, V value) {
- cacheMap.put(key, value);
- //双向链表中添加结点
- addNode(key, value);
- }
- public V get(K key){
- Node node = getNode(key);
- //移动到头结点
- moveToHead(node) ;
- return cacheMap.get(key);
- }
- private void moveToHead(Node node){
- //如果是最后的一个节点
- if (node.next == null){
- node.tail.next=null;
- tailer=node.tail;
- nodeCount -- ;
- }
- //如果是本来就是头节点 不作处理
- if (node.tail == null){
- return ;
- }
- //如果处于中间节点
- if (node.tail != null && node.next != null){
- //它的上一节点指向它的下一节点 也就删除当前节点
- node.tail.next=node.next;
- nodeCount -- ;
- }
- //最后在头部增加当前节点
- //注意这里需要重新 new 一个对象,不然原本的node 还有着下面的引用,会造成内存溢出。
- node = new Node<>(node.getKey(),node.getValue()) ;
- addHead(node) ;
- }
- /**
- * 链表查询 效率较低
- * @param key
- * @return
- */
- private Node getNode(K key){
- for (Node node = header;node!=null;node=node.next){
- if (node.getKey().equals(key)){
- return node ;
- }
- }
- return null ;
- }
- /**
- * 写入头结点
- * @param key
- * @param value
- */
- private void addNode(K key, V value) {
- Node node = new Node<>(key, value);
- //容量满了删除最后一个
- if (cacheSize == nodeCount) {
- //删除尾结点
- delTail();
- }
- //写入头结点
- addHead(node);
- }
- /**
- * 添加头结点
- *
- * @param node
- */
- private void addHead(Node node) {
- if (header==null){
- tailer=node;
- }else{
- header.tail=node;
- node.next=header;
- }
- header=node;
- nodeCount++;
- }
- private void delTail() {
- //把尾结点从缓存中删除
- cacheMap.remove(tailer.getKey());
- tailer.tail.next=null;
- tailer=tailer.tail;
- nodeCount--;
- }
- private class Node {
- private K key;
- private V value;
- Node tail;
- Node next;
- public Node(K key, V value) {
- this.key = key;
- this.value = value;
- }
- public Node() {
- }
- public K getKey() {
- return key;
- }
- public void setKey(K key) {
- this.key = key;
- }
- public V getValue() {
- return value;
- }
- public void setValue(V value) {
- this.value = value;
- }
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder() ;
- for (Node node = header;node!=null;node=node.next){
- if (node.getKey()!=null){
- sb.append(node.getKey()).append(":")
- .append(node.getValue())
- .append("-->");
- }
- }
- return sb.toString();
- }
- public static void main(String[] args) {
- LRUMap lruMap=new LRUMap<>(3);
- lruMap.put("1","1");
- lruMap.put("2","2");
- lruMap.put("3","3");
- lruMap.get("1");
- lruMap.put("4","4");
- System.out.println(lruMap.toString());
- System.out.println(lruMap.cacheSize);
- }
-
-}
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/leetcode/Day1.java b/src/main/java/com/algorithm/study/demo/algorithm/leetcode/Day1.java
deleted file mode 100644
index 4b154aa..0000000
--- a/src/main/java/com/algorithm/study/demo/algorithm/leetcode/Day1.java
+++ /dev/null
@@ -1,24 +0,0 @@
-package com.algorithm.study.demo.algorithm.leetcode;
-
-/**
- * @author xun2.liu
- * @title: Day1
- * @projectName algorithm-study
- * @description: TODO
- * @date 2020/1/7 15:05
- */
-public class Day1 {
- public static void main(String[] args) {
- System.out.println(Integer.MAX_VALUE);
- }
-
- /***
- * 给出一个32位的有符号的整数,你需要将这个整数中每位上的数字进行反转
- * https://leetcode-cn.com/problems/reverse-integer/
- * @param x
- * @return
- */
- public static int reverse(int x){
- return 0;
- }
-}
From 7579e995e006ab76da79bae482789272a92675ce Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Mon, 11 May 2020 15:22:10 +0800
Subject: [PATCH 25/61] =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/java/com/algorithm/study/demo/enums/MainTest.java | 6 ------
.../java/com/algorithm/study/demo/{ => util}/GZIPUtils.java | 2 +-
.../com/algorithm/study/demo/{ => util}/JodaTimeUtil.java | 2 +-
.../java/com/algorithm/study/demo/{ => util}/ZipUtil.java | 2 +-
4 files changed, 3 insertions(+), 9 deletions(-)
rename src/main/java/com/algorithm/study/demo/{ => util}/GZIPUtils.java (99%)
rename src/main/java/com/algorithm/study/demo/{ => util}/JodaTimeUtil.java (98%)
rename src/main/java/com/algorithm/study/demo/{ => util}/ZipUtil.java (97%)
diff --git a/src/main/java/com/algorithm/study/demo/enums/MainTest.java b/src/main/java/com/algorithm/study/demo/enums/MainTest.java
index 3bbd4ef..85a7c04 100644
--- a/src/main/java/com/algorithm/study/demo/enums/MainTest.java
+++ b/src/main/java/com/algorithm/study/demo/enums/MainTest.java
@@ -1,12 +1,6 @@
package com.algorithm.study.demo.enums;
-import com.algorithm.study.demo.JodaTimeUtil;
-import com.alibaba.fastjson.JSON;
-import com.google.common.collect.Maps;
-
import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.*;
/**
* @author xun2.liu
diff --git a/src/main/java/com/algorithm/study/demo/GZIPUtils.java b/src/main/java/com/algorithm/study/demo/util/GZIPUtils.java
similarity index 99%
rename from src/main/java/com/algorithm/study/demo/GZIPUtils.java
rename to src/main/java/com/algorithm/study/demo/util/GZIPUtils.java
index 299f705..15c1fe5 100644
--- a/src/main/java/com/algorithm/study/demo/GZIPUtils.java
+++ b/src/main/java/com/algorithm/study/demo/util/GZIPUtils.java
@@ -1,4 +1,4 @@
-package com.algorithm.study.demo;
+package com.algorithm.study.demo.util;
import com.alibaba.fastjson.JSON;
diff --git a/src/main/java/com/algorithm/study/demo/JodaTimeUtil.java b/src/main/java/com/algorithm/study/demo/util/JodaTimeUtil.java
similarity index 98%
rename from src/main/java/com/algorithm/study/demo/JodaTimeUtil.java
rename to src/main/java/com/algorithm/study/demo/util/JodaTimeUtil.java
index 94ff6bf..04432fc 100644
--- a/src/main/java/com/algorithm/study/demo/JodaTimeUtil.java
+++ b/src/main/java/com/algorithm/study/demo/util/JodaTimeUtil.java
@@ -1,4 +1,4 @@
-package com.algorithm.study.demo;
+package com.algorithm.study.demo.util;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
diff --git a/src/main/java/com/algorithm/study/demo/ZipUtil.java b/src/main/java/com/algorithm/study/demo/util/ZipUtil.java
similarity index 97%
rename from src/main/java/com/algorithm/study/demo/ZipUtil.java
rename to src/main/java/com/algorithm/study/demo/util/ZipUtil.java
index 851c4c4..765e78b 100644
--- a/src/main/java/com/algorithm/study/demo/ZipUtil.java
+++ b/src/main/java/com/algorithm/study/demo/util/ZipUtil.java
@@ -1,4 +1,4 @@
-package com.algorithm.study.demo;
+package com.algorithm.study.demo.util;
/**
* @title: ZipUtil
From 6ca7f645eb093a3723f9d71833522a3798dcb441 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Mon, 11 May 2020 15:22:23 +0800
Subject: [PATCH 26/61] =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../study/demo/algorithm/dynamicprogramming/demo1.java | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/dynamicprogramming/demo1.java b/src/main/java/com/algorithm/study/demo/algorithm/dynamicprogramming/demo1.java
index 4488b09..31b98a5 100644
--- a/src/main/java/com/algorithm/study/demo/algorithm/dynamicprogramming/demo1.java
+++ b/src/main/java/com/algorithm/study/demo/algorithm/dynamicprogramming/demo1.java
@@ -67,8 +67,8 @@ private static int fib3(int n){
}
/**
- * DP table 数组的迭代解法优化
- * 空间复杂度降为 O(1)
+ *DP table 数组的迭代解法优化
+ *空间复杂度降为 O(1)
*自底向上计算斐波那契数列
* @param n
* @return
From ab583727a1a6177183cb15f3ae5fc2349a9e4fb7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=88=98=E5=8B=8B?=
Date: Wed, 13 May 2020 16:50:25 +0800
Subject: [PATCH 27/61] leetcode
---
.../demo/algorithm/leetcode/Solution.java | 87 +++++++++++++------
.../demo/algorithm/leetcode/Solution2.java | 58 +++++++++++++
.../demo/algorithm/leetcode/Solution3.java | 63 ++++++++++++++
3 files changed, 181 insertions(+), 27 deletions(-)
create mode 100644 src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution2.java
create mode 100644 src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution3.java
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution.java b/src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution.java
index cb825b6..0c30ee1 100644
--- a/src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution.java
+++ b/src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution.java
@@ -1,43 +1,76 @@
package com.algorithm.study.demo.algorithm.leetcode;
+import com.algorithm.study.demo.LRUCache.LRUCache;
+
/**
* @author xun2.liu
* @title: Solution
* @projectName algorithm-study
- * @description: TODO
+ * @description:
+ * 给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,
+ * 并且它们的每个节点只能存储 一位 数字。
+ *
+ * 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
+ *
+ * 您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
+ * 示例:
+ *
+ * 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
+ * 输出:7 -> 0 -> 8
+ * 原因:342 + 465 = 807
+ *
+ * 来源:力扣(LeetCode)
+ * 链接:https://leetcode-cn.com/problems/add-two-numbers
* @date 2020/5/9 14:42
*/
public class Solution {
+ static class ListNode{
+ private int val;
+ private ListNode next;
+ private ListNode(int val){
+ this.val=val;
+ }
+
+ }
- /**
- * 实现 int sqrt(int x) 函数。
- * 计算并返回 x 的平方根,其中 x 是非负整数。
- *
- * 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。
- *
- * 示例 1:
- *
- * 输入: 4
- * 输出: 2
- * 示例 2:
- *
- * 输入: 8
- * 输出: 2
- * 说明: 8 的平方根是 2.82842...,由于返回类型是整数,小数部分将被舍去。
- * @param x
- * @return
- */
- public static int mySqrt(int x) {
- if (x==0){
- return 0;
+ public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
+ //返回的结果,初始化
+ ListNode result=new ListNode(0);
+ //j结果游标
+ ListNode curr=result;
+ //满十进1,存储进位
+ int carry=0;
+ while(l1!=null || l2!=null){
+ int p1=l1==null?0:l1.val;
+ int p2=l2==null?0:l2.val;
+ //计算当前两数相加后的值
+ int sum=p1+p2+carry;
+ //计算相加后的值的进位
+ carry=sum/10;
+ //存储当前相加后的值除以10的余数
+ curr.next=new ListNode(sum%10);
+ //游标指向下个节点
+ curr=curr.next;
+
+ if (l1!=null){
+ l1=l1.next;
+ }
+ if (l2!=null){
+ l2=l2.next;
+ }
}
- if (x==1){
- return 1;
+ if (carry>0){
+ curr.next=new ListNode(carry);
}
- return x/2;
+ return result.next;
}
-
public static void main(String[] args) {
- System.out.println(mySqrt(8));
+ ListNode a=new ListNode(5);
+ ListNode b=new ListNode(5);
+
+ ListNode result = addTwoNumbers(a, b);
+ for (ListNode node=result;node!=null;node=node.next){
+ System.out.println(node.val);
+ }
}
}
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution2.java b/src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution2.java
new file mode 100644
index 0000000..611281a
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution2.java
@@ -0,0 +1,58 @@
+package com.algorithm.study.demo.algorithm.leetcode;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author xun2.liu
+ * @title: Solution2
+ * @projectName algorithm-study
+ * @description:
+ * 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
+ * 示例 1:
+ *
+ * 输入: "abcabcbb"
+ * 输出: 3
+ * 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
+ *
+ * 来源:力扣(LeetCode)
+ * 链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters
+ * @date 2020/5/13 15:51
+ */
+public class Solution2 {
+ /**
+ * 使用滑动窗口
+ * 定义一个 map 数据结构存储 (k, v),其中 key 值为字符,value 值为字符位置 +1,加 1 表示从字符位置后一个才开始不重复
+ * 我们定义不重复子串的开始位置为 start,结束位置为 end
+ * 随着 end 不断遍历向后,会遇到与 [start, end] 区间内字符相同的情况,此时将字符作为 key 值,获取其 value 值,并更新 start,此时 [start, end]
+ * 区间内不存在重复字符
+ * 无论是否更新 start,都会更新其 map 数据结构和结果 ans。
+ * 时间复杂度:O(n)O(n)
+ * @param s
+ * @return
+ */
+ public static int lengthOfLongestSubstring(String s) {
+ //最长子串 的长度
+ int resultLen=0;
+ //key为字符 value为end
+ Map map= new HashMap<>();
+ //初始化起始位置和结束位置
+ int start=0;
+ for(int end=0;end counter=new HashMap<>();
+ for(int n:nums){
+ counter.put(n,counter.getOrDefault(n,0)+1);
+ }
+ //使用每个数字出现的次数作为排序规则来建立初始化一个优先队列
+ PriorityQueue heap=new PriorityQueue<>((n1,n2)-> counter.get(n1)-counter.get(n2));
+ //把数字写入优先队列中
+ for(int num:counter.keySet()){
+ heap.add(num);
+ //如果优先队列中的元素大于前K个就删除,因为默认是升序。
+ if(heap.size()>k){
+ heap.poll();
+ }
+ }
+ //取出前K个元素从优先队列中
+ int[] result=new int[k];
+ for(int i=0;i
Date: Thu, 14 May 2020 19:57:01 +0800
Subject: [PATCH 28/61] =?UTF-8?q?=E4=B8=A4=E4=B8=AA=E6=95=B4=E6=95=B0?=
=?UTF-8?q?=E7=9B=B8=E9=99=A4=E5=BE=97=E5=88=B0=E5=BE=AA=E7=8E=AF=E5=B0=8F?=
=?UTF-8?q?=E6=95=B0=EF=BC=8C=E6=B1=82=E5=BE=AA=E7=8E=AF=E8=8A=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../demo/algorithm/leetcode/Solution4.java | 51 +++++++++++++++++++
1 file changed, 51 insertions(+)
create mode 100644 src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution4.java
diff --git a/src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution4.java b/src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution4.java
new file mode 100644
index 0000000..f597b07
--- /dev/null
+++ b/src/main/java/com/algorithm/study/demo/algorithm/leetcode/Solution4.java
@@ -0,0 +1,51 @@
+package com.algorithm.study.demo.algorithm.leetcode;
+
+
+import java.util.*;
+
+/**
+ * @author xun2.liu
+ * @title: Solution4
+ * @projectName algorithm-study
+ * @description: 两个整数相除得到循环小数,求循环节
+ * @date 2020/5/14 19:01
+ */
+public class Solution4 {
+
+ public static String function(int a, int b){
+ //存储商和余数
+ List