class Solution {
public:int totalNQueens(int n) {//initialize chessboardvector<vector<bool>> boardconf;vector<bool> orig_row;orig_row.assign(n,false);boardconf.assign(n,orig_row);int totalNum = 0;for(int j = 0; j < n; j++){totalNum+= NQueens(boardconf,0,j,n);}return totalNum;}int NQueens(vector<vector<bool>> cb, int i, int j,int n){if(i == n-1){if(not_attack(cb,i,j,n)) return1;elsereturn0;}else{if...
Problem:
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2...
题目这里有 n 个航班,它们分别从 1 到 n 进行编号。有一份航班预订表 bookings ,表中第 i 条预订记录 \(bookings[i] = [first_i, last_i, seats_i]\) 意味着在从 \(first_i\) 到 \(last_i\) (包含 \(first_i\) 和 \(last_i\))的 每个航班 上预订了 \(seats_i\) 个座位。请你返回一个长度为 n 的数组 answer,其中 answer[i] 是航班 i 上预订的座位总数。示例 1:输入:bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
输出:[...
题目:
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array A = [1,1,1,2,2,3],
Your function should return length = 5, and A is now [1,1,2,2,3].
思路:
可以借鉴Remove Duplicates from Sorted Array的两个指针移动的方法。不过需要考虑一些细节。比如,我们需要将小于3个的重复数字都不断往前赋值过去,超过3个才只移动point指针,向前搜索不等的数字。找...
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)
Github: https://github.com/illuz/leetcode035. Search for a Range (Medium) 链接:题目:https://leetcode.com/problems/search-for-a-range/
代码(github):https://github.com/illuz/leetcode题意:在有序数组中找到一个数的范围。(由于数有反复)分析:还是二分搜索变形。(C++)直接用 C++ STL 的 lower_bound 和 upper_bound 偷懒。(Java)直接从普通的二分改一下...
题目:Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Update (2014-11-02):The signature of the function had been updated to return the index instead of the pointer. If you still see your function signature returns a char * or String, please click the reload button to reset your code definition.Tag:String; Two Pointers体会...
题目:
Given a binary tree, return the zigzag level order traversal of its nodes‘ values. (ie, from left to right, then right to left for the next level and alternate between).
For example:
Given binary tree {3,9,20,#,#,15,7}, 3/ 9 20/ 15 7return its zigzag level order traversal as:[[3],[20,9],[15,7]
]confused what "{1,#,2,3}" means? ' ref='nofollow'>> read more on how binary tree is ...
1.题目大意A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.For example, these are arithmetic sequence:1, 3, 5, 7, 9
7, 7, 7, 7
3, -1, -5, -9The following sequence is not arithmetic.1, 1, 2, 5, 7A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of intege...
题目: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...