LeetCode解题报告—— Word Search & Subsets II & Decode Ways
1. Word Search
Given a 2D board and a word, find if the word exists in the grid.
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.
For example,
Given board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
word = "ABCCED"
, -> returns true
,
word = "SEE"
, -> returns true
,
word = "ABCB"
, -> returns false
.
思路:其实我感觉LeetCode上中等难度的题目大多数都可以用递归和dp来解决。遍历矩阵,如果遇到的这个字符和要匹配的单词中的第一个字符相等,那么接下来是看这个字符的上下左右(不越界的情况)是不是和单词中的第二个字符相等的,以此类推,实际上是在不断循环的调用单个字符是否匹配这个方法,如果匹配则往下层递归,不匹配返回上层,当递归层数等于word字符数时,表明找到完全匹配项了,返回true。剩下一个要注意的点是,避免字符重用,不能出现往上走又往下走这种递归情况。
class Solution {
public boolean exist(char[][] board, String word) {
char[] w = word.toCharArray();
for (int y=0; y<board.length; y++) {
for (int x=0; x<board[y].length; x++) {
if (exist(board, y, x, w, 0)) return true;
}
}
return false;
} private boolean exist(char[][] board, int y, int x, char[] word, int i) {
if (i == word.length) return true;
if (y<0 || x<0 || y == board.length || x == board[y].length) return false;
if (board[y][x] != word[i]) return false;
board[y][x] = "*"; // 将当前位置设置未一个非字母字符以免后续遍历时再回到此位置时出现重复利用字符的情况
boolean exist = exist(board, y, x+1, word, i+1)
|| exist(board, y, x-1, word, i+1)
|| exist(board, y+1, x, word, i+1)
|| exist(board, y-1, x, word, i+1);
board[y][x] = word[i]; // 遍历结束后需置回原来字符
return exist;
}
}
2. Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,2]
, a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
思路:Subset题目的改进版,原数组中的元素可以重复,所以难点是怎么处理数组中重复的元素,对于之前数组元素不重复的Subset,解决方法是先排序一遍,从长度0到数组长度n循环递归,加入一个值,再对其后面的数组部分以同样的方法递归。对于本题目,可以确定重复的子集发生的情形是子集长度一样的情况,所以需要再从长度0到数组长度n循环递归中的循环中加一些代码来防止重复。
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
List<Integer> each = new ArrayList<>();
helper(res, each, 0, nums);
return res;
}
public void helper(List<List<Integer>> res, List<Integer> each, int pos, int[] n) {
if (pos <= n.length) {
res.add(each);
}
int i = pos;
while (i < n.length) {
each.add(n[i]);
helper(res, new ArrayList<>(each), i + 1, n);
each.remove(each.size() - 1);
i++;
while (i < n.length && n[i] == n[i - 1]) {i++;} // 避免重复
}
return;
}
The Basic idea is: use “while (i < n.length && n[i] == n[i - 1]) {i++;}” to avoid the duplicate. For example, the input is 2 2 2 3 4. Consider the helper function. The process is:
- each.add(n[i]); --> add first 2 (index 0)
- helper(res, new ArrayList<>(each), i + 1, n); --> go to recursion part, list each is <2 (index 0)>
- while (i < n.length && n[i] == n[i - 1]) {i++;} --> after this, i == 3, add the element as in subset I
3. Decode Ways
A message containing letters from A-Z
is being encoded to numbers using the following mapping:
'A' -> 1
'B' -> 2
...
'Z' -> 26
Given an encoded message containing digits, determine the total number of ways to decode it.
For example,
Given encoded message "12"
, it could be decoded as "AB"
(1 2) or "L"
(12). The number of ways decoding "12"
is 2.
思路:咋看一下,可以用穷举来解决,那么应该可以使用dp的思路来解决这道题目。确定了是用dp之后,需要确定的数组dp[i]的索引和值的意义,以及是自底向上还是自顶向下。dp[i]代表的是给定的字符串message长度为i时总共有多少种decode方法,这里有一点要注意的是message中可能存在的0。对于这题的dp思路,从左向右,也就是从低向上,先来赋初值。dp[0]=1代表message是空时,encode way只有一种,对于dp[1],即message中的第一个值,如果是0的话decode way的个数是0,否则为1。接下来怎么dp才是难点,对于message中的位置i, 如果这个位置上的数字是1到9的那么这个数字和dp[i-1]是一种组合,如果这个数字和前一个构成的数在10到26之间,那么这两个数字和dp[i-2]又能构成一种decode way。
import java.util.*; public class LeetCode{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String message=sc.nextLine();
System.out.println(numDecodings(message));
} static int numDecodings(String s){
if(s==null || s.length()==0) return 0;
int n=s.length();
int[] dp=new int[n+1];
dp[0]=1;
dp[1]=s.charAt(0)!='0'? 1:0;
for(int i=2; i<=n; i++){
int first=Integer.parseInt(s.substring(i-1,i));
int second=Integer.parseInt(s.substring(i-2,i));
if(first>=1&&first<=9){
dp[i]=dp[i-1];
}
if(second>=10&&second<=26){
dp[i]+=dp[i-2]; // 要将两种情况计和
}
}
return dp[n];
}
}
LeetCode解题报告—— Word Search & Subsets II & Decode Ways的更多相关文章
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
- leetcode 解题报告 Word Ladder II
题目不多说了.见https://oj.leetcode.com/problems/word-ladder-ii/ 这一题我反复修改了两天半.尝试过各种思路,总是报TLE.终于知道这一题为什么是leet ...
- LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal
1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...
- leetcode 解题报告 Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal
1. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that ...
- 【leetcode】212. Word Search II
Given an m x n board of characters and a list of strings words, return all words on the board. Each ...
随机推荐
- HDU 5651 逆元
xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- idea 创建多模块时模块类无法引入
我的原因是类的位置方的不对,由于刚搭建的项目,本来只想做个测试,就直接在java下创建类,然而这居然是个深坑,模块引入了也无法引入这个模块的类. 解决方法:创建com.***.***包后的类可以正常引 ...
- LightOJ 1375 - LCM Extreme 莫比乌斯反演或欧拉扩展
题意:给出n [1,3*1e6] 求 并模2^64. 思路:先手写出算式 观察发现可以化成 那么关键在于如何求得i为1~n的lcm(i,n)之和.可以知道lcm(a,b)为ab/gcd(a,b) 变换 ...
- Tomcat 7下如何利用 catalina.properties 部署公用类
Tomcat 有很多配置文件,其中一个是 catalina.properties ,本文介绍catalina.properties 中的设置项. 一.组成 catalina.properties ...
- bzoj 1036: [ZJOI2008]树的统计Count——树链剖分
Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. Q ...
- [BZOJ2440]完全平方数解题报告|莫比乌斯函数的应用
完全平方数 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平方数.他觉得这些数看起来很令人难受.由此,他也讨厌所有是完全平方数的正整数倍的数.然而这丝毫不影响他对其他数的热爱. 这天是小X的生日 ...
- 【TYVJ】1520 树的直径
[算法]树的直径 memset(a,0,sizeof(a)) #include<cstdio> #include<algorithm> #include<cstring& ...
- How to write educational schema.
Sometimes, writing such educational schemas could be of much use, and creating such docs can be bene ...
- 23、redis如何实现主从复制?以及数据同步机制?
redis主从复制 和Mysql主从复制的原因一样,Redis虽然读取写入的速度都特别快,但是也会产生读压力特别大的情况.为了分担读压力,Redis支持主从复制,Redis的主从结构可以采用一主多从或 ...
- jQuery mobile 滑动打开面板
一.首先在<head></head>里面引入jQuery库.jQuery mobile库以及jQuery mobile样式 <link rel="stylesh ...