中级农民
- 积分
- 100
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2016-1-24
- 最后登录
- 1970-1-1
|
没看懂的为什么楼主ccbd是2的,为啥[][]数组的2个分量长度还不一样
- public class Solution {
- public int strstr(char[] c1, char[] c2) {
- for (int i = 0; i + c2.length <= c1.length; i++) {
- boolean match = true;
- for (int j = 0; j < c2.length; j++) {
- if (c1[i + j] != c2[j]) {
- match = false;
- break;
- }
- }
- if (match) {
- return i;
- }
- }
- return -1;
- }
- int twoone(int i, int j, int m, int n) {
- return i * n + j;
- }
- int[] onetwo(int i, int m, int n) {
- return new int[] {i / n, i % n};
- }
- public int strstr(char[][] c1, char[] c2) {
- int m = c1.length;
- int n = c1[0].length;
- for (int i = 0; i + c2.length <= m * n; i++) {
- boolean match = true;
- for (int j = 0; j < c2.length; j++) {
- int idx[] = onetwo(i + j, m, n);
- if (c1[idx[0]][idx[1]] != c2[j]) {
- match = false;
- break;
- }
- }
- if (match) {
- return i;
- }
- }
- return -1;
- }
- public static void main(String[] args) {
- Solution s = new Solution();
- System.out.println(s.strstr(new char[] {'a', 'b', 'c', 'c', 0}, new char[] {'c', 'c'}));
- System.out.println(s.strstr(new char[][] {{'a', 'b', 'c', 'c'}, {'c', 'b', 'd', 0}},
- new char[] {'c', 'c', 'b', 'd'}));
- System.out.println(
- s.strstr(new char[][] {{'a', 'b', 'c', 'c'}, {'c', 'b', 'd', 0}}, new char[] {'b', 'd'}));
- return;
- }
- }
复制代码 |
|