题目: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 duplicate exists in the array.Tag:Array; Binary Search体会:这道题和Find the Minimum In Rotated Sorted Array肯定是有联系啦,毕竟给的都是Rotated Sorted Ar...
题目:A city‘s skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B). The geometric information of each building is represented by a t...
题目:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectivelyin the grid.For example,There is one obstacle in the middle of a 3x3 grid as illustrated below.[[0,0,0],[0,1,0],[0,0,0] ] The total number of unique paths is 2.Note: m and n will be at most 100.思路:这道题和Leet...
第k个排列给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列。按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下:"123""132""213""231""312""321"给定 n 和 k,返回第 k 个排列。说明:给定 n 的范围是 [1, 9]。给定 k 的范围是[1, n!]。示例 1:输入: n = 3, k = 3输出: "213"示例 2: 输入: n = 4, k = 9输出: "2314" 思路:观察一下: 我们取k=17。首先数组都是从0开始计数的,所以k-1=16。从16开始,先可以...
class Solution { public:vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {if(nums.empty() || nums[0].empty()){return {};}auto h = nums.size();auto w = nums[0].size();if(h*w != r*c){return nums;}vector<vector<int>> res(r, vector<int>(c, 0));for(int i=0;i<r;i++){for(int j=0;j<c;j++){res[i][j] = nums[(i*c + j)/w][(i*c + j)%w];}}return res;} };原文:https://www.cnblogs.com/th...
题目: Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.Example 1: Input: [1,4,3,2]Output: 4 Explanation: n is 2, and the maximum sum of pairs is 4. Note: n is a positive integer, which is in the range of [1, 10000]. All the integers in the arra...
题目描述:给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)例如:给定二叉树 [3,9,20,null,null,15,7], 3/ 9 20/ 15 7 返回其自底向上的层次遍历为:[[15,7],[9,20],[3] ]/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NU...
Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times.You may assume that the array is non-empty and the majority element always exist in the array.个人博客:http://www.cnblogs.com/wdfwolf3/。题目比较好理解,在含有n个元素的数组中找出出现次数超过?n/2?的元素,假设数组不为空而且这个数是一定存在的。1.moore-voting算法这个算法就是为...
给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。说明:解集不能包含重复的子集。示例:输入: nums = [1,2,3]输出:[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]class Solution { public:vector<vector<int>> subsets(vector<int>& nums) {vector<vector<int> > res(1);sort(nums.begin(), nums.end());for (int i = 0; i < nums.size(); ++i) {int size = res.size();for (int j = 0; j <...
题目:Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).Example 1:Input: [1,3,5,4,7] Output: 3 Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3. Even though [1,3,5,7] is also an increasing subsequence, it‘s not a continuous one where 5 and 7 are separated by 4. Example 2:Input: [2,2,2,2,2] Output: ...
Problem: Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0Solution:二分查找,当找不到时l=r+1,所以根据最后一次l和r的变动来判定应该插入的位置,如果最后一次是l=mid+...
题目:A robot is located at the top-left corner of a m x n grid (marked ‘Start‘ in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish‘ in the diagram below).How many possible unique paths are there?Above is a 7 x 3 grid. How many possible unique paths are there?Note: m and n will...
题目: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,null,null,15,7], 3/ 9 20/ 15 7 return its level order traversal as:[[3],[9,20],[15,7] ]分析:给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。可以立刻想到两种方法,也就是BFS和DFS。先说BFS,BFS实际...
题目:给定一个二叉树,检查它是否是镜像对称的。例如,二叉树 [1,2,2,3,4,4,3] 是对称的。1/22/ \ /3443 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:1/22\ 33链接:https://leetcode-cn.com/problems/symmetric-tree最开始的思路是用层次遍历,每一行看看是不是回文数组,但后来发现例如样例2那样的树就会判断错误,递归也没想好怎么个调用思路。 正确的递归思路:来源:https://leetcode-cn.com/u/haventmetyo...
问题: 难度:easy 说明: 感觉不像是 leetcode 的 easy 题,题目实际要求其实是: 输入给出一个 String[] ,日志里面都是 空格字符隔开,只有 小写字母和 数字,空格隔开各个字符串都是 先小写字母 + 后数字, 然后 String[i] 第一个 空格前面的字符串 作为符号,将 符号后面的所有 空格以及字母字符 按照 ASCII码表排序,而将所有数字字符 的 排序认为比小写字母还要后,相当于 ASCII 码表里面,数字的码表值比 小写字母大: ...