add
This commit is contained in:
parent
f6d2c9c195
commit
0a6b066776
38
5.leetcode/src/com/fanxb/common/Q125.java
Normal file
38
5.leetcode/src/com/fanxb/common/Q125.java
Normal file
@ -0,0 +1,38 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
/**
|
||||
* 题目地址: https://leetcode-cn.com/problems/partition-labels/
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/9 15:10
|
||||
*/
|
||||
public class Q125 {
|
||||
|
||||
public boolean isPalindrome(String s) {
|
||||
char[] chars = s.toLowerCase().toCharArray();
|
||||
int i=0,j=chars.length-1;
|
||||
while (i<j){
|
||||
char ci = chars[i];
|
||||
if(!isOk(ci)) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
char cj = chars[j];
|
||||
if(!isOk(cj)){
|
||||
j--;
|
||||
continue;
|
||||
}
|
||||
if(ci !=cj) return false;
|
||||
i++;j--;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isOk(char ci){
|
||||
return (ci>='a' && ci<='z') || (ci>='0' && ci<='9');
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] prices = {7, 1, 5, 3, 6, 4};
|
||||
}
|
||||
}
|
27
5.leetcode/src/com/fanxb/common/Q209.java
Normal file
27
5.leetcode/src/com/fanxb/common/Q209.java
Normal file
@ -0,0 +1,27 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Q209 {
|
||||
|
||||
public int minSubArrayLen(int target, int[] nums) {
|
||||
int res = Integer.MAX_VALUE, l = 0, r = 0;
|
||||
int sum = 0;
|
||||
while (r < nums.length) {
|
||||
//循环添加右边的元素到窗口中
|
||||
sum += nums[r];
|
||||
//如果和大于target了,就开始减掉左边的元素,并计算最小值
|
||||
while (sum >= target) {
|
||||
res = Math.min(res, r - l + 1);
|
||||
sum -= nums[l];
|
||||
l++;
|
||||
}
|
||||
r++;
|
||||
}
|
||||
return res == Integer.MAX_VALUE ? 0 : res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] s = {1, 2, 3, 1};
|
||||
}
|
||||
}
|
@ -39,7 +39,32 @@ public class Q3 {
|
||||
return Math.max(characters.size(), res);
|
||||
}
|
||||
|
||||
public int lengthOfLongestSubstring1(String s) {
|
||||
int l = 0, r = 0, length = s.length();
|
||||
int[] map = new int[129];
|
||||
int res = 0;
|
||||
while (r < length) {
|
||||
char c = s.charAt(r);
|
||||
boolean has = map[c] == 1;
|
||||
if (has) {
|
||||
res = Math.max(res, r - l);
|
||||
//遇到重复的,把l右移直到排除掉重复的
|
||||
while (true) {
|
||||
char c1 = s.charAt(l);
|
||||
map[c1] = 0;
|
||||
l++;
|
||||
if (c1 == c) break;
|
||||
}
|
||||
} else if (r == length - 1) {
|
||||
res = Math.max(res, r - l + 1);
|
||||
}
|
||||
map[c] = 1;
|
||||
r++;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q3().lengthOfLongestSubstring("bbbbbbbbbbbbbbbbb"));
|
||||
System.out.println(new Q3().lengthOfLongestSubstring1("abcabcbb"));
|
||||
}
|
||||
}
|
||||
|
35
5.leetcode/src/com/fanxb/common/Q392.java
Normal file
35
5.leetcode/src/com/fanxb/common/Q392.java
Normal file
@ -0,0 +1,35 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
/**
|
||||
* 两数相加
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2021/6/1
|
||||
**/
|
||||
public class Q392 {
|
||||
public boolean isSubsequence(String s, String t) {
|
||||
int sLength=s.length(),tLength=t.length(),si=0,ti=0;
|
||||
while (si<sLength && ti<tLength){
|
||||
if(t.charAt(ti) == s.charAt(si)){
|
||||
si++;ti++;
|
||||
continue;
|
||||
}
|
||||
int i=ti+1;
|
||||
for(;i<tLength;i++){
|
||||
if(t.charAt(i)==s.charAt(si)){
|
||||
ti = i+1;
|
||||
si++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(i==tLength){
|
||||
si=0;
|
||||
ti++;
|
||||
}
|
||||
}
|
||||
return si>=sLength;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user