【Lintcode】033.N-Queens】的更多相关文章

题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration…
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. Example For n=4, there are 2 distinct solutions. 题解: Solution 1 () class Solution { public: void dfs(int &res, int n, vec…
 二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.int start = 0, end = nums.length - 1.循环结束条件为start + 1 < end ,即相邻时结束循环,所以最后需判断start和end中哪个是目标值.指针变化为start = mid,取后半区间:end = mid,取前半区间. 经典二分搜索:1. First p…
题目: The code base version is an integer start from 1 to n. One day, someone committed a bad version in the code case, so it caused this version and the following versions are all failed in the unit tests. Find the first bad version. You can call isBa…
问题描述: 实现atoi这个函数,将一个字符串转换为整数.如果没有合法的整数,返回0.如果整数超出了32位整数的范围,返回INT_MAX(2147483647)如果是正整数,或者INT_MIN(-2147483648)如果是负整数. 样例 "10" =>10 "-1" => -1 "123123123123123" => 2147483647 "1.0" => 1 问题分析: 这道题特别恶心,虽然思路很…
问题描述: 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母. 样例 给出 A = "ABCD" B = "ACD",返回 true 给出 A = "ABCD" B = "AABC", 返回 false 注意事项 在 A 中出现的 B 字符串里的字符不需要连续或者有序. 问题分析: 实质上利用的是哈希表的思想.只有大写字母,一共26个,遍历A的时候,往里面压,遍历B的时候,往外边弹,如果…
问题分析: 我们通过遍历两个链表拿到每个位的值,两个值加上前一位进位值(0或者1)模10就是该位的值,除以10就是向高位的进位值(0或者1). 由于两个链表可以不一样长,所以要及时判断,一旦为null,该位的值就要变成0. 有一种情况比较特殊,比如:1->1->1->null, 9->8->8->null,最终结果为0->0->0->1->null,需要保留最高位. 而1->1->1->null, 9->8->7-&…
问题分析: 声明当前指针和上一个指针即可. 问题求解: public class Solution { public ListNode removeElements(ListNode head, int val) { ListNode preListNode = new ListNode(0);// 上一个指针 ListNode nowListNode = head;// 当前指针 ListNode returnListNode = null;// 需要返回的链表 preListNode.nex…
问题分析: 用两个指针分别遍历即可. 问题求解: public class Solution { /** * @param nums1 an integer array * @param nums2 an integer array * @return an integer array */ public int[] intersection(int[] nums1, int[] nums2) { List<Integer> list = new ArrayList<Integer>…
问题分析: 既然返回值没有重复,我们不妨将结果放进set中,然后对两个set进行比较. 问题求解: public class Solution { /** * @param nums1 an integer array * @param nums2 an integer array * @return an integer array */ public int[] intersection(int[] nums1, int[] nums2) { Set<Integer> set1 = new…
Half and Half 类型题 二分法的精髓在于判断目标值在前半区间还是后半区间,Half and Half类型难点在不能一次判断,可能需要一次以上的判断条件. Maximum Number in Mountain Sequence Given a mountain sequence of n integers which increase firstly and then decrease, find the mountain top. 样例  Given nums = [1, 2, 4,…
题目描述: Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example: Given "b…
题目: 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). You are given a target value to search. If found in the array return its index, otherwise return -1.…
题目: Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1 x 1, compute how much water it is able to trap after raining. Example Given 5*4 matrix [12,13,0,12] [13,4,13,12] [13,8,10,12] [12,13,12,12] [13,13…
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.   Example Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. 题解: 读题可知,区间[a,b]内容积由边缘最小值决定,Two pointer思想,…
题目: Given an array of integers, how many three numbers can be found in the array, so that we can build an triangle whose three edges length is the three numbers that we find? Example Given array S = [3,4,6,7], return 3. They are: [3,4,6] [3,6,7] [4,6…
题目: For a given source string and a target string, you should output the first index(from 0) of target string in source string. If target does not exist in source, just return -1. Clarification Do I need to implement KMP Algorithm in a real interview…
题目: Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find it. Notice You may assume that the array is non-empty and the majority number always exist in the array. Have you met this que…
题目: 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). Example Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 return its bottom-up level order tr…
题目: Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2. Example For s1 = "aabcc", s2 = "dbbca" When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false.…
题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relat…
题目: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Clarification What's the definition of Longest Common Subsequence? https://en.wikipedia.org/wiki/Longest_common_subsequence_problem http://ba…
题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: Insert a character Delete a character Replace a…
题目: Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return the length of the LIS. Clarification What's the definition of longest increasing subsequence? The longest increasing subsequence problem is to fin…
题目: Count the number of k's between 0 and n. k can be 0 - 9. Example if n = 12, k = 1 in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] we have FIVE 1's (1, 10, 11, 12) 题解: Solution 1 () class Solution { public: int digitCounts(int k, int n) { ) { ; } ;…
题目: Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary  Example Given:start…
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination.  Example Given candidate set [10,1,6,7,2,1,5]…
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Example Given candidate set [2,3,6,7] …
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. Example Given s = "aab", return: [ ["aa","b"], ["a","a","…
题目: 题解: Solution 1 () class Solution { public: vector<vector<int> > subsets(vector<int> &S) { vector<vector<int> > res{{}}; sort(S.begin(), S.end()); ; i < S.size(); ++i) { int size = res.size(); ; j < size; ++j) {…