add
This commit is contained in:
parent
cbb59d2f9c
commit
9612be20a5
47
5.leetcode/src/com/fanxb/common/Q14.java
Normal file
47
5.leetcode/src/com/fanxb/common/Q14.java
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package com.fanxb.common;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA
|
||||||
|
*
|
||||||
|
* @author fanxb
|
||||||
|
* Date: 2020/6/10 10:49
|
||||||
|
*/
|
||||||
|
public class Q14 {
|
||||||
|
public String longestCommonPrefix(String[] strs) {
|
||||||
|
if (strs.length == 1) return strs[0];
|
||||||
|
StringBuilder res = new StringBuilder();
|
||||||
|
int length = strs.length;
|
||||||
|
int minLength = Stream.of(strs).map(String::length).min(Integer::compare).orElse(0);
|
||||||
|
char a, b, c;
|
||||||
|
for (int i = 0; i < minLength; i++) {
|
||||||
|
for (int j = 1; j < length; j++) {
|
||||||
|
if (strs[j].charAt(i) != strs[j - 1].charAt(i)) return res.toString();
|
||||||
|
}
|
||||||
|
res.append(strs[0].charAt(i));
|
||||||
|
}
|
||||||
|
return res.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String longestCommonPrefix1(String[] strs) {
|
||||||
|
if (strs.length == 1) return strs[0];
|
||||||
|
String res = strs[0];
|
||||||
|
for (int i = 1; i < strs.length; i++) {
|
||||||
|
StringBuilder temp = new StringBuilder();
|
||||||
|
for (int j = 0; j < Math.min(res.length(), strs[i].length()); j++) {
|
||||||
|
if (res.charAt(j) == strs[i].charAt(j)) temp.append(res.charAt(j));
|
||||||
|
else break;
|
||||||
|
}
|
||||||
|
if (temp.isEmpty()) return "";
|
||||||
|
res = temp.toString();
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Q14 instance = new Q14();
|
||||||
|
}
|
||||||
|
}
|
28
5.leetcode/src/com/fanxb/common/Q151.java
Normal file
28
5.leetcode/src/com/fanxb/common/Q151.java
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package com.fanxb.common;
|
||||||
|
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
public class Q151 {
|
||||||
|
public String reverseWords(String s) {
|
||||||
|
Stack<String> stack = new Stack<>();
|
||||||
|
StringBuilder temp = new StringBuilder();
|
||||||
|
for (int i = 0; i < s.length(); i++) {
|
||||||
|
if (s.charAt(i) == ' ') {
|
||||||
|
if (!temp.isEmpty()) {
|
||||||
|
stack.push(temp.toString());
|
||||||
|
temp = new StringBuilder();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
temp.append(s.charAt(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!temp.isEmpty()) stack.push(temp.toString());
|
||||||
|
StringBuilder res = new StringBuilder();
|
||||||
|
res.append(stack.pop());
|
||||||
|
while (!stack.isEmpty()) res.append(" ").append(stack.pop());
|
||||||
|
return res.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,7 @@
|
|||||||
package com.fanxb.common;
|
package com.fanxb.common;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class Q6 {
|
public class Q6 {
|
||||||
public String convert(String s, int numRows) {
|
public String convert(String s, int numRows) {
|
||||||
if (numRows == 1) {
|
if (numRows == 1) {
|
||||||
@ -39,7 +41,40 @@ public class Q6 {
|
|||||||
return new String(strs);
|
return new String(strs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String convert1(String s, int numRows) {
|
||||||
|
int length = s.length();
|
||||||
|
if (length <= 1 || numRows == 1) return s;
|
||||||
|
int nSize = numRows * 2 - 2;
|
||||||
|
int lineNum = (numRows - 1) * (length / nSize + 1);
|
||||||
|
char[][] chars = new char[numRows][lineNum];
|
||||||
|
for (int i = 0; i < numRows; i++) Arrays.fill(chars[i], ' ');
|
||||||
|
int count = 0, m = 0, n = 0;
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
chars[m][n] = s.charAt(i);
|
||||||
|
count++;
|
||||||
|
if (count == nSize) {
|
||||||
|
count = 0;
|
||||||
|
m--;
|
||||||
|
n++;
|
||||||
|
} else if (count < numRows) {
|
||||||
|
m++;
|
||||||
|
} else {
|
||||||
|
m--;
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
for (int i = 0; i < numRows; i++) {
|
||||||
|
for (int j = 0; j < lineNum; j++) {
|
||||||
|
if (chars[i][j] != ' ') {
|
||||||
|
builder.append(chars[i][j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println(new Q6().convert("PAYPALISHIRING", 4));
|
System.out.println(new Q6().convert1("PAYPALISHIRING", 4));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user