leecode更新
This commit is contained in:
parent
381f8efdde
commit
ee7a15d432
|
|
@ -33,7 +33,7 @@ public class Example7 {
|
|||
env.setParallelism(1);
|
||||
|
||||
env
|
||||
.readTextFile("D:\\example\\self_example\\Big_data_example\\Flink\\src\\main\\resources\\UserBehavior.csv")
|
||||
.readTextFile("E:\\self_example\\Big_data_example\\Flink\\src\\main\\resources\\UserBehavior.csv")
|
||||
.map(new MapFunction<String, UserBehavior>() {
|
||||
@Override
|
||||
public UserBehavior map(String value) throws Exception {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,92 @@
|
|||
package com.markilue.leecode.hot100;
|
||||
|
||||
import com.sun.deploy.util.StringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
*@BelongsProject: Leecode
|
||||
*@BelongsPackage: com.markilue.leecode.hot100
|
||||
*@Author: markilue
|
||||
*@CreateTime: 2023-03-01 10:11
|
||||
*@Description:
|
||||
* TODO 力扣7题 整数反转:
|
||||
* 给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。
|
||||
* 如果反转后整数超过 32 位的有符号整数的范围 [−231, 231 − 1] ,就返回 0。
|
||||
* 假设环境不允许存储 64 位整数(有符号或无符号)。
|
||||
*@Version: 1.0
|
||||
*/
|
||||
public class T07_Reverse {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
// String s = "00205";
|
||||
// System.out.println(Integer.parseInt(s));205
|
||||
|
||||
// int x=-120;
|
||||
int x = -2147483648;
|
||||
System.out.println(reverse1(x));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 思路:把它转化为字符串,在进行反转
|
||||
* 速度击败17.3% 内存击败53.2% 2ms
|
||||
* @param x
|
||||
* @return
|
||||
*/
|
||||
public int reverse(int x) {
|
||||
boolean flag = true;//记录这个数是否是正数
|
||||
if (x < 0) flag = false;
|
||||
|
||||
String s = "" + Math.abs((long) x);//这里必须要转为long,不然Integer.Min不能进行反转
|
||||
char[] chars = s.toCharArray();
|
||||
//反转字符串
|
||||
reverseString(chars);
|
||||
long result = Long.parseLong(new String(chars));
|
||||
if (!flag) result = -result;
|
||||
|
||||
if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) return 0;
|
||||
|
||||
return (int) result;
|
||||
|
||||
}
|
||||
|
||||
public void reverseString(char[] chars) {
|
||||
int left = 0;
|
||||
int right = chars.length - 1;
|
||||
char temp;
|
||||
while (left < right) {
|
||||
temp = chars[left];
|
||||
chars[left] = chars[right];
|
||||
chars[right] = temp;
|
||||
left++;
|
||||
right--;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 思路:考虑直接把它当整数进行反转
|
||||
* 速度击败100% 内存击败56.5% 0ms
|
||||
* @param x
|
||||
* @return
|
||||
*/
|
||||
public int reverse1(int x) {
|
||||
boolean flag = true;//记录这个数是否是正数
|
||||
if (x < 0) flag = false;
|
||||
|
||||
long y = (long) x;
|
||||
long result = 0;
|
||||
|
||||
while (y != 0) {
|
||||
result = result * 10 + y % 10;
|
||||
y /= 10;
|
||||
}
|
||||
|
||||
if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) return 0;
|
||||
|
||||
|
||||
return (int) result;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,151 @@
|
|||
package com.markilue.leecode.hot100;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*@BelongsProject: Leecode
|
||||
*@BelongsPackage: com.markilue.leecode.hot100
|
||||
*@Author: markilue
|
||||
*@CreateTime: 2023-03-01 10:53
|
||||
*@Description:
|
||||
* TODO 力扣8题 字符串转换整数(atoi):
|
||||
* 请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数)。
|
||||
* 函数 myAtoi(string s) 的算法如下:
|
||||
* 读入字符串并丢弃无用的前导空格
|
||||
* 检查下一个字符(假设还未到字符末尾)为正还是负号,读取该字符(如果有)。 确定最终结果是负数还是正数。 如果两者都不存在,则假定结果为正。
|
||||
* 读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。字符串的其余部分将被忽略。
|
||||
* 将前面步骤读入的这些数字转换为整数(即,"123" -> 123, "0032" -> 32)。如果没有读入数字,则整数为 0 。必要时更改符号(从步骤 2 开始)。
|
||||
* 如果整数数超过 32 位有符号整数范围 [−231, 231 − 1] ,需要截断这个整数,使其保持在这个范围内。具体来说,小于 −231 的整数应该被固定为 −231 ,大于 231 − 1 的整数应该被固定为 231 − 1 。
|
||||
* 返回整数作为最终结果。
|
||||
* 注意:
|
||||
* 本题中的空白字符只包括空格字符 ' ' 。
|
||||
* 除前导空格或数字后的其余字符串外,请勿忽略 任何其他字符。
|
||||
*@Version: 1.0
|
||||
*/
|
||||
public class T08_MyAtoi {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String s = " ss-125-sa7894e ";
|
||||
String s1 = " words and 987 ";
|
||||
String s2 = " wor98ds and 7 ";
|
||||
String s3 = " 9words and 87 ";
|
||||
String s5 = " 9 8words and 87 ";
|
||||
String s6 = " -9 8words and 87 ";
|
||||
String s4 = "2147483648";
|
||||
String s7 = "20000000000000000000";
|
||||
// System.out.println(myAtoi(s));
|
||||
// System.out.println(myAtoi(s1));
|
||||
// System.out.println(myAtoi(s2));
|
||||
// System.out.println(myAtoi(s3));
|
||||
System.out.println(myAtoi(s4));
|
||||
System.out.println(myAtoi(s5));
|
||||
System.out.println(myAtoi(s6));
|
||||
System.out.println(myAtoi(s7));
|
||||
}
|
||||
|
||||
/**
|
||||
* 思路:利用StringBuilder记录
|
||||
* 速度击败100% 内存击败84.23%
|
||||
* @param s
|
||||
* @return
|
||||
*/
|
||||
public int myAtoi(String s) {
|
||||
|
||||
char[] chars = s.toCharArray();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
boolean flag = false;//记录是否已经开始匹配到数字 :避免后续有 ‘-’的情况
|
||||
|
||||
for (char aChar : chars) {
|
||||
if (((aChar == '-' || aChar == '+') && !flag) || (aChar - '0' >= 0 && aChar - '0' <= 9)) {
|
||||
//是数字;或者是开头的-
|
||||
sb.append(aChar);
|
||||
flag = true;
|
||||
} else if (!flag && aChar == ' ') {
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (sb.length() == 0 || (sb.length() == 1 && (sb.charAt(0) == '-' || sb.charAt(0) == '+'))) return 0;
|
||||
|
||||
// long result = Long.parseLong(sb.toString());//超出长度
|
||||
boolean flag1 = true;//记录这个数是不是正数
|
||||
long result = 0;
|
||||
int startIndex = 0;
|
||||
if (sb.charAt(0) == '-') {
|
||||
flag1 = false;
|
||||
startIndex = 1;
|
||||
} else if (sb.charAt(0) == '+') {
|
||||
startIndex = 1;
|
||||
}
|
||||
|
||||
for (int i = startIndex; i < sb.length(); i++) {
|
||||
|
||||
if (flag1) {
|
||||
result = result * 10 + (sb.charAt(i) - '0');
|
||||
} else {
|
||||
result = result * 10 - (sb.charAt(i) - '0');
|
||||
}
|
||||
if (result < Integer.MIN_VALUE) return Integer.MIN_VALUE;
|
||||
if (result > Integer.MAX_VALUE) return Integer.MAX_VALUE;
|
||||
}
|
||||
|
||||
return (int) result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 官方解法:有限状态机
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public int myAtoi1(String str) {
|
||||
Automaton automaton = new Automaton();
|
||||
int length = str.length();
|
||||
for (int i = 0; i < length; ++i) {
|
||||
automaton.get(str.charAt(i));
|
||||
}
|
||||
return (int) (automaton.sign * automaton.ans);
|
||||
}
|
||||
|
||||
|
||||
class Automaton {
|
||||
public int sign = 1;
|
||||
public long ans = 0;
|
||||
private String state = "start";
|
||||
private Map<String, String[]> table = new HashMap<String, String[]>() {{
|
||||
put("start", new String[]{"start", "signed", "in_number", "end"});
|
||||
put("signed", new String[]{"end", "end", "in_number", "end"});
|
||||
put("in_number", new String[]{"end", "end", "in_number", "end"});
|
||||
put("end", new String[]{"end", "end", "end", "end"});
|
||||
}};
|
||||
|
||||
public void get(char c) {
|
||||
state = table.get(state)[get_col(c)];
|
||||
if ("in_number".equals(state)) {
|
||||
ans = ans * 10 + c - '0';
|
||||
ans = sign == 1 ? Math.min(ans, (long) Integer.MAX_VALUE) : Math.min(ans, -(long) Integer.MIN_VALUE);
|
||||
} else if ("signed".equals(state)) {
|
||||
sign = c == '+' ? 1 : -1;
|
||||
}
|
||||
}
|
||||
|
||||
private int get_col(char c) {
|
||||
if (c == ' ') {
|
||||
return 0;
|
||||
}
|
||||
if (c == '+' || c == '-') {
|
||||
return 1;
|
||||
}
|
||||
if (Character.isDigit(c)) {
|
||||
return 2;
|
||||
}
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,163 @@
|
|||
package com.markilue.leecode.hot100;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
*@BelongsProject: Leecode
|
||||
*@BelongsPackage: com.markilue.leecode.hot100
|
||||
*@Author: markilue
|
||||
*@CreateTime: 2023-03-01 12:09
|
||||
*@Description:
|
||||
* TODO 力扣11题 盛最多的水:
|
||||
* 给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。
|
||||
* 找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
|
||||
* 返回容器可以储存的最大水量。
|
||||
* 说明:你不能倾斜容器。
|
||||
*@Version: 1.0
|
||||
*/
|
||||
public class T09_MaxArea {
|
||||
|
||||
@Test
|
||||
public void test(){
|
||||
int[] height={1,8,6,2,5,4,8,3,7};
|
||||
int[] height1={1,0,0,0,0,0,0,2,2};
|
||||
System.out.println(maxArea1(height));
|
||||
}
|
||||
|
||||
/**
|
||||
* 思路:似乎可以使用动态规划:
|
||||
* TODO DP五部曲:
|
||||
* 1.dp定义: dp[i][j]表示以i开头以j结尾的容器能盛多少水
|
||||
* 2.dp状态转移方程:
|
||||
* 1.dp[i][j]<min(nums[i],nums[j-1])
|
||||
* dp[i][j]=dp[i][j-1]-(j-1-i)*(min-nums[j])+nums[j]
|
||||
* 2.min(nums[i],nums[j-1])<dp[i][j]<max(nums[i],nums[j-1])&& min=nums[j-1]
|
||||
* dp[i][j]=dp[i][j-1]+(j-1-i)*(nums[j]-min)+nums[j]
|
||||
* 3. min=nums[i]
|
||||
* dp[i][j]=dp[i][j-1]+nums[i]
|
||||
* 4.dp[i][j]>max(nums[i],nums[j-1]) && min=nums[j-1]
|
||||
* dp[i][j]=dp[i][j-1]+(j-1-i)*(max-min)+max
|
||||
* 3.dp初始化:dp[i][i]=0; dp[i][i+1]=min(nums[i],nums[i+1])
|
||||
* 4.dp遍历顺序:
|
||||
* 5.dp举例推导:
|
||||
* [1 8 6 2 5 4 8 3 7]
|
||||
* 1 0 1 2
|
||||
* 8
|
||||
* 6
|
||||
* 2
|
||||
* 5
|
||||
* 4
|
||||
* 8
|
||||
* 3
|
||||
* 7
|
||||
* @param height
|
||||
* @return
|
||||
*/
|
||||
public int maxArea(int[] height) {
|
||||
|
||||
int[][] dp = new int[height.length][height.length];
|
||||
|
||||
// for (int i = 0; i < dp.length; i++) {
|
||||
// dp[i][i] = 0;
|
||||
// }
|
||||
int result = 0;
|
||||
|
||||
for (int i = 0; i < dp.length; i++) {
|
||||
for (int j = i + 1; j < dp[0].length; j++) {
|
||||
int min = Math.min(height[i], height[j - 1]);
|
||||
int max = Math.max(height[i], height[j - 1]);
|
||||
if (height[j] < Math.min(height[i], height[j - 1])) {
|
||||
dp[i][j] = dp[i][j - 1] - (j - 1 - i) * (min - height[j]) + height[j];
|
||||
} else if (min == height[i]) {
|
||||
dp[i][j] = dp[i][j - 1] + height[i];
|
||||
} else if (min <= height[j] && height[j] <= max) {
|
||||
dp[i][j] = dp[i][j - 1] + (j - 1 - i) * (height[j] - min) + height[j];
|
||||
} else {
|
||||
dp[i][j] = dp[i][j - 1] + (j - 1 - i) * (max - min) + max;
|
||||
}
|
||||
if(result<dp[i][j]) result=dp[i][j];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public int maxArea1(int[] height) {
|
||||
|
||||
int[] dp=new int[height.length];
|
||||
|
||||
// for (int i = 0; i < dp.length; i++) {
|
||||
// dp[i][i] = 0;
|
||||
// }
|
||||
int result = 0;
|
||||
|
||||
for (int i = 0; i < dp.length; i++) {
|
||||
dp=new int[height.length];
|
||||
for (int j = i + 1; j < dp.length; j++) {
|
||||
int min = Math.min(height[i], height[j - 1]);
|
||||
int max = Math.max(height[i], height[j - 1]);
|
||||
if (height[j] < Math.min(height[i], height[j - 1])) {
|
||||
dp[j] = dp[j - 1] - (j - 1 - i) * (min - height[j]) + height[j];
|
||||
} else if (min == height[i]) {
|
||||
dp[j] = dp[j - 1] + height[i];
|
||||
} else if (min <= height[j] && height[j] <= max) {
|
||||
dp[j] = dp[j - 1] + (j - 1 - i) * (height[j] - min) + height[j];
|
||||
} else {
|
||||
dp[j] = dp[j - 1] + (j - 1 - i) * (max - min) + max;
|
||||
}
|
||||
if (result < dp[j]) result = dp[j];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 官方双指针法,本质上还是让分别上让左右两个为界,计算雨水,得出最大值
|
||||
* 速度击败59.94% 内存击败56.49% 4ms
|
||||
* @param height
|
||||
* @return
|
||||
*/
|
||||
public int maxArea2(int[] height) {
|
||||
int l = 0, r = height.length - 1;
|
||||
int ans = 0;
|
||||
while (l < r) {
|
||||
int area = Math.min(height[l], height[r]) * (r - l);
|
||||
ans = Math.max(ans, area);
|
||||
if (height[l] <= height[r]) {
|
||||
++l;
|
||||
}
|
||||
else {
|
||||
--r;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 官方最快:
|
||||
* 本质上就是在官方题解的方法上,增加了一层while循环,减少判断次数
|
||||
* 速度击败100% 内存击败29.31% 1ms
|
||||
* @param height
|
||||
* @return
|
||||
*/
|
||||
public int maxArea3(int[] height) {
|
||||
if(height == null || height.length == 0) return 0;
|
||||
int left = 0, right = height.length - 1,area = 0;
|
||||
while(left < right){
|
||||
if(height[left] <= height[right]){
|
||||
int pre = height[left];
|
||||
area = Math.max(area, pre * (right - left));
|
||||
while(left < right && height[left] <= pre) left++;
|
||||
}else{
|
||||
int pre = height[right];
|
||||
area = Math.max(area, pre * (right - left));
|
||||
while(left < right && height[right] <= pre) right--;
|
||||
}
|
||||
}
|
||||
return area;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue