剑指offer_by牛客网

2017/9/6

二维数组中的查找 m*n

在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。

找是否存在target

我的想法,for循环判断每行首尾,再进行二分

O(m*logn)

 1 public class Solution {
 2     public boolean Find(int target, int [][] array) {
 3         if (array == null || array.length == 0 || array[0].length == 0) {
 4             return false;
 5         }
 6         int row = array.length;
 7         int col = array[0].length;
 8         for (int i = 0; i < row; i++) {
 9             int start = 0;
10             int end = col - 1;
11             if (array[i][0] <= target && array[i][end] >= target) {
12                while (start + 1 < end) {
13                    int mid = start + (end - start) / 2;
14                     if (array[i][mid] == target) {
15                         return true;
16                     } else if (array[i][mid] > target) {
17                         end = mid;
18                     } else{
19                         start = mid;
20                     }
21                 } 
22                 if (array[i][start] == target) {
23                     return true;
24                 }
25                 if (array[i][end] == target) {
26                     return true;
27                 }
28             }
29             
30         }
31         return false;
32     }
33 }
View Code

 答案:

从左下角(或右上角,进行二分)时间复杂度O(m+n)

 1 public class Solution {
 2     public boolean Find(int target, int [][] array) {
 3         if (array == null || array.length == 0 || array[0].length == 0) {
 4             return false;
 5         }
 6         int row = array.length;
 7         int col = array[0].length;
 8         int i = 0;
 9         int j = col - 1;
10         while (i < row && j >= 0) {
11             if (array[i][j] == target) {
12                 return true;
13             } else if (array[i][j] > target) {
14                 j--;
15             } else {
16                 i++;
17             }
18         }
19         return false;
20     }
21 }
View Code

2017/9/7

从尾到头打印链表。

 1 import java.util.ArrayList;
 2 public class Solution {
 3     public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
 4         ArrayList<Integer> result = new ArrayList<>();
 5         ArrayList<Integer> list = new ArrayList<>();
 6         if (listNode == null) {
 7             return result;
 8         }
 9         ListNode tmp = listNode;
10         while (tmp != null) {
11             result.add(tmp.val);
12             tmp = tmp.next;
13         }
14         for (int i = result.size() - 1; i >= 0; i--) {
15             list.add(result.get(i));
16         } 
17         return list;
18     }
19 }
View Code

2017/9/21

复杂链表的复制

random指针

 1 /*
 2 public class RandomListNode {
 3     int label;
 4     RandomListNode next = null;
 5     RandomListNode random = null;
 6 
 7     RandomListNode(int label) {
 8         this.label = label;
 9     }
10 }
11 */
12 public class Solution {
13     public RandomListNode Clone(RandomListNode pHead)
14     {
15         if (pHead == null) {
16             return null;
17         }
18         step1(pHead);
19         step2(pHead);
20         return step3(pHead);
21         
22     }
23     private void step1(RandomListNode pHead) {
24         RandomListNode temp = pHead;
25         while (temp != null) {
26             RandomListNode cur = new RandomListNode(temp.label);
27             cur.next = temp.next;
28             cur.random = null;
29             temp.next = cur;
30             temp = cur.next;
31         }
32         return;
33     }
34     private void step2(RandomListNode pHead) {
35         RandomListNode old = pHead;
36         RandomListNode newtemp = pHead;
37         while (old != null) {
38             newtemp = old.next;
39             if (old.random != null) {
40                 newtemp.random = old.random.next;
41             }
42             old = newtemp.next;
43         }
44     }
45     private RandomListNode step3(RandomListNode pHead) {
46         RandomListNode newHead = pHead.next;
47         RandomListNode oldtemp = pHead;
48         RandomListNode newtemp = pHead;
49         while (oldtemp.next != null) {
50             newtemp = oldtemp.next;
51             oldtemp.next = newtemp.next;
52             oldtemp = newtemp;
53         }
54         return newHead;
55     }
56 }
View Code

2017/9/22

字符串的排列

 1 import java.util.*;
 2 public class Solution {
 3     public ArrayList<String> Permutation(String str) {
 4        ArrayList<String> result = new ArrayList<>();
 5         if (str == null || str.length() == 0)
 6            return result;
 7 //        if (str.length() <= 1)
 8 //            return result.add(str);
 9         char[] zhu = str.toCharArray();
10         ArrayList<Character> temp = new ArrayList<>();
11         int[] visited = new int[zhu.length];
12         for (int i = 0; i < zhu.length; i++) {
13             visited[i] = 0;
14         }
15         Arrays.sort(zhu);
16         dfs(zhu, result, temp, visited);
17         return result;
18         
19     }
20     private void dfs(char[] zhu, ArrayList<String> result, ArrayList<Character> temp, int[] visited) {
21         if (temp.size() == zhu.length) {
22             StringBuilder str = new StringBuilder();
23             for (Character character : temp) {//
24                 str.append(character);
25             }
26             result.add(str.toString());
27         }
28         for (int i = 0; i < zhu.length; i++){
29             if (visited[i] == 0) {
30                 if (i != 0 && zhu[i] == zhu[i - 1] && visited[i - 1] == 0) {
31                     continue;
32                 }
33                 temp.add(zhu[i]);
34                 visited[i] = 1;
35                 dfs(zhu, result, temp, visited);
36                 temp.remove(temp.size() - 1);
37                 visited[i] = 0;
38             }
39         }
40         return;
41     }
42 }
View Code
原文地址:https://www.cnblogs.com/yunyouhua/p/7485960.html