leecode更新

This commit is contained in:
markilue 2023-01-31 12:32:04 +08:00
parent 989eeb2243
commit e948057876
9 changed files with 237 additions and 4 deletions

View File

@ -16,7 +16,7 @@ import java.util.List;
* 你可以按 任何顺序 返回答案
* @Version: 1.0
*/
public class Combine {
public class T01_Combine {
@Test

View File

@ -17,7 +17,7 @@ import java.util.List;
* 返回 所有可能的有效组合的列表 该列表不能包含相同的组合两次组合可以以任何顺序返回
* @Version: 1.0
*/
public class combinationSum3 {
public class T02_CombinationSum3 {
@Test

View File

@ -14,7 +14,7 @@ import java.util.*;
* 给出数字到字母的映射如下与电话按键相同注意 1 不对应任何字母
* @Version: 1.0
*/
public class IetterCombinations {
public class T03_LetterCombinations {
@Test
public void test() {

View File

@ -0,0 +1,58 @@
package com.markilue.leecode.backtrace.second;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.backtrace.second
*@Author: dingjiawen
*@CreateTime: 2023-01-31 10:41
*@Description:
* TODO 二刷力扣77题 组合:
* 给定两个整数 n k返回范围 [1, n] 中所有可能的 k 个数的组合
* 你可以按 任何顺序 返回答案
*@Version: 1.0
*/
public class T01_Combine {
@Test
public void test() {
int n = 4;
int k = 2;
System.out.println(combine(n, k));
}
List<List<Integer>> result = new ArrayList<>();
List<Integer> cur = new ArrayList<>();
public List<List<Integer>> combine(int n, int k) {
backtracking(n, k, 1);
return result;
}
/**
* 回溯法
* 剪枝前速度击败35.29%内存击败55.4% 17ms
* 剪枝后速度击败99.99%内存击败74.46% 1ms
* @param n
* @param k
* @param now
*/
public void backtracking(int n, int k, int now) {
if (cur.size() == k) {
List<Integer> list = new ArrayList<>(cur);
result.add(list);
return;
}
for (int i = now; i <= n-(k-cur.size())+1; i++) {
//剪枝后面怎么都不够了肯定不用了
cur.add(i);
backtracking(n, k, i + 1);
cur.remove(cur.size() - 1);
}
}
}

View File

@ -0,0 +1,61 @@
package com.markilue.leecode.backtrace.second;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.backtrace.second
*@Author: dingjiawen
*@CreateTime: 2023-01-31 11:07
*@Description:
* TODO 二刷力扣216题 组合总和III:
* 找出所有相加之和为 n k 个数的组合且满足下列条件
* 只使用数字1到9
* 每个数字 最多使用一次
* 返回 所有可能的有效组合的列表 该列表不能包含相同的组合两次组合可以以任何顺序返回
*@Version: 1.0
*/
public class T02_CombinationSum3 {
@Test
public void test() {
int k = 3, n = 7;
System.out.println(combinationSum3(k, n));
}
@Test
public void test1() {
int k = 9, n = 45;
System.out.println(combinationSum3(k, n));
}
List<List<Integer>> result = new ArrayList<>();
List<Integer> cur = new ArrayList<>();
public List<List<Integer>> combinationSum3(int k, int n) {
backtracking(k, n, 0, 1);
return result;
}
public void backtracking(int k, int n, int sum, int now) {
if (k == cur.size()) {
if (sum == n) {
result.add(new ArrayList<>(cur));
}
return;
}
int flag = n - sum > 9 ? 9 : n - sum;
for (int i = now; i <= flag - (k - cur.size()) + 1; i++) {
cur.add(i);
sum += i;
backtracking(k, n, sum, i + 1);
sum -= i;
cur.remove(cur.size() - 1);
}
}
}

View File

@ -0,0 +1,70 @@
package com.markilue.leecode.backtrace.second;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.backtrace.second
*@Author: dingjiawen
*@CreateTime: 2023-01-31 11:34
*@Description:
* TODO 二刷力扣17题 电话号码的字母组合:
* 给定一个仅包含数字 2-9 的字符串返回所有它能表示的字母组合答案可以按 任意顺序 返回
* 给出数字到字母的映射如下与电话按键相同注意 1 不对应任何字母
*@Version: 1.0
*/
public class T03_LetterCombinations {
@Test
public void test() {
// String s = "01";
// for (int i = 0; i < s.length(); i++) {
// System.out.println(s.charAt(i) - '0');
// }
String digits = "23";
System.out.println(letterCombinations(digits));
}
List<String> result = new ArrayList<>();
StringBuilder builder = new StringBuilder();
Map<Character, char[]> map = new HashMap<Character, char[]>() {{
put('2', new char[]{'a', 'b', 'c'});
put('3', new char[]{'d', 'e', 'f'});
put('4', new char[]{'g', 'h', 'i'});
put('5', new char[]{'j', 'k', 'l'});
put('6', new char[]{'m', 'n', 'o'});
put('7', new char[]{'p', 'q', 'r', 's'});
put('8', new char[]{'t', 'u', 'v'});
put('9', new char[]{'w', 'x', 'y', 'z'});
}};
public List<String> letterCombinations(String digits) {
if(digits.length()==0){
return result;
}
char[] chars = digits.toCharArray();
backtracking(chars, chars.length, 0);
return result;
}
public void backtracking(char[] chars, int total, int now) {
if (builder.length() == total) {
result.add(builder.toString());
return;
}
for (char c : map.get(chars[now])) {
builder.append(c);
backtracking(chars, total, now + 1);
builder.deleteCharAt(builder.length() - 1);
}
}
}

View File

@ -18,7 +18,7 @@ public class T14_1_BuildTreeII {
@Test
public void test() {
int[] preorder = {3, 9, 20, 15, 7}, inorder = {9, 3, 15, 20, 7};
TreeNode node = buildTree(preorder, inorder);
TreeNode node = buildTree1(preorder, inorder);
System.out.println(levelOrder(node));
}

View File

@ -0,0 +1,21 @@
import scrapy
import requests
class qidianSpider(scrapy.Spider):
name = 'qidian'
start_urls =['https://vipreader.qidian.com/chapter/1031940621/742792391']
def parse(self, response, **kwargs):
divs=response.xpath('//div[@class="read-content j_readContent"]')
print(divs)
if __name__ == '__main__':
url = "https://vipreader.qidian.com/chapter/1031940621/742792391/"
response = requests.get(url=url)
qidianSpider().parse(response=response)

View File

@ -0,0 +1,23 @@
import requests
from lxml import etree
import scrapy
url="https://vipreader.qidian.com/chapter/1031940621/742792391/"
response=requests.get(url=url)
# print(response.text)
html=etree.HTML(response.text)
total=html.xpath('//*[@id="j_742792391"]/p')
# total=html.xpath('//*[@id="j_742792391"]/p[15]/text()')
total=html.xpath('//div[@class="read-content j_readContent"]/p')
print(total)
textList=[]
for i in range(10):
# print(p)
textList.append(html.xpath('//*[@id="j_742792391"]/p[{0}]/text()'.format(i)))
# textList.append(html.xpath('//*[@id="j_742792391"]/p[1]/text()'))
# print(text1)
# print(text2)
print(textList)