12
返回列表 发新帖
楼主: davidsuger
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] 一个软件测试的刷题记录

🔗
 楼主| davidsuger 2015-9-3 08:06:06 | 只看该作者
全局:
本帖最后由 davidsuger 于 2015-9-3 08:15 编辑

解法2:
//先算出转换后的长度,建一个新数组,然后再转换
        public static String compressString2(String str) {
                if (str == null) {
                        return null;
                }
                if (str.isEmpty()) {
                        return str;
                }

                int newLenth = countCompression(str);
                if (newLenth > str.length()) {
                        return str;
                }

                char[] newStr = new char[newLenth];
                int index = 0;
                char current = str.charAt(0);
                int count = 0;
                for (int i = 0; i < str.length(); i++) {
                        if (str.charAt(i) == current) {
                                count++;
                        } else {
                                newStr[index] = current;
                                // 把数量变成字符加在新字符串后面
                                char[] countCharArray = String.valueOf(count).toCharArray();
                                for (int j = 0; j < countCharArray.length; j++) {
                                        index++;
                                        newStr[index] = countCharArray[j];
                                }
                                index++;
                                current = str.charAt(i);
                                count = 1;
                        }
                }
               
                //放最后一组
                newStr[index] = current;
                // 把数量变成字符加在新字符串后面
                char[] countCharArray = String.valueOf(count).toCharArray();
                for (int j = 0; j < countCharArray.length; j++) {
                        index++;
                        newStr[index] = countCharArray[j];
                }

                return String.valueOf(newStr);

        }
        //算转换后的字符串长度
        public static int countCompression(String str) {
                if (str == null || str.isEmpty()) {
                        return 0;
                }

                int length = 0;
                int count = 0;
                char current = str.charAt(0);
                for (int i = 0; i < str.length(); i++) {
                        if (str.charAt(i) == current) {
                                count++;
                        } else {
                                length += 1 + String.valueOf(count).length();
                                current = str.charAt(i);
                                count = 1;
                        }
                }
                length += 1 + String.valueOf(count).length();
                return length;
        }
回复

使用道具 举报

🔗
 楼主| davidsuger 2015-9-3 08:16:04 | 只看该作者
全局:
解法3:
//最坏的情况就是转换后的长度增加一倍,建一个两倍长度的数组,然后再转换,然后trim()
                public static String compressString3(String str) {
                        if (str == null) {
                                return null;
                        }
                        if (str.isEmpty()) {
                                return str;
                        }

                        char[] newStr = new char[2*str.length()];
                        int index = 0;
                        char current = str.charAt(0);
                        int count = 0;
                        for (int i = 0; i < str.length(); i++) {
                                if (str.charAt(i) == current) {
                                        count++;
                                } else {
                                        newStr[index] = current;
                                        // 把数量变成字符加在新字符串后面
                                        char[] countCharArray = String.valueOf(count).toCharArray();
                                        for (int j = 0; j < countCharArray.length; j++) {
                                                index++;
                                                newStr[index] = countCharArray[j];
                                        }
                                        index++;
                                        current = str.charAt(i);
                                        count = 1;
                                }
                        }
                       
                        //放最后一组
                        newStr[index] = current;
                        // 把数量变成字符加在新字符串后面
                        char[] countCharArray = String.valueOf(count).toCharArray();
                        for (int j = 0; j < countCharArray.length; j++) {
                                index++;
                                newStr[index] = countCharArray[j];
                        }

                        return String.valueOf(newStr).trim();

                }
回复

使用道具 举报

🔗
 楼主| davidsuger 2015-9-3 08:43:46 | 只看该作者
全局:
1.6 Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?
给定一幅由NxN矩阵表示的图像,其中每个像素的大小为4字节,编写一个方法,将图像旋转90度。不占用额外内存空间能否做到?
回复

使用道具 举报

🔗
 楼主| davidsuger 2015-9-3 09:19:18 | 只看该作者
全局:
解法1:
        // 先每行左右翻转,然后对角线翻转
        public static void rotate(int[][] matrix) {
                for (int i = 0; i < matrix.length; i++) {
                        for (int j = 0; j < matrix[i].length / 2; j++) {
                                int temp = matrix[i][j];
                                matrix[i][j] = matrix[i][matrix[i].length - j - 1];
                                matrix[i][matrix[i].length - j - 1] = temp;
                        }
                }
                for (int i = 0; i < matrix.length; i++) {
                        for (int j = 0; j < matrix[i].length - i; j++) {
                                int temp = matrix[i][j];
                                matrix[i][j] = matrix[matrix.length - j - 1][matrix[i].length - i
                                                - 1];
                                matrix[matrix.length - j - 1][matrix[i].length - i - 1] = temp;
                        }
                }
        }
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表