Skip to content

Commit b3807ce

Browse files
committed
custom code demo
1 parent 5ab8441 commit b3807ce

16 files changed

+840
-1
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1-
# leetcode-question
1+
# leetcode-question
2+
[leetcode-editor](https://github.com/shuzijun/leetcode-question) custom code demo
3+
## leetcode-editor config
4+
CodeFileName:
5+
```java
6+
$!velocityTool.camelCaseName(${question.titleSlug})
7+
```
8+
TemplateConstant:
9+
```java
10+
${question.content}
11+
12+
package com.shuzijun.leetcode.editor.en;
13+
public class $!velocityTool.camelCaseName(${question.titleSlug}){
14+
public static void main(String[] args) {
15+
Solution solution = new $!velocityTool.camelCaseName(${question.titleSlug})().new Solution();
16+
}
17+
${question.code}
18+
}
19+
```

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.shuzijun</groupId>
8+
<artifactId>leetcode-question</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
12+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.shuzijun.leetcode;
2+
3+
/**
4+
* Created with IntelliJ IDEA.
5+
* User: shuzijun
6+
* Date: 2018-11-22
7+
* Time: 20:52
8+
* To change this template use File | Settings | File Templates.
9+
*/
10+
public class HelloWorld {
11+
12+
public static void main(String[] args) {
13+
System.out.println("hello world");
14+
}
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.shuzijun.leetcode;
2+
3+
/**
4+
5+
*/
6+
public class ListNode {
7+
public int val;
8+
public ListNode next;
9+
10+
public ListNode(int x) {
11+
val = x;
12+
}
13+
14+
@Override
15+
public String toString() {
16+
final StringBuilder sb = new StringBuilder().append(val);
17+
if (next != null) {
18+
sb.append("->").append(next);
19+
}
20+
return sb.toString();
21+
}
22+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
2+
//
3+
// You may assume the two numbers do not contain any leading zero, except the number 0 itself.
4+
//
5+
// Example:
6+
//
7+
//
8+
//Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
9+
//Output: 7 -> 0 -> 8
10+
//Explanation: 342 + 465 = 807.
11+
//
12+
//
13+
14+
package com.shuzijun.leetcode.editor.en;
15+
16+
import com.shuzijun.leetcode.ListNode;
17+
18+
public class AddTwoNumbers {
19+
public static void main(String[] args) {
20+
Solution solution = new AddTwoNumbers().new Solution();
21+
}
22+
23+
24+
//leetcode submit region begin(Prohibit modification and deletion)
25+
26+
/**
27+
* Definition for singly-linked list.
28+
* public class ListNode {
29+
* int val;
30+
* ListNode next;
31+
* ListNode(int x) { val = x; }
32+
* }
33+
*/
34+
class Solution {
35+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
36+
int carrying = 0;
37+
ListNode returnListNode = null;
38+
ListNode thisListNode = null;
39+
while (l1 != null || l2 != null || carrying != 0) {
40+
int val1 = 0;
41+
int val2 = 0;
42+
if (l1 != null) {
43+
val1 = l1.val;
44+
l1 = l1.next;
45+
}
46+
if (l2 != null) {
47+
val2 = l2.val;
48+
l2 = l2.next;
49+
}
50+
int val = val1 + val2 + carrying;
51+
if (returnListNode == null) {
52+
returnListNode = new ListNode(val % 10);
53+
thisListNode = returnListNode;
54+
} else {
55+
ListNode nextListNode = new ListNode(val % 10);
56+
thisListNode.next = nextListNode;
57+
thisListNode = nextListNode;
58+
59+
}
60+
carrying = val / 10;
61+
62+
}
63+
return returnListNode;
64+
}
65+
}
66+
//leetcode submit region end(Prohibit modification and deletion)
67+
68+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
2+
//
3+
// Note: You may not slant the container and n is at least 2.
4+
//
5+
//
6+
//
7+
//
8+
//
9+
// The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
10+
//
11+
//
12+
//
13+
// Example:
14+
//
15+
//
16+
//Input: [1,8,6,2,5,4,8,3,7]
17+
//Output: 49
18+
//
19+
20+
package com.shuzijun.leetcode.editor.en;
21+
22+
public class ContainerWithMostWater {
23+
public static void main(String[] args) {
24+
Solution solution = new ContainerWithMostWater().new Solution();
25+
int[] height = {1, 8, 6, 2, 5, 4, 8, 3, 7};
26+
int max = solution.maxArea(height);
27+
System.out.println(max);
28+
}
29+
30+
31+
//leetcode submit region begin(Prohibit modification and deletion)
32+
class Solution {
33+
public int maxArea(int[] height) {
34+
int max = 0;
35+
int l = 0;
36+
int r = height.length - 1;
37+
while (l < r) {
38+
int h = Math.min(height[l], height[r]);
39+
max = Math.max(max, (r - l) * h);
40+
if (height[l] < height[r]) {
41+
l++;
42+
} else {
43+
r--;
44+
}
45+
}
46+
return max;
47+
}
48+
}
49+
//leetcode submit region end(Prohibit modification and deletion)
50+
51+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
//Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
3+
//
4+
//
5+
//
6+
//For example, given n = 3, a solution set is:
7+
//
8+
//
9+
//[
10+
// "((()))",
11+
// "(()())",
12+
// "(())()",
13+
// "()(())",
14+
// "()()()"
15+
//]
16+
//
17+
18+
package com.shuzijun.leetcode.editor.en;
19+
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
public class GenerateParentheses {
24+
public static void main(String[] args) {
25+
Solution solution = new GenerateParentheses().new Solution();
26+
System.out.print(solution.generateParenthesis(3));
27+
}
28+
29+
30+
//leetcode submit region begin(Prohibit modification and deletion)
31+
class Solution {
32+
public List<String> generateParenthesis(int n) {
33+
List<String> ans = new ArrayList();
34+
backtrack(ans, "", 0, 0, n);
35+
return ans;
36+
}
37+
38+
public void backtrack(List<String> ans, String cur, int open, int close, int max) {
39+
if (cur.length() == max * 2) {
40+
ans.add(cur);
41+
return;
42+
}
43+
44+
if (open < max)
45+
backtrack(ans, cur + "(", open + 1, close, max);
46+
if (close < open)
47+
backtrack(ans, cur + ")", open, close + 1, max);
48+
}
49+
50+
}
51+
//leetcode submit region end(Prohibit modification and deletion)
52+
53+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
2+
//
3+
// Example 1:
4+
//
5+
//
6+
//Input: 121
7+
//Output: true
8+
//
9+
//
10+
// Example 2:
11+
//
12+
//
13+
//Input: -121
14+
//Output: false
15+
//Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
16+
//
17+
//
18+
// Example 3:
19+
//
20+
//
21+
//Input: 10
22+
//Output: false
23+
//Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
24+
//
25+
//
26+
// Follow up:
27+
//
28+
// Coud you solve it without converting the integer to a string?
29+
//
30+
31+
package com.shuzijun.leetcode.editor.en;
32+
33+
public class PalindromeNumber {
34+
public static void main(String[] args) {
35+
Solution solution = new PalindromeNumber().new Solution();
36+
System.out.println(solution.isPalindrome(10));
37+
}
38+
39+
40+
//leetcode submit region begin(Prohibit modification and deletion)
41+
class Solution {
42+
public boolean isPalindrome(int x) {
43+
if (x < 0 || (x % 10 == 0 && x != 0)) {
44+
return false;
45+
} else if (x < 10) {
46+
return true;
47+
}
48+
int i = 0;
49+
while (true) {
50+
i = i * 10 + x % 10;
51+
x = x / 10;
52+
if (i == x) {
53+
return true;
54+
} else if (i > x && (i % x < 10 && i / 10 == x)) {
55+
return true;
56+
} else if (i > x) {
57+
return false;
58+
}
59+
}
60+
}
61+
}
62+
//leetcode submit region end(Prohibit modification and deletion)
63+
64+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//Given a 32-bit signed integer, reverse digits of an integer.
2+
//
3+
// Example 1:
4+
//
5+
//
6+
//Input: 123
7+
//Output: 321
8+
//
9+
//
10+
// Example 2:
11+
//
12+
//
13+
//Input: -123
14+
//Output: -321
15+
//
16+
//
17+
// Example 3:
18+
//
19+
//
20+
//Input: 120
21+
//Output: 21
22+
//
23+
//
24+
// Note:
25+
//Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
26+
//
27+
28+
package com.shuzijun.leetcode.editor.en;
29+
30+
public class ReverseInteger {
31+
public static void main(String[] args) {
32+
Solution solution = new ReverseInteger().new Solution();
33+
}
34+
35+
36+
//leetcode submit region begin(Prohibit modification and deletion)
37+
class Solution {
38+
public int reverse(int x) {
39+
int rev = 0;
40+
while (x != 0) {
41+
int pop = x % 10;
42+
x /= 10;
43+
if (rev > Integer.MAX_VALUE / 10 || (rev == Integer.MAX_VALUE / 10 && pop > 7)) return 0;
44+
if (rev < Integer.MIN_VALUE / 10 || (rev == Integer.MIN_VALUE / 10 && pop < -8)) return 0;
45+
rev = rev * 10 + pop;
46+
}
47+
return rev;
48+
}
49+
}
50+
//leetcode submit region end(Prohibit modification and deletion)
51+
52+
}

0 commit comments

Comments
 (0)