Giving a string with number from 1-n in random order, but miss 1 number.Find that number.

Notice n <= 30
Example

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的更多相关文章

  1. [LeetCode] Missing Number 丢失的数字

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  2. Single Number,Single Number II

    Single Number Total Accepted: 103745 Total Submissions: 218647 Difficulty: Medium Given an array of ...

  3. [LeetCode] 268. Missing Number ☆(丢失的数字)

    转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...

  4. Leetcode-268 Missing Number

    #268.  Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...

  5. 【leetcode】Single Number && Single Number II(ORZ 位运算)

    题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...

  6. Missing number

    Missing number 题目: Description There is a permutation without two numbers in it, and now you know wh ...

  7. 【LeetCode】268. Missing Number

    Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...

  8. 【LeetCode】264. Ugly Number II

    Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...

  9. 【题解】【位操作】【Leetcode】Single Number II

    Given an array of integers, every element appears three times except for one. Find that single one. ...

随机推荐

  1. ggplot2画图

    早在N年前就听说这个包画图不错,一直没机会用,终于等到了.相比前面trendline这个包的可视化功能强大得多. ggplot2需要使用dataframe,其实就是一个N维数组, install.pa ...

  2. <property name="hibernate.hbm2ddl.auto">update</property> 问题

    其实这个hibernate.hbm2ddl.auto参数的作用主要用于:自动创建|更新|验证数据库表结构.如果不是此方面的需求建议set value="none".create:每 ...

  3. OnSen UI结合AngularJs打造”美团"APP"我的”页面 --Hybrid App

    1.页面效果图: 演示地址:http://www.nxl123.cn/bokeyuan/meiTuanDemo_mine/ 2.核心代码 mine.html: <ons-page id=&quo ...

  4. c++的const总结

    转自:http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777416.html 为什么使用const?采用符号常量写出的代码更容易维护:指 ...

  5. 内核开启VF小结

    2017-8-29 16:33:40 内核开启VF小结: 1. eth2上创建4个VFecho 4 > /sys/class/net/eth2/device/sriov_numvfs2. 关闭e ...

  6. 2.4 UML类图

    类图定义 类class的定义 具有相同属性.操作.方法.关系或者行为的一组对象的描述符 类是真实世界事物的抽象 问题领域的类:在对系统建模时,将会涉及到如何识别业务系统中的事物,这些事物构 成了整个业 ...

  7. pycharm安装步骤

    python环境配置教程 https://jingyan.baidu.com/article/c45ad29c05c208051653e270.html 由于安装Pycharm时忘记截图了,所以详细安 ...

  8. canal入门Demo

    关于canal具体的原理,以及应用场景,可以参考开发文档:https://github.com/alibaba/canal 下面给出canal的入门Demo (一)部署canal服务器 可以参考官方文 ...

  9. Matlab-4:追赶法(crout分解)工具箱

    function x=chase (a,b,c,f) % the method of chaase******************************* % a, b, c,分别是是方程组的下 ...

  10. poj-1026-置换

    Cipher Bob and Alice started to use a brand-new encoding scheme. Surprisingly it is not a Public Key ...