Skip to content

Commit ccc9a97

Browse files
committed
20161009
1 parent fd64d6e commit ccc9a97

File tree

8 files changed

+239
-1
lines changed

8 files changed

+239
-1
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class Solution {
2+
public boolean isOneEditDistance(String s, String t) {
3+
if (s.length()>t.length()) {
4+
return isOneEditDistance(t, s);
5+
}
6+
if (s.length()+1<t.length()) {
7+
return false;
8+
}
9+
char[] cs=s.toCharArray();
10+
char[] ct=t.toCharArray();
11+
for (int i = 0; i < cs.length; i++) {
12+
if (cs[i]!=ct[i]) {
13+
if (s.length()==t.length()) {
14+
return s.substring(i+1,s.length()).equals(t.substring(i+1,t.length()));
15+
}else{
16+
return s.substring(i,s.length()).equals(t.substring(i+1,t.length()));
17+
}
18+
}
19+
}
20+
return s.length()!=t.length();
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class Solution {
2+
public int longestPalindrome(String s) {
3+
int[] nums=new int[52];
4+
char[] cs=s.toCharArray();
5+
for (int i = 0; i < cs.length; i++) {
6+
if (cs[i]>=97) {
7+
nums[cs[i]-97+26]++;
8+
}else{
9+
nums[cs[i]-65]++;
10+
}
11+
}
12+
int ans=0;
13+
for (int i = 0; i < nums.length; i++) {
14+
ans+=nums[i]/2*2;
15+
if (ans%2==0) {
16+
ans+=nums[i]%2;
17+
}
18+
}
19+
return ans;
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class Solution {
2+
public String addStrings(String num1, String num2) {
3+
StringBuffer sb=new StringBuffer();
4+
int l1=num1.length();
5+
int l2=num2.length();
6+
char[] c1=num1.toCharArray();
7+
char[] c2=num2.toCharArray();
8+
int carry=0;
9+
for (int i = 0; i <l1||i<l2; i++) {
10+
int a1=i<l1?c1[l1-1-i]-48:0;
11+
int a2=i<l2?c2[l2-1-i]-48:0;
12+
int ans=a1+a2+carry;
13+
sb.append(ans%10);
14+
carry=ans/10;
15+
}
16+
if (carry==1) {
17+
sb.append("1");
18+
}
19+
return sb.reverse().toString();
20+
}
21+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
public class Solution {
2+
boolean[][] rPO;
3+
int[][] v4PO;
4+
boolean[][] rAO;
5+
int[][] v4AO;
6+
int m;
7+
int n;
8+
int[][] matrix;
9+
10+
public List<int[]> pacificAtlantic(int[][] matrix) {
11+
this.matrix = matrix;
12+
m = matrix.length;
13+
List<int[]> res = new ArrayList<int[]>();
14+
if (m == 0) {
15+
return res;
16+
}
17+
n = matrix[0].length;
18+
rPO = new boolean[m][n];
19+
v4PO = new int[m][n];
20+
rAO = new boolean[m][n];
21+
v4AO = new int[m][n];
22+
for (int i = 0; i < m; i++) {
23+
visitForPacificOcean(i, 0, 0);
24+
visitForAtlanticOcean(i, n - 1, 0);
25+
}
26+
for (int i = 0; i < n; i++) {
27+
visitForPacificOcean(0, i, 0);
28+
visitForAtlanticOcean(m - 1, i, 0);
29+
}
30+
for (int i = 0; i < m; i++) {
31+
for (int j = 0; j < n; j++) {
32+
if (rPO[i][j] && rAO[i][j]) {
33+
int[] ans = { i, j };
34+
res.add(ans);
35+
}
36+
}
37+
}
38+
return res;
39+
}
40+
41+
public void visitForPacificOcean(int x, int y, int lastHeight) {
42+
if (x >= 0 && x < m && y >= 0 && y < n && v4PO[x][y] == 0 && matrix[x][y] >= lastHeight) {
43+
rPO[x][y] = true;
44+
v4PO[x][y] = 1;
45+
visitForPacificOcean(x - 1, y, matrix[x][y]);
46+
visitForPacificOcean(x + 1, y, matrix[x][y]);
47+
visitForPacificOcean(x, y - 1, matrix[x][y]);
48+
visitForPacificOcean(x, y + 1, matrix[x][y]);
49+
}
50+
}
51+
52+
public void visitForAtlanticOcean(int x, int y, int lastHeight) {
53+
if (x >= 0 && x < m && y >= 0 && y < n && v4AO[x][y] == 0 && matrix[x][y] >= lastHeight) {
54+
rAO[x][y] = true;
55+
v4AO[x][y] = 1;
56+
visitForAtlanticOcean(x - 1, y, matrix[x][y]);
57+
visitForAtlanticOcean(x + 1, y, matrix[x][y]);
58+
visitForAtlanticOcean(x, y - 1, matrix[x][y]);
59+
visitForAtlanticOcean(x, y + 1, matrix[x][y]);
60+
}
61+
}
62+
}

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
|158|[Read N Characters Given Read4 II - Call multiple times](https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/)|Hard|noNote|no|no|no|no|no|
161161
|159|[Longest Substring with At Most Two Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/)|Hard|noNote|no|no|no|no|no|
162162
|160|[Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/)|Easy|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/160. Intersection of Two Linked Lists/Solution.java)|[Python](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/160. Intersection of Two Linked Lists/Solution.py)|no|no|no|
163-
|161|[One Edit Distance](https://leetcode.com/problems/one-edit-distance/)|Medium|noNote|no|no|no|no|no|
163+
|161|[One Edit Distance](https://leetcode.com/problems/one-edit-distance/)|Medium|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/161. One Edit Distance/Solution.java)|no|no|no|no|
164164
|162|[Find Peak Element](https://leetcode.com/problems/find-peak-element/)|Medium|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/162. Find Peak Element/Solution.java)|no|no|no|no|
165165
|163|[Missing Ranges](https://leetcode.com/problems/missing-ranges/)|Medium|noNote|no|no|no|no|no|
166166
|164|[Maximum Gap](https://leetcode.com/problems/maximum-gap/)|Hard|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/164. Maximum Gap/Solution.java)|no|no|no|no|
@@ -390,3 +390,11 @@
390390
|405|[Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal/)|Easy|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/405. Convert a Number to Hexadecimal/Solution.java)|no|no|no|no|
391391
|406|[Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/)|Medium|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/406. Queue Reconstruction by Height/Solution.java)|no|no|no|no|
392392
|407|[Trapping Rain Water II](https://leetcode.com/problems/trapping-rain-water-ii/)|Hard|noNote|no|no|no|no|no|
393+
|408|[Valid Word Abbreviation](https://leetcode.com/problems/valid-word-abbreviation/)|Easy|noNote|no|no|no|no|no|
394+
|409|[Longest Palindrome](https://leetcode.com/problems/longest-palindrome/)|Easy|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/409. Longest Palindrome/Solution.java)|no|no|no|no|
395+
|410|[Split Array Largest Sum](https://leetcode.com/problems/split-array-largest-sum/)|Hard|noNote|no|no|no|no|no|
396+
|411|[Minimum Unique Word Abbreviation](https://leetcode.com/problems/minimum-unique-word-abbreviation/)|Hard|noNote|no|no|no|no|no|
397+
|415|[Add Strings](https://leetcode.com/problems/add-strings/)|Easy|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/415. Add Strings/Solution.java)|no|no|no|no|
398+
|416|[Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/)|Medium|noNote|no|no|no|no|no|
399+
|417|[Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/)|Medium|noNote|[Java](https://github.com/corpsepiges/leetcode/blob/master/Algorithms/417. Pacific Atlantic Water Flow/Solution.java)|no|no|no|no|
400+
|418|[Sentence Screen Fitting](https://leetcode.com/problems/sentence-screen-fitting/)|Hard|noNote|no|no|no|no|no|

java/409. Longest Palindrome.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class Solution {
2+
public int longestPalindrome(String s) {
3+
int[] nums=new int[52];
4+
char[] cs=s.toCharArray();
5+
for (int i = 0; i < cs.length; i++) {
6+
if (cs[i]>=97) {
7+
nums[cs[i]-97+26]++;
8+
}else{
9+
nums[cs[i]-65]++;
10+
}
11+
}
12+
int ans=0;
13+
for (int i = 0; i < nums.length; i++) {
14+
ans+=nums[i]/2*2;
15+
if (ans%2==0) {
16+
ans+=nums[i]%2;
17+
}
18+
}
19+
return ans;
20+
}
21+
}

java/415. Add Strings.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class Solution {
2+
public String addStrings(String num1, String num2) {
3+
StringBuffer sb=new StringBuffer();
4+
int l1=num1.length();
5+
int l2=num2.length();
6+
char[] c1=num1.toCharArray();
7+
char[] c2=num2.toCharArray();
8+
int carry=0;
9+
for (int i = 0; i <l1||i<l2; i++) {
10+
int a1=i<l1?c1[l1-1-i]-48:0;
11+
int a2=i<l2?c2[l2-1-i]-48:0;
12+
int ans=a1+a2+carry;
13+
sb.append(ans%10);
14+
carry=ans/10;
15+
}
16+
if (carry==1) {
17+
sb.append("1");
18+
}
19+
return sb.reverse().toString();
20+
}
21+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
public class Solution {
2+
boolean[][] rPO;
3+
int[][] v4PO;
4+
boolean[][] rAO;
5+
int[][] v4AO;
6+
int m;
7+
int n;
8+
int[][] matrix;
9+
10+
public List<int[]> pacificAtlantic(int[][] matrix) {
11+
this.matrix = matrix;
12+
m = matrix.length;
13+
List<int[]> res = new ArrayList<int[]>();
14+
if (m == 0) {
15+
return res;
16+
}
17+
n = matrix[0].length;
18+
rPO = new boolean[m][n];
19+
v4PO = new int[m][n];
20+
rAO = new boolean[m][n];
21+
v4AO = new int[m][n];
22+
for (int i = 0; i < m; i++) {
23+
visitForPacificOcean(i, 0, 0);
24+
visitForAtlanticOcean(i, n - 1, 0);
25+
}
26+
for (int i = 0; i < n; i++) {
27+
visitForPacificOcean(0, i, 0);
28+
visitForAtlanticOcean(m - 1, i, 0);
29+
}
30+
for (int i = 0; i < m; i++) {
31+
for (int j = 0; j < n; j++) {
32+
if (rPO[i][j] && rAO[i][j]) {
33+
int[] ans = { i, j };
34+
res.add(ans);
35+
}
36+
}
37+
}
38+
return res;
39+
}
40+
41+
public void visitForPacificOcean(int x, int y, int lastHeight) {
42+
if (x >= 0 && x < m && y >= 0 && y < n && v4PO[x][y] == 0 && matrix[x][y] >= lastHeight) {
43+
rPO[x][y] = true;
44+
v4PO[x][y] = 1;
45+
visitForPacificOcean(x - 1, y, matrix[x][y]);
46+
visitForPacificOcean(x + 1, y, matrix[x][y]);
47+
visitForPacificOcean(x, y - 1, matrix[x][y]);
48+
visitForPacificOcean(x, y + 1, matrix[x][y]);
49+
}
50+
}
51+
52+
public void visitForAtlanticOcean(int x, int y, int lastHeight) {
53+
if (x >= 0 && x < m && y >= 0 && y < n && v4AO[x][y] == 0 && matrix[x][y] >= lastHeight) {
54+
rAO[x][y] = true;
55+
v4AO[x][y] = 1;
56+
visitForAtlanticOcean(x - 1, y, matrix[x][y]);
57+
visitForAtlanticOcean(x + 1, y, matrix[x][y]);
58+
visitForAtlanticOcean(x, y - 1, matrix[x][y]);
59+
visitForAtlanticOcean(x, y + 1, matrix[x][y]);
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)