leetcode - 01

The number appears only once

Given a non-empty array of integers, in addition to an element appears only once, the rest of each element appears twice . To find out that only appears once in the elements.
Your algorithm should have linear time complexity. You can not use the extra space to achieve it?
Example 1:

输入: [2,2,1]
输出: 1
//1.交换律:a ^ b ^ c <=> a ^ c ^ b
//2.任何数于0异或为任何数 0 ^ n => n
//3.相同的数异或为0: n ^ n => 0
//利用以上三个特性即可解答本题。
class Solution {
    public int singleNumber(int[] nums) {
        int temp=0;
        for(int num:nums){
            temp^=num;
        }
        return temp;
    }
}

Most of the elements

Given an array of size n, the majority of the elements found therein. Most of the elements in the array refers to the number of occurrences greater than ⌊ n / 2 ⌋ elements.
You can assume that the array is non-empty, and there is always most elements of a given array.
Example 1:

输入: [2,2,1,1,1,2,2]
输出: 2
//1.根据题目可知道至少有一个元素得数量是大于其他元素各自的数量。
//2.定义存活元素及存活元素数量、然后遍历数组每次判断下一个元素是否是本元素若是则元素数量加一,否则元素数量减一。若减至零则替换掉存活元素。
//3.经过遍历后由于多数元素数量大于n/2最终遍历结束后存活下来肯定是多数元素。
class Solution {
    public int majorityElement(int[] nums) {
        int tmpNum=nums[0],tmpCount=1;
        for(int i=1;i<nums.length;i++){
            int nowNum=nums[i];
            if(nowNum==tmpNum){
                tmpCount++;
            }else{
                tmpCount--;
            }
            if(tmpCount<=0){
                tmpNum=nowNum;
                tmpCount=1;
            }
        }
        return tmpNum;
    }
}

Search two-dimensional matrix

Write an efficient algorithm to search mxn matrix matrix in a target value target. This matrix has the following characteristics:

  • Elements of each row in ascending order from left to right.
  • Element of each column from top to bottom in ascending order.

Example:

现有矩阵 matrix 如下:
[
  [1,   4,  7, 11, 15],
  [2,   5,  8, 12, 19],
  [3,   6,  9, 16, 22],
  [10, 13, 14, 17, 24],
  [18, 21, 23, 26, 30]
]
给定 target = 5,返回 true。
给定 target = 20,返回 false。
//1.从左上角或者右下角出发遍历。
//2.判断target与选取值的大小关系判断是向左走还是向下走。
//3.找到则直接返回true否则遍历结束返回false。
class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        if(matrix.length<=0){
            return false;
        }
        int row=0;
        int col=matrix[0].length-1;
        while(row<=matrix.length-1&&col>=0){
            int val=matrix[row][col];
            if(target>val){
                row++;
            }else if(target<val){
                col--;
            }else{
                return true;
            }
        }
        return false;
    }
}

Merge two ordered arrays

Given two ordered arrays of integers and nums2 nums1, incorporated into the nums2 nums1 in an ordered array such as a num1.

  • And initializing the number of elements nums1 nums2 of m and n.
  • You can assume nums1 sufficient space (space equal to or greater than m + n) to save the elements of nums2.
    Example:
输入:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6],       n = 3
输出: [1,2,2,3,5,6]
//1.判断nums2中的数是否完全加入到nums1中
//2.判断nums1中是否还有待加入的数
//3.将从nums1和nums2中获取到的数判断大小,取大的数赋值到往nums1尾部逐个添加。
class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int i = m-1, j = n-1, k = m+n-1;
        while(j>=0){
            nums1[k--] = i >= 0 && nums1[i]>nums2[j] ? nums1[i--] : nums2[j--];
        }
    }
}

Guess you like

Origin www.cnblogs.com/cjunn/p/12229333.html
01