Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc475a62

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent8f0f41b commitc475a62

7 files changed

+211
-5
lines changed

‎leetcode/22. Generate Parentheses.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,25 @@ class Solution {
106106
}
107107
}
108108
}
109-
```
109+
```
110+
111+
###Fourth time
112+
* Method 1: recursion
113+
```Java
114+
class Solution {
115+
private List<String> result;
116+
public List<String> generateParenthesis(int n) {
117+
this.result = new ArrayList<>();
118+
if(n <= 0) return result;
119+
dfs("", n, n);
120+
return result;
121+
}
122+
private void dfs(String cur, int left, int right){
123+
if(left == 0 && right == 0) result.add(new String(cur));
124+
else{
125+
if(left > 0) dfs(cur + "(", left - 1, right);
126+
if(right > left) dfs(cur + ")", left, right - 1);
127+
}
128+
}
129+
}
130+
```

‎leetcode/268. Missing Number.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Solution {
3939
}
4040
```
4141

42-
###Second time
42+
###Second time
4343
1. We first get the sum of the array and the missing value is the deduction between expect sum and sum.
4444
```Java
4545
classSolution {
@@ -52,4 +52,25 @@ class Solution {
5252
return expect- sum;
5353
}
5454
}
55-
```
55+
```
56+
57+
###Third Time
58+
* Method 1: Another method to find missing number in a sorted array.
59+
```Java
60+
class Solution {
61+
public int missingNumber(int[] nums) {
62+
Arrays.sort(nums);
63+
int low = 0, high = nums.length - 1;
64+
return binarySearch(nums, low, high);
65+
}
66+
private int binarySearch(int[] nums, int low, int high){
67+
int mid = low + (high - low) / 2;
68+
if(nums[mid] == mid && nums[mid + 1] != mid + 1) return mid + 1;
69+
else if(nums[mid] != mid){
70+
return binarySearch(nums, low, mid - 1);
71+
}else{
72+
return binarySearch(nums, mid + 1, high);
73+
}
74+
}
75+
}
76+
```

