提交 0554cec6 编写于 作者: L liu13

20190626

上级 eb428505
package code;
import java.util.Stack;
/*
* 1028. Recover a Tree From Preorder Traversal
* 题意:从先序遍历恢复二叉树
* 难度:Hard
* 分类:Depth-first Search
* 思路:用栈存储,迭代比较就可以了
* Tips:
*/
public class lc1028 {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public TreeNode recoverFromPreorder(String S) {
int level, val;
Stack<TreeNode> stack = new Stack<>();
for (int i = 0; i < S.length();) {
for (level = 0; S.charAt(i) == '-'; i++) {
level++;
}
for (val = 0; i < S.length() && S.charAt(i) != '-'; i++) { //数字可能是大于9
val = val * 10 + (S.charAt(i) - '0');
}
while (stack.size() > level) { //用stack的size和depth比就可以了
stack.pop();
}
TreeNode node = new TreeNode(val);
if (!stack.isEmpty()) {
if (stack.peek().left == null) {
stack.peek().left = node;
} else {
stack.peek().right = node;
}
}
stack.add(node);
}
while (stack.size() > 1) {
stack.pop();
}
return stack.pop();
}
}
package code;
/*
* 1093. Statistics from a Large Sample
* 题意:从数组里统计出均值,最大,最小,中位数,众数
* 难度:Medium
* 分类:
* 思路:
* Tips:中位数记住可能是两个数的均值,可能是中间的一个数,用 pos/2 来处理
*/
public class lc1093 {
public double[] sampleStats(int[] count) {
double[] res = new double[5];
res[0] = -1;
res[1] = -1;
int mode_num = 0;
int count_num = 0;
for (int i = 0; i < count.length ; i++) {
if(res[0]==-1 && count[i]>0) res[0] = i;
if(count[i]>0 && i>res[1]) res[1] = i;
res[2] += count[i]*i;
count_num += count[i];
if(count[i]>mode_num) {
res[4] = i;
mode_num = count[i];
}
}
int medium_pos1 = (count_num+1)/2; //中位数
int medium_pos2 = (count_num+2)/2;
count_num = 0;
for (int i = 0; i < count.length ; i++) {
count_num += count[i];
if(medium_pos1>0&&medium_pos1<=count_num) {
res[3]+= i;
medium_pos1 = -1;
}
if(medium_pos2>0&&medium_pos2<=count_num) {
res[3]+= i;
medium_pos2 = -1;
}
}
res[2] = res[2]/count_num;
res[3] = res[3]/2;
return res;
}
}
package code;
/*
* 1094. Car Pooling
* 题意:
* 难度:Medium
* 分类:
* 思路:lc56 类似的题,但该题的集合没有传递覆盖的特性,所以遍历的时候又加了个循环,N^2
* 非常巧妙的方法,每次记录上车,下车人数,然后遍历一遍进行模拟上下车
* Tips:lc253, lc56
*/
import java.util.Arrays;
import java.util.Comparator;
public class lc1094 {
public boolean carPooling(int[][] trips, int capacity) {
Arrays.sort(trips, new Comparator<int[]>() { // 记住这种写法
@Override
public int compare(int[] o1, int[] o2) {
if(o1[1]!=o2[1])
return o1[1] - o2[1];
else
return o2[2] - o1[2];
}
});
for (int i = 0; i < trips.length ; i++) {
int current = trips[i][0];
for (int j = i-1; j >=0 ; j--) { //往前找是否重叠
if(trips[j][2]>trips[i][1]) current += trips[j][0];
}
if(current>capacity) return false;
}
return true;
}
public boolean carPooling2(int[][] trips, int capacity) {
int stops[] = new int[1001];
for (int t[] : trips) {
stops[t[1]] += t[0];
stops[t[2]] -= t[0];
}
for (int i = 0; capacity >= 0 && i < 1001; ++i) capacity -= stops[i];
return capacity >= 0;
}
}
......@@ -20,6 +20,6 @@ public class lc139 {
dp[i] = true;
}
}
return dp[s.length()+1];
return dp[s.length()];
}
}
......@@ -12,7 +12,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class lc22 {
public class lc22 {
public static void main(String[] args) {
System.out.println(generateParenthesis2(4));
}
......
package code;
/*
* 3. Longest Substring Without Repeating Characters
* 题意:找出字符串中没有重复子串的最大长度
* 题意:找出字符串中没有重复字母的最大长度
* 难度:Medium
* 分类:Hash Table, Two Pointers, String
* 算法:两个指针,记录没有重复字母的子串的首和尾
......
......@@ -40,12 +40,12 @@ public class lc32 {
public static int longestValidParentheses2(String s) {
//栈方法 ()(())
Stack<Integer> st = new Stack();
st.add(-1);
st.add(-1); //栈内先入-1
int res = 0;
for (int i = 0; i < s.length() ; i++) {
char ch = s.charAt(i);
if(ch=='(')
st.add(i);
st.push(i);
else if(ch==')'){
st.pop();
if(st.isEmpty())
......
......@@ -24,6 +24,6 @@ public class lc49 {
m.put(key,l);
}
}
return new ArrayList(m.values());
return new ArrayList(m.values()); //学下这句的语法
}
}
......@@ -10,7 +10,7 @@ package code;
public class lc50 {
public double myPow(double x, int n) {
if(n == 0) return 1;
if(n==Integer.MIN_VALUE && x!=1 && x!=-1) return 0;
if(n==Integer.MIN_VALUE && x!=1 && x!=-1) return 0; //负数最小值,结果溢出
if(n<0){
n = -n;
x = 1/x; //负数变成 1/x
......
......@@ -2,7 +2,7 @@ package code;
/*
* 63. Unique Paths II
* 题意:路径数
* 题意:路径数,添加了障碍
* 难度:Medium
* 分类:Array, Dynamic Programming
* 思路:和lc64, lc62思路一样
......
package code;
/*
* 31. Search in Rotated Sorted Array II
* 81. Search in Rotated Sorted Array II
* 题意:在翻转有序数组中查找指定数,数组中可能有相等的元素
* 难度:Medium
* 分类:Array, Binary Search
......
package interview;
import java.util.Scanner;
public class byte1 {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int res = Integer.MIN_VALUE;
int[] dp = new int[n];
dp[0] = arr[0];
for (int i = 1; i < n; i++) {
dp[i] = Math.max(dp[i-1], arr[i]+i);
}
for (int i = 1; i < n ; i++) {
res = Math.max(arr[i]-i+dp[i-1], res);
}
System.out.println(res);
}
public static void main2 (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int res = 0;
for (int i = 0; i < n ; i++) {
for (int j = 0; j < i ; j++) {
int temp = arr[i]+arr[j]-i+j;
res = Math.max(res, temp);
}
}
System.out.println(res);
}
}
package interview;
import java.util.Scanner;
public class byte2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
if(m*n==0) {
System.out.println(0);
return;
}
int[][] grid = new int[n][m];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
grid[i][j] = sc.nextInt();
}
}
int count = 0;
for (int i = 0; i < n ; i++) {
for (int j = 0; j < m ; j++) {
if( grid[i][j]==1 ){
count++;
grid[i][j] = 0; //置0, 下次不用考虑
search(grid,i,j);
}
}
}
System.out.println(count);
}
public static void search(int[][] grid, int i, int j){
if( i>0 && grid[i-1][j]==1) {
grid[i-1][j] = 0; //置0, 下次不用考虑
search(grid, i - 1, j);
}
if( j>0 && grid[i][j-1]==1) {
grid[i][j-1] = 0;
search(grid, i, j-1);
}
if( i+1<grid.length && grid[i+1][j]==1) {
grid[i+1][j] = 0;
search(grid, i + 1, j);
}
if( j+1<grid[0].length && grid[i][j+1]==1) {
grid[i][j+1] = 0;
search(grid, i, j+1);
}
if( i>0 && j>0 && grid[i-1][j-1]==1){
grid[i-1][j-1] = 0;
search(grid, i-1, j-1);
}
if( i+1<grid.length && j+1<grid[0].length && grid[i+1][j+1]==1){
grid[i+1][j+1] = 0;
search(grid, i+1, j+1);
}
if( i>0 && j+1<grid[0].length && grid[i-1][j+1]==1){
grid[i-1][j+1] = 0;
search(grid, i-1, j+1);
}
if( i+1<grid.length && j>0 && grid[i+1][j-1]==1){
grid[i+1][j-1] = 0;
search(grid, i+1, j-1);
}
return;
}
}
package interview;
import java.util.Scanner;
public class byte21 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int w = sc.nextInt();
int b = sc.nextInt();
int[][] dp = new int[5000][2];
int i = 1;
dp[0][0] = 1;
dp[0][1] = 1;
for (; i < 5000; i++) {
if(w-i>=0){
dp[i][0] = dp[i-1][0] + dp[i-1][1];//白
w=w-i;
}else{
dp[i][0] = 0;
}
if(b-i>=0){
dp[i][1] = dp[i-1][0] + dp[i-1][1];//黑
b = b-i;
}else{
dp[i][1] = 0;
}
if(dp[i][0]==0&&dp[i][1]==0) break;
}
System.out.println(i-1);
System.out.println(dp[i-1][0]+dp[i-1][1]);
}
}
package interview;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class byte22 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
String str2 = sc.nextLine();
String[] str_arr = str2.split(" ");
List<String> str_list = new ArrayList<>();
for(String s : str_arr) str_list.add(s);
System.out.println(helper(str1, str_list));
}
public static String helper(String s, List<String> wordDict) {
boolean[] dp = new boolean[s.length()+1];
dp[0] = true;
for (int i = 1; i < dp.length ; i++) {
for (int j = 0; j < i ; j++) { //遍历之前计算出来的结果
if( dp[j]==true && wordDict.contains(s.substring(j,i)))
dp[i] = true;
}
}
if(dp[s.length()]) return "True";
return "Fales";
}
}
package interview;
import java.util.Scanner;
public class byte23 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
}
}
package interview;
import java.util.Scanner;
import java.util.Stack;
public class byte3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
Stack<Character> st = new Stack();
int i = 0;
char[] str_arr = s.toCharArray();
StringBuilder res = new StringBuilder();
while(i<s.length()){
if(str_arr[i]=='#'){
StringBuilder temp = new StringBuilder();
StringBuilder num = new StringBuilder();
while(st.peek()!='%'){
temp.insert(0, st.pop());
}
st.pop(); // pop出 [
while(!st.isEmpty() && Character.isDigit(st.peek())){
num.insert(0, st.pop());
}
StringBuilder temp2 = new StringBuilder();
for (int j = 0; j < Integer.valueOf(num.toString()) ; j++) {
temp2.append(temp.toString());
}
//入栈
for (int j = 0; j < temp2.toString().length() ; j++) {
st.push(temp2.toString().toCharArray()[j]);
}
}else{
st.push(str_arr[i]);
}
i++;
}
// 别忘了末尾的
StringBuilder sb1 = new StringBuilder();
while(!st.isEmpty()){
sb1.insert(0, st.pop());
}
res.append(sb1);
System.out.println(res.toString());
}
}
......@@ -235,3 +235,4 @@ LeetCode 指南
| 1025 [Java](./code/lc1025.java)
| 1026 [Java](./code/lc1026.java)
| 1027 [Java](./code/lc1027.java)
| 1028 [Java](./code/lc1028.java)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册