剑指 Offer打卡 数组与矩阵
生活随笔
收集整理的這篇文章主要介紹了
剑指 Offer打卡 数组与矩阵
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
二維數組中的查找
public class Solution {public static void main(String[] args) {System.out.println(new Solution().Find(2, new int[][]{{1, 1}}));}public boolean Find(int target, int [][] array) {if(array == null || array.length == 0 || array[0].length == 0) {return false;}int rows = array.length, cols = array[0].length;int r = 0, c = cols - 1;while(r <= rows - 1 && c >= 0) {if(target == array[r][c]) {return true;} else if(target > array[r][c]) {r++;} else {c--;}}return false;} }數組中重復的數字
public class Solution {/*** 3. 數組中重復的數字* 要求時間復雜度 O(N),空間復雜度 O(1)。因此不能使用排序的方法,也不能使用額外的標記數組。** 對于這種數組元素在 [0, n-1] 范圍內的問題,可以將值為 i 的元素調整到第 i 個位置上進行求解。在調整過程中,* 如果第 i 位置上已經有一個值為 i 的元素,就可以知道 i 值重復。** 以 (2, 3, 1, 0, 2, 5) 為例,遍歷到位置 4 時,該位置上的數為 2,但是第 2 個位置上已經有一個 2 的值了,* 因此可以知道 2 重復** @param args*/public static void main(String[] args) {System.out.println(new Solution().duplicate(new int[]{2, 3, 1, 0, 2, 5}));}public int duplicate(int[] nums) {for (int i = 0; i < nums.length; i++) {while (nums[i] != i) {if (nums[i] == nums[nums[i]]) {return nums[i];}swap(nums, i, nums[i]);// 1 3 2 0 2 5// 3 1 2 0 2 5}swap(nums, i, nums[i]);// 0 1 2 3 2 5}return -1;}private void swap(int[] nums, int i, int j) {int t = nums[i];nums[i] = nums[j];nums[j] = t;} }替換空格
public class Solution {public static void main(String[] args) {System.out.println(new Solution().replaceSpace("1 2 3 3"));}/*** 替換空格** 代碼中的類名、方法名、參數名已經指定,請勿修改,直接返回方法規定的值即可** @param s string字符串* @return string字符串*/public String replaceSpace(String s) {StringBuffer result = new StringBuffer(s);int p1 = result.length() - 1;for (int i = 0; i <= p1; i++) {if (result.charAt(i) == ' ')//這里是兩個空格哈result.append(" ");}int p2 = result.length() - 1;while (p1 >= 0 && p2 > p1) {char c = result.charAt(p1--);if (c == ' ') {result.setCharAt(p2--, '0');result.setCharAt(p2--, '2');result.setCharAt(p2--, '%');} else {result.setCharAt(p2--, c);}}return result.toString();} }第一個只出現一次的字符位置
/*** 在一個長為 字符串中找到第一個只出現一次的字符,并返回它的位置,* 如果沒有則返回 -1(需要區分大小寫).(從0開始計數)** @author inke219223m*/ public class Solution {public static void main(String[] args) {System.out.println(new Solution().firstUniqChar(""));System.out.println(new Solution().firstUniqChar("cc"));}public int FirstNotRepeatingChar1(String str) {int[] cnts = new int[128];for (int i = 0; i < str.length(); i++)cnts[str.charAt(i)]++;for (int i = 0; i < str.length(); i++)//條件 cnts[str.charAt(i)] == 1if (cnts[str.charAt(i)] == 1) return i;return -1;}public char FirstNotRepeatingChar(String s) {int[] cnts = new int[128];for (int i = 0; i < s.length(); i++)cnts[s.charAt(i)]++;for (int i = 0; i < s.length(); i++)//條件 cnts[str.charAt(i)] == 1if (cnts[s.charAt(i)] == 1) return s.charAt(i);return ' ';}public char firstUniqChar(String s) {if (s.equals("")) return ' ';int[] cnts = new int[128];for (int i = 0; i < s.length(); i++) {cnts[s.charAt(i)]++;}for (int i = 0; i < s.length(); i++) {if (cnts[s.charAt(i)] == 1)return s.charAt(i);}return ' ';} }順時針打印矩陣
public class Solution {public static void main(String[] args) {System.out.println(new Solution().printMatrix(new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}));System.out.println(new Solution().spiralOrder(new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}));}public ArrayList<Integer> printMatrix(int[][] matrix) {ArrayList<Integer> ret = new ArrayList<>();int r1 = 0, r2 = matrix.length - 1, c1 = 0, c2 = matrix[0].length - 1;while (r1 <= r2 && c1 <= c2) {//上for (int i = c1; i <= c2; i++) {//第一行 [r1][i]ret.add(matrix[r1][i]);}//右for (int i = r1 + 1; i <= r2; i++) {ret.add(matrix[i][c2]);}if (r1 != r2) {//下for (int i = c2 - 1; i >= c1; i--) {ret.add(matrix[r2][i]);}}if (c1 != c2) {//左for (int i = r2 - 1; i > r1; i--) {ret.add(matrix[i][r1]);}}r1++;r2--;c1++;c2--;}return ret;}public int[] spiralOrder(int[][] matrix) {ArrayList<Integer> ret = new ArrayList<>();int r1 = 0, r2 = matrix.length - 1, c1 = 0, c2 = matrix[0].length - 1;while (r1 <= r2 && c1 <= c2) {//上for (int i = c1; i <= c2; i++) {//第一行 [r1][i]ret.add(matrix[r1][i]);}//右for (int i = r1 + 1; i <= r2; i++) {ret.add(matrix[i][c2]);}if (r1 != r2) {//下for (int i = c2 - 1; i >= c1; i--) {ret.add(matrix[r2][i]);}}if (c1 != c2) {//左for (int i = r2 - 1; i > r1; i--) {ret.add(matrix[i][r1]);}}r1++;r2--;c1++;c2--;}return ret.stream().mapToInt(Integer::valueOf).toArray();} }done
總結
以上是生活随笔為你收集整理的剑指 Offer打卡 数组与矩阵的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最新版mac使用m1芯片,使用nvm安装
- 下一篇: Mac安装 ohmyzsh发生443错误