|
1 | 1 | --- |
2 | 2 | layout: post |
3 | 3 | category : LeetCode |
4 | | -tagline: "调换链表每对节点的位置" |
5 | | -tags : [LeetCode,面试,链表,,递归,顺序调换] |
| 4 | +tagline: "利用map,特殊要求的字符串比较" |
| 5 | +tags : [LeetCode,面试,map,字符串比较] |
6 | 6 | --- |
7 | 7 | {% include JB/setup %} |
8 | 8 |
|
9 | 9 | <h4>{{page.title|remove_first:'Leetcode之'}}</h4> |
10 | 10 |
|
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: |
12 | 15 | > |
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"]` |
15 | 18 | > |
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). |
17 | 21 |
|
18 | | -解法:链表题目首先想到的就是递归,这题就是简单的递归,突然想起来了,转置链表,用递归就能很快实现。 |
| 22 | +解法:这题没想出来,一直陷入KMP中无法自拔,主要不好解决的地方在于`L`中的字符串可以是重复的,否则可以直接用标记出来。 |
| 23 | + |
| 24 | +这题的题解使用`HashMap`处理的。遍历`S`的可能满足条件部分,因为L中字符串长度确定,用`cur`记录单词出现的次数,并和标准的`map`做比较。 |
19 | 25 |
|
20 | 26 | 代码如下: |
21 | 27 |
|
22 | 28 | //{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 | | - */ |
34 | 29 | 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"})); |
44 | 63 | } |
45 | 64 | } |
0 commit comments