leetcode-探索数组

1. 寻找数组的中心索引
/*
执行用时:1 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:40.5 MB, 在所有 Java 提交中击败了28.57%的用户
*/
 public int pivotIndex(int[] nums) {
    
    
        int sum=0,left = 0;
        for (int i=0;i<nums.length;i++){
    
    
            sum+=nums[i];
            }
        for (int i = 0;i<nums.length;i++){
    
    
            //判断中心索引前面数之和是否其后面数之和
            if (left==sum-left-nums[i]){
    
    
                return i;
            }
            left+=nums[i];
        }
        return -1;
    }
2.搜索插入位置
/*
二分搜索
执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:39.3 MB, 在所有 Java 提交中击败了5.55%的用户
*/
class Solution {
    
    
    public int searchInsert(int[] nums, int target) {
    
    
        int begin = 0, end = nums.length-1;
        int mid=0;
        while (begin<=end){
    
    
            mid = (begin+end)/2;
            if (nums[mid]==target) return mid;
            else if(nums[mid]>target) end = mid-1;
            else begin = mid+1;
        }
        return nums[mid]<target?mid+1:mid;
    }
}

3.合并区间

在这里插入图片描述


/*
借鉴大神 甜姨的力扣题解。
执行用时:9 ms, 在所有 Java 提交中击败了47.52%的用户
内存消耗:42.1 MB, 在所有 Java 提交中击败了42.46%的用户
*/
class Solution {
    
    
      public int[][] merge(int[][] intervals) {
    
    
        Arrays.sort(intervals,(v1,v2)->v1[0]==v2[0]?v1[1]-v2[1]:v1[0]-v2[0]);
        int [][] ans = new int[intervals.length][2];
        int count = -1;
        for (int[] temp :intervals){
    
    
        	//当首次插入或者temp左区间取值比前面值的右区间取值大
            if (count==-1||ans[count][1]<temp[0]){
    
    
                ans[++count] = temp;
            } else {
    
    
            //两个区间有交集,取取值比较大的右区间
                ans[count][1] = Math.max(ans[count][1],temp[1]);
            }
        }
        return Arrays.copyOf(ans, count+1);
    }
}
4.零矩阵
/*
暴力搜索,用栈存储数组中值为0的位置点,随后取出并将其行列变为0
执行用时:3 ms, 在所有 Java 提交中击败了20.12%的用户
内存消耗:41.3 MB,在所有 Java 提交中击败了100.00%的用户
*/
class Solution {
    
    
public void setZeroes(int[][] matrix) {
    
    
        int len = matrix.length, len2 = matrix[0].length;
        Stack<Integer> stack = new Stack<>();
        int count = 0;
        for (int i=0;i<len;i++){
    
    
            for (int j =0;j<len2;j++){
    
    
                if (matrix[i][j]==0){
    
    
                    stack.push(i*len2+j);
                    count++;
                }

            }
        }
        while (!stack.isEmpty()){
    
    
            int i = stack.pop();
            
                for (int j=0;j<len2;j++){
    
    
                    matrix[i/len2][j]=0;
                }
                for (int k =0;k<len;k++){
    
    
                    matrix[k][i%len2]=0;
                }

            }
        }
}


/*
评论区大佬的双百做法。
执行用时:1 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:41.4 MB, 在所有 Java 提交中击败了100.00%的用户
*/
class Solution {
    
    
public void setZeroes(int[][] matrix) {
    
    
        boolean[] line = new boolean[matrix.length];
        boolean[] column = new boolean[matrix[0].length];
        // 找出要清零的行列
        for (int i = 0; i < matrix.length; i++) {
    
    
            for (int j = 0; j < matrix[0].length; j++) {
    
    
                if (matrix[i][j] == 0) {
    
    
                    line[i] = true;
                    column[j] = true;
                }
            }
        }
        
        // 开始对行清零
        for (int i = 0; i < matrix.length; i++) {
    
    
            if (line[i]) {
    
    
                for (int j = 0; j < matrix[0].length; j++) {
    
    
                    matrix[i][j] = 0;
                }
            }
        }

        // 开始对列清零
        for (int i = 0; i < matrix[0].length; i++) {
    
    
            if (column[i]) {
    
    
                for (int j = 0; j < matrix.length; j++) {
    
    
                    matrix[j][i] = 0;
                }
            }
        }

        }
}
5.旋转矩阵
/*
看了题解的思路。
执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:39.9 MB, 在所有 Java 提交中击败了100.00%的用户
*/
class Solution {
    
    
    public void rotate(int[][] matrix) {
    
    
        //主对角线进行翻转
        for(int i =1;i<matrix.length;i++){
    
    
            for (int j =0;j<i;j++){
    
    
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i]=temp;
            }
        }
        //对于中心线进行翻转
        for (int i=0;i<matrix.length;i++){
    
    
            for (int j=0;j<matrix[0].length/2;j++){
    
    
                int temp = matrix[i][j];
                matrix[i][j] = matrix[i][matrix[0].length-j-1];
                matrix[i][matrix[0].length-j-1]= temp;
            }
        }
    }
}
6.反转字符串
/*
执行用时:1 ms, 在所有 Java 提交中击败了99.89%的用户
内存消耗:46.4 MB, 在所有 Java 提交中击败了94.55%的用户
*/
class Solution {
    
    
    public void reverseString(char[] s) {
    
    
        int i = 0, j =s.length-1;
        while (i<j){
    
    
            char temp = s[i];
            s[i] = s[j];
            s[j]= temp;
            i++;j--;
        }
    }
}
7.数组拆分 I
/*
简单排个序,取偶数下标的值。
执行用时:13 ms, 在所有 Java 提交中击败了95.76%的用户
内存消耗:41.8 MB, 在所有 Java 提交中击败了8.33%的用户
*/
class Solution {
    
    
   public int arrayPairSum(int[] nums) {
    
    
        Arrays.sort(nums);
        int sum = 0;
        for (int i=0;i<nums.length;i+=2){
    
    
            sum += nums[i];
        }
        return sum;
    }
}
8. 两数之和 II - 输入有序数组
/*
执行用时:2 ms, 在所有 Java 提交中击败了31.91%的用户
内存消耗:39.9 MB, 在所有 Java 提交中击败了6.67%的用户
*/
class Solution {
    
    
       public int[] twoSum(int[] nums, int target) {
    
    
            HashMap<Integer,Integer> hashMap = new HashMap<Integer, Integer>();
            int i ;
            for(i=0;i<nums.length;i++){
    
    
                if(hashMap.containsKey(nums[i])){
    
    
                    return new int[]{
    
    hashMap.get(nums[i])+1,i+1};
                }
//              把值放入hashMap中
                hashMap.put(target-nums[i],i);
            }
            return null;
            }
}
9.最大连续1的个数
/*
菜鸡哭泣
执行用时:277 ms, 在所有 Java 提交中击败了5.18%的用户
内存消耗:41.6 MB, 在所有 Java 提交中击败了5.26%的用户
*/
class Solution {
    
    
    public int findMaxConsecutiveOnes(int[] nums) {
    
    
        if (nums.length==0||nums == null){
    
    
            return 0;
        }
        int count = 0;
        for (int i=0;i<nums.length;i++){
    
    
            if (nums[i]==1){
    
    
                int temp = judge(nums,i);
                if (count<temp){
    
    
                    count = temp;
                }
            }
        }
        return count;
    }

    private int judge(int[] nums, int i) {
    
    
        int count = 0;
        while (i<nums.length&&nums[i]==1){
    
    
            count ++;
            i++;
        }
        return count;
    }
}


