Skip to content

Commit 3d03800

Browse files
committed
update
1 parent c676e46 commit 3d03800

4 files changed

Lines changed: 268 additions & 49 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
layout: post
3+
category : LeetCode
4+
tagline: "正则表达式匹配2"
5+
tags : [LeetCode,面试,正则匹配,贪心,递归]
6+
---
7+
{% include JB/setup %}
8+
9+
<h4>{{page.title|remove_first:'Leetcode之'}}</h4>
10+
11+
>I Given an array of non-negative integers, you are initially positioned at the first index of the array.
12+
>
13+
>Each element in the array represents your maximum jump length at that position.
14+
>
15+
>Your goal is to reach the last index in the minimum number of jumps.
16+
>
17+
>For example:
18+
Given array `A = [2,3,1,1,4]`
19+
>
20+
>The minimum number of jumps to reach the last index is `2`. (Jump 1 step from index `0` to `1`, then `3` steps to the last index.)
21+
22+
23+
解法:一开始想了是`O(n^2)`的算法,用`flag[i]`来记录从`i`到末尾的最小步数,然后从后往前填充`flag[i]`,算法TLE。
24+
25+
之后想了一种用`flag[i]`来表示第`i`步能到达的最远距离,遍历数组,如遍历到元素`j`,找出能到达`j`的最小步数,用来更新`flag[i+1]`。思想是对的,算法不够优化。
26+
27+
代码如下:能AC
28+
29+
//{java}
30+
public class Solution {
31+
public int jump(int[] A) {
32+
if(A.length==1)
33+
return 0;
34+
int flag[] = new int[A.length];
35+
flag[A.length-1]=0;
36+
flag[1]=A[0];
37+
int max=1;
38+
for(int i=1;i<A.length-1;i++)
39+
{
40+
int j=1;
41+
for(j=1;j<=max;j++)
42+
{
43+
//第j步就可以到达i
44+
if(flag[j]>=i)
45+
{
46+
break;
47+
}
48+
}
49+
//j+1步最远能到哪?
50+
if(i+A[i]>flag[j+1])
51+
{
52+
flag[j+1]=i+A[i];
53+
if(j+1>max)
54+
max=j+1;
55+
}
56+
}
57+
for(int i=1;i<A.length;i++)
58+
if(flag[i]>=A.length-1)
59+
return i;
60+
return 0;
61+
}
62+
}
63+
64+
之后想到了一种`O(n)`的方法,遍历数组,每次只遍历只有第`i`步才能到点,如`1231221`,第一次遍历`1`,第二次遍历`2`,第三次遍历`31`,第四次遍历`122`,再走一步即可到末尾,总共需要4步。即每次只遍历到上一次遍历时更新的最大距离处。`O(n)`算法。
65+
66+
代码如下:
67+
68+
//{java}
69+
public class Solution {
70+
public int jump(int[] A) {
71+
if(A.length==1)
72+
return 0;
73+
int step=0;
74+
int last_max=0;
75+
int max=0;
76+
while(max<A.length-1)
77+
{
78+
int t =max;
79+
for(int i=last_max;i<=t&&i<A.length-1;i++)
80+
{
81+
if(i+A[i]>max)
82+
{
83+
max=i+A[i];
84+
}
85+
}
86+
last_max=t+1;
87+
step++;
88+
}
89+
return step;
90+
}
91+
}
92+
93+
代码写得显得有些繁琐,Discuss中有人贴出了更简洁的代码。思想是一样的。
94+
95+
//{java}
96+
public class Solution {
97+
public int jump(int[] A) {
98+
int step=0;
99+
int last_max=0;
100+
int max=0;
101+
for(int i=0;i<A.length;i++)
102+
{
103+
if(i>last_max)
104+
{
105+
last_max=max;
106+
step++;
107+
}
108+
max=Math.max(i+A[i],max);
109+
}
110+
return step;
111+
}
112+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
layout: post
3+
category : LeetCode
4+
tagline: "包含子串的最小窗口"
5+
tags : [LeetCode,面试,字符子串,最小窗口]
6+
---
7+
{% include JB/setup %}
8+
9+
<h4>{{page.title|remove_first:'Leetcode之'}}</h4>
10+
11+
> Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
12+
>
13+
For example,
14+
S = `"ADOBECODEBANC"`
15+
T = `"ABC"`
16+
>
17+
Minimum window is `"BANC"`.
18+
>
19+
Note:
20+
If there is no such window in S that covers all characters in T, return the emtpy string `""`.
21+
>
22+
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
23+
24+
25+
解法:好想不好编,调试了好多次。和最长非重复子串很像。这里需要先记录Target string中字符分布。e往后扩展,对S进行扫描,当扫描到一个窗口满足要求时,再缩小s确定最小的窗口。中间有个常数数量级的循环来判断窗口是否包含了T。
26+
27+
代码如下:
28+
29+
//{java}
30+
public class Solution {
31+
public String minWindow(String S, String T) {
32+
int stardard[] =new int[256];
33+
for(int i=0;i<T.length();i++)
34+
{
35+
stardard[T.charAt(i)]++;
36+
}
37+
int flag[] = new int[256];
38+
int s=0,e=0;
39+
boolean e_go = true;
40+
int min = 0x7fffffff;
41+
String re = "";
42+
while(s<e+T.length() && (e<S.length()||e_go==false))
43+
{
44+
if(e_go)
45+
{
46+
char c = S.charAt(e++);
47+
flag[c]++;
48+
if(stardard[c]!=0&&flag[c]>=stardard[c])
49+
{
50+
boolean get =true;
51+
for(int i=0;i<256;i++)
52+
{
53+
if(stardard[i]!=0 && flag[i]<stardard[i])
54+
{
55+
get=false;
56+
break;
57+
}
58+
}
59+
if(get)
60+
{
61+
if(e-s<min)
62+
{
63+
re = S.substring(s,e);
64+
min = e-s;
65+
}
66+
e_go=false;
67+
continue;
68+
}
69+
}
70+
}else
71+
{
72+
char c = S.charAt(s++);
73+
flag[c]--;
74+
if(stardard[c]!=0&&flag[c]<stardard[c])
75+
{
76+
e_go=true;
77+
continue;
78+
}
79+
if(e-s<min)
80+
{
81+
re = S.substring(s,e);
82+
min = e-s;
83+
}
84+
}
85+
}
86+
return re;
87+
}
88+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
layout: post
3+
category : LeetCode
4+
tagline: "深处优先搜索字符串"
5+
tags : [LeetCode,面试,DFS,深度优先,遍历]
6+
---
7+
{% include JB/setup %}
8+
9+
<h4>{{page.title|remove_first:'Leetcode之'}}</h4>
10+
11+
> Given a 2D board and a word, find if the word exists in the grid.
12+
>
13+
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
14+
>
15+
For example,
16+
Given board =
17+
>
18+
[
19+
["ABCE"],
20+
["SFCS"],
21+
["ADEE"]
22+
]
23+
>
24+
word = `"ABCCED"`, -> returns `true`,
25+
word = `"SEE"`, -> returns `true`,
26+
word = `"ABCB"`, -> returns `false`.
27+
28+
解法:深度优先遍历,一开始超时,是将`flag`的初始化放在两层循环内了
29+
这样消耗很大,当数组太大时,二维数组初始化消耗也很大,可以认为是`O(n^2)`。深搜主要关键是确定递归的参数。
30+
31+
改进后的代码如下:
32+
33+
//{java}
34+
public class Solution {
35+
public boolean exist(char[][] board, String word) {
36+
boolean flag[][]=new boolean[board.length][board[0].length];
37+
for(int i=0;i<board.length;i++)
38+
{
39+
for(int j=0;j<board[0].length;j++)
40+
{
41+
if(slove(board,flag,i,j,word,0))
42+
{
43+
return true;
44+
}
45+
}
46+
}
47+
return false;
48+
}
49+
public boolean slove(char [][]board,boolean [][]flag,int i,int j,String word,int k)
50+
{
51+
if(i<0 || j<0 || i>=board.length || j>=board[0].length || flag[i][j] || board[i][j]!=word.charAt(k))
52+
return false;
53+
if(k==word.length()-1)
54+
return true;
55+
flag[i][j]=true;
56+
if(slove(board,flag,i+1,j,word,k+1)||slove(board,flag,i-1,j,word,k+1)||slove(board,flag,i,j+1,word,k+1)||slove(board,flag,i,j-1,word,k+1))
57+
{
58+
return true;
59+
}
60+
flag[i][j]=false;
61+
return false;
62+
}
63+
}

‎index.md‎

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,10 @@
11
---
22
layout: page
3-
title: Hello World!
4-
tagline: Supporting tagline
3+
title : Archive
4+
header : Post Archive
5+
group: navigation
56
---
67
{% include JB/setup %}
78

8-
Read [Jekyll Quick Start](http://jekyllbootstrap.com/usage/jekyll-quick-start.html)
9-
10-
Complete usage and documentation available at: [Jekyll Bootstrap](http://jekyllbootstrap.com)
11-
12-
## Update Author Attributes
13-
14-
In `_config.yml` remember to specify your own data:
15-
16-
title : My Blog =)
17-
18-
author :
19-
name : Name Lastname
20-
email : blah@email.test
21-
github : username
22-
twitter : username
23-
24-
The theme should reference these variables whenever needed.
25-
26-
## Sample Posts
27-
28-
This blog contains sample posts which help stage pages and blog data.
29-
When you don't need the samples anymore just delete the `_posts/core-samples` folder.
30-
31-
$ rm -rf _posts/core-samples
32-
33-
Here's a sample "posts list".
34-
35-
<ul class="posts">
36-
{% for post in site.posts %}
37-
<li><span>{{ post.date | date_to_string }}</span> &raquo; <a href="{{ BASE_PATH }}{{ post.url }}">{{ post.title }}</a></li>
38-
{% endfor %}
39-
</ul>
40-
41-
## To-Do
42-
43-
This theme is still unfinished. If you'd like to be added as a contributor, [please fork](http://github.com/plusjade/jekyll-bootstrap)!
44-
We need to clean up the themes, make theme usage guides with theme-specific markup examples.
45-
46-
'''
47-
asdasdas
48-
'''
49-
50-
public static void main(String [])
51-
{
52-
Sysotem.out.println("s");
53-
}
54-
9+
{% assign posts_collate = site.posts %}
10+
{% include JB/posts_collate %}

0 commit comments

Comments
 (0)