1. 數(shù)組與矩陣

[toc]


3. 數(shù)組中重復(fù)的數(shù)字

思路:數(shù)組數(shù)字范圍為1-n,統(tǒng)計count[num]即可

class Solution {
    public int findRepeatNumber(int[] nums) {
        int n = nums.length;
        int[] count = new int[n];
        for(int num: nums) {
            if(count[num] != 0 ){
                return num;
            } else {
                count[num] ++;
            }
        }
        return -1;
    }
}

4. 二維數(shù)組中的查找

思路: 從左下角開始查找

class Solution {
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        if(matrix.length == 0)
            return false;

        int m = matrix.length, n = matrix[0].length;
        int i = m - 1, j = 0;
        while(i >= 0 && j < n) {
            if(target > matrix[i][j]) {
                j++;
            } else if(target < matrix[i][j]) {
                i--;
            } else {
                return true;
            }
        }
        return false;
    }
}

5. 替換空格

思路:
a)找出空格數(shù)量,計算結(jié)果的長度
b)倒序添加02%

class Solution {
    public String replaceSpace(String s) {
        if(s.length() == 0)
            return s;
        char[] chars = s.toCharArray();
        int spaceCount = 0;
        for(char c : chars) {
            if(c == ' ')
                spaceCount++;
        }

        char[] res = new char[chars.length + spaceCount * 2];
        int index = res.length - 1;
        for(int i = chars.length - 1; i >= 0;i--){
            if(chars[i] == ' ') {
                res[index] = '0';
                res[index - 1] = '2';
                res[index - 2] = '%';
                index = index - 3;
            } else {
                res[index] = chars[i]; 
                index --;
            }
        }
        return String.valueOf(res);
    }
}

?? 29. 順時針打印矩陣

關(guān)鍵點: 注意從右到左、從下到上 進行check

class Solution {
    public int[] spiralOrder(int[][] matrix) {
        int m = matrix.length;
        if(m == 0) {
            return new int[]{};
        }
        int n = matrix[0].length;
        int[] res = new int[m * n];
        int index = 0;
        int left = 0, right = n -1 , top = 0 , bottom = m - 1;
        while(left <= right && top <= bottom) {
            // 左到右
            for(int i = left;i <= right;i ++) {
                res[index++] = matrix[top][i];
            }
            top++;
            // 上到下
            for(int i = top;i <= bottom;i++){
                res[index++] = matrix[i][right];
            }
            right--;
            // !!!!!
            if(top <= bottom) {
                for(int i = right;i >= left;i--) {
                    res[index++] = matrix[bottom][i];
                }
                bottom--;
            }

            if(left <= right) {
                for(int i = bottom;i >= top;i--) {
                    res[index++] = matrix[i][left];
                }
                left++;
            }
        }
        return res;
    }
}

50. 第一個只出現(xiàn)一次的字符位置

思路:因為字符,ASCII 范圍0-128,使用count方法

class Solution {
    public char firstUniqChar(String s) {
        if(s.length() == 0)
            return ' ';
        int[] counts = new int[128];
        for(char c: s.toCharArray()) {
            counts[c - '0']++;
        }
        for(char c: s.toCharArray()) {
            if(counts[c - '0'] == 1) {
                return c;
            }
        }
        return ' ';

    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容