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. 关于nodejs访问mysql的思考

    nodejs要访问mysql数据库,首先必须要安装包mysql,命令:npm install mysql.安装成功后的访问数据库代码如下: var mysql = require('mysql'); ...

  2. CURL操作

    具体代码如下: <?php$curl=curl_init(); //初始化$url='http://www.ecshop.com';//curl_setopt(curl资源,选项标志,选项值)c ...

  3. 分享:selenium(一) xpath

    xpath无所不能定位.   https://www.w3.org/TR/xpath/all/#axes 两个神器:firebug.xpath-checker 举例:混合定位 //td[a//fron ...

  4. Getting started with Processing 第十三章——延伸(2)

    与 Arduino 联动 在 Processing 中,可以通过:import processing.serial.* Serial port; //声明串口对象port = new Serial(t ...

  5. Getting Started with Processing 第五章的easing问题(2)

    程序代码清单如下: float x; float y; float px; float py; float easing = 0.05; void setup(){ size(480,120); st ...

  6. spring cloud: Hystrix(三):健康指数 health Indicator

    spring cloud: Hystrix(三):健康指数 health Indicator ribbon+hystrix 当使用Hystrix时(spring-cloud-starter-hystr ...

  7. java ----> 手动编译java项目

    环境: jdk1.8,cmd,notepad++ 创建java工程test,创建文件夹: src classes lib 说明: src 放置.java文件 classes 放置.class文件 li ...

  8. Hadoop – The Definitive Guide Examples,,IntelliJ

    IntelliJ Project for Building Hadoop – The Definitive Guide Examples http://vichargrave.com/intellij ...

  9. 函数和函数模版在一个。cpp中的情况!(除了左移和右移,其他的不要用友元函数!!!)

    // 友元函数和运算符重载的碰撞.cpp : 定义控制台应用程序的入口点. // #include <iostream> using namespace std; template < ...

  10. csu oj 1343 Long Long

    Description 现在有两个单调递增序列,第一个序列有N个整数,第二个序列有M个整数,现在你可以从第一个序列中选一个数x,然后从第二个序列中选一个数y,那么有多少种情况满足x+y<=K呢? ...