leetcode N刷匯總151-200題

  1. Find Minimum in Rotated Sorted Array
    Suppose an array sorted in ascending order 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]).Find the minimum element.You may assume no duplicate exists in the array.
#include <bits/stdc++.h>
using namespace std;
/**
 * 原題鏈接
 * https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/
 */
class Solution {
  public:
    int findMin(vector<int> &rotateArray) {
        int left = 0, right = rotateArray.size() - 1;
        while (left < right) {
            int mid = (left + right) / 2;
            if (rotateArray[mid] < rotateArray[right]) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return rotateArray[left];
    }
};
  1. Find Minimum in Rotated Sorted Array II
    Suppose an array sorted in ascending order 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]).Find the minimum element.The array may contain duplicates.
#include <bits/stdc++.h>
using namespace std;
/**
 * 原題鏈接
 * https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description/
 */
class Solution {
  public:
    int findMin(vector<int> &nums) {
        int m = nums.size();
        int left = 0, right = m - 1;
        while (left < right) {
            int middle = (left + right) / 2;
            if (nums[middle] < nums[right]) {
                right = middle;
            } else if (nums[middle] > nums[right]) {
                left = middle + 1;
            } else {
                right--;
            }
        }
        return nums[left];
    }
};
  1. Largest Number
    Given a list of non negative integers, arrange them such that they form the largest number.
#include <bits/stdc++.h>
using namespace std;
/**
 * 原題鏈接
 * https://leetcode.com/problems/largest-number/description/
 *
 */
class Solution {
  public:
    string largestNumber(vector<int> &nums) {
        sort(nums.begin(), nums.end(), [](int a, int b) {
            string stra = to_string(a), strb = to_string(b);
            return stra + strb > strb + stra;
        });
        int m = nums.size();
        string res;
        for (int i = 0; i < m; i++) {
            res += to_string(nums[i]);
        }
        while (res[0] == '0' && res.size() > 1)
            res.erase(0, 1);
        return res;
    }
};
  1. Repeated DNA Sequences
    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.
#include <bits/stdc++.h>
using namespace std;

/**
 * 原題鏈接
 * https://leetcode.com/problems/repeated-dna-sequences/description/
 */
class Solution {
  public:
    vector<string> findRepeatedDnaSequences(string s) {
        int m = s.size();
        set<string> dir1, dir2;
        vector<string> res;
        for (int i = 0; i < m - 9; i++) {
            string str = s.substr(i, 10);
            if (!dir1.insert(str).second && dir2.insert(str).second) {
                res.push_back(str);
            }
        }
        return res;
    }
};
  1. Rotate Array
    Given an array, rotate the array to the right by k steps, where k is non-negative.
#include <bits/stdc++.h>
using namespace std;
/**
 * 原題鏈接
 * https://leetcode.com/problems/rotate-array/discuss/
 */
class Solution {
  public:
    void rotate(vector<int> &nums, int k) {
        int m = nums.size();
        k = k % m;
        reverse(nums.begin(), nums.begin() + m - k);
        reverse(nums.begin() + m - k, nums.end());
        reverse(nums.begin(), nums.end());
    }
};
  1. Reverse Bits
    Reverse bits of a given 32 bits unsigned integer.
#include <bits/stdc++.h>
using namespace std;
/**
 * 原題鏈接
 * https://leetcode.com/problems/reverse-bits/description/
 */

class Solution {
  public:
    uint32_t reverseBits(uint32_t n) {
        uint32_t res = 0;
        for (int i = 0; i < 32; i++, n >>= 1) {
            res <<= 1;
            res |= (n & 1);
        }
        return res;
    }
};
  1. Number of 1 Bits
    Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).
#include <bits/stdc++.h>
using namespace std;
/**
 * 原題鏈接
 * https://leetcode.com/problems/number-of-1-bits/description/
 */
class Solution {
  public:
    int hammingWeight(uint32_t n) {
        int res = 0;
        while (n) {
            n = n & (n - 1);
            res++;
        }
        return res;
    }
};
  1. House Robber
    You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
    Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
#include <bits/stdc++.h>
using namespace std;
/**
 * 原題鏈接
 * https://leetcode.com/problems/house-robber/description/
 */
class Solution {
  public:
    int rob(vector<int> &nums) {
        int m = nums.size();
        int pre = 0, cur = 0;
        for (int i = 0; i < m; i++) {
            int temp = cur;
            cur = max(cur, pre + nums[i]);
            pre = temp;
        }
        return cur;
    }
};
  1. Binary Tree Right Side View
    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
#include <bits/stdc++.h>
using namespace std;
/**
 * 原題鏈接
 * https://leetcode.com/problems/binary-tree-right-side-view/description/
 */

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
  public:
    vector<int> rightSideView(TreeNode *root) {
        vector<int> res;
        helper(root, 0, res);
        return res;
    }
    void helper(TreeNode *root, int level, vector<int> &res) {
        if (root == NULL)
            return;
        if (res.size() == level)
            res.push_back(root->val);
        helper(root->right, level + 1, res);
        helper(root->left, level + 1, res);
    }
};
  1. Number of Islands
    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
#include <bits/stdc++.h>
using namespace std;
/**
 * 原題鏈接
 * https://leetcode.com/problems/number-of-islands/description/
 */
class Solution {
  public:
    int numIslands(vector<vector<char>> &grid) {
        if (grid.size() == 0)
            return 0;
        int m = grid.size(), n = grid[0].size(), res = 0;
        vector<vector<bool>> visit(m, vector<bool>(n, false));
        vector<vector<int>> direction = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (!visit[i][j] && grid[i][j] == '1') {
                    helper(i, j, visit, direction, grid);
                    res++;
                }
            }
        }
        return res;
    }
    void helper(int row, int col, vector<vector<bool>> &visit,
                vector<vector<int>> &direction, vector<vector<char>> &grid) {
        visit[row][col] = true;
        for (int i = 0; i < 4; i++) {
            int newRow = row + direction[i][0];
            int newCol = col + direction[i][1];
            if (newRow >= 0 && newRow < visit.size() && newCol >= 0 &&
                newCol < visit[0].size() && !visit[newRow][newCol] &&
                grid[newRow][newCol] == '1') {
                helper(newRow, newCol, visit, direction, grid);
            }
        }
    }
};
最后編輯于
?著作權(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)容