commit a project on new repository
This commit is contained in:
6
.lightly/settings.toml
Normal file
6
.lightly/settings.toml
Normal file
@ -0,0 +1,6 @@
|
||||
ports = []
|
||||
[languages.java]
|
||||
language = "java"
|
||||
src_folder = "src/main/java/"
|
||||
|
||||
[languages.java.run]
|
6
JavaProject/.lightly/settings.toml
Normal file
6
JavaProject/.lightly/settings.toml
Normal file
@ -0,0 +1,6 @@
|
||||
ports = []
|
||||
[languages.java]
|
||||
language = "java"
|
||||
src_folder = "src/main/java/"
|
||||
|
||||
[languages.java.run]
|
69
JavaProject/pom.xml
Normal file
69
JavaProject/pom.xml
Normal file
@ -0,0 +1,69 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.1.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>JavaProject</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>SpringBootProject</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入poi -->
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi</artifactId>
|
||||
<version>3.17</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-ooxml</artifactId>
|
||||
<version>3.17</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-scratchpad</artifactId>
|
||||
<version>3.17</version>
|
||||
</dependency>
|
||||
<!-- 引入swagger相关jar包 -->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
<version>2.8.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
<version>2.8.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
13
JavaProject/src/main/java/com/example/demo/Application.java
Normal file
13
JavaProject/src/main/java/com/example/demo/Application.java
Normal file
@ -0,0 +1,13 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
package com.example.demo.controller;
|
||||
|
||||
public class ExcelConvertHtml {
|
||||
|
||||
public String static resolveExcel(MultipartFile file) throws Exception {
|
||||
try {
|
||||
File toFile = null;
|
||||
if (file.equals("") || file.getSIze() <= 0) {
|
||||
file = null;
|
||||
} else {
|
||||
InputStream ins = null;
|
||||
ins = file.getInputStream():
|
||||
toFile = new File(file.getOriginalFilename());
|
||||
inputStreamToFile(ins, toFile);
|
||||
ins.close();
|
||||
}
|
||||
// 获取数据
|
||||
FileInputStream input = new FileInputStream(toFile);
|
||||
Workbook workbook = new XSSFWorkbook(input);
|
||||
Sheet sheet = workbook.getSheetAt(0);
|
||||
// create html table
|
||||
Element table = new Element(Tag.valueOf("table"), "");
|
||||
table.attr("border", "1");
|
||||
table.attr("cellspacing", "0");
|
||||
table.attr("cellpadding", "0");
|
||||
// Add column widths
|
||||
Element colgroup = new Element(Tag.valueOf("colgroup"), "");
|
||||
for (int i = 0; i < sheet.getRow(0).getLastCellNum(); i++) {
|
||||
Element col = new Element(Tag.valueOf("col"), "");
|
||||
col.attr("width", "150px");// set column width
|
||||
colgroup.appendChild(col);
|
||||
}
|
||||
table.appendChild(colgroup);
|
||||
|
||||
// Add header row
|
||||
Element thead = new Element(Tag.valueOf("thread"), "");
|
||||
Element tr = new Element(Tag.valueOf("tr"), "");
|
||||
tr.attr("bgcolor", "#CCCCCC");
|
||||
for (int i = 0; i < 2; i++) {
|
||||
Cell cell = sheet.getRow(0).getCell(i);
|
||||
String value = cell.getStringCellValue();
|
||||
Element th = new Element(Tag.valueOf("th"), "");
|
||||
th.text(value);
|
||||
if (i == 0) {
|
||||
// Merge first two columns
|
||||
th.attr("colspan", "2");
|
||||
}
|
||||
tr.appendChild(th);
|
||||
}
|
||||
for (int i = 2; i < sheet,getRow(0).getLastCellNum();
|
||||
i++){
|
||||
Cell cell = sheet.getRow(0).getCell(i);
|
||||
String value = cell.getStringCellValue();
|
||||
Element th = new Element(Tag.valueOf("th"), "");
|
||||
th.text(value);
|
||||
tr.appendChild(th);
|
||||
}
|
||||
thread.appendChild(tr);
|
||||
table.appendChild(thread);
|
||||
|
||||
// Add data rows
|
||||
Element tbody = new Element(Tag.valueOf("tbody"), "");
|
||||
// for (int i = 1; i <= sheet.getLastRowNum(); i++) {
|
||||
// Element trData
|
||||
// }
|
||||
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
|
||||
Element trData = new Element(Tag.valueOf("tr"), "");
|
||||
for (int j = 0; j < sheet.getRow(i).getLastCellNum(); j++) {
|
||||
Cell cell = sheet.getRow(i).getCell(j);
|
||||
String value = "";
|
||||
if (cell.getCellType() == CellType.NUMERIC) {
|
||||
value = String.valueOf(cell.getNumericCellValue());
|
||||
} else {
|
||||
value = cell.getStringCellValue();
|
||||
}
|
||||
Element td = new Element(Tag.valueOf("td"), "");
|
||||
td.text(value);
|
||||
if (j < 2) {
|
||||
td.attr("align", "left");
|
||||
}
|
||||
trData.appendChild(td);
|
||||
}
|
||||
tbody.appendChild(trData);
|
||||
}
|
||||
table.appendChild(tbody); // Convert to HTML
|
||||
Document doc = new Document("");
|
||||
doc.appendChild(table);
|
||||
String html = doc.outerHtml();
|
||||
// Write to file
|
||||
FileWriter writer = new FileWriter(new File("output.html"));
|
||||
writer.write(html);
|
||||
writer.close();
|
||||
} catch (Exception ex) {
|
||||
System.out.println(ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.example.demo.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Controller
|
||||
@RestMapping
|
||||
public class HelloController {
|
||||
|
||||
@GetMapping(value = "/")
|
||||
public String index() {
|
||||
return "Hello World!";
|
||||
}
|
||||
}
|
1
JavaProject/src/main/resources/application.properties
Normal file
1
JavaProject/src/main/resources/application.properties
Normal file
@ -0,0 +1 @@
|
||||
|
@ -0,0 +1,13 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class ApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
90
output.html
Normal file
90
output.html
Normal file
@ -0,0 +1,90 @@
|
||||
<table border="1" cellspacing="0" cellpadding="5">
|
||||
<colgroup>
|
||||
<col width="150px">
|
||||
<col width="150px">
|
||||
<col width="150px">
|
||||
<col width="150px">
|
||||
<col width="150px">
|
||||
<col width="150px">
|
||||
<col width="150px">
|
||||
<col width="150px">
|
||||
<col width="150px">
|
||||
<col width="150px">
|
||||
<col width="150px">
|
||||
<col width="150px">
|
||||
<col width="150px">
|
||||
<col width="150px">
|
||||
<col width="150px">
|
||||
<col width="150px">
|
||||
<col width="150px">
|
||||
<col width="150px">
|
||||
<col width="150px">
|
||||
</colgroup>
|
||||
<thead>
|
||||
<tr bgcolor="#CCCCCC">
|
||||
<th colspan="2">主机名</th>
|
||||
<th>管理IP</th>
|
||||
<th>数据IP</th>
|
||||
<th>所在资源池</th>
|
||||
<th>操作系统</th>
|
||||
<th>设备编码</th>
|
||||
<th>所在楼宇</th>
|
||||
<th>机房</th>
|
||||
<th>机架</th>
|
||||
<th>机位</th>
|
||||
<th>设备序列号</th>
|
||||
<th>配置信息</th>
|
||||
<th>CPU型号</th>
|
||||
<th>内存型号</th>
|
||||
<th>内存数</th>
|
||||
<th>vcore数</th>
|
||||
<th>磁盘容量</th>
|
||||
<th>磁盘详细信息</th>
|
||||
<th>主机描述</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td align="left">hostname1</td>
|
||||
<td align="left">127.0.0.1</td>
|
||||
<td>10.10.10.1</td>
|
||||
<td>第一资源池</td>
|
||||
<td>CentOS</td>
|
||||
<td>A01-01-101-1A01</td>
|
||||
<td>A01</td>
|
||||
<td>1</td>
|
||||
<td>101</td>
|
||||
<td>1A01</td>
|
||||
<td>123456789</td>
|
||||
<td>1*1C/1GB/1*1GSSD/1*1TBSATA</td>
|
||||
<td>型号1</td>
|
||||
<td>型号1</td>
|
||||
<td>1GB</td>
|
||||
<td>1</td>
|
||||
<td>1TB</td>
|
||||
<td>1*1TB</td>
|
||||
<td>模板主机</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="left">hostname2</td>
|
||||
<td align="left">127.0.0.2</td>
|
||||
<td>10.10.10.2</td>
|
||||
<td>第二资源池</td>
|
||||
<td>CentOS</td>
|
||||
<td>A01-01-101-1A02</td>
|
||||
<td>A02</td>
|
||||
<td>1</td>
|
||||
<td>101</td>
|
||||
<td>1A02</td>
|
||||
<td>223456789</td>
|
||||
<td>1*1C/1GB/1*1GSSD/1*1TBSATA</td>
|
||||
<td>型号2</td>
|
||||
<td>型号2</td>
|
||||
<td>1GB</td>
|
||||
<td>1</td>
|
||||
<td>1TB</td>
|
||||
<td>1*1TB</td>
|
||||
<td>模板主机</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
78
pom.xml
Normal file
78
pom.xml
Normal file
@ -0,0 +1,78 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.1.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>JavaProject</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>SpringBootProject</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
<properties>
|
||||
<java.version>8</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入poi -->
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi</artifactId>
|
||||
<version>3.17</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-ooxml</artifactId>
|
||||
<version>3.17</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-scratchpad</artifactId>
|
||||
<version>3.17</version>
|
||||
</dependency>
|
||||
<!-- 引入swagger相关jar包 -->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
<version>2.8.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
<version>2.8.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jsoup</groupId>
|
||||
<artifactId>jsoup</artifactId>
|
||||
<version>1.10.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
13
src/main/java/com/example/demo/Application.java
Normal file
13
src/main/java/com/example/demo/Application.java
Normal file
@ -0,0 +1,13 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
42
src/main/java/com/example/demo/CalculatorPrefectNumber.java
Normal file
42
src/main/java/com/example/demo/CalculatorPrefectNumber.java
Normal file
@ -0,0 +1,42 @@
|
||||
package com.example.demo;
|
||||
|
||||
/**
|
||||
* @author shixiang
|
||||
* @date 2025/7/1 14:01
|
||||
*/
|
||||
public class CalculatorPrefectNumber {
|
||||
public static int countPrefectNumber(int n) {
|
||||
if (n < 6) {
|
||||
return 0;
|
||||
}
|
||||
int count = 0;
|
||||
for (int num = 6; num <= n; num++) {
|
||||
if (isPrefectNumber(num)) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
private static boolean isPrefectNumber(int num) {
|
||||
if (num < 2) {
|
||||
return false;
|
||||
}
|
||||
int sum = 1;
|
||||
for (int i = 2; i * i <= num; i++) {
|
||||
if (num % i == 0) {
|
||||
sum += i;
|
||||
if (i != num / i) {
|
||||
sum += num / i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return num == sum;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int i = countPrefectNumber(6);
|
||||
int i1 = countPrefectNumber(28);
|
||||
System.out.println("i=" + i + ",i1=" + i1);
|
||||
}
|
||||
}
|
96
src/main/java/com/example/demo/LinkedListRemover.java
Normal file
96
src/main/java/com/example/demo/LinkedListRemover.java
Normal file
@ -0,0 +1,96 @@
|
||||
package com.example.demo;
|
||||
|
||||
/**
|
||||
* @author shixiang
|
||||
* @date 2025/7/1 13:36
|
||||
*/
|
||||
|
||||
// 单向链表节点类(使用无参构造)
|
||||
class ListNode {
|
||||
int val;
|
||||
ListNode next;
|
||||
|
||||
// 无参构造函数
|
||||
public ListNode() {
|
||||
}
|
||||
|
||||
// 带值的构造函数(可选)
|
||||
public ListNode(int val) {
|
||||
this.val = val;
|
||||
}
|
||||
}
|
||||
|
||||
public class LinkedListRemover {
|
||||
|
||||
public ListNode removeNthFromEnd(ListNode head, int n) {
|
||||
// 创建虚拟头节点(使用无参构造)
|
||||
ListNode dummy = new ListNode();
|
||||
dummy.next = head; // 指向实际头节点
|
||||
|
||||
// 初始化快慢指针
|
||||
ListNode fast = dummy;
|
||||
ListNode slow = dummy;
|
||||
|
||||
// 快指针先前进n步
|
||||
for (int i = 0; i < n; i++) {
|
||||
fast = fast.next;
|
||||
}
|
||||
|
||||
// 同时移动双指针,直到快指针到达最后一个节点
|
||||
while (fast.next != null) {
|
||||
fast = fast.next;
|
||||
slow = slow.next;
|
||||
}
|
||||
|
||||
// 删除目标节点:跳过要移除的节点
|
||||
slow.next = slow.next.next;
|
||||
|
||||
// 返回新的头节点(虚拟头节点的下一个)
|
||||
return dummy.next;
|
||||
}
|
||||
|
||||
// 辅助方法:打印链表(用于测试)
|
||||
public static void printList(ListNode head) {
|
||||
ListNode current = head;
|
||||
while (current != null) {
|
||||
System.out.print(current.val + " ");
|
||||
current = current.next;
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
// 测试用例
|
||||
public static void main(String[] args) {
|
||||
// 创建链表 1->2->3->4->5
|
||||
ListNode head = new ListNode();
|
||||
head.val = 1;
|
||||
head.next = new ListNode();
|
||||
head.next.val = 2;
|
||||
head.next.next = new ListNode();
|
||||
head.next.next.val = 3;
|
||||
head.next.next.next = new ListNode();
|
||||
head.next.next.next.val = 4;
|
||||
head.next.next.next.next = new ListNode();
|
||||
head.next.next.next.next.val = 5;
|
||||
|
||||
LinkedListRemover remover = new LinkedListRemover();
|
||||
|
||||
System.out.print("原始链表: ");
|
||||
printList(head); // 输出: 1 2 3 4 5
|
||||
|
||||
// 移除倒数第2个节点(值为4)
|
||||
head = remover.removeNthFromEnd(head, 4);
|
||||
System.out.print("移除后: ");
|
||||
printList(head); // 输出: 1 2 3 5
|
||||
|
||||
// 测试边界情况:移除头节点(倒数第4个)
|
||||
head = remover.removeNthFromEnd(head, 4);
|
||||
System.out.print("移除头节点后: ");
|
||||
printList(head); // 输出: 2 3 5
|
||||
|
||||
// 测试边界情况:移除尾节点(倒数第3个)
|
||||
head = remover.removeNthFromEnd(head, 3);
|
||||
System.out.print("移除尾节点后: ");
|
||||
printList(head); // 输出: 3 5
|
||||
}
|
||||
}
|
44
src/main/java/com/example/demo/PrefectNumberCalculator.java
Normal file
44
src/main/java/com/example/demo/PrefectNumberCalculator.java
Normal file
@ -0,0 +1,44 @@
|
||||
package com.example.demo;
|
||||
|
||||
/**
|
||||
* @author shixiang
|
||||
* @date 2025/7/1 12:37
|
||||
*/
|
||||
public class PrefectNumberCalculator {
|
||||
public static int countPerfectNumber(int n) {
|
||||
if (n < 6) {
|
||||
return 0;
|
||||
}
|
||||
int count = 0;
|
||||
for (int num = 6; num <= n; num++) {
|
||||
if (isPerfectNumber(num)) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
private static boolean isPerfectNumber(int num) {
|
||||
if (num < 2) {
|
||||
return false;
|
||||
}
|
||||
int sum = 1;
|
||||
for (int i = 2; i * i <= num; i++) {
|
||||
if (num % i == 0) {
|
||||
sum += i;
|
||||
if (i != num / i) {
|
||||
sum += num / i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return sum == num;
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int i1 = countPerfectNumber(6);
|
||||
int i = countPerfectNumber(28);
|
||||
int i2 = countPerfectNumber(29);
|
||||
System.out.println("i1" + i1 + ",i2=" + i2 + ",i=" + i);
|
||||
}
|
||||
}
|
92
src/main/java/com/example/demo/RemoveReversePosition.java
Normal file
92
src/main/java/com/example/demo/RemoveReversePosition.java
Normal file
@ -0,0 +1,92 @@
|
||||
package com.example.demo;
|
||||
|
||||
/**
|
||||
* 移除指定倒数位置的单向链表位置
|
||||
*
|
||||
* @author shixiang
|
||||
* @date 2025/7/3 10:26
|
||||
*/
|
||||
|
||||
public class RemoveReversePosition {
|
||||
|
||||
static class ListNode {
|
||||
int val;
|
||||
ListNode next;
|
||||
|
||||
// 无参构造函数
|
||||
public ListNode() {
|
||||
}
|
||||
|
||||
// 带值的构造函数(可选)
|
||||
public ListNode(int val) {
|
||||
this.val = val;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 具体思路:我们通过快慢指针(双指针方式)做处理即可
|
||||
* 比如1-2-3-4-5 n=2 ==>本质上就是取第4位的节点并删除=》其实就是将1-2-3-5 即找到第三个节点,然后使用current.next= current.next.next;
|
||||
* 其实本质上就是找到倒数n-1的位置,所以假设链表长度为L 那么这个位置就是L-n(比如L等于5,那么n=2,即需要找到第三个的位置即L-n
|
||||
*
|
||||
* @param head
|
||||
* @param n
|
||||
* @return
|
||||
*/
|
||||
public static ListNode removeList(ListNode head, int n) {
|
||||
// 定义一个虚拟节点用于关联最初始的链表
|
||||
ListNode dummp = new ListNode();
|
||||
dummp.next = head;
|
||||
ListNode fast = dummp;
|
||||
ListNode slow = dummp;
|
||||
//那么我们先移动fast的位置n个位置 此时fast的后续位置为L-N;
|
||||
for (int i = 0; i < n; i++) {
|
||||
fast = fast.next;
|
||||
}
|
||||
// 所以这个时候我们只需要判断fast.next!=null,然后同时移动fast和slow即可
|
||||
while (fast.next != null) {
|
||||
fast = fast.next;
|
||||
slow = slow.next;
|
||||
}
|
||||
// 此时slow的节点位置在L-N上
|
||||
// 那么只需要去删除slow的下一节点位置即可
|
||||
slow.next = slow.next.next;
|
||||
// 注意这里slow节点与最初始的时候dummp定义的引用地址保持一致。
|
||||
return dummp.next;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 1,2,3,4,5 n =2 =>1,2,3,5
|
||||
ListNode test1 = new ListNode(1);
|
||||
test1.next = new ListNode(2);
|
||||
test1.next.next = new ListNode(3);
|
||||
test1.next.next.next = new ListNode(4);
|
||||
test1.next.next.next.next = new ListNode(5);
|
||||
printList(test1);
|
||||
ListNode listNode1 = removeList(test1, 2);
|
||||
printList(listNode1);
|
||||
ListNode test2 = new ListNode(1);
|
||||
test2.next = new ListNode(2);
|
||||
printList(test2);
|
||||
ListNode listNode2 = removeList(test2, 1);
|
||||
printList(listNode2);
|
||||
ListNode test3 = new ListNode(1);
|
||||
printList(test3);
|
||||
ListNode listNode3 = removeList(test3, 1);
|
||||
printList(listNode3);
|
||||
}
|
||||
|
||||
// 辅助方法:打印链表(用于测试)
|
||||
public static void printList(ListNode head) {
|
||||
ListNode current = head;
|
||||
if (current == null) {
|
||||
System.out.print("[]");
|
||||
return;
|
||||
}
|
||||
while (current != null) {
|
||||
System.out.print(current.val + " ");
|
||||
current = current.next;
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
}
|
28
src/main/java/com/example/demo/Test.java
Normal file
28
src/main/java/com/example/demo/Test.java
Normal file
@ -0,0 +1,28 @@
|
||||
package com.example.demo;
|
||||
|
||||
/**
|
||||
* @author shixiang
|
||||
* @date 2025/7/1 16:04
|
||||
*/
|
||||
public class Test {
|
||||
// n= 1 1
|
||||
// n = 2 2
|
||||
// n = 3 3
|
||||
// n = 4 5
|
||||
public static int getMethod(int n) {
|
||||
if (n <= 1) {
|
||||
return 1;
|
||||
}
|
||||
if (n == 2) {
|
||||
return 2;
|
||||
}
|
||||
int method = getMethod(n - 1);
|
||||
int method1 = getMethod(n-2);
|
||||
return method+method1;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int method = getMethod(5);
|
||||
|
||||
}
|
||||
}
|
83
src/main/java/com/example/demo/TestLinkedListRemover.java
Normal file
83
src/main/java/com/example/demo/TestLinkedListRemover.java
Normal file
@ -0,0 +1,83 @@
|
||||
package com.example.demo;
|
||||
|
||||
/**
|
||||
* @author shixiang
|
||||
* @date 2025/7/1 13:51
|
||||
*/
|
||||
public class TestLinkedListRemover {
|
||||
// 单向链表节点类(使用无参构造)
|
||||
static class ListNode {
|
||||
int val;
|
||||
ListNode next;
|
||||
|
||||
// 无参构造函数
|
||||
public ListNode() {
|
||||
}
|
||||
|
||||
// 带值的构造函数(可选)
|
||||
public ListNode(int val) {
|
||||
this.val = val;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 创建链表 1->2->3->4->5
|
||||
ListNode head = new ListNode();
|
||||
head.val = 1;
|
||||
head.next = new ListNode();
|
||||
head.next.val = 2;
|
||||
head.next.next = new ListNode();
|
||||
head.next.next.val = 3;
|
||||
head.next.next.next = new ListNode();
|
||||
head.next.next.next.val = 4;
|
||||
head.next.next.next.next = new ListNode();
|
||||
head.next.next.next.next.val = 5;
|
||||
|
||||
System.out.print("原始链表: ");
|
||||
printList(head); // 输出: 1 2 3 4 5
|
||||
ListNode listNode = removeList(head, 2);
|
||||
System.out.print("移除指定位置: ");
|
||||
printList(listNode); // 输出: 1 2 3 4 5
|
||||
|
||||
ListNode head1 = new ListNode();
|
||||
head1.val = 1;
|
||||
head1.next = new ListNode();
|
||||
head1.next.val = 2;
|
||||
ListNode listNode1 = removeList(head1, 1);
|
||||
System.out.print("移除指定位置: ");
|
||||
printList(listNode1);
|
||||
|
||||
}
|
||||
|
||||
// 辅助方法:打印链表(用于测试)
|
||||
public static void printList(ListNode head) {
|
||||
ListNode current = head;
|
||||
while (current != null) {
|
||||
System.out.print(current.val + " ");
|
||||
current = current.next;
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static ListNode removeList(ListNode node, int n) {
|
||||
ListNode dumpNode = new ListNode();
|
||||
dumpNode.next = node;
|
||||
ListNode fast = dumpNode;
|
||||
ListNode slow = dumpNode;
|
||||
|
||||
// 先移动n步
|
||||
for (int i = 0; i < n; i++) {
|
||||
fast = fast.next;
|
||||
}
|
||||
// 这里移动慢指针L-n步骤
|
||||
while (fast.next != null) {
|
||||
fast = fast.next;
|
||||
slow = slow.next;
|
||||
}
|
||||
slow.next = slow.next.next;
|
||||
return dumpNode.next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
113
src/main/java/com/example/demo/controller/ExcelConvertHtml.java
Normal file
113
src/main/java/com/example/demo/controller/ExcelConvertHtml.java
Normal file
@ -0,0 +1,113 @@
|
||||
//package com.example.demo.controller;
|
||||
//
|
||||
//import org.apache.poi.ss.usermodel.Cell;
|
||||
//import org.apache.poi.ss.usermodel.CellType;
|
||||
//import org.apache.poi.ss.usermodel.Sheet;
|
||||
//import org.apache.poi.ss.usermodel.Workbook;
|
||||
//import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
//import org.jsoup.*;
|
||||
//import org.jsoup.nodes.*;
|
||||
//import org.jsoup.parser.*;
|
||||
//import org.jsoup.select.*;
|
||||
//import org.springframework.web.multipart.MultipartFile;
|
||||
//
|
||||
//import java.io.File;
|
||||
//import java.io.FileInputStream;
|
||||
//import java.io.FileWriter;
|
||||
//import java.io.InputStream;
|
||||
//
|
||||
//public class ExcelConvertHtml {
|
||||
//
|
||||
// public String static resolveExcel(MultipartFile file) throws Exception {
|
||||
// try {
|
||||
// File toFile = null;
|
||||
// if (file.equals("") || file.getSize() <= 0) {
|
||||
// file = null;
|
||||
// } else {
|
||||
// InputStream ins = null;
|
||||
// ins = file.getInputStream();
|
||||
// toFile = new File(file.getOriginalFilename());
|
||||
// inputStreamToFile(ins, toFile);
|
||||
// ins.close();
|
||||
// }
|
||||
// // 获取数据
|
||||
// FileInputStream input = new FileInputStream(toFile);
|
||||
// Workbook workbook = new XSSFWorkbook(input);
|
||||
// Sheet sheet = workbook.getSheetAt(0);
|
||||
// // create html table
|
||||
// Element table = new Element(Tag.valueOf("table"), "");
|
||||
// table.attr("border", "1");
|
||||
// table.attr("cellspacing", "0");
|
||||
// table.attr("cellpadding", "0");
|
||||
// // Add column widths
|
||||
// Element colgroup = new Element(Tag.valueOf("colgroup"), "");
|
||||
// for (int i = 0; i < sheet.getRow(0).getLastCellNum(); i++) {
|
||||
// Element col = new Element(Tag.valueOf("col"), "");
|
||||
// col.attr("width", "150px");// set column width
|
||||
// colgroup.appendChild(col);
|
||||
// }
|
||||
// table.appendChild(colgroup);
|
||||
//
|
||||
// // Add header row
|
||||
// Element thead = new Element(Tag.valueOf("thread"), "");
|
||||
// Element tr = new Element(Tag.valueOf("tr"), "");
|
||||
// tr.attr("bgcolor", "#CCCCCC");
|
||||
// for (int i = 0; i < 2; i++) {
|
||||
// Cell cell = sheet.getRow(0).getCell(i);
|
||||
// String value = cell.getStringCellValue();
|
||||
// Element th = new Element(Tag.valueOf("th"), "");
|
||||
// th.text(value);
|
||||
// if (i == 0) {
|
||||
// // Merge first two columns
|
||||
// th.attr("colspan", "2");
|
||||
// }
|
||||
// tr.appendChild(th);
|
||||
// }
|
||||
// for (int i = 2; i < sheet.getRow(0).getLastCellNum(); i++) {
|
||||
// Cell cell = sheet.getRow(0).getCell(i);
|
||||
// String value = cell.getStringCellValue();
|
||||
// Element th = new Element(Tag.valueOf("th"), "");
|
||||
// th.text(value);
|
||||
// tr.appendChild(th);
|
||||
// }
|
||||
// thread.appendChild(tr);
|
||||
// table.appendChild(thread);
|
||||
//
|
||||
// // Add data rows
|
||||
// Element tbody = new Element(Tag.valueOf("tbody"), "");
|
||||
// // for (int i = 1; i <= sheet.getLastRowNum(); i++) {
|
||||
// // Element trData
|
||||
// // }
|
||||
// for (int i = 1; i <= sheet.getLastRowNum(); i++) {
|
||||
// Element trData = new Element(Tag.valueOf("tr"), "");
|
||||
// for (int j = 0; j < sheet.getRow(i).getLastCellNum(); j++) {
|
||||
// Cell cell = sheet.getRow(i).getCell(j);
|
||||
// String value = "";
|
||||
// if (cell.getCellType() == CellType.NUMERIC) {
|
||||
// value = String.valueOf(cell.getNumericCellValue());
|
||||
// } else {
|
||||
// value = cell.getStringCellValue();
|
||||
// }
|
||||
// Element td = new Element(Tag.valueOf("td"), "");
|
||||
// td.text(value);
|
||||
// if (j < 2) {
|
||||
// td.attr("align", "left");
|
||||
// }
|
||||
// trData.appendChild(td);
|
||||
// }
|
||||
// tbody.appendChild(trData);
|
||||
// }
|
||||
// table.appendChild(tbody); // Convert to HTML
|
||||
// Document doc = new Document("");
|
||||
// doc.appendChild(table);
|
||||
// String html = doc.outerHtml();
|
||||
// // Write to file
|
||||
// FileWriter writer = new FileWriter(new File("output.html"));
|
||||
// writer.write(html);
|
||||
// writer.close();
|
||||
// } catch (Exception ex) {
|
||||
// System.out.println(ex.getMessage());
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
//}
|
@ -0,0 +1,16 @@
|
||||
package com.example.demo.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Controller
|
||||
@RequestMapping
|
||||
public class HelloController {
|
||||
|
||||
@GetMapping(value = "/")
|
||||
public String index() {
|
||||
return "Hello World!";
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
package com.example.demo.service;
|
||||
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.CellType;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.jsoup.nodes.Element;
|
||||
import org.jsoup.parser.Tag;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileWriter;
|
||||
|
||||
|
||||
public class ExcelToHtmlConverter {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
// C:\\Users\\Lenovo\\Desktop\\1
|
||||
FileInputStream input = new FileInputStream(new File("D:\\uploads\\input.xlsx"));
|
||||
Workbook workbook = new XSSFWorkbook(input);
|
||||
Sheet sheet = workbook.getSheetAt(0);
|
||||
// Create HTML table
|
||||
Element table = new Element(Tag.valueOf("table"), "");
|
||||
table.attr("border", "1");
|
||||
table.attr("cellspacing", "0");
|
||||
table.attr("cellpadding", "10");
|
||||
// Add column widths
|
||||
Element colgroup = new Element(Tag.valueOf("colgroup"), "");
|
||||
for (int i = 0; i < sheet.getRow(0).getLastCellNum(); i++) {
|
||||
Element col = new Element(Tag.valueOf("col"), "");
|
||||
// Set column width
|
||||
col.attr("width", "100px");
|
||||
colgroup.appendChild(col);
|
||||
}
|
||||
table.appendChild(colgroup);
|
||||
// Add header row
|
||||
Element thead = new Element(Tag.valueOf("thead"), "");
|
||||
Element tr = new Element(Tag.valueOf("tr"), "");
|
||||
tr.attr("bgcolor", "#CCCCCC"); // Set background color
|
||||
for (int i = 0; i < 2; i++) {
|
||||
Cell cell = sheet.getRow(0).getCell(i);
|
||||
String value = cell.getStringCellValue();
|
||||
Element th = new Element(Tag.valueOf("th"), "");
|
||||
th.text(value);
|
||||
if (i == 0) {
|
||||
th.attr("colspan", "2"); // Merge first two columns
|
||||
}
|
||||
tr.appendChild(th);
|
||||
}
|
||||
for (int i = 2; i < sheet.getRow(0).getLastCellNum(); i++) {
|
||||
Cell cell = sheet.getRow(0).getCell(i);
|
||||
String value = cell.getStringCellValue();
|
||||
Element th = new Element(Tag.valueOf("th"), "");
|
||||
th.text(value);
|
||||
tr.appendChild(th);
|
||||
}
|
||||
thead.appendChild(tr);
|
||||
table.appendChild(thead);
|
||||
// Add data rows
|
||||
Element tbody = new Element(Tag.valueOf("tbody"), "");
|
||||
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
|
||||
Element trData = new Element(Tag.valueOf("tr"), "");
|
||||
for (int j = 0; j < sheet.getRow(i).getLastCellNum(); j++) {
|
||||
Cell cell = sheet.getRow(i).getCell(j);
|
||||
String value = "";
|
||||
if (cell.getCellTypeEnum() == CellType.NUMERIC) {
|
||||
value = String.valueOf(cell.getNumericCellValue());
|
||||
} else {
|
||||
value = cell.getStringCellValue();
|
||||
}
|
||||
Element td = new Element(Tag.valueOf("td"), "");
|
||||
td.text(value);
|
||||
if (j < 2) {
|
||||
td.attr("align", "left");
|
||||
}
|
||||
trData.appendChild(td);
|
||||
}
|
||||
tbody.appendChild(trData);
|
||||
}
|
||||
table.appendChild(tbody);
|
||||
// Convert to HTML
|
||||
Document doc = new Document("");
|
||||
doc.appendChild(table);
|
||||
String html = doc.outerHtml();
|
||||
// Write to file
|
||||
FileWriter writer = new FileWriter(new File("D:\\uploads\\output.html"));
|
||||
writer.write(html);
|
||||
writer.close();
|
||||
} catch (Exception ex) {
|
||||
System.out.println(ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
BIN
src/main/java/com/example/demo/service/input.xlsx
Normal file
BIN
src/main/java/com/example/demo/service/input.xlsx
Normal file
Binary file not shown.
1
src/main/resources/application.properties
Normal file
1
src/main/resources/application.properties
Normal file
@ -0,0 +1 @@
|
||||
|
13
src/test/java/com/example/demo/ApplicationTests.java
Normal file
13
src/test/java/com/example/demo/ApplicationTests.java
Normal file
@ -0,0 +1,13 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class ApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user