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. ...
随机推荐
- Superclass和Constructor Chaining
A subclass inherits accessible date fields and methods from its superclass. Does it inherit construc ...
- Apollo快速安装视频教程
参见视频 https://www.csdn.net/article/a/2018-05-14/15948340
- 雷林鹏分享:XML 总结 下一步学习什么呢?
XML 总结 下一步学习什么呢? XML 总结 XML 可用于交换.共享和存储数据. XML 文档形成 树状结构,在"根"和"叶子"的分支机构开始的. XML ...
- spring boot ----> jpa连接和操作mysql数据库
环境: centos6.8,jdk1.8.0_172,maven3.5.4,vim,spring boot 1.5.13,mysql-5.7.23 1.引入jpa起步依赖和mysql驱动jar包 &l ...
- English trip EM2-LP-3A Gifts Teacher:Patrick
课上内容(Lesson) 词汇(Key Word ) Identify vt. 确定:识别:使参与:把…看成一样 objects n. 物体(object的复数):目标 # UFO = ...
- 分享WCF文件传输---WCFFileTransfer
前几天分享了分享了WCF聊天程序--WCFChat , 本文和大家一起分享利用WCF实现文件的传输.程序运行效果:接收文件端:发送文件端:连接WCF服务,选择要传输的文件文件传输成功:我们会在保存文件 ...
- 使用jquery.mobile和WebSQL实现记事本功能
1.记事本列表页 1.1.页面结构与样式: <div data-role="page" id="home"> <div data-role=& ...
- android--------自定义控件 之 ViewGroup
前面几篇讲了自定义控件的组合控件,地址:http://www.cnblogs.com/zhangqie/p/8985612.html 今天这篇博文主要来说说 自定义控件的 ViewGroup. 什么是 ...
- 电脑用U盘启动
除了根据提示按DEL或者F2进入到BIOS界面更改设置之外. 还可以在开机时按F8或F12进入到引导界面,可直接选择USB. 当把登录用户登录,其他用户都被禁用时,电脑登不进去.要制作启动U盘,进入到 ...
- lanmp环境中创建个软连接
进入default,创建关于框架的链接 然后编辑配置文件 这样就可以在线上访问了.