Leetcode题解(24)

73. Set Matrix Zeroes

分析:如果没有空间限制,这道题就很简单,但是要求空间复杂度为O(1),因此需要一些技巧。代码如下(copy网上的代码)

class Solution {
public:
    void setZeroes(vector<vector<int> > &matrix)
{
    bool bColZero = false, bRowZero = false;

    if (matrix.size() == 0 || matrix[0].size() == 0)
    {
        return;
    }

    // Mark bColZero true when col[0] contains zero.
    for (size_t row = 0; row < matrix.size(); ++row)
    {
        if (!matrix[row][0]) bColZero = true;
    }

    // Mark bRowZero true when row[0] contains zero.
    for (size_t col = 0; col < matrix[0].size(); ++col)
    {
        if (!matrix[0][col]) bRowZero = true;
    }

    // Map zero points to row[0] and col[0].
    for (size_t row = 1; row < matrix.size(); ++row)
    {
        for (size_t col = 1; col < matrix[row].size(); ++col)
        {
            if (!matrix[row][col])
            {
                matrix[0][col] = 0;
                matrix[row][0] = 0;
            }
        }
    }

    // Set zero according to row[0] and col[0].
    for (size_t row = 1; row < matrix.size(); ++row)
    {
        for (size_t col = 1; col < matrix[row].size(); ++col)
        {
           if (!matrix[row][0] || !matrix[0][col])
           {
               matrix[row][col] = 0;
           }
        }
    }

    // Process col[0].
    if (bColZero)
    {
        for (size_t row = 0; row < matrix.size(); ++row)
        {
            matrix[row][0] = 0;
        }
    }

    // Process row[0].
    if (bRowZero)
    {
        for (size_t col = 0; col < matrix[0].size(); ++col)
        {
            matrix[0][col] = 0;
        }
    }
}
};

------------------------------------------------------------------------------分割线-------------------------------------------------------------------

74. Search a 2D Matrix

题目

分析,这道题目在《剑指offer》上出现过,思想是分段查找,只是查找的起点是右上角的元素,代码如下:

 1 class Solution {
 2 public:
 3     bool searchMatrix(vector<vector<int> > &matrix, int target) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         int i = 0, j = matrix[0].size() - 1;
 7
 8         while (i < matrix.size() && j >= 0)
 9         {
10             if (target == matrix[i][j])
11                 return true;
12             else if (target < matrix[i][j])
13                 j--;
14             else
15                 i++;
16         }
17
18         return false;
19     }
20 };

--------------------------------------------------------------------分割线------------------------------------------------------------------------------

75. Sort Colors

题目

分析:简单题目,可以直接统计0,1,2的个数,然后赋值即可

代码如下:

class Solution {
public:
    void sortColors(vector<int>& nums) {
        int size = nums.size();
        int zero=0,one=0,two = 0;
        int i,j;
        for(i=0;i<size;i++)
        {
            if(0 == nums[i])
                zero++;
            else if(1 == nums[i])
                one++;
            else
                two++;
        }
        i=0;
        j=0;
        for(j=0;j<zero;j++)
            nums[i++]=0;
        for(j=0;j<one;j++)
            nums[i++]=1;
        for(j=0;j<two;j++)
            nums[i++]=2;

    }
};
时间: 2024-10-29 19:10:39

Leetcode题解(24)的相关文章

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)

题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)二叉树可空 2)思路:a.根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(

leetcode题解:Valid Palindrome(判断回文)

题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider tha

leetcode题解:Valid Parentheses(栈的应用-括号匹配)

题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&

leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)实现与根据先序和中序遍历构造二叉树相似,题目参考请进 算法思想 中序序列:C.B.E.D.F.A.H.G.J.I 后序序列:C.E.F.D.B.H.J.I.G.A 递归思路: 根据后序遍历的特点,知道后序

leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)

题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order tr

leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] co