[剑指offer] 模拟篇

发布时间 2023-09-15 21:43:00作者: Vivid-BinGo

JZ29 顺时针打印矩阵

 1 /* 模拟 */
 2 public class JZ29_1
 3 {
 4     public static ArrayList<Integer> printMatrix(int[][] matrix)
 5     {
 6         ArrayList<Integer> res = new ArrayList<>();
 7         int left = 0, right = matrix[0].length - 1, up = 0, bottom = matrix.length - 1;
 8         while (left <= right && up <= bottom)
 9         {
10             for (int i = left; i <= right; i++)
11                 res.add(matrix[up][i]);
12             for (int i = up + 1; i <= bottom - 1; i++)
13                 res.add(matrix[i][right]);
14             for (int i = right; i >= left && up != bottom; i--)
15                 res.add(matrix[bottom][i]);
16             for (int i = bottom - 1; i >= up + 1 && left != right; i--)
17                 res.add(matrix[i][left]);
18             left++;
19             right--;
20             up++;
21             bottom--;
22         }
23         return res;
24     }
25 }
26 
27 /* ⭐走不动了就向右拐⭐ */
28 public class JZ29_2
29 {
30     public static boolean[][] vis;
31     public static int m;
32     public static int n;
33 
34     public static ArrayList<Integer> printMatrix(int[][] matrix)
35     {
36         m = matrix.length;
37         n = matrix[0].length;
38         vis = new boolean[m][n];
39 
40         ArrayList<Integer> res = new ArrayList<>();
41         int cur_x = 0, cur_y = 0, index = 0;
42         int[] ori_x = new int[]{0, 1, 0, -1};
43         int[] ori_y = new int[]{1, 0, -1, 0};
44         while (judge(cur_x, cur_y))
45         {
46             vis[cur_x][cur_y] = true;
47             res.add(matrix[cur_x][cur_y]);
48             if (!judge(cur_x + ori_x[index], cur_y + ori_y[index]))
49                 index = (index + 1) % 4;
50             cur_x = cur_x + ori_x[index];
51             cur_y = cur_y + ori_y[index];
52         }
53         return res;
54     }
55 
56     public static boolean judge(int cur_x, int cur_y)
57     {
58         return cur_x >= 0 && cur_x < m && cur_y >= 0 && cur_y < n && !vis[cur_x][cur_y];
59     }
60 }
61 
62 /* 递归遍历 */
63 public class JZ29_3
64 {
65     public static ArrayList<Integer> res = new ArrayList<>();
66 
67     public static ArrayList<Integer> printMatrix(int[][] matrix)
68     {
69         recursion(matrix, 0, matrix[0].length - 1, 0, matrix.length - 1);
70         return res;
71     }
72 
73     public static void recursion(int[][] matrix, int left, int right, int up, int bottom)
74     {
75         if (left > right || up > bottom) return;
76 
77         for (int i = left; i <= right; i++)
78             res.add(matrix[up][i]);
79         for (int i = up + 1; i <= bottom - 1; i++)
80             res.add(matrix[i][right]);
81         for (int i = right; i >= left && up != bottom; i--)
82             res.add(matrix[bottom][i]);
83         for (int i = bottom - 1; i >= up + 1 && left != right; i--)
84             res.add(matrix[i][left]);
85         recursion(matrix, left + 1, right - 1, up + 1, bottom - 1);
86     }
87 
88     public static void main(String[] args)
89     {
90         int[][] martix = SimulateUtils.createMartix(3, 5);
91         ArrayList<Integer> integers = printMatrix(martix);
92         integers.forEach(System.out::println);
93     }
94 }

JZ61 扑克牌顺子

 1 /* 滑动窗口 */
 2 public class JZ61_1
 3 {
 4     public static boolean IsContinuous(int[] numbers)
 5     {
 6         int[] cnt = new int[14];
 7         int left = 0, right = 0, need = 5;
 8         for (int number : numbers)
 9         {
10             cnt[number]++;
11             if (number != 0 && cnt[number] > 1)
12                 return false;
13         }
14         while (right < cnt.length)
15         {
16             if (right - left < 5)
17             {
18                 right++;
19                 if (right < cnt.length && cnt[right] > 0)
20                     need--;
21             }
22             else
23             {
24                 if (need <= cnt[0])
25                     return true;
26                 left++;
27                 if (cnt[left] > 0)
28                     need++;
29             }
30         }
31         return false;
32     }
33 }
34 
35 /* maxValue - minValue < 5 */
36 public class JZ61_2
37 {
38     public static boolean IsContinuous(int[] numbers)
39     {
40         int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
41         for (int num : numbers)
42         {
43             if (num == 0) continue;
44 
45             if (max == num)
46                 return false;
47             if (max < num)
48                 max = num;
49             if (min == num)
50                 return false;
51             if (min > num)
52                 min = num;
53         }
54         return max - min <= 4;
55     }
56 }
57 
58 /* 统计间隔数 */
59 public class JZ61_3
60 {
61     public static boolean IsContinuous(int[] numbers)
62     {
63         Arrays.sort(numbers);
64         int i = 0, zeros = 0, gap = 0;
65         while (numbers[i] == 0)
66         {
67             zeros++;
68             i++;
69         }
70         for (; i < numbers.length - 1; i++)
71         {
72             if (numbers[i + 1] - numbers[i] == 0) return false;
73             gap = gap + numbers[i + 1] - numbers[i] - 1;
74         }
75         return gap <= zeros;
76     }
77 }

