- 浏览: 139847 次
-
文章分类
- 全部博客 (189)
- Tree (14)
- Dynamic Programming (34)
- Array (20)
- Search (1)
- Hash (12)
- Backtracking (22)
- Divide and Conque (8)
- Greedy (6)
- Stack (12)
- software (0)
- List (7)
- Math (22)
- Two pointers (16)
- String (20)
- Linux (1)
- Sliding Window (4)
- Finite State Machine (1)
- Breadth-first Search (7)
- Graph (4)
- DFS (6)
- BFS (3)
- Sort (9)
- 基础概念 (2)
- 沟通表达 (0)
- Heap (2)
- Binary Search (15)
- 小结 (1)
- Bit Manipulation (8)
- Union Find (4)
- Topological Sort (1)
- PriorityQueue (1)
- Design Pattern (1)
- Design (1)
- Iterator (1)
- Queue (1)
最新评论
-
likesky3:
看了数据结构书得知并不是迭代和递归的区别,yb君的写法的效果是 ...
Leetcode - Graph Valid Tree -
likesky3:
迭代和递归的区别吧~
Leetcode - Graph Valid Tree -
qb_2008:
还有一种find写法:int find(int p) { i ...
Leetcode - Graph Valid Tree -
qb_2008:
要看懂这些技巧的代码确实比较困难。我是这么看懂的:1. 明白这 ...
Leetcode - Single Num II -
qb_2008:
public int singleNumber2(int[] ...
Leetcode - Single Num II
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high.
For example,
Given low = "50", high = "100", return 3. Because 69, 88, and 96 are three strobogrammatic numbers.
Note:
Because the range might be a large number, the low and high numbers are represented as string.
[分析]
思路1:比较容易想到的思路,利用题II的思路构造出low.length 和high.length之间的所有strobogrammatic number, 然后计数在low-high范围内的那些数字个数。
思路2:参考https://leetcode.com/discuss/50624/clean-and-easy-understanding-java-solution 在构造过程中判断并计数,且构造方式是从两边往中间,而思路1是从中间忘两边。在leetcode的运行时间优于思路1
思路3:受思路2启发,改造思路1,也在构造过程中判断,修改过程各种bug调试好久,而且运行时间远高于思路1和思路2,无言~
Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high.
For example,
Given low = "50", high = "100", return 3. Because 69, 88, and 96 are three strobogrammatic numbers.
Note:
Because the range might be a large number, the low and high numbers are represented as string.
[分析]
思路1:比较容易想到的思路,利用题II的思路构造出low.length 和high.length之间的所有strobogrammatic number, 然后计数在low-high范围内的那些数字个数。
思路2:参考https://leetcode.com/discuss/50624/clean-and-easy-understanding-java-solution 在构造过程中判断并计数,且构造方式是从两边往中间,而思路1是从中间忘两边。在leetcode的运行时间优于思路1
思路3:受思路2启发,改造思路1,也在构造过程中判断,修改过程各种bug调试好久,而且运行时间远高于思路1和思路2,无言~
public class Solution { // Method 2: ref Map<Character, Character> map = new HashMap<>(); { map.put('1', '1'); map.put('8', '8'); map.put('6', '9'); map.put('9', '6'); map.put('0', '0'); } String low = "", high = ""; public int strobogrammaticInRange(String low, String high) { this.low = low; this.high = high; int result = 0; for(int n = low.length(); n <= high.length(); n++){ int[] count = new int[1]; strobogrammaticInRange(new char[n], count, 0, n-1); result += count[0]; } return result; } private void strobogrammaticInRange(char[] arr, int[] count, int lo, int hi){ if(lo > hi){ String s = new String(arr); if((arr[0] != '0' || arr.length == 1) && compare(low, s) && compare(s, high)){ count[0]++; } return; } for(Character c: map.keySet()){ arr[lo] = c; arr[hi] = map.get(c); if((lo == hi && c == map.get(c)) || lo < hi) strobogrammaticInRange(arr, count, lo+1, hi-1); } } private boolean compare(String a, String b){ if(a.length() != b.length()) return a.length() < b.length(); int i = 0; while(i < a.length() &&a.charAt(i) == b.charAt(i)) i++; return i == a.length() ? true: a.charAt(i) <= b.charAt(i); } // Method 1 public int strobogrammaticInRange1(String low, String high) { int count = 0; int minLen = low.length(), maxLen = high.length(); for (int n = minLen; n <= maxLen; n++) { List<String> candidates = recur(n, n); if (n == minLen || n == maxLen) { for (String cand : candidates) { if (isLess(low, cand) && isLess(cand, high)) count++; } } else { count += candidates.size(); } } return count; } public List<String> recur(int k, int n) { List<String> result = new ArrayList<String>(); if (k <= 0) { result.add(""); return result; } if (k == 1) { result.add("0"); result.add("1"); result.add("8"); return result; } List<String> subResult = recur(k - 2, n); for (String substr : subResult) { if (k < n) result.add('0' + substr + '0'); result.add('1' + substr + '1'); result.add('8' + substr + '8'); result.add('6' + substr + '9'); result.add('9' + substr + '6'); } return result; } private boolean isLess(String a, String b) { if (a.length() != b.length()) return a.length() <= b.length() ? true : false; int i = 0; while (i < a.length() && a.charAt(i) == b.charAt(i)) i++; return i == a.length() ? true : a.charAt(i) <= b.charAt(i); } }
public class Solution { // Method 3 String low = null, high = null; public int strobogrammaticInRange(String low, String high) { this.low = low; this.high = high; int count = 0; int minLen = low.length(), maxLen = high.length(); for (int n = minLen; n <= maxLen; n++) { List<String> candidates = recur(n, n, minLen, maxLen); count += candidates.size(); } return count; } Map<Character, Character> map = new HashMap<>(); { map.put('1', '1'); map.put('8', '8'); map.put('6', '9'); map.put('9', '6'); } public List<String> recur(int k, int n, int minLen, int maxLen) { List<String> result = new ArrayList<String>(); if (k <= 0) { result.add(""); return result; } if (k == 1) { if (k < n || isLessOrEqual(this.low, "0") && isLessOrEqual("0", this.high)) result.add("0"); if (k < n || isLessOrEqual(this.low, "1") && isLessOrEqual("1", this.high)) result.add("1"); if (k < n || isLessOrEqual(this.low, "8") && isLessOrEqual("8", this.high)) result.add("8"); return result; } List<String> subResult = recur(k - 2, n, minLen, maxLen); for (String substr : subResult) { if (k < n) result.add('0' + substr + '0'); for (Character c : map.keySet()) { String cand = c + substr + map.get(c); if (k == n) { if ((minLen < n && n < maxLen) || isLessOrEqual(this.low, cand) && isLessOrEqual(cand, this.high)) result.add(cand); } else { result.add(cand); } } } return result; } private boolean isLessOrEqual(String a, String b) { if (a.length() != b.length()) return a.length() <= b.length() ? true : false; int i = 0; while (i < a.length() && a.charAt(i) == b.charAt(i)) i++; return i == a.length() ? true : a.charAt(i) <= b.charAt(i); } }
发表评论
-
Leetcode - Integer to English Words
2015-09-04 20:53 1113[分析] 这题通过率之所以非常低是因为有很多corner ca ... -
Leetcode - Basic Calculator II
2015-08-27 09:16 919mplement a basic calculator to ... -
Leetcode - Factorial Trailing Zeroes
2015-08-25 09:00 444[思路] 数乘积结果的后缀0,其实就是数结果中有多少个因子10 ... -
Leetcode - Ugly Number II
2015-08-24 22:54 1177[分析] 暴力的办法就是从1开始检查每个数是否是丑数,发现丑数 ... -
Leetcode - Excel Sheet Column Title
2015-08-24 10:24 650[分析] 十进制转26进制,需要注意的是26进制是以1为最小数 ... -
Leetcode - Max Points on a Line
2015-08-23 15:30 742[分析] 两条直线若包含一个公共点且斜率相同,则为同一条直线。 ... -
Leetcode - Fraction to Recurring Decimal
2015-08-23 10:05 482[分析] 处理int型整数运算时,为避免溢出,省事的做法就是内 ... -
Leetcode - Count Primes
2015-08-22 13:42 524[ref] https://en.wikipedia.org/ ... -
Leetcode - Strobogrammatic Number
2015-08-22 10:48 1104A strobogrammatic number is a n ... -
Leetcode - Add Binary
2015-08-21 09:28 484[分析] 从低位往高位逐位相加,就是这么一个简单的题却花了我一 ... -
Leetcode - Rotate Image
2015-08-19 19:51 508[分析] 自己的思路:从外到内一圈圈顺时针旋转90度,坐标映射 ... -
Missing Ranges
2015-08-19 09:48 526[分析] 此题若不考虑极大值极小值相关的corner case ... -
Leetcode - Bitwise AND of Number Range
2015-08-17 09:41 519Given a range [m, n] where 0 &l ... -
Leetcode - Pow(x, n)
2015-08-11 09:45 474[分析] 数值计算类型题目,二分法或者借助位运算,本题两种方法 ... -
Leetcode - Divide Two Integers
2015-08-11 09:00 458[分析] 不能使用乘、除、取模运算,直接的思路当然是一次减一 ... -
Leetcode - sqrt(x)
2015-08-10 21:40 825[分析] 这是一道数值计算的题目,Code Ganker中指出 ... -
Leetcode - Permutation Sequence
2015-08-01 17:19 523原题链接:https://leetcode.com/probl ... -
Leetcode - Next Permutation
2015-08-01 16:38 703原题链接:https://leetcode.com/probl ... -
Leetcode - Multiply String
2015-06-15 09:39 696Given two numbers represented a ... -
Leetcode - Calculator
2015-06-10 09:31 559[分析] 思路1:逆序遍历字符串,数字和右括号保存在一个堆栈s ...
相关推荐
leetcode 浇花力扣解决方案 简单的 #0001 - Two Sum #0007 - Reverse Integer #0009 - Palindrome Number #0035 - Search Insert Position #0058 - Length of Last Word #0066 - Plus One #0083 - Remove Duplicates...
260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [C++](./C++/single-number-iii.cpp) [Python](./Python/single-number-iii.py) | _O(n)_ | _O(1)_ | Medium || 268| [Missing ...
III [递归(尽管 dp 可能会更快),时间?]。 1186 一次删减的最大子数组总和【O(n), dp(memory:O(1)), 类似于maxium subarry】 53 最大子数组 [O(n), dp(memory:O(1))] 523 连续子数组总和 [O(n^2), 1d dp(memory:O...
在日常的工作和学习中,你是否常常为处理复杂的数据、生成高质量的文本或者进行精准的图像识别而烦恼?DeepSeek 或许就是你一直在寻找的解决方案!它以其高效、智能的特点,在各个行业都展现出了巨大的应用价值。然而,想要充分发挥 DeepSeek 的优势,掌握从入门到精通的知识和技能至关重要。本文将从实际应用的角度出发,为你详细介绍 DeepSeek 的基本原理、操作方法以及高级技巧。通过系统的学习,你将能够轻松地运用 DeepSeek 解决实际问题,提升工作效率和质量,让自己在职场和学术领域脱颖而出。现在,就让我们一起开启这场实用又高效的学习之旅吧!
前端分析-2023071100789
基于kinect的3D人体建模C++完整代码.cpp
搞机工具箱10.1.0.7z
GRU+informer时间序列预测(Python完整源码和数据),python代码,pytorch架构,适合各种时间序列直接预测。 适合小白,注释清楚,都能看懂。功能如下: 代码基于数据集划分为训练集测试集。 1.多变量输入,单变量输出/可改多输出 2.多时间步预测,单时间步预测 3.评价指标:R方 RMSE MAE MAPE,对比图 4.数据从excel/csv文件中读取,直接替换即可。 5.结果保存到文本中,可以后续处理。 代码带数据,注释清晰,直接一键运行即可,适合新手小白。
在日常的工作和学习中,你是否常常为处理复杂的数据、生成高质量的文本或者进行精准的图像识别而烦恼?DeepSeek 或许就是你一直在寻找的解决方案!它以其高效、智能的特点,在各个行业都展现出了巨大的应用价值。然而,想要充分发挥 DeepSeek 的优势,掌握从入门到精通的知识和技能至关重要。本文将从实际应用的角度出发,为你详细介绍 DeepSeek 的基本原理、操作方法以及高级技巧。通过系统的学习,你将能够轻松地运用 DeepSeek 解决实际问题,提升工作效率和质量,让自己在职场和学术领域脱颖而出。现在,就让我们一起开启这场实用又高效的学习之旅吧!
基于ANSYS LSDyna的DEM-SPH-FEM耦合模拟滑坡入水动态行为研究,基于ANSYS LSDyna的DEM-SPH-FEM耦合的滑坡入水模拟分析研究,基于ansys lsdyna的滑坡入水模拟dem-sph-fem耦合 ,基于ANSYS LSDyna; 滑坡入水模拟; DEM-SPH-FEM 耦合,基于DEM-SPH-FEM耦合的ANSYS LSDyna滑坡入水模拟
auto_gptq-0.6.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
复件 复件 建设工程可行性研究合同[示范文本].doc
13考试真题最近的t64.txt
好用我已经解决报错问题
# 踏入C语言的奇妙编程世界 在编程的广阔宇宙中,C语言宛如一颗璀璨恒星,以其独特魅力与强大功能,始终占据着不可替代的地位。无论你是编程小白,还是有一定基础想进一步提升的开发者,C语言都值得深入探索。 C语言的高效性与可移植性令人瞩目。它能直接操控硬件,执行速度快,是系统软件、嵌入式开发的首选。同时,代码可在不同操作系统和硬件平台间轻松移植,极大节省开发成本。 学习C语言,能让你深入理解计算机底层原理,培养逻辑思维和问题解决能力。掌握C语言后,再学习其他编程语言也会事半功倍。 现在,让我们一起开启C语言学习之旅。这里有丰富教程、实用案例、详细代码解析,助你逐步掌握C语言核心知识和编程技巧。别再犹豫,加入我们,在C语言的海洋中尽情遨游,挖掘无限可能,为未来的编程之路打下坚实基础!
auto_gptq-0.4.2-cp38-cp38-win_amd64.whl
自动立体库设计方案.pptx
# 踏入C语言的奇妙编程世界 在编程的广阔宇宙中,C语言宛如一颗璀璨恒星,以其独特魅力与强大功能,始终占据着不可替代的地位。无论你是编程小白,还是有一定基础想进一步提升的开发者,C语言都值得深入探索。 C语言的高效性与可移植性令人瞩目。它能直接操控硬件,执行速度快,是系统软件、嵌入式开发的首选。同时,代码可在不同操作系统和硬件平台间轻松移植,极大节省开发成本。 学习C语言,能让你深入理解计算机底层原理,培养逻辑思维和问题解决能力。掌握C语言后,再学习其他编程语言也会事半功倍。 现在,让我们一起开启C语言学习之旅。这里有丰富教程、实用案例、详细代码解析,助你逐步掌握C语言核心知识和编程技巧。别再犹豫,加入我们,在C语言的海洋中尽情遨游,挖掘无限可能,为未来的编程之路打下坚实基础!
在日常的工作和学习中,你是否常常为处理复杂的数据、生成高质量的文本或者进行精准的图像识别而烦恼?DeepSeek 或许就是你一直在寻找的解决方案!它以其高效、智能的特点,在各个行业都展现出了巨大的应用价值。然而,想要充分发挥 DeepSeek 的优势,掌握从入门到精通的知识和技能至关重要。本文将从实际应用的角度出发,为你详细介绍 DeepSeek 的基本原理、操作方法以及高级技巧。通过系统的学习,你将能够轻松地运用 DeepSeek 解决实际问题,提升工作效率和质量,让自己在职场和学术领域脱颖而出。现在,就让我们一起开启这场实用又高效的学习之旅吧!
在日常的工作和学习中,你是否常常为处理复杂的数据、生成高质量的文本或者进行精准的图像识别而烦恼?DeepSeek 或许就是你一直在寻找的解决方案!它以其高效、智能的特点,在各个行业都展现出了巨大的应用价值。然而,想要充分发挥 DeepSeek 的优势,掌握从入门到精通的知识和技能至关重要。本文将从实际应用的角度出发,为你详细介绍 DeepSeek 的基本原理、操作方法以及高级技巧。通过系统的学习,你将能够轻松地运用 DeepSeek 解决实际问题,提升工作效率和质量,让自己在职场和学术领域脱颖而出。现在,就让我们一起开启这场实用又高效的学习之旅吧!