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 ...
随机推荐
- ContestHunter暑假欢乐赛 SRM 02
惨不忍睹 3个小时都干了些什么... 日常按顺序从A题开始(难度居然又不是递增的 第一眼A题就觉得很简单...写到一半才发现woc那是个环.感觉一下子复杂了,按照链的方法扩展的话要特判很多东西... ...
- [APIO2017]商旅
link 这题卡我精度,调了一晚上才调对,因为没有想到图还可以不连通 其实可以预处理出好多东西,距离($dis(u,v)$),买卖物品(从$u$到$v$买卖物品的最大利润,例($max{S_{u,i} ...
- Change the IPTables log file
http://www.networkinghowtos.com/howto/change-the-iptables-log-file/ An important aspect of any f ...
- postgresql pgagent 的安装及使用
pgagent 作为postgresql的一个任务调度代理,在postgresql 9.0 以前 是附带在pgadmin 包下面的,只是默认不安装,9.0之后作为了一个单独是的安装包.所以要使用pga ...
- snmp实用篇
简单网络管理协议(SNMP)是 TCP/IP协议簇的一个应用层协议.在1988年被制定,并被Internet体系结构委员会(IAB)采纳作为一个短期的网络管理解决方案:由于 SNMP的简单性,在Int ...
- 阐述ArrayList、Vector、LinkedList的存储性能和特性?
ArrayList 和Vector他们底层的实现都是一样的,都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组元素移动等内 ...
- 会话技术: Cookie 和 Session
会话技术 会话技术:从浏览器开始访问服务器,到关闭浏览器,这期间发生了许多次请求和响应,这个过程就叫做一次会话. Cookie 和 Session 都是处理会话技术的两种具体实现,Cookie将数据保 ...
- 使用Docker搭建Django,Nginx,R,Python部署环境
转载自https://blog.csdn.net/The_One_is_all/article/details/76063968 基本环境: Ubuntu 16.10 docker 17.06.0-c ...
- Windows、Linux及Mac查看端口和杀死进程
本文介绍如何在Windows.Linux及Mac下查看端口和杀死进程. Windows下查看端口和杀死进程 查看占用端口号的进程号:netstat –ano | findstr "指定端口号 ...
- 【BZOJ1072】【SCOI2007】排列 [状压DP]
排列 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 给一个数字串s和正整数d, 统计s有多 ...