‎leetcode/273. Integer to English Words.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,59 @@ Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Th
117117
return result;
118118
}
119119
}
120+
```
121+
122+
###Third Time
123+
* Method 1: String
124+
```Java
125+
class Solution {
126+
private static final String[] units = new String[]{"", "Thousand", "Million", "Billion"};
127+
private static final String[] ones = new String[]{"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
128+
private static String[] tens = new String[]{"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
129+
private static String[] overTens = new String[]{"", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
130+
public String numberToWords(int num) {
131+
if(num == 0) return "Zero";
132+
String res = "";
133+
int bigUnitIndex = 0;
134+
while(num > 0){
135+
int cur = num % 1000;
136+
String curString = parse(cur);
137+
num /= 1000;
138+
if(bigUnitIndex == 0){
139+
res = curString;
140+
++bigUnitIndex;
141+
continue;
142+
}
143+
if(curString.length() != 0){
144+
res = curString + " " + units[bigUnitIndex] + (res.length() == 0 ? "": " ") + res;
145+
}
146+
bigUnitIndex++;
147+
}
148+
return res;
149+
}
150+
private String parse(int num){
151+
if(num < 10) return ones[num];
152+
else if(num > 10 && num < 20) return overTens[num - 10];
153+
else{
154+
String res = "";
155+
if(num % 100 > 10 && num % 100 < 20){
156+
res = overTens[num % 100 - 10];
157+
num /= 100;
158+
}else{
159+
if(num % 10 != 0){
160+
res = ones[num % 10];
161+
}
162+
num /= 10;
163+
if(num % 10 != 0){
164+
res = tens[num % 10] + (res.length() == 0 ? "": " " + res);
165+
}
166+
num /= 10;
167+
}
168+
if(num % 10 != 0){
169+
res = ones[num % 10] + " Hundred" + (res.length() == 0 ? "": " ") + res;
170+
}
171+
return res;
172+
}
173+
}
174+
}
120175
```

‎leetcode/344. Reverse String.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,22 @@ class Solution {
5555
arr[j]= temp;
5656
}
5757
}
58-
```
58+
```
59+
60+
###Second Time
61+
* Method 1: in-place, 2 pointers
62+
```Java
63+
class Solution {
64+
public void reverseString(char[] s) {
65+
int low = 0, high = s.length - 1;
66+
while(low < high){
67+
swap(s, low++, high--);
68+
}
69+
}
70+
private void swap(char[] s, int a, int b){
71+
char temp = s[a];
72+
s[a] = s[b];
73+
s[b] = temp;
74+
}
75+
}
76+
```

‎leetcode/93. Restore IP Addresses.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,31 @@ Output: ["255.255.11.135", "255.255.111.35"]
4040
}
4141
}
4242
```
43+
44+
###Amazon Session
45+
* Method 1: recursion
46+
```Java
47+
class Solution {
48+
private List<String> result;
49+
private String s;
50+
public List<String> restoreIpAddresses(String s) {
51+
this.result = new ArrayList<>();
52+
this.s = s;
53+
dfs(0, 0, "");
54+
return this.result;
55+
}
56+
private void dfs(int index, int count, String temp){
57+
if(index == s.length() && count == 4) this.result.add(temp);
58+
else if(count < 4){
59+
for(int i = index + 1; i <= index + 3 && i <= s.length(); i++){
60+
String sub = s.substring(index, i);
61+
int cur = Integer.parseInt(sub);
62+
if(sub.charAt(0) == '0' && sub.length() != 1) return;
63+
if(cur >= 0 && cur <= 255){
64+
dfs(i, count + 1, temp + (temp.length() == 0 ? "": '.') + cur);
65+
}
66+
}
67+
}
68+
}
69+
}
70+
```

‎leetcode/94. Binary Tree Inorder Traversal.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,32 @@ class Solution {
2828
inorder(n.right, result);
2929
}
3030
}
31-
```
31+
```
32+
33+
###Second time
34+
* Method 1: Recursion
35+
```Java
36+
/**
37+
* Definition for a binary tree node.
38+
* public class TreeNode {
39+
* int val;
40+
* TreeNode left;
41+
* TreeNode right;
42+
* TreeNode(int x) { val = x; }
43+
* }
44+
*/
45+
class Solution {
46+
private List<Integer> result;
47+
public List<Integer> inorderTraversal(TreeNode root) {
48+
this.result = new ArrayList<>();
49+
dfs(root);
50+
return this.result;
51+
}
52+
private void dfs(TreeNode node){
53+
if(node == null) return;
54+
dfs(node.left);
55+
this.result.add(node.val);
56+
dfs(node.right);
57+
}
58+
}
59+
```

‎leetcode/97. Interleaving String.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,38 @@ class Solution {
7979
}
8080
}
8181
```
82+
83+
###Third time
84+
* Method 1: dp
85+
```Java
86+
class Solution {
87+
public boolean isInterleave(String s1, String s2, String s3) {
88+
char[] arr1 = s1.toCharArray();
89+
char[] arr2 = s2.toCharArray();
90+
char[] arr3 = s3.toCharArray();
91+
if(arr1.length + arr2.length != arr3.length) return false;
92+
boolean[][] dp = new boolean[arr1.length + 1][arr2.length + 1];
93+
dp[0][0] = true;
94+
for(int i = 0; i < arr1.length; i++){
95+
if(arr1[i] == arr3[i])
96+
dp[i + 1][0] = dp[i][0];
97+
}
98+
for(int i = 0; i < arr2.length; i++){
99+
if(arr2[i] == arr3[i])
100+
dp[0][i + 1] = dp[0][i];
101+
}
102+
for(int i = 1; i <= arr1.length; i++){
103+
for(int j = 1; j <= arr2.length; j++){
104+
// dp[i][j] saves if i + j - 1 can be constructed by i && j
105+
if(arr3[i + j - 1] == arr1[i - 1]){
106+
dp[i][j] = dp[i - 1][j];
107+
}
108+
if(arr3[i + j - 1] == arr2[j - 1]){
109+
dp[i][j] |= dp[i][j - 1];
110+
}
111+
}
112+
}
113+
return dp[arr1.length][arr2.length];
114+
}
115+
}
116+
```

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp