Find the Missing Number II
Giving a string with number from 1-n in random order, but miss 1 number.Find that number.
Notice n <= 30
Given n = 20, str = 19201234567891011121314151618
return 17
这题是网上借鉴别人的代码 一开始没有想到要用DFS 利用这道题顺便复习了下DFS的schema 注意flag 是用来剪枝避免重复计算的
public class Solution {
/**
* @param n an integer
* @param str a string with number from 1-n
* in random order and miss one number
* @return an integer
*/
int ans=0;
boolean flag =false;
public int findMissing2(int n, String str) {
// Write your code here
boolean[] appear = new boolean[n+1];
dfs(0, n, str, appear);
return ans;
}
private void dfs(int i, int n, String s, boolean[] appear){
if(i>=s.length()||flag){
if(!flag){
for(int k=1; k<=n;k++){
if(!appear[k]){
ans = k;
}
}
}
flag = true;
return;
}
int sum = s.charAt(i)-'0';
if(sum ==0){
return;
}
int j = i+1;
while(sum<=n){
if(!appear[sum]){
appear[sum] = true;
dfs(j, n, s, appear);
appear[sum] = false;
}
if(j>=s.length()) return;
sum = 10*sum + (s.charAt(j++)-'0');
}
}
}
Find the Missing Number II的更多相关文章
- [LeetCode] Missing Number 丢失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- Single Number,Single Number II
Single Number Total Accepted: 103745 Total Submissions: 218647 Difficulty: Medium Given an array of ...
- [LeetCode] 268. Missing Number ☆(丢失的数字)
转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...
- Leetcode-268 Missing Number
#268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...
- 【leetcode】Single Number && Single Number II(ORZ 位运算)
题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...
- Missing number
Missing number 题目: Description There is a permutation without two numbers in it, and now you know wh ...
- 【LeetCode】268. Missing Number
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...
- 【LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- 【题解】【位操作】【Leetcode】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
随机推荐
- HeadFirst Ruby 第十五章总结 Saving and loading data
前言 在上一章讲述了如何进行基础的操作,比如 处理 GET 请求的 get route, 再比如下载 gem 等等方面的知识.在这一章节,作者告诉我们如何储存.处理数据.整个过程分三步走: 首先,当 ...
- LeetCode--371--两整数之和
问题描述: 方法: class Solution(object): def getSum(self, a, b): """ :type a: int :type b: i ...
- linux 下如何安装memcached 和启动服务
一.安装gcc # yum -y install gcc 二.安装libevent # wget http://www.monkey.org/~provos/libevent-2.0.12-stabl ...
- New task CodeForces - 788E (线段树优化dp)
比较套路的一个题, 对每个数维护一颗线段树来转移就好了. #include <iostream> #include <algorithm> #include <cstdi ...
- Merge K Sorted List(含Merge Two Sorted LIst) leetcode java
问题描述: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complex ...
- 2017-2018 ACM-ICPC, NEERC, Northern Subregional ContestG - Grand Test
题意:找三条同起点同终点的不相交的路径 题解:用tarjan的思想,记录两个low表示最小和次小的dfs序,以及最小和次小的位置,如果次小的dfs序比dfn小,那么说明有两条返祖边,那么就是满足条件的 ...
- vivadio关联notepad++的关键式
D:\Program Files\Notepad++\notepad++.exe +[line number] [file name]
- CCF-CSP 201312-5 I'm stuck !
I'm stuck 试题编号: 201312-5 试题名称: I’m stuck! 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给定一个R行C列的地图,地图的每一个方格可能 ...
- 【LeetCode】矩阵操作
1. 矩阵旋转 将 n × n 矩阵顺时针旋转 90°. 我的思路是 “ 从外到内一层一层旋转 ”. 一个 n × n 矩阵有 (n + 1) / 2 层,每层有 4 部分,将这 4 部分旋转. 顺时 ...
- [洛谷 P3239] [HNOI2015]亚瑟王
[HNOI2015]亚瑟王 题目描述 小 K 不慎被 LL 邪教洗脑了,洗脑程度深到他甚至想要从亚瑟王邪教中脱坑.他决定,在脱坑之前,最后再来打一盘亚瑟王.既然是最后一战,就一定要打得漂亮.众所周知, ...