NC_100_ATOI NC_102_lowestCommonAncestor NC_104_COMPARE_VERSION NC_109_ISLAND_NUM

package org.example.interview.practice;

/**
 * @author xianzhe.ma
 * @date 2021/11/3
 */

public class NC_100_ATOI {

    public int atoi(String str) {
        // write code here
        str = str.trim();//去掉前后的空格
        //如果为空,直接返回0
        if (str.length() == 0)
            return 0;
        int index = 0;//遍历字符串中字符的位置
        int res = 0;//最终结果
        int sign = 1;//符号,1是正数,-1是负数,默认为正数
        int length = str.length();
        //判断符号
        if (str.charAt(index) == '-' || str.charAt(index) == '+')
            sign = str.charAt(index++) == '+' ? 1 : -1;
        for (; index < length; ++index) {
            //取出字符串中字符,然后转化为数字
            int digit = str.charAt(index) - '0';

            //按照题中的要求,读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。
            //字符串的其余部分将被忽略。如果读取了非数字,后面的都要忽略
            if (digit < 0 || digit > 9)
                break;
            //越界处理
            if (res > Integer.MAX_VALUE / 10 || (res == Integer.MAX_VALUE / 10 && digit > Integer.MAX_VALUE % 10))
                return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            else
                res = res * 10 + digit;
        }
        return sign * res;
    }
}
package org.example.interview.practice;

/**
 * @author xianzhe.ma
 * @date 2021/9/3
 */

public class NC_102_lowestCommonAncestor {

    public int lowestCommonAncestor (TreeNode root, int o1, int o2) {
        // write code here
        return helper(root, o1, o2).val;
    }

    private TreeNode helper(TreeNode root, int o1, int o2) {
        if (root == null || root.val == o1 || root.val == o2) {
            return root;
        }

        TreeNode left = helper(root.left, o1, o2);
        TreeNode right = helper(root.right, o1, o2);

        if (left == null) {
            return right;
        }

        if (right == null) {
            return left;
        }

        return root;
    }

      public static class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
      }

}
package org.example.interview.practice;

/**
 * @author xianzhe.ma
 * @date 2021/11/7
 */

public class NC_104_COMPARE_VERSION {

    public static int compare (String version1, String version2) {
        // write code here
        String[] array1 = version1.split("\\.");
        String[] array2 = version2.split("\\.");

        int len1 = array1.length;
        int len2 = array2.length;

        if (len1 != len2) {
            if (len1 < len2) {
                for (int i = 0;i<len2 - len1;i++) {
                    version1 = version1 + ".0";
                }
            } else {
                for (int i = 0;i<len1 - len2;i++) {
                    version2 = version2 + ".0";
                }
            }
        }
        array1 = version1.split("\\.");
        array2 = version2.split("\\.");
        for (int i = 0;i<array2.length;i++) {
            String str1 = array1[i];
            String str2 = array2[i];
            while (str1.startsWith("0") && str1.length() > 1) {
                str1 = str1.substring(1);
            }

            while (str2.startsWith("0") && str2.length() > 1) {
                str2 = str2.substring(1);
            }

            if (str1.length() != str2.length()) {
                if (str1.length() < str2.length()) {
                    return -1;
                }
                else {
                    return 1;
                }
            }

            //比较每一位
            char[] chars1 = str1.toCharArray();
            char[] chars2 = str2.toCharArray();
            for (int j = 0;j<chars1.length;j++) {
                char char1 = chars1[j];
                char char2 = chars2[j];
                if (char1 != char2) {
                    if (char1 - '0' > char2 -'0') {
                        return 1;
                    } else {
                        return -1;
                    }
                }
            }
        }

        return 0;
    }

    public static void main (String[] args) {
        String version1 = "134.105.202.15.33.83.60.151.38.150.82.113.141.168.7.24.78.1.80.1";
        String version2 = "134.105.202.15.33.83.60.151.38.150.82.113.141.168.7.94.26.39.167.186.105.132";

        System.out.println(compare(version1, version2));
    }
}
package org.example.interview.practice;

/**
 * @author xianzhe.ma
 * @date 2021/7/24
 */

public class NC_109_ISLAND_NUM {

    public int solve (char[][] grid) {
        // write code here
        if (grid == null || grid.length == 0) {
            return 0;
        }

        int nr = grid.length;
        int nc = grid[0].length;
        int num_islands = 0;
        for (int r = 0; r < nr; ++r) {
            for (int c = 0; c < nc; ++c) {
                if (grid[r][c] == '1' ) {
                    num_islands++;
                    dfs(grid, r, c);
                }
            }
        }

        return num_islands;
    }

    public void dfs(char[][] grid, int r, int c) {
        int nr = grid.length;
        int nc = grid[0].length;

        if (r < 0 || c < 0 || r >= nr || c >= nc || grid[r][c] == '0') {
            return;
        }

        grid[r][c] = '0';
        dfs(grid, r - 1, c);
        dfs(grid, r + 1, c);
        dfs(grid, r, c - 1);
        dfs(grid, r, c + 1);
    }
}

 

posted on 2022-02-11 15:38  MaXianZhe  阅读(30)  评论(0编辑  收藏  举报

导航