JZ67 把字符串转换成整数(atoi)

 1 /* 模拟 */
 2 public class JZ67_1
 3 {
 4     public static int StrToInt(String s)
 5     {
 6         int idx = 0, res = 0, posFlag = 1;
 7         while (idx < s.length() && s.charAt(idx) == ' ')
 8             idx++;
 9         if (idx == s.length()) return 0;
10         if (s.charAt(idx) == '-')
11         {
12             posFlag = -1;
13             idx++;
14         }
15         else if (s.charAt(idx) == '+') idx++;
16         while (idx < s.length() && Character.isDigit(s.charAt(idx)))
17         {
18             if (posFlag == 1 && (res > Integer.MAX_VALUE / 10 || (res == Integer.MAX_VALUE / 10 && s.charAt(idx) - '0' > Integer.MAX_VALUE % 10)))
19                 return Integer.MAX_VALUE;
20             if (posFlag == -1 && (-res < Integer.MIN_VALUE / 10 || (-res == Integer.MIN_VALUE / 10 && s.charAt(idx) - '0' > -(Integer.MIN_VALUE % 10))))
21                 return Integer.MIN_VALUE;
22             res = res * 10 + s.charAt(idx) - '0';
23             idx++;
24         }
25         return res * posFlag;
26     }
27 }

JZ20 表示数值的字符串

  1 /* 边遍历边判断 */
  2 public class JZ20_1
  3 {
  4     public static boolean isNumeric(String str)
  5     {
  6         str = str.trim();
  7         boolean meetDot = false, meetAlphaE = false, meetNum = false;
  8         for (int i = 0; i < str.length(); i++)
  9         {
 10             char s = str.charAt(i);
 11             if (Character.isDigit(s) || s == '.' || Character.toLowerCase(s) == 'e' || s == '+' || s == '-')
 12             {
 13                 if (s == '+' || s == '-')
 14                 {
 15                     if (i + 1 == str.length() || str.charAt(i + 1) == '-' || Character.toLowerCase(str.charAt(i + 1)) == 'e')
 16                         return false;
 17                     if (i != 0 && Character.toLowerCase(str.charAt(i - 1)) != 'e')
 18                         return false;
 19                 }
 20                 if (s == '.')
 21                 {
 22                     if (meetDot || meetAlphaE)
 23                         return false;
 24                     else
 25                         meetDot = true;
 26                 }
 27                 if (Character.toLowerCase(s) == 'e')
 28                 {
 29                     if (meetAlphaE || !meetNum || i == str.length() - 1)
 30                         return false;
 31                     else
 32                         meetAlphaE = true;
 33                 }
 34                 if (Character.isDigit(s))
 35                     meetNum = true;
 36             }
 37             else return false;
 38         }
 39         return meetNum;
 40     }
 41 }
 42 
 43 /* 以 e 为分界线,分成前后两部分,分别判断 */
 44 public class JZ20_2
 45 {
 46     public static boolean isNumeric(String str)
 47     {
 48         long count = 0;
 49         str = str.toLowerCase().trim();
 50         int idxE = str.indexOf('e');
 51         if (idxE == -1)
 52         {
 53             count = str.chars().filter(s -> s == '.').count();
 54             if (count > 1 || !judge(str)) return false;
 55         }
 56         else
 57         {
 58             String fir = str.substring(0, idxE);
 59             count = fir.chars().filter(s -> s == '.').count();
 60             if (count > 1 || !judge(fir)) return false;
 61 
 62             String sec = str.substring(idxE + 1);
 63             count = sec.chars().filter(s -> s == 'e').count();
 64             if (count > 0 || sec.indexOf('.') != -1 || !judge(sec))
 65                 return false;
 66         }
 67         return true;
 68     }
 69 
 70     public static boolean judge(String str)
 71     {
 72         int i = 0;
 73         boolean meetNum = false;
 74         if (str.length() == 0) return false;
 75         if (str.charAt(i) == '+' || str.charAt(i) == '-')
 76             i++;
 77         if (i == str.length()) return false;
 78         for (; i < str.length(); i++)
 79         {
 80             if (Character.isDigit(str.charAt(i)))
 81                 meetNum = true;
 82             else if (str.charAt(i) != '.')
 83                 return false;
 84         }
 85         return meetNum;
 86     }
 87 }
 88 
 89 /* regex */
 90 public class JZ20_3
 91 {
 92     public boolean isNumeric(String str)
 93     {
 94         String p = "^[+-]?(\\d*\\.\\d+|\\d+(\\.\\d*)?)(?:[eE][+-]?\\d+)?$";
 95         return Pattern.matches(p, str.trim());
 96     }
 97 }
 98 
 99 /* ⭐剑指⭐ */
100 public class JZ20_4
101 {
102     public static int idx = 0;
103 
104     public static boolean isNumeric(String str)
105     {
106         StringBuilder s = new StringBuilder(str.trim().toLowerCase());
107         if (s.length() == 0) return false;
108         boolean numeric = scanInteger(s);
109         if (idx < s.length() && s.charAt(idx) == '.')
110         {
111             ++idx;
112             boolean hasNumbers = scanUnsignedInteger(s);
113             numeric = hasNumbers || numeric;
114         }
115         if (idx < s.length() && s.charAt(idx) == 'e')
116         {
117             ++idx;
118             numeric = numeric && scanInteger(s);
119         }
120         return numeric && (idx == s.length());
121 
122     }
123 
124     public static boolean scanInteger(StringBuilder str)
125     {
126         if (idx >= str.length()) return false;
127         if (str.charAt(idx) == '+' || str.charAt(idx) == '-')
128             ++idx;
129         return scanUnsignedInteger(str);
130     }
131 
132     public static boolean scanUnsignedInteger(StringBuilder str)
133     {
134         if (idx >= str.length()) return false;
135         int begin = idx;
136         while (idx < str.length() && str.charAt(idx) >= '0' && str.charAt(idx) <= '9')
137             ++idx;
138         return idx > begin;
139     }
140 }