@@ -174,77 +174,6 @@ public boolean exist(char[][] board, String word) {
174174
175175
176176
177- ## 4、机器人的运动范围(66)
178-
179- [ 机器人的运动范围] ( https://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8?tpId=13&tqId=11219&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking )
180-
181- ``` java
182- // 思路:
183- // flood-fill 算法
184- private int m;
185- private int n;
186-
187- private boolean [][] visited;
188-
189- private int [][] d= {
190- {0 ,1 }, // 向右
191- {- 1 ,0 },// 向上
192- {0 ,- 1 },// 向左
193- {1 ,0 } // 向下
194- };
195-
196- private boolean inArea(int x,int y){
197- return (x>= 0 && x< m) && (y>= 0 && y< n);
198- }
199-
200- // 获取一个数各个位上的数字相加
201- private int getNum(int num){
202- int sum = 0 ;
203- while (num> 0 ){
204- sum += num % 10 ;
205- num/= 10 ;
206- }
207- return sum;
208- }
209-
210- private boolean valid(int threshlod ,int x,int y){
211- return (getNum(x)+ getNum(y))<= threshlod;
212- }
213-
214- private int walk(int threshold,int startx,int starty){
215- visited[startx][starty]= true ;
216- int walks = 0 ;
217- if (valid(threshold,startx,starty)){
218- walks= 1 ;
219- }
220- for (int i= 0 ;i< 4 ;i++ ){
221- int newX = startx+ d[i][0 ];
222- int newY = starty+ d[i][1 ];
223- if (inArea(newX,newY)){
224- if (! visited[newX][newY] && valid(threshold,newX,newY)){
225- walks += walk(threshold,newX,newY);
226- }
227- }
228- }
229- return walks;
230- }
231-
232- public int movingCount(int threshold, int rows, int cols) {
233- if (threshold< 0 ){ // threshold<0,则机器人就不能走了
234- return 0 ;
235- }
236- m = rows;
237- if (m== 0 ){
238- return 0 ;
239- }
240- n= cols;
241- visited = new boolean [m][n];
242- return walk(threshold,0 ,0 );
243- }
244- ```
245-
246-
247-
248177## 5、全排列
249178
250179[ 全排列] ( https://leetcode-cn.com/problems/permutations/ )
@@ -267,7 +196,7 @@ public List<List<Integer>> permute(int[] nums) {
267196 return res;
268197}
269198
270- // p中保存一个有index的元素的排列
199+ // p中保存一个有 index 的元素的排列
271200// 向这个排列的末尾添加第 (index+1) 个元素,组成有(index+1) 个元素排列
272201private void generatePermutation(int [] nums,int index,List<Integer > p){
273202 if (index== nums. length){
@@ -335,7 +264,7 @@ private void findUniquePermutation(int[] nums,int index,List<Integer> p){
335264
336265
337266
338- ## 7、字符串的排列
267+ ## * 7、字符串的排列
339268
340269[ 字符串的排列] ( https://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7?tpId=13&tqId=11180&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking )
341270
@@ -386,7 +315,36 @@ private void permute(char[] chs,int index,StringBuilder p){
386315[ 字母大小写全排列] ( https://leetcode-cn.com/problems/letter-case-permutation/ )
387316
388317``` java
318+ private List<String > res;
319+
320+ // 处理 index 位置的数据
321+ // 如果 index 位置是字母的话,则替换
322+ private void replaceLetter(int index,StringBuilder p){
323+ if (index== p. length()){
324+ res. add(p. toString());
325+ return ;
326+ }
327+ char ch= p. charAt(index);
328+ if (Character . isLetter(ch)){
329+ p. setCharAt(index,Character . toUpperCase(ch));
330+ replaceLetter(index+ 1 ,p);
331+ p. setCharAt(index,Character . toLowerCase(ch));
332+ replaceLetter(index+ 1 ,p);
333+ }else {
334+ replaceLetter(index+ 1 ,p);
335+ }
336+ return ;
337+ }
389338
339+ public List<String > letterCasePermutation(String S ) {
340+ res= new ArrayList<> ();
341+ if (S == null || S . length()== 0 ){
342+ return res;
343+ }
344+ StringBuilder p= new StringBuilder (S );
345+ replaceLetter(0 ,p);
346+ return res;
347+ }
390348```
391349
392350
@@ -443,7 +401,73 @@ public List<String> letterCombinations(String digits) {
443401[ 组合] ( https://leetcode-cn.com/problems/combinations/ )
444402
445403``` java
404+ // 思路一:暴力解法
405+
406+ private List<List<Integer > > res;
407+
408+ // c存储已经找到的组合
409+ // 从start开始搜索新的元素
410+ private void findCombination(int n,int k,int start,List<Integer > c){
411+ if (k== c. size()){
412+ res. add(new ArrayList<> (c));
413+ return ;
414+ }
415+ for (int i= start;i<= n;i++ ){
416+ c. add(i);
417+ findCombination(n,k,i+ 1 ,c);
418+ c. remove(c. size()- 1 );
419+ }
420+ return ;
421+ }
422+
423+ public List<List<Integer > > combine(int n, int k) {
424+ res= new ArrayList<> ();
425+ if (n<= 0 || k<= 0 || n< k){
426+ return res;
427+ }
428+ List<Integer > c= new ArrayList<> ();
429+ findCombination(n,k,1 ,c);
430+ return res;
431+ }
432+ ```
433+
434+
435+
436+ ``` java
437+ // 思路二:优化,进行剪枝
438+ private List<List<Integer > > res;
439+
440+ // c存储已经找到的组合
441+ // 从start开始搜索新的元素
442+ private void findCombination(int n,int k,int start,List<Integer > c){
443+ if (k== c. size()){
444+ res. add(new ArrayList<> (c));
445+ return ;
446+ }
447+
448+ // TODO:优化--剪枝
449+ // c存储的是已经找到的组合。
450+ // 此时还剩下k-c.size()个空位,
451+ // 则 [i...n]之间的元素最少要有 k-c.size() 个,即 n-i+1 >= k-c.size()
452+ // 所以有 i <= n-(k-c.size())+1
453+
454+ for (int i= start;i<= n- (k- c. size())+ 1 ;i++ ){
455+ c. add(i);
456+ findCombination(n,k,i+ 1 ,c);
457+ c. remove(c. size()- 1 );
458+ }
459+ return ;
460+ }
446461
462+ public List<List<Integer > > combine(int n, int k) {
463+ res= new ArrayList<> ();
464+ if (n<= 0 || k<= 0 || n< k){
465+ return res;
466+ }
467+ List<Integer > c= new ArrayList<> ();
468+ findCombination(n,k,1 ,c);
469+ return res;
470+ }
447471```
448472
449473
0 commit comments