Skip to content

Commit 19ccb3a

Browse files
committed
update
1 parent 50d8119 commit 19ccb3a

3 files changed

Lines changed: 282 additions & 27 deletions
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
12+
>Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
13+
>
14+
>For "(()", the longest valid parentheses substring is "()", which has length = 2.
15+
>
16+
>Another example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4.
17+
18+
解法:哎,不好干啊,一开始只管考虑右括号。忽略了`"()(()"`这种情况左括号也阻碍连续匹配的情况。用栈把匹配的左括号也标记起来。然后同时考虑左括号和右括号,再扫一遍字符串。
19+
20+
21+
代码如下:虽然过了,但是太长了,哎。
22+
23+
//{java}
24+
public class Solution {
25+
public int longestValidParentheses(String s) {
26+
int stack[] = new int[s.length()];
27+
int top=0;
28+
int count=0;
29+
int max=0;
30+
int flag[] = new int[s.length()];
31+
for(int i=0;i<s.length();i++)
32+
{
33+
char c = s.charAt(i);
34+
if(c=='(')
35+
stack[top++]=i;
36+
else
37+
{
38+
if(top>0)
39+
{
40+
int j= stack[--top];
41+
flag[j]=1;
42+
}
43+
}
44+
}
45+
int zuokuohao=0;
46+
for(int i=0;i<s.length();i++)
47+
{
48+
char c = s.charAt(i);
49+
if(c=='(')
50+
{
51+
zuokuohao++;
52+
if(flag[i]==1)
53+
count++;
54+
else
55+
{
56+
max=Math.max(max,count);
57+
count=0;
58+
}
59+
}
60+
else
61+
{
62+
zuokuohao--;
63+
if(zuokuohao<0)
64+
{
65+
max=Math.max(max,count);
66+
count=0;
67+
zuokuohao=0;
68+
}
69+
}
70+
}
71+
return Math.max(max,count)*2;
72+
}
73+
}
74+
75+
76+
下面代码是比较标准的,只要扫一遍字符串即可。
77+
78+
//{java}
79+
public class Solution {
80+
public int longestValidParentheses(String s) {
81+
int stack[] = new int[s.length()];
82+
int top=0,last=-1;
83+
int max=0;
84+
int flag[] = new int[s.length()];
85+
for(int i=0;i<s.length();i++)
86+
{
87+
char c = s.charAt(i);
88+
if(c=='(')
89+
stack[top++]=i;
90+
else
91+
{
92+
if(top>0)
93+
{
94+
int j= stack[--top];
95+
if(top==0)
96+
max=Math.max(max,i-last);
97+
else
98+
//栈中最近那个左括号
99+
max=Math.max(max,i-stack[top-1]);
100+
}else
101+
{
102+
//发生了一次不匹配
103+
last=i;
104+
}
105+
}
106+
}
107+
return max;
108+
}
109+
}
110+
111+
112+
Discuss里面还有一个clever的boy给出一段很机智的代码
113+
114+
//{cpp}
115+
class LVP{
116+
117+
public:
118+
int lvp( string s ){
119+
int len = s.size();
120+
if( !len ) // if string is empty, return 0 instantly.
121+
return 0;
122+
for( int i = 0;i < len;i++ )
123+
if( s[i] == ')' )
124+
for( int j = i - 1;j >= 0;j-- ) // for every ')’,find the first match '(' before it
125+
if( s[j] == '(' ) { // and mark the two with a special flag,like '0'
126+
s[i] = '0';
127+
s[j] = '0';
128+
break;
129+
}
130+
int max = 0,temp = 0;
131+
for( int i = 0;i < len;i++ ) { // the problem changed to find the longest '0' in the string
132+
if( s[i] == '0' )
133+
temp++;
134+
else {
135+
max = temp > max ? temp : max;
136+
temp = 0;
137+
}
138+
}
139+
max = temp > max ? temp : max; // longest '0'sequence may still stored in temp
140+
return max;
141+
}
142+
};
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
layout: post
3+
category : LeetCode
4+
tagline: "利用map,特殊要求的字符串比较"
5+
tags : [LeetCode,面试,map,字符串比较]
6+
---
7+
{% include JB/setup %}
8+
9+
<h4>{{page.title|remove_first:'Leetcode之'}}</h4>
10+
11+
12+
>Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
13+
>
14+
>If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
15+
>
16+
>The replacement must be in-place, do not allocate extra memory.
17+
>
18+
>Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
19+
`1,2,3 → 1,3,2`
20+
`3,2,1 → 1,2,3`
21+
`1,1,5 → 1,5,1`
22+
23+
解法:不好干啊,推了半天,终于一次性AC。数组的后方`i`开始搜索(后方是一个降序数组),在`i`的后方找到比`num[i]`大的最小的数(在降序数组中找大于`num[i]`的最小的数,可以二分找更快),交换。交换后`i`以后元素仍然是一个降序的排列。然后转置该部分元素。
24+
25+
代码如下:
26+
27+
//{java}
28+
public class Solution {
29+
public void nextPermutation(int[] num) {
30+
int i,j;
31+
waiceng:
32+
for(i=num.length-2;i>=0;i--)
33+
{
34+
//这里直接从数组最小的元素开始找,二分找应该更快
35+
for(j=num.length-1;j>i;j--)
36+
{
37+
if(num[i]<num[j])
38+
{
39+
int t = num[i];
40+
num[i]=num[j];
41+
num[j]=t;
42+
break waiceng;
43+
}
44+
}
45+
}
46+
int s = i+1;int e=num.length-1;
47+
while(s<e)
48+
{
49+
int t = num[s];
50+
num[s]=num[e];
51+
num[e]=t;
52+
s++;e--;
53+
}
54+
}
55+
}
56+
57+
58+
//搜索元素部分替换成二分
59+
60+
//{java}
61+
public class Solution {
62+
public void nextPermutation(int[] num) {
63+
int i,j;
64+
waiceng:
65+
for(i=num.length-2;i>=0;i--)
66+
{
67+
int s=i+1,e=num.length-1;
68+
while(s<=e-1)
69+
{
70+
int t =(s+e+1)/2;
71+
if(num[t]>num[i])
72+
s=t;
73+
else
74+
e=t-1;
75+
}
76+
if(num[e]>num[i])
77+
{
78+
int t = num[i];
79+
num[i]=num[e];
80+
num[e]=t;
81+
break;
82+
}
83+
}
84+
85+
int s = i+1;int e=num.length-1;
86+
while(s<e)
87+
{
88+
int t = num[s];
89+
num[s]=num[e];
90+
num[e]=t;
91+
s++;e--;
92+
}
93+
}
94+
}
Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,64 @@
11
---
22
layout: post
33
category : LeetCode
4-
tagline: "调换链表每对节点的位置"
5-
tags : [LeetCode,面试,链表,,递归,顺序调换]
4+
tagline: "利用map,特殊要求的字符串比较"
5+
tags : [LeetCode,面试,map,字符串比较]
66
---
77
{% include JB/setup %}
88