/*
我魔怔了,根本不需要挨个判断从每个元素开始的情况,如果两个1是连续的话,后面的1肯定比前面的1短,只需要从刚开始为1的地方开始计数就好了 
---官方解答

执行用时:2 ms, 在所有 Java 提交中击败了95.20%的用户
内存消耗:41.3 MB, 在所有 Java 提交中击败了5.26%的用户
*/
class Solution {
    
    
  public int findMaxConsecutiveOnes(int[] nums) {
    
    
    int count = 0;
    int maxCount = 0;
    for(int i = 0; i < nums.length; i++) {
    
    
      if(nums[i] == 1) {
    
    
        // Increment the count of 1's by one.
        count += 1;
      } else {
    
    
        // Find the maximum till now.
        maxCount = Math.max(maxCount, count);
        // Reset count of 1.
        count = 0;
      }
    }
    return Math.max(maxCount, count);
  }
}
10.移除元素
/*
执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:38 MB, 在所有 Java 提交中击败了5.68%的用户
*/
class Solution {
    
    
    public int removeElement(int[] nums, int val) {
    
    
        int i =0 ;
        int j;
        for (j=0;j<nums.length;j++){
    
    
            if (nums[j]!=val){
    
    
                nums[i]=nums[j];
                i++;
            }
        }
        return i;
    }
}
11.长度最小的子数组
/*
执行用时:2 ms, 在所有 Java 提交中击败了86.15%的用户
内存消耗:39.9 MB, 在所有 Java 提交中击败了6.67%的用户
*/
class Solution {
    
    
    public int minSubArrayLen(int s, int[] nums) {
    
    
        if (nums.length==0) return 0;
        int begin = 0,end = 0;
        int ans = Integer.MAX_VALUE;
        int sum = 0;
        while (end<nums.length){
    
    
            sum+=nums[end];
            while (sum>=s){
    
    
                ans = Math.min(ans, end-begin+1);
                sum-=nums[begin];
                begin++;
            }
            end++;
        }
        return ans==Integer.MAX_VALUE?0:ans;
    }
}
12.杨辉三角
/*
课本上的知识:将三角两边全部置为1,剩下的为上面两个值的和

执行用时:1 ms, 在所有 Java 提交中击败了77.51%的用户
内存消耗:37.2 MB, 在所有 Java 提交中击败了9.09%的用户
*/
class Solution {
    
    
    public List<List<Integer>> generate(int numRows) {
    
    
        List<List<Integer>> ans = new ArrayList<>();
        for (int i = 0 ;i<numRows;i++){
    
    
            ArrayList<Integer> sub=new ArrayList<Integer>();
            for (int j=0 ;j<=i;j++){
    
    
                if (j==0||j==i) sub.add(1);
                else sub.add(ans.get(i-1).get(j-1)+ans.get(i-1).get(j));
            }
            ans.add(sub);
        }
        return ans;
    }
}
13.杨辉三角Ⅱ
/*
执行用时:2 ms, 在所有 Java 提交中击败了49.62%的用户
内存消耗:37.2 MB, 在所有 Java 提交中击败了5.88%的用户
*/
class Solution {
    
    
    public List<Integer> getRow(int rowIndex) {
    
    
        List<List<Integer>> ans = new ArrayList<>();
        for (int i = 0 ;i<rowIndex;i++){
    
    
            ArrayList<Integer> sub=new ArrayList<Integer>();
            for (int j=0 ;j<=i;j++){
    
    
                if (j==0||j==i) sub.add(1);
                else sub.add(ans.get(i-1).get(j-1)+ans.get(i-1).get(j));
            }
            ans.add(sub);
        }
        return ans.get(rowIndex-1);
    }
}
14.寻找旋转排序数组中的最小值
/*
暴力搜索
执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:39.6 MB, 在所有 Java 提交中击败了5.55%的用户
*/
class Solution {
    
    
    public int findMin(int[] nums) {
    
    
        for (int i = 0; i < nums.length - 1; i++) {
    
    
            if (nums[i + 1] < nums[i]) return nums[i + 1];
        }
        return nums[0];
    }
}
15.删除排序数组中的重复项
/*
双指针
执行用时:1 ms, 在所有 Java 提交中击败了98.02%的用户
内存消耗:41.8 MB, 在所有 Java 提交中击败了5.74%的用户
*/
class Solution {
    
    
    public int removeDuplicates(int[] nums) {
    
    
        int j = 0 ;
        for (int i = 1;i<nums.length;i++){
    
    
            if (nums[i]!=nums[j]){
    
    
                nums[++j] = nums[i];
                
            }
        }
        return j+1;
    }
}
16.移动零
/*
双指针
执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:40.1 MB, 在所有 Java 提交中击败了5.62%的用户
*/
class Solution {
    
    
    public void moveZeroes(int[] nums) {
    
    
        int i = 0 ,count = 0;
        for (int j = 0;j<nums.length;j++){
    
    
            if (nums[j]!=0) nums[i++] = nums[j];
            else count++;
        }
        for (int j = nums.length-1;j>nums.length-1-count;j--){
    
    
            nums[j] = 0;
        }
    }
}
17.最长回文子串
/*
双指针
执行用时:42 ms, 在所有 Java 提交中击败了63.67%的用户
内存消耗:38.3 MB, 在所有 Java 提交中击败了24.10%的用户
*/
class Solution {
    
    
    public String longestPalindrome(String s) {
    
    
        if (s == null || s.length() < 1) return "";
        int res = 0;
        int begin=0 ,end=0 ;
        for (int i = 0;i<s.length();i++){
    
    
            int len1 = judge(i,i,s);
            int len2 = judge(i,i+1,s);
             int temp = Math.max(len1, len2);
             if (res<temp){
    
    
                 begin = i-(temp-1)/2;
                 end = i+temp/2;
                 res = temp;
             }
        }
        return s.substring(begin, end+1);
    }

    private int judge(int begin, int end, String s) {
    
    
        while (begin>=0&&end<=s.length()-1&&s.charAt(begin)==s.charAt(end)){
    
    
            begin--;end++;
        }
        return end-begin-1;
    }
}
18.反转字符串中的单词 III
/*
执行用时:4 ms, 在所有 Java 提交中击败了93.26%的用户
内存消耗:40.3 MB, 在所有 Java 提交中击败了5.00%的用户
*/
class Solution {
    
    
    public String reverseWords(String s) {
    
    
        char[] res = s.toCharArray();
        int i = 0,j = 0;
        while (j<s.length()){
    
    
            while (j<s.length()&&res[j]!=' ') j++;
            reverse(i, j-1,res);
            i = j+1;
            j = i;
        }
        return  new String(res);

    }

    private void reverse(int begin, int end, char[] s) {
    
    
        while (begin<end){
    
    
            char temp = s[begin];
            s[begin++] = s[end];
            s[end--] = temp;
        }
    }
}


/*
无情api杀手
执行用时:8 ms, 在所有 Java 提交中击败了48.55%的用户
内存消耗:40.6 MB, 在所有 Java 提交中击败了5.00%的用户
*/
class Solution {
    
    
    public String reverseWords(String s) {
    
    
        String[] words = s.split(" ");
        StringBuilder res = new StringBuilder();
        for (String word:words){
    
    
            res.append(new StringBuffer(word).reverse()+" ");
        }
        return res.toString().trim();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41458842/article/details/107325433