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. ...
随机推荐
- nodejs模拟http发送请求
首先需要安装模块request,然后代码如下: //模拟发送http请求 var request = require("request"); //get请求 request('ht ...
- python float转为decimal
73.2413793103 ======= 73.2414 <type 'float'> ======= <class 'decimal.Decimal'> 当断言这两个值相等 ...
- spring boot: 支持jsp,支持freemarker
spring boot: 支持jsp,支持freemarker 支持jsp: 加入依赖 <!--jsp--> <dependency> <groupId>org.a ...
- 雷林鹏分享:XML - E4X
XML - E4X E4X 向 JavaScript 添加了对 XML 的直接支持. E4X 实例 var employees= Tove 32 Jani 26 ; document.write(em ...
- 利用Vistual Studio自带的xsd.exe工具,根据XML自动生成XSD
利用Vistual Studio自带的xsd.exe工具,根据XML自动生成XSD 1, 命令提示符-->找到vs自带的xsd.exe工具所在的文件夹 例如: C:\Program Files ...
- 自己动手编写IPv4地址包含关系测试的小脚本
工作中需要对地址包含关系进行测试,现有ipaddress标准库和IPy无法满足,于是自己动手编写小脚本,主要实现== , in, <等专用功能,其他功能可以后续用到再补充,例如迭代打印网段内所有 ...
- uva11609
以三个人组队为例,3组合是:C(3,0)=1,3,3,1.还有队长的选择.有 1*0,3*1,3*2,1*3种. 组合数: 1 3 3 1 ...
- android -------- 打开本地浏览器或指定浏览器加载,打电话,打开第三方app
开发中常常有打开本地浏览器加载url或者指定浏览器加载, 还有打开第三方app, 如 打开高德地图 百度地图等 在Android程序中我们可以通过发送隐式Intent来启动系统默认的浏览器. 如果手机 ...
- php二分法查找
//二分查找(数组里查找某个元素) function bin_sch($array, $low, $high, $k) { if ($low <= $high) { $mid = intval( ...
- Composer 的学习
一.Composer简介 Composer 是PHP用来管理依赖关系的工具. 使用 composer 的必要前提有: 1.PHP版本要高于PHP5.3.2 2.PHP支持OpenSSL扩展 3.安装有 ...