diff --git a/src/main/java/net/lensfrex/oj/collector/Collector.java b/src/main/java/net/lensfrex/oj/collector/Collector.java index a3593f5..139fb50 100644 --- a/src/main/java/net/lensfrex/oj/collector/Collector.java +++ b/src/main/java/net/lensfrex/oj/collector/Collector.java @@ -1,22 +1,25 @@ package net.lensfrex.oj.collector; -import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonParser; -import net.lensfrex.oj.collector.data.Result; +import com.google.gson.*; +import net.lensfrex.oj.collector.data.QuestionDetail; +import net.lensfrex.oj.collector.utils.IOUtil; import net.lensfrex.oj.collector.utils.NetworkUtil; +import net.lensfrex.oj.collector.utils.Random; import org.apache.http.client.utils.URIBuilder; import java.io.IOException; import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; -import java.util.Map; public class Collector { - private static final String API_BASE = "https://oj.wust-acm.top/api/problem"; + private static final String API_BASE = "https://leetcode.cn/graphql/"; + + private static final String DETAILS_REQUEST_BODY = IOUtil.inputStreamToString(Record.class.getResourceAsStream("/details_request_body.txt"), StandardCharsets.UTF_8); + + private static final String INDEX_REQUEST_BODY = IOUtil.inputStreamToString(Record.class.getResourceAsStream("/index_request_body.txt"), StandardCharsets.UTF_8); private static final int PAGE_LIMIT = 100; @@ -33,61 +36,99 @@ public class Collector { private HashMap getHeaders() { HashMap headers = new HashMap<>(); - headers.put("User-Agent", "lensfrex/0.0.1 CanYouSeeMe/0.0.2 IJustLearning/0.0.3"); - headers.put("Host", "oj.wust-acm.top"); + headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.66 Safari/537.36 Edg/103.0.1264.44"); + headers.put("Host", "leetcode.cn"); + headers.put("Origin", "https://leetcode.cn"); headers.put("Accept-Encoding", "gzip, deflate, br"); + headers.put("Content-Type", "application/json"); return headers; } - private Result[] getResults(String url, Map headers) throws IOException { - String json = NetworkUtil.get(url, headers); - - JsonArray resultJsonArray = JsonParser.parseString(json).getAsJsonObject() - .getAsJsonObject("data") - .getAsJsonArray("results"); - - return new Gson().fromJson(resultJsonArray, Result[].class); - } - - public ArrayList collectAllResults() { - ArrayList results = new ArrayList<>(); + // +// private QuestionDetail[] getResults(String url, Map headers) throws IOException { +// String json = NetworkUtil.get(url, headers); +// +// JsonArray resultJsonArray = JsonParser.parseString(json).getAsJsonObject() +// .getAsJsonObject("data") +// .getAsJsonArray("results"); +// +// return new Gson().fromJson(resultJsonArray, QuestionDetail[].class); +// } + + private final Record record = new Record(); + private static final Random random = new Random(); + + public ArrayList collectAllQuestion() { + ArrayList QuestionDetails = new ArrayList<>(); try { - String url = generatePageRequestUrl(API_BASE, 0, 1, 1); HashMap headers = getHeaders(); - String json = NetworkUtil.get(url, headers); + String json = NetworkUtil.post(API_BASE, String.format(INDEX_REQUEST_BODY, 0, 1), headers); int totalResult = JsonParser.parseString(json).getAsJsonObject() .getAsJsonObject("data") + .getAsJsonObject("problemsetQuestionList") .getAsJsonPrimitive("total").getAsInt(); int page = (totalResult / PAGE_LIMIT) + (totalResult % PAGE_LIMIT == 0 ? 0 : 1); + ArrayList questionNames = new ArrayList<>(totalResult); System.out.println("Total results: " + totalResult); System.out.println("Page Limit: " + PAGE_LIMIT); System.out.println("Total pages: " + page); + // Fetch all question list for (int i = 1; i <= page; i++) { int offset = (i - 1) * PAGE_LIMIT; System.out.println("\n------------------------------------------------------"); System.out.println("Getting page " + i); System.out.println("Offset " + offset); - url = generatePageRequestUrl(API_BASE, offset, PAGE_LIMIT, i); - System.out.println("Request URL: " + url); + json = NetworkUtil.post(API_BASE, String.format(INDEX_REQUEST_BODY, offset, PAGE_LIMIT), headers); - Result[] results1 = getResults(url, headers); + JsonArray questionJsonArray = + JsonParser.parseString(json).getAsJsonObject() + .getAsJsonObject("data") + .getAsJsonObject("problemsetQuestionList") + .getAsJsonArray("questions"); - results.addAll(Arrays.asList(results1)); + for (JsonElement jsonElement : questionJsonArray) { + questionNames.add(jsonElement.getAsJsonObject().get("titleSlug").getAsString()); + } - System.out.println("Successful get " + results1.length + " result(s)"); + System.out.println("Got result " + questionJsonArray.size()); } - } catch (URISyntaxException | IOException e) { + + System.out.println("Total result: " + questionNames.size()); + + // Fetch all question details + QuestionDetail detail = null; + for (String questionTitleSlug : questionNames) { + System.out.println("Getting question: " + questionTitleSlug); + json = NetworkUtil.post(API_BASE, String.format(DETAILS_REQUEST_BODY, questionTitleSlug), headers); + + JsonObject question = + JsonParser.parseString(json).getAsJsonObject() + .getAsJsonObject("data") + .getAsJsonObject("question"); + + detail = new Gson().fromJson(question, QuestionDetail.class); + record.writeToFile( + "D:\\ojs-leetcode", + String.valueOf(detail.getId()), + detail.getChineseTitle() == null ? detail.getTitle() : detail.getChineseTitle(), + record.fillText(detail)); + + Thread.sleep(random.getRandomNumber(120)); + } + + } catch (IOException | InterruptedException e) { throw new RuntimeException(e); } - return results; + System.out.println("Finish."); + return QuestionDetails; } -} +} \ No newline at end of file diff --git a/src/main/java/net/lensfrex/oj/collector/Main.java b/src/main/java/net/lensfrex/oj/collector/Main.java index 9324d25..21f8ffe 100644 --- a/src/main/java/net/lensfrex/oj/collector/Main.java +++ b/src/main/java/net/lensfrex/oj/collector/Main.java @@ -1,14 +1,12 @@ package net.lensfrex.oj.collector; -import net.lensfrex.oj.collector.data.Result; +import net.lensfrex.oj.collector.data.QuestionDetail; import net.lensfrex.oj.collector.utils.IOUtil; -import net.lensfrex.oj.collector.utils.ParamFiller; import java.io.IOException; import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; -import java.util.HashMap; public class Main { @@ -27,13 +25,13 @@ public class Main { private void run() throws URISyntaxException, IOException { Record record = new Record(); - ArrayList results = new Collector().collectAllResults(); - - String context = ""; - for (Result result : results) { - context = record.fillText(result); - - record.writeToFile(location, result.getId(), result.getTitle(), context); - } + ArrayList questionResults = new Collector().collectAllQuestion(); +// +// String context = ""; +// for (QuestionDetail questionResult : questionResults) { +// context = record.fillText(questionResult); +// +// record.writeToFile(location, String.valueOf(questionResult.getId()), questionResult.getChineseTitle(), context); +// } } } diff --git a/src/main/java/net/lensfrex/oj/collector/Record.java b/src/main/java/net/lensfrex/oj/collector/Record.java index ce897a8..93e23b5 100644 --- a/src/main/java/net/lensfrex/oj/collector/Record.java +++ b/src/main/java/net/lensfrex/oj/collector/Record.java @@ -1,64 +1,57 @@ package net.lensfrex.oj.collector; -import net.lensfrex.oj.collector.data.Result; -import net.lensfrex.oj.collector.data.Sample; +import net.lensfrex.oj.collector.data.QuestionDetail; +import net.lensfrex.oj.collector.data.Tag; import net.lensfrex.oj.collector.utils.IOUtil; import net.lensfrex.oj.collector.utils.ParamFiller; import java.io.File; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; -import java.util.ResourceBundle; import java.util.regex.Pattern; public class Record { private static final String MAIN_TEMPLATE = IOUtil.inputStreamToString(Record.class.getResourceAsStream("/template.txt"), StandardCharsets.UTF_8); - private static final String EXAMPLE_TEMPLATE = IOUtil.inputStreamToString(Record.class.getResourceAsStream("/exampleTemplates.txt"), StandardCharsets.UTF_8); - public String fillText(Result results) { + public String fillText(QuestionDetail results) { + System.out.println("Filling: " + results.getChineseTitle()); HashMap params = new HashMap<>(); - params.put("title", results.getTitle()); - params.put("description", results.getDescription()); - params.put("inputDescription", results.getInputDescription()); - params.put("outputDescription", results.getOutputDescription()); - params.put("timeLimit", results.getTimeLimit().toString()); - params.put("memoryLimit", results.getMemoryLimit().toString()); - params.put("level", results.getDifficulty()); - params.put("id", results.getId()); - params.put("tags", results.getTags().toString()); - params.put("hint", results.getHint()); - StringBuffer exampleStringBuffer = new StringBuffer(); - List samples = results.getSamples(); - int sampleSize = samples.size(); - for (int i = 0; i < sampleSize; i++) { - exampleStringBuffer.append(fillExampleText(samples.get(i), i + 1)); + if (results.isPaidOnly()) { + params.put("englishContent", "这道题是~付~费~内容哦"); + } else { + params.put("englishContent", results.getTitle()); } - params.put("examples", exampleStringBuffer.toString()); + params.put("englishTitle", results.getTitle()); + params.put("title", results.getChineseTitle()); + params.put("content", results.getChineseContent()); + params.put("hints", results.getDifficulty()); + params.put("id", results.getId()); - return ParamFiller.fill(MAIN_TEMPLATE, params); - } + StringBuilder tagStringBuilder = new StringBuilder(); + List tags = results.getTags(); + for (Tag tag : tags) { + tagStringBuilder.append('[').append(tag.getChineseName()).append("] "); + } - private String fillExampleText(Sample sample, int index) { - HashMap params = new HashMap<>(); - if (index == 1) { - params.put("index", ""); + params.put("tags", tagStringBuilder.toString()); - } else { - params.put("index", String.valueOf(index)); + StringBuilder hintStringBuilder = new StringBuilder(); + List hints = results.getHints(); + for (String hint : hints) { + hintStringBuilder.append('[').append(hint).append("]\n\n"); } - params.put("inputExamples", sample.getInput()); - params.put("outputExamples", sample.getOutput()); - return ParamFiller.fill(EXAMPLE_TEMPLATE, params); + params.put("hints", hintStringBuilder.toString()); + + return ParamFiller.fill(MAIN_TEMPLATE, params); } private static final Pattern FilePattern = Pattern.compile("[\\\\/:*?\"<>|]"); - public static String filenameFilter(String str) { + private static String filenameFilter(String str) { return str == null ? null : FilePattern.matcher(str).replaceAll(""); } diff --git a/src/main/java/net/lensfrex/oj/collector/data/QuestionDetail.java b/src/main/java/net/lensfrex/oj/collector/data/QuestionDetail.java new file mode 100644 index 0000000..83fa320 --- /dev/null +++ b/src/main/java/net/lensfrex/oj/collector/data/QuestionDetail.java @@ -0,0 +1,114 @@ + +package net.lensfrex.oj.collector.data; + +import com.google.gson.annotations.SerializedName; + +import java.util.List; + +@SuppressWarnings("unused") +public class QuestionDetail { + @SerializedName("questionFrontendId") + private String id; + + private String title; + @SerializedName("translatedTitle") + private String chineseTitle; + + private String content; + @SerializedName("translatedContent") + private String chineseContent; + + private String difficulty; + private List hints; + + @SerializedName("topicTags") + private List tags; + + private boolean isPaidOnly; + + public boolean isPaidOnly() { + return isPaidOnly; + } + + public void setPaidOnly(boolean paidOnly) { + isPaidOnly = paidOnly; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getChineseTitle() { + return chineseTitle; + } + + public void setChineseTitle(String chineseTitle) { + this.chineseTitle = chineseTitle; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public String getChineseContent() { + return chineseContent; + } + + public void setChineseContent(String chineseContent) { + this.chineseContent = chineseContent; + } + + public String getDifficulty() { + return difficulty; + } + + public void setDifficulty(String difficulty) { + this.difficulty = difficulty; + } + + public List getHints() { + return hints; + } + + public void setHints(List hints) { + this.hints = hints; + } + + public List getTags() { + return tags; + } + + public void setTags(List tags) { + this.tags = tags; + } + + @Override + public String toString() { + String sb = "QuestionDetail{" + "id=" + id + + ", title='" + title + '\'' + + ", chineseTitle='" + chineseTitle + '\'' + + ", content='" + content + '\'' + + ", chineseContent='" + chineseContent + '\'' + + ", difficulty='" + difficulty + '\'' + + ", hints=" + hints + + ", tags=" + tags + + '}'; + return sb; + } +} diff --git a/src/main/java/net/lensfrex/oj/collector/data/Result.java b/src/main/java/net/lensfrex/oj/collector/data/Result.java deleted file mode 100644 index 299c7d0..0000000 --- a/src/main/java/net/lensfrex/oj/collector/data/Result.java +++ /dev/null @@ -1,154 +0,0 @@ - -package net.lensfrex.oj.collector.data; - -import java.util.List; -import javax.annotation.Generated; - -import com.google.gson.annotations.Expose; -import com.google.gson.annotations.SerializedName; - -@Generated("net.hexar.json2pojo") -@SuppressWarnings("unused") -public class Result { - @SerializedName("_id") - private String id; - @Expose - private String title; - @Expose - private Object contest; - @Expose - private String description; - @SerializedName("input_description") - private String inputDescription; - @SerializedName("output_description") - private String outputDescription; - @Expose - private String difficulty; - @Expose - private String hint; - @SerializedName("memory_limit") - private Long memoryLimit; - @SerializedName("time_limit") - private Long timeLimit; - @Expose - private List samples; - @Expose - private List tags; - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public Object getContest() { - return contest; - } - - public void setContest(Object contest) { - this.contest = contest; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public String getDifficulty() { - return difficulty; - } - - public void setDifficulty(String difficulty) { - this.difficulty = difficulty; - } - - public String getHint() { - return hint; - } - - public void setHint(String hint) { - this.hint = hint; - } - - public Long getMemoryLimit() { - return memoryLimit; - } - - public void setMemoryLimit(Long memoryLimit) { - this.memoryLimit = memoryLimit; - } - - public Long getTimeLimit() { - return timeLimit; - } - - public void setTimeLimit(Long timeLimit) { - this.timeLimit = timeLimit; - } - - public String getInputDescription() { - return inputDescription; - } - - public void setInputDescription(String inputDescription) { - this.inputDescription = inputDescription; - } - - public String getOutputDescription() { - return outputDescription; - } - - public void setOutputDescription(String outputDescription) { - this.outputDescription = outputDescription; - } - - public List getSamples() { - return samples; - } - - public void setSamples(List samples) { - this.samples = samples; - } - - public List getTags() { - return tags; - } - - public void setTags(List tags) { - this.tags = tags; - } - - - - @Override - public String toString() { - final StringBuffer sb = new StringBuffer("Result{"); - sb.append("id='").append(id).append('\''); - sb.append(", title='").append(title).append('\''); - sb.append(", contest=").append(contest); - sb.append(", description='").append(description).append('\''); - sb.append(", difficulty='").append(difficulty).append('\''); - sb.append(", hint='").append(hint).append('\''); - sb.append(", memoryLimit=").append(memoryLimit); - sb.append(", timeLimit=").append(timeLimit); - sb.append(", inputDescription='").append(inputDescription).append('\''); - sb.append(", outputDescription='").append(outputDescription).append('\''); - sb.append(", samples=").append(samples); - sb.append(", tags=").append(tags); - sb.append('}'); - return sb.toString(); - } -} diff --git a/src/main/java/net/lensfrex/oj/collector/data/Sample.java b/src/main/java/net/lensfrex/oj/collector/data/Sample.java deleted file mode 100644 index 32f4ffa..0000000 --- a/src/main/java/net/lensfrex/oj/collector/data/Sample.java +++ /dev/null @@ -1,32 +0,0 @@ - -package net.lensfrex.oj.collector.data; - -import javax.annotation.Generated; -import com.google.gson.annotations.Expose; - -@Generated("net.hexar.json2pojo") -@SuppressWarnings("unused") -public class Sample { - - @Expose - private String input; - @Expose - private String output; - - public String getInput() { - return input; - } - - public void setInput(String input) { - this.input = input; - } - - public String getOutput() { - return output; - } - - public void setOutput(String output) { - this.output = output; - } - -} diff --git a/src/main/java/net/lensfrex/oj/collector/data/Tag.java b/src/main/java/net/lensfrex/oj/collector/data/Tag.java new file mode 100644 index 0000000..d2f4de7 --- /dev/null +++ b/src/main/java/net/lensfrex/oj/collector/data/Tag.java @@ -0,0 +1,26 @@ +package net.lensfrex.oj.collector.data; + +import com.google.gson.annotations.SerializedName; + +public class Tag { + private String name; + + @SerializedName("translatedName") + private String chineseName; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getChineseName() { + return chineseName; + } + + public void setChineseName(String chineseName) { + this.chineseName = chineseName; + } +} diff --git a/src/main/java/net/lensfrex/oj/collector/utils/NetworkUtil.java b/src/main/java/net/lensfrex/oj/collector/utils/NetworkUtil.java index b3c3b02..8be0c4e 100644 --- a/src/main/java/net/lensfrex/oj/collector/utils/NetworkUtil.java +++ b/src/main/java/net/lensfrex/oj/collector/utils/NetworkUtil.java @@ -4,7 +4,12 @@ import org.apache.commons.codec.digest.DigestUtils; import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ResponseHandler; +import org.apache.http.client.config.CookieSpecs; +import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; @@ -21,6 +26,26 @@ public class NetworkUtil { public static final String REQUEST_USERAGENT = "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.7113.93 Safari/537.36"; + public static String post(String url, String data, Map headers) throws IOException { + CloseableHttpClient httpClient = HttpClients.createDefault(); + RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build(); + + HttpPost httpPost = new HttpPost(url); + httpPost.setConfig(requestConfig); + + for (String key : headers.keySet()) { + httpPost.setHeader(key, headers.get(key)); + } + + ResponseHandler responseHandler = new BasicResponseHandler(); + + httpPost.setEntity(new StringEntity(data, "utf-8")); + String response = httpClient.execute(httpPost, responseHandler); + httpClient.close(); + + return response; + } + public static String get(String url, Map headers) throws IOException { CloseableHttpClient httpClient = HttpClients.createDefault(); // HttpGet httpGet = new HttpGet("https://oj.wust-acm.top/api/problem?paging=true&offset=0&limit=10&page=1"); diff --git a/src/main/resources/details_request_body.txt b/src/main/resources/details_request_body.txt new file mode 100644 index 0000000..05a6d3b --- /dev/null +++ b/src/main/resources/details_request_body.txt @@ -0,0 +1 @@ +{"operationName":"questionData","variables":{"titleSlug":"%s"},"query":"query questionData($titleSlug: String!) { question(titleSlug: $titleSlug) { questionId questionFrontendId categoryTitle boundTopicId title titleSlug content translatedTitle translatedContent isPaidOnly difficulty likes dislikes isLiked similarQuestions contributors { username profileUrl avatarUrl __typename } langToValidPlayground topicTags { name slug translatedName __typename } companyTagStats codeSnippets { lang langSlug code __typename } stats hints solution { id canSeeDetail __typename } status sampleTestCase metaData judgerAvailable judgeType mysqlSchemas enableRunCode envInfo book { id bookName pressName source shortDescription fullDescription bookImgUrl pressImgUrl productUrl __typename } isSubscribed isDailyQuestion dailyRecordStatus editorType ugcQuestionId style exampleTestcases jsonExampleTestcases __typename }}"} \ No newline at end of file diff --git a/src/main/resources/index_request_body.txt b/src/main/resources/index_request_body.txt new file mode 100644 index 0000000..06366b6 --- /dev/null +++ b/src/main/resources/index_request_body.txt @@ -0,0 +1 @@ +{"query":" query problemsetQuestionList($categorySlug: String, $limit: Int, $skip: Int, $filters: QuestionListFilterInput) { problemsetQuestionList( categorySlug: $categorySlug limit: $limit skip: $skip filters: $filters ) { hasMore total questions { acRate difficulty freqBar frontendQuestionId isFavor paidOnly solutionNum status title titleCn titleSlug topicTags { name nameTranslated id slug } extra { hasVideoSolution topCompanyTags { imgUrl slug numSubscribed } } } }} ","variables":{"categorySlug":"","skip":%d,"limit":%d,"filters":{}}} \ No newline at end of file diff --git a/src/main/resources/leetcode-details-request.json b/src/main/resources/leetcode-details-request.json new file mode 100644 index 0000000..8724082 --- /dev/null +++ b/src/main/resources/leetcode-details-request.json @@ -0,0 +1,8 @@ +{ + "operationName": "questionData", + "variables": { + "titleSlug": "two-sum" + }, + "query": "query questionData($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n questionId\n questionFrontendId\n categoryTitle\n boundTopicId\n title\n titleSlug\n content\n translatedTitle\n translatedContent\n isPaidOnly\n difficulty\n likes\n dislikes\n isLiked\n similarQuestions\n contributors {\n username\n profileUrl\n avatarUrl\n __typename\n }\n langToValidPlayground\n topicTags {\n name\n slug\n translatedName\n __typename\n }\n companyTagStats\n codeSnippets {\n lang\n langSlug\n code\n __typename\n }\n stats\n hints\n solution {\n id\n canSeeDetail\n __typename\n }\n status\n sampleTestCase\n metaData\n judgerAvailable\n judgeType\n mysqlSchemas\n enableRunCode\n envInfo\n book {\n id\n bookName\n pressName\n source\n shortDescription\n fullDescription\n bookImgUrl\n pressImgUrl\n productUrl\n __typename\n }\n isSubscribed\n isDailyQuestion\n dailyRecordStatus\n editorType\n ugcQuestionId\n style\n exampleTestcases\n jsonExampleTestcases\n __typename\n }\n}\n" +} + diff --git a/src/main/resources/leetcode-details.json b/src/main/resources/leetcode-details.json new file mode 100644 index 0000000..17fb7d1 --- /dev/null +++ b/src/main/resources/leetcode-details.json @@ -0,0 +1,177 @@ +{ + "data": { + "question": { + "questionId": "1", + "questionFrontendId": "1", + "categoryTitle": "Algorithms", + "boundTopicId": 2, + "title": "Two Sum", + "titleSlug": "two-sum", + "content": "

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

\n\n

You may assume that each input would have exactly one solution, and you may not use the same element twice.

\n\n

You can return the answer in any order.

\n\n

 

\n

Example 1:

\n\n
\nInput: nums = [2,7,11,15], target = 9\nOutput: [0,1]\nExplanation: Because nums[0] + nums[1] == 9, we return [0, 1].\n
\n\n

Example 2:

\n\n
\nInput: nums = [3,2,4], target = 6\nOutput: [1,2]\n
\n\n

Example 3:

\n\n
\nInput: nums = [3,3], target = 6\nOutput: [0,1]\n
\n\n

 

\n

Constraints:

\n\n
    \n\t
  • 2 <= nums.length <= 104
  • \n\t
  • -109 <= nums[i] <= 109
  • \n\t
  • -109 <= target <= 109
  • \n\t
  • Only one valid answer exists.
  • \n
\n\n

 

\nFollow-up: Can you come up with an algorithm that is less than O(n2time complexity?", + "translatedTitle": "\u4e24\u6570\u4e4b\u548c", + "translatedContent": "

\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums \u548c\u4e00\u4e2a\u6574\u6570\u76ee\u6807\u503c target\uff0c\u8bf7\u4f60\u5728\u8be5\u6570\u7ec4\u4e2d\u627e\u51fa \u548c\u4e3a\u76ee\u6807\u503c target  \u7684\u90a3 \u4e24\u4e2a \u6574\u6570\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u6570\u7ec4\u4e0b\u6807\u3002

\n\n

\u4f60\u53ef\u4ee5\u5047\u8bbe\u6bcf\u79cd\u8f93\u5165\u53ea\u4f1a\u5bf9\u5e94\u4e00\u4e2a\u7b54\u6848\u3002\u4f46\u662f\uff0c\u6570\u7ec4\u4e2d\u540c\u4e00\u4e2a\u5143\u7d20\u5728\u7b54\u6848\u91cc\u4e0d\u80fd\u91cd\u590d\u51fa\u73b0\u3002

\n\n

\u4f60\u53ef\u4ee5\u6309\u4efb\u610f\u987a\u5e8f\u8fd4\u56de\u7b54\u6848\u3002

\n\n

 

\n\n

\u793a\u4f8b 1\uff1a

\n\n
\n\u8f93\u5165\uff1anums = [2,7,11,15], target = 9\n\u8f93\u51fa\uff1a[0,1]\n\u89e3\u91ca\uff1a\u56e0\u4e3a nums[0] + nums[1] == 9 \uff0c\u8fd4\u56de [0, 1] \u3002\n
\n\n

\u793a\u4f8b 2\uff1a

\n\n
\n\u8f93\u5165\uff1anums = [3,2,4], target = 6\n\u8f93\u51fa\uff1a[1,2]\n
\n\n

\u793a\u4f8b 3\uff1a

\n\n
\n\u8f93\u5165\uff1anums = [3,3], target = 6\n\u8f93\u51fa\uff1a[0,1]\n
\n\n

 

\n\n

\u63d0\u793a\uff1a

\n\n
    \n\t
  • 2 <= nums.length <= 104
  • \n\t
  • -109 <= nums[i] <= 109
  • \n\t
  • -109 <= target <= 109
  • \n\t
  • \u53ea\u4f1a\u5b58\u5728\u4e00\u4e2a\u6709\u6548\u7b54\u6848
  • \n
\n\n

\u8fdb\u9636\uff1a\u4f60\u53ef\u4ee5\u60f3\u51fa\u4e00\u4e2a\u65f6\u95f4\u590d\u6742\u5ea6\u5c0f\u4e8e O(n2) \u7684\u7b97\u6cd5\u5417\uff1f

\n", + "isPaidOnly": false, + "difficulty": "Easy", + "likes": 14780, + "dislikes": 0, + "isLiked": null, + "similarQuestions": "[{\"title\": \"3Sum\", \"titleSlug\": \"3sum\", \"difficulty\": \"Medium\", \"translatedTitle\": \"\\u4e09\\u6570\\u4e4b\\u548c\"}, {\"title\": \"4Sum\", \"titleSlug\": \"4sum\", \"difficulty\": \"Medium\", \"translatedTitle\": \"\\u56db\\u6570\\u4e4b\\u548c\"}, {\"title\": \"Two Sum II - Input Array Is Sorted\", \"titleSlug\": \"two-sum-ii-input-array-is-sorted\", \"difficulty\": \"Medium\", \"translatedTitle\": \"\\u4e24\\u6570\\u4e4b\\u548c II - \\u8f93\\u5165\\u6709\\u5e8f\\u6570\\u7ec4\"}, {\"title\": \"Two Sum III - Data structure design\", \"titleSlug\": \"two-sum-iii-data-structure-design\", \"difficulty\": \"Easy\", \"translatedTitle\": \"\\u4e24\\u6570\\u4e4b\\u548c III - \\u6570\\u636e\\u7ed3\\u6784\\u8bbe\\u8ba1\"}, {\"title\": \"Subarray Sum Equals K\", \"titleSlug\": \"subarray-sum-equals-k\", \"difficulty\": \"Medium\", \"translatedTitle\": \"\\u548c\\u4e3a K \\u7684\\u5b50\\u6570\\u7ec4\"}, {\"title\": \"Two Sum IV - Input is a BST\", \"titleSlug\": \"two-sum-iv-input-is-a-bst\", \"difficulty\": \"Easy\", \"translatedTitle\": \"\\u4e24\\u6570\\u4e4b\\u548c IV - \\u8f93\\u5165 BST\"}, {\"title\": \"Two Sum Less Than K\", \"titleSlug\": \"two-sum-less-than-k\", \"difficulty\": \"Easy\", \"translatedTitle\": \"\\u5c0f\\u4e8e K \\u7684\\u4e24\\u6570\\u4e4b\\u548c\"}]", + "contributors": [], + "langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false}", + "topicTags": [ + { + "name": "Array", + "slug": "array", + "translatedName": "\u6570\u7ec4", + "__typename": "TopicTagNode" + }, + { + "name": "Hash Table", + "slug": "hash-table", + "translatedName": "\u54c8\u5e0c\u8868", + "__typename": "TopicTagNode" + } + ], + "companyTagStats": null, + "codeSnippets": [ + { + "lang": "C++", + "langSlug": "cpp", + "code": "class Solution {\npublic:\n vector twoSum(vector& nums, int target) {\n\n }\n};", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Java", + "langSlug": "java", + "code": "class Solution {\n public int[] twoSum(int[] nums, int target) {\n\n }\n}", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Python", + "langSlug": "python", + "code": "class Solution(object):\n def twoSum(self, nums, target):\n \"\"\"\n :type nums: List[int]\n :type target: int\n :rtype: List[int]\n \"\"\"", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Python3", + "langSlug": "python3", + "code": "class Solution:\n def twoSum(self, nums: List[int], target: int) -> List[int]:", + "__typename": "CodeSnippetNode" + }, + { + "lang": "C", + "langSlug": "c", + "code": "\n\n/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* twoSum(int* nums, int numsSize, int target, int* returnSize){\n\n}", + "__typename": "CodeSnippetNode" + }, + { + "lang": "C#", + "langSlug": "csharp", + "code": "public class Solution {\n public int[] TwoSum(int[] nums, int target) {\n\n }\n}", + "__typename": "CodeSnippetNode" + }, + { + "lang": "JavaScript", + "langSlug": "javascript", + "code": "/**\n * @param {number[]} nums\n * @param {number} target\n * @return {number[]}\n */\nvar twoSum = function(nums, target) {\n\n};", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Ruby", + "langSlug": "ruby", + "code": "# @param {Integer[]} nums\n# @param {Integer} target\n# @return {Integer[]}\ndef two_sum(nums, target)\n\nend", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Swift", + "langSlug": "swift", + "code": "class Solution {\n func twoSum(_ nums: [Int], _ target: Int) -> [Int] {\n\n }\n}", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Go", + "langSlug": "golang", + "code": "func twoSum(nums []int, target int) []int {\n\n}", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Scala", + "langSlug": "scala", + "code": "object Solution {\n def twoSum(nums: Array[Int], target: Int): Array[Int] = {\n\n }\n}", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Kotlin", + "langSlug": "kotlin", + "code": "class Solution {\n fun twoSum(nums: IntArray, target: Int): IntArray {\n\n }\n}", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Rust", + "langSlug": "rust", + "code": "impl Solution {\n pub fn two_sum(nums: Vec, target: i32) -> Vec {\n\n }\n}", + "__typename": "CodeSnippetNode" + }, + { + "lang": "PHP", + "langSlug": "php", + "code": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer $target\n * @return Integer[]\n */\n function twoSum($nums, $target) {\n\n }\n}", + "__typename": "CodeSnippetNode" + }, + { + "lang": "TypeScript", + "langSlug": "typescript", + "code": "function twoSum(nums: number[], target: number): number[] {\n\n};", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Racket", + "langSlug": "racket", + "code": "(define/contract (two-sum nums target)\n (-> (listof exact-integer?) exact-integer? (listof exact-integer?))\n\n )", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Erlang", + "langSlug": "erlang", + "code": "-spec two_sum(Nums :: [integer()], Target :: integer()) -> [integer()].\ntwo_sum(Nums, Target) ->\n .", + "__typename": "CodeSnippetNode" + }, + { + "lang": "Elixir", + "langSlug": "elixir", + "code": "defmodule Solution do\n @spec two_sum(nums :: [integer], target :: integer) :: [integer]\n def two_sum(nums, target) do\n\n end\nend", + "__typename": "CodeSnippetNode" + } + ], + "stats": "{\"totalAccepted\": \"3.6M\", \"totalSubmission\": \"6.8M\", \"totalAcceptedRaw\": 3565226, \"totalSubmissionRaw\": 6770752, \"acRate\": \"52.7%\"}", + "hints": [ + "A really brute force way would be to search for all possible pairs of numbers but that would be too slow. Again, it's best to try out brute force solutions for just for completeness. It is from these brute force solutions that you can come up with optimizations.", + "So, if we fix one of the numbers, say
x
, we have to scan the entire array to find the next number
y
which is
value - x
where value is the input parameter. Can we change our array somehow so that this search becomes faster?", + "The second train of thought is, without changing the array, can we use additional space somehow? Like maybe a hash map to speed up the search?" + ], + "solution": { + "id": "30", + "canSeeDetail": true, + "__typename": "ArticleNode" + }, + "status": null, + "sampleTestCase": "[2,7,11,15]\n9", + "metaData": "{\n \"name\": \"twoSum\",\n \"params\": [\n {\n \"name\": \"nums\",\n \"type\": \"integer[]\"\n },\n {\n \"name\": \"target\",\n \"type\": \"integer\"\n }\n ],\n \"return\": {\n \"type\": \"integer[]\",\n \"size\": 2\n },\n \"manual\": false\n}", + "judgerAvailable": true, + "judgeType": "small", + "mysqlSchemas": [], + "enableRunCode": true, + "envInfo": "{\"cpp\":[\"C++\",\"

\\u7248\\u672c\\uff1aclang 11<\\/code> \\u91c7\\u7528\\u6700\\u65b0C++ 17\\u6807\\u51c6\\u3002<\\/p>\\r\\n\\r\\n

\\u7f16\\u8bd1\\u65f6\\uff0c\\u5c06\\u4f1a\\u91c7\\u7528-O2<\\/code>\\u7ea7\\u4f18\\u5316\\u3002AddressSanitizer<\\/a> \\u4e5f\\u88ab\\u5f00\\u542f\\u6765\\u68c0\\u6d4bout-of-bounds<\\/code>\\u548cuse-after-free<\\/code>\\u9519\\u8bef\\u3002<\\/p>\\r\\n\\r\\n

\\u4e3a\\u4e86\\u4f7f\\u7528\\u65b9\\u4fbf\\uff0c\\u5927\\u90e8\\u5206\\u6807\\u51c6\\u5e93\\u7684\\u5934\\u6587\\u4ef6\\u5df2\\u7ecf\\u88ab\\u81ea\\u52a8\\u5bfc\\u5165\\u3002<\\/p>\"],\"java\":[\"Java\",\"

\\u7248\\u672c\\uff1aOpenJDK 17<\\/code>\\u3002\\u53ef\\u4ee5\\u4f7f\\u7528Java 8\\u7684\\u7279\\u6027\\u4f8b\\u5982\\uff0clambda expressions \\u548c stream API\\u3002<\\/p>\\r\\n\\r\\n

\\u4e3a\\u4e86\\u65b9\\u4fbf\\u8d77\\u89c1\\uff0c\\u5927\\u90e8\\u5206\\u6807\\u51c6\\u5e93\\u7684\\u5934\\u6587\\u4ef6\\u5df2\\u88ab\\u5bfc\\u5165\\u3002<\\/p>\\r\\n\\r\\n

\\u5305\\u542b Pair \\u7c7b: https:\\/\\/docs.oracle.com\\/javase\\/8\\/javafx\\/api\\/javafx\\/util\\/Pair.html <\\/p>\"],\"python\":[\"Python\",\"

\\u7248\\u672c\\uff1a Python 2.7.12<\\/code><\\/p>\\r\\n\\r\\n

\\u4e3a\\u4e86\\u65b9\\u4fbf\\u8d77\\u89c1\\uff0c\\u5927\\u90e8\\u5206\\u5e38\\u7528\\u5e93\\u5df2\\u7ecf\\u88ab\\u81ea\\u52a8 \\u5bfc\\u5165\\uff0c\\u5982\\uff1aarray<\\/a>, bisect<\\/a>, collections<\\/a>\\u3002\\u5982\\u679c\\u60a8\\u9700\\u8981\\u4f7f\\u7528\\u5176\\u4ed6\\u5e93\\u51fd\\u6570\\uff0c\\u8bf7\\u81ea\\u884c\\u5bfc\\u5165\\u3002<\\/p>\\r\\n\\r\\n

\\u6ce8\\u610f Python 2.7 \\u5c06\\u57282020\\u5e74\\u540e\\u4e0d\\u518d\\u7ef4\\u62a4<\\/a>\\u3002 \\u5982\\u60f3\\u4f7f\\u7528\\u6700\\u65b0\\u7248\\u7684Python\\uff0c\\u8bf7\\u9009\\u62e9Python 3\\u3002<\\/p>\"],\"c\":[\"C\",\"

\\u7248\\u672c\\uff1aGCC 8.2<\\/code>\\uff0c\\u91c7\\u7528GNU11\\u6807\\u51c6\\u3002<\\/p>\\r\\n\\r\\n

\\u7f16\\u8bd1\\u65f6\\uff0c\\u5c06\\u4f1a\\u91c7\\u7528-O1<\\/code>\\u7ea7\\u4f18\\u5316\\u3002 AddressSanitizer<\\/a>\\u4e5f\\u88ab\\u5f00\\u542f\\u6765\\u68c0\\u6d4bout-of-bounds<\\/code>\\u548cuse-after-free<\\/code>\\u9519\\u8bef\\u3002<\\/p>\\r\\n\\r\\n

\\u4e3a\\u4e86\\u4f7f\\u7528\\u65b9\\u4fbf\\uff0c\\u5927\\u90e8\\u5206\\u6807\\u51c6\\u5e93\\u7684\\u5934\\u6587\\u4ef6\\u5df2\\u7ecf\\u88ab\\u81ea\\u52a8\\u5bfc\\u5165\\u3002<\\/p>\\r\\n\\r\\n

\\u5982\\u60f3\\u4f7f\\u7528\\u54c8\\u5e0c\\u8868\\u8fd0\\u7b97, \\u60a8\\u53ef\\u4ee5\\u4f7f\\u7528 uthash<\\/a>\\u3002 \\\"uthash.h\\\"\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5bfc\\u5165\\u3002\\u8bf7\\u770b\\u5982\\u4e0b\\u793a\\u4f8b:<\\/p>\\r\\n\\r\\n

1. \\u5f80\\u54c8\\u5e0c\\u8868\\u4e2d\\u6dfb\\u52a0\\u4e00\\u4e2a\\u5bf9\\u8c61\\uff1a<\\/b>\\r\\n

\\r\\nstruct hash_entry {\\r\\n    int id;            \\/* we'll use this field as the key *\\/\\r\\n    char name[10];\\r\\n    UT_hash_handle hh; \\/* makes this structure hashable *\\/\\r\\n};\\r\\n\\r\\nstruct hash_entry *users = NULL;\\r\\n\\r\\nvoid add_user(struct hash_entry *s) {\\r\\n    HASH_ADD_INT(users, id, s);\\r\\n}\\r\\n<\\/pre>\\r\\n<\\/p>\\r\\n\\r\\n

2. \\u5728\\u54c8\\u5e0c\\u8868\\u4e2d\\u67e5\\u627e\\u4e00\\u4e2a\\u5bf9\\u8c61\\uff1a<\\/b>\\r\\n

\\r\\nstruct hash_entry *find_user(int user_id) {\\r\\n    struct hash_entry *s;\\r\\n    HASH_FIND_INT(users, &user_id, s);\\r\\n    return s;\\r\\n}\\r\\n<\\/pre>\\r\\n<\\/p>\\r\\n\\r\\n

3. \\u4ece\\u54c8\\u5e0c\\u8868\\u4e2d\\u5220\\u9664\\u4e00\\u4e2a\\u5bf9\\u8c61\\uff1a<\\/b>\\r\\n

\\r\\nvoid delete_user(struct hash_entry *user) {\\r\\n    HASH_DEL(users, user);  \\r\\n}\\r\\n<\\/pre>\\r\\n<\\/p>\"],\"csharp\":[\"C#\",\"

C# 10<\\/a> \\u8fd0\\u884c\\u5728 .NET 6 \\u4e0a<\\/p>\"],\"javascript\":[\"JavaScript\",\"

\\u7248\\u672c\\uff1aNode.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n

\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a --harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n

lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n

\\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue<\\/a> \\u548c datastructures-js\\/queue<\\/a>\\u3002<\\/p>\"],\"ruby\":[\"Ruby\",\"

\\u4f7f\\u7528Ruby 3.1<\\/code>\\u6267\\u884c<\\/p>\\r\\n\\r\\n

\\u4e00\\u4e9b\\u5e38\\u7528\\u7684\\u6570\\u636e\\u7ed3\\u6784\\u5df2\\u5728 Algorithms \\u6a21\\u5757\\u4e2d\\u63d0\\u4f9b\\uff1ahttps:\\/\\/www.rubydoc.info\\/github\\/kanwei\\/algorithms\\/Algorithms<\\/p>\"],\"swift\":[\"Swift\",\"

\\u7248\\u672c\\uff1aSwift 5.5.2<\\/code><\\/p>\\r\\n\\r\\n

\\u6211\\u4eec\\u901a\\u5e38\\u4fdd\\u8bc1\\u66f4\\u65b0\\u5230 Apple\\u653e\\u51fa\\u7684\\u6700\\u65b0\\u7248Swift<\\/a>\\u3002\\u5982\\u679c\\u60a8\\u53d1\\u73b0Swift\\u4e0d\\u662f\\u6700\\u65b0\\u7248\\u7684\\uff0c\\u8bf7\\u8054\\u7cfb\\u6211\\u4eec\\uff01\\u6211\\u4eec\\u5c06\\u5c3d\\u5feb\\u66f4\\u65b0\\u3002<\\/p>\"],\"golang\":[\"Go\",\"

\\u7248\\u672c\\uff1aGo 1.17<\\/code><\\/p>\\r\\n\\r\\n

\\u652f\\u6301 https:\\/\\/godoc.org\\/github.com\\/emirpasic\\/gods<\\/a> \\u7b2c\\u4e09\\u65b9\\u5e93\\u3002<\\/p>\"],\"python3\":[\"Python3\",\"

\\u7248\\u672c\\uff1aPython 3.10<\\/code><\\/p>\\r\\n\\r\\n

\\u4e3a\\u4e86\\u65b9\\u4fbf\\u8d77\\u89c1\\uff0c\\u5927\\u90e8\\u5206\\u5e38\\u7528\\u5e93\\u5df2\\u7ecf\\u88ab\\u81ea\\u52a8 \\u5bfc\\u5165\\uff0c\\u5982array<\\/a>, bisect<\\/a>, collections<\\/a>\\u3002 \\u5982\\u679c\\u60a8\\u9700\\u8981\\u4f7f\\u7528\\u5176\\u4ed6\\u5e93\\u51fd\\u6570\\uff0c\\u8bf7\\u81ea\\u884c\\u5bfc\\u5165\\u3002<\\/p>\\r\\n\\r\\n

\\u5982\\u9700\\u4f7f\\u7528 Map\\/TreeMap \\u6570\\u636e\\u7ed3\\u6784\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 sortedcontainers<\\/a> \\u5e93\\u3002<\\/p>\"],\"scala\":[\"Scala\",\"

\\u7248\\u672c\\uff1aScala 2.13<\\/code><\\/p>\"],\"kotlin\":[\"Kotlin\",\"

\\u7248\\u672c\\uff1aKotlin 1.3.10<\\/code><\\/p>\"],\"rust\":[\"Rust\",\"

\\u7248\\u672c\\uff1arust 1.58.1<\\/code><\\/p>\\r\\n\\r\\n

\\u652f\\u6301 crates.io \\u7684 rand<\\/a><\\/p>\"],\"php\":[\"PHP\",\"

PHP 8.1<\\/code>.<\\/p>\\r\\n\\r\\n

With bcmath module.<\\/p>\"],\"typescript\":[\"TypeScript\",\"

TypeScript 4.5.4<\\/p>\\r\\n\\r\\n

Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\"],\"racket\":[\"Racket\",\"

Racket CS<\\/a> v8.3<\\/p>\\r\\n\\r\\n

\\u4f7f\\u7528 #lang racket<\\/p>\\r\\n\\r\\n

\\u5df2\\u9884\\u5148 (require data\\/gvector data\\/queue data\\/order data\\/heap). \\u82e5\\u9700\\u4f7f\\u7528\\u5176\\u5b83\\u6570\\u636e\\u7ed3\\u6784\\uff0c\\u53ef\\u81ea\\u884c require\\u3002<\\/p>\"],\"erlang\":[\"Erlang\",\"Erlang\\/OTP 24.2\"],\"elixir\":[\"Elixir\",\"Elixir 1.13.0 with Erlang\\/OTP 24.2\"]}", + "book": null, + "isSubscribed": false, + "isDailyQuestion": false, + "dailyRecordStatus": null, + "editorType": "CKEDITOR", + "ugcQuestionId": null, + "style": "LEETCODE", + "exampleTestcases": "[2,7,11,15]\n9\n[3,2,4]\n6\n[3,3]\n6", + "jsonExampleTestcases": "[\"[2,7,11,15]\\n9\", \"[3,2,4]\\n6\", \"[3,3]\\n6\"]", + "__typename": "QuestionNode" + } + } +} \ No newline at end of file diff --git a/src/main/resources/leetcode-index.json b/src/main/resources/leetcode-index.json new file mode 100644 index 0000000..e771052 --- /dev/null +++ b/src/main/resources/leetcode-index.json @@ -0,0 +1,63 @@ +{ + "data": { + "problemsetQuestionList": { + "__typename": "QuestionListNode", + "questions": [ + { + "__typename": "QuestionLightNode", + "acRate": 0.5265447404489151, + "difficulty": "EASY", + "freqBar": 0, + "paidOnly": false, + "status": "NOT_STARTED", + "frontendQuestionId": "1", + "isFavor": false, + "solutionNum": 18322, + "title": "Two Sum", + "titleCn": "两数之和", + "titleSlug": "two-sum", + "topicTags": [ + { + "id": "wg0rh", + "name": "Array", + "slug": "array", + "nameTranslated": "数组", + "__typename": "CommonTagNode" + }, + { + "id": "wzve3", + "name": "Hash Table", + "slug": "hash-table", + "nameTranslated": "哈希表", + "__typename": "CommonTagNode" + } + ], + "extra": { + "companyTagNum": 394, + "hasVideoSolution": true, + "topCompanyTags": [ + { + "imgUrl": "https://pic.leetcode-cn.com/5352d58e4b296e0704f20cfd99ecaec7af71d079b015923c72928650312c55b8-Messages Image(3349252251).png", + "slug": "amazon", + "__typename": "CommonTagNode" + }, + { + "imgUrl": "https://pic.leetcode-cn.com/45a64add888e66ff6d3c551bed948528715996937b877aaf6fdc08eae74789f5-google-logo-png-open-2000.png", + "slug": "google", + "__typename": "CommonTagNode" + }, + { + "imgUrl": "https://pic.leetcode-cn.com/cca55ecdfb504378955a9bf4f2f33897bb84f04f9cbf84ea96321ce5d959ee13-adobe1.png", + "slug": "adobe", + "__typename": "CommonTagNode" + } + ], + "__typename": "QuestionExtraInfoNode" + } + } + ], + "hasMore": true, + "total": 2696 + } + } +} \ No newline at end of file diff --git a/src/main/resources/template.txt b/src/main/resources/template.txt index 64bd1e8..3ecd3f5 100644 --- a/src/main/resources/template.txt +++ b/src/main/resources/template.txt @@ -1,25 +1,20 @@ -# {{title}} +# {{englishTitle}} | {{title}} -------------------------------- ## 题目: -{{description}} +{{englishContent}} -## 输入: -> {{inputDescription}} +--- -## 输出: -> {{outputDescription}} +{{content}} -------------------------------- -## 提示:{{hint}} +## 提示: -{{examples}} +{{hints}} -------------------------------- -- 时间限制:{{timeLimit}} -- 内存限制:{{memoryLimit}} - -- 等级:{{level}} +- 难度:{{level}} - id:{{id}} - 标签:{{tags}}