99
<h4>{{page.title|remove_first:'Leetcode之'}}</h4>
1010

11-
>Given a linked list, swap every two adjacent nodes and return its head.
11+
12+
> You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.
13+
>
14+
>For example, given:
1215
>
13-
>For example,
14-
Given `1->2->3->4`, you should return the list as `2->1->4->3`.
16+
>S: `"barfoothefoobarman"`
17+
>L: `["foo", "bar"]`
1518
>
16-
>Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
19+
>You should return the indices: `[0,9]`.
20+
>(order does not matter).
1721
18-
解法:链表题目首先想到的就是递归,这题就是简单的递归,突然想起来了,转置链表,用递归就能很快实现。
22+
解法:这题没想出来,一直陷入KMP中无法自拔,主要不好解决的地方在于`L`中的字符串可以是重复的,否则可以直接用标记出来。
23+
24+
这题的题解使用`HashMap`处理的。遍历`S`的可能满足条件部分,因为L中字符串长度确定,用`cur`记录单词出现的次数,并和标准的`map`做比较。
1925

2026
代码如下:
2127

2228
//{java}
23-
/**
24-
* Definition for singly-linked list.
25-
* public class ListNode {
26-
* int val;
27-
* ListNode next;
28-
* ListNode(int x) {
29-
* val = x;
30-
* next = null;
31-
* }
32-
* }
33-
*/
3429
public class Solution {
35-
public ListNode swapPairs(ListNode head) {
36-
if(head==null||head.next==null)
37-
return head;
38-
ListNode q = head;
39-
ListNode p = head.next;
40-
ListNode k = swapPairs(p.next);
41-
q.next=k;
42-
p.next=q;
43-
return p;
30+
public List<Integer> findSubstring(String S, String[] L) {
31+
HashMap<String,Integer> map = new HashMap<String,Integer>();
32+
List<Integer> result = new ArrayList<Integer>();
33+
for(String l:L)
34+
{
35+
if(map.get(l)==null)
36+
map.put(l,1);
37+
else
38+
map.put(l,map.get(l)+1);
39+
}
40+
int ll = L[0].length();
41+
for(int i=0;i<S.length()-ll*L.length+1;i++)
42+
{
43+
int j=0;
44+
HashMap<String,Integer> cur = new HashMap<String,Integer>();
45+
for(j=0;j<L.length;j++)
46+
{
47+
String l = S.substring(j*ll+i,j*ll+i+ll);
48+
if(cur.get(l)==null)
49+
cur.put(l,1);
50+
else
51+
cur.put(l,cur.get(l)+1);
52+
if(map.get(l)==null||cur.get(l)>map.get(l))
53+
break;
54+
}
55+
if(j==L.length)
56+
result.add(i);
57+
}
58+
return result;
59+
}
60+
public static void main(String a[])
61+
{
62+
System.out.println(new Solution().findSubstring("barfoothefoobarman",new String[]{"foo","bar"}));
4463
}
4564
}

0 commit comments

Comments
 (0)