LeetCode 【1】 Two Sum --001】的更多相关文章

5月箴言 住进布达拉宫,我是雪域最大的王.流浪在拉萨街头,我是世间最美的情郎.—— 仓央嘉措 从本周起每周研究一个算法,并以swift实现之 001 -- Two Sum (两数之和) 题干英文版: Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have ex…
[L4]N-Queens 解题报告 N-Queens Total Accepted: 16418 Total Submissions: 63309 My Submissions 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 so…
[题意]对于n个数,找出一些数使得它们的和能被n整除,输出任意一组方案,n<=10^6. [算法]构造/结论 [题解]引用自:http://www.cnblogs.com/Sakits/p/7407103.html by Sakits 对n个数求前缀和,即sum[i]=sigma(a[1~i])%n,若sum[i]=0则找到答案,否则n个前缀和填充1~n-1,根据抽屉原理必有两个前缀和相同,则中间那一段数字之和是n的倍数. #include<cstdio> ],a[]; int main…
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. Note: All numbers (including target) will be posi…
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 解题: 实用递归的方法,题目很简单,每深…
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1 return true…
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. Note: All numbers (including target) will…
Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that…
六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123Output: 321 Example 2: Input: -123Output: -321 Example 3: Input: 120Output: 21 Note:Assum…
Combination Sum 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. Note: All numbers (includi…
典型的深搜,剪枝的时候需要跳过曾经搜索过的相同的数目,既满足nums[i]=nums[i-1]&&visit[i-1]==0,visit[i-1]==0可以说明该点已经测试过. #include <stdio.h> #include <string.h> #define MAXNUM 1005 int nums[MAXNUM]; int visit[MAXNUM]; int t, n; void output() { ; ; i<t; ++i) { if (j…
题解:发现可以通过枚举区间将区间和相同的元组记录在一个表中,对于答案来说,在同一个表中的元组的选择才会对答案产生贡献.发现每一个表中都是一个个区间,问题转化成了对于每一个表来说,选择若干个不相交的区间,使得选择出来的区间数量最多,直接贪心即可. 代码如下 #include <bits/stdc++.h> using namespace std; typedef pair<int,int> P; const int maxn=1501; int n,a[maxn],sum[maxn]…
题目大意:给定一个 N 个值组成的序列,求序列中区间和相同的不相交区间段数量的最大值. 题解:设 \(dp[i][j]\) 表示到区间 [i,j] 时,与区间 [i,j] 的区间和相同的不相交区间数量是多少.可以枚举之前的区间进行状态转移,时间复杂度为 \(O(n^4)\). 代码如下 #include <bits/stdc++.h> using namespace std; const int maxn=51; int n,ans,x,y,a[maxn],sum[maxn],dp[maxn]…
题目大意:给定一个 N 个点的序列,求有多少个区间满足\(\oplus_{i=l}^ra[i]=\sum\limits_{i=l}^ra[i]\). 题解: 小结论:\(a\oplus b=a+b\rightarrow a\&b=0\). 对每个点来说,考虑向右延伸能够满足条件的右端点的位置,显然右端点的位置单调不减,双指针扫一遍即可. 代码如下 #include <cstdio> using namespace std; const int maxn=2e5+10; int n,a[…
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 思路: 递归求解,只是要保存当前…
Description 前缀和(prefix sum)\(S_i=\sum_{k=1}^i a_i\). 前前缀和(preprefix sum) 则把\(S_i\)作为原序列再进行前缀和.记再次求得前缀和第i个是\(SS_i\) 给一个长度n的序列\(a_1, a_2, \cdots, a_n\)有两种操作: Modify i x:把\(a_i\)改成\(x\): Query i:查询\(SS_i\) Input 第一行给出两个整数N,M.分别表示序列长度和操作个数 接下来一行有N个数,即给定的…
3155: Preprefix sum 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3155 把给出的a_i当成查分数组d_i做就可以了. XJB搞一搞就过了. code: #include <iostream> #include <cstdio> using namespace std; #define int long long const int wx=200017; inline int read(){ int…
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [], [,4], [6,,7], [4,,8,3] ] The minimum path sum from top to bottom is 11 (i…
problem 1022. Sum of Root To Leaf Binary Numbers 参考 1. Leetcode_easy_1022. Sum of Root To Leaf Binary Numbers; 完…
problem 985. Sum of Even Numbers After Queries class Solution { public: vector<int> sumEvenAfterQueries(vector<int>& A, vector<vector<int>>& queries) { vector<int> res; ; ==) sum +=a; for(auto query:queries) { ]]%== )…
problem 633. Sum of Square Numbers 题意: solution1: 可以从c的平方根,注意即使c不是平方数,也会返回一个整型数.然后我们判断如果 i*i 等于c,说明c就是个平方数,只要再凑个0,就是两个平方数之和,返回 true:如果不等于的话,那么算出差值 c - i*i,如果这个差值也是平方数的话,返回 true.遍历结束后返回 false, class Solution { public: bool judgeSquareSum(int c) { ; --…
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……sigh) //HDOJ 3415 #include<queue> #include<cmath> #include<vector> #include<cstdio> #include<cstring> #include<cstdlib>…
数学题.f(n) = 2^(n-1) mod (1e9+7). #include <cstdio> #define MAXN 100005 char buf[MAXN]; __int64 phi = 1e9+; __int64 mod = 1e9+; __int64 power2(__int64 n) { __int64 ret = , ; --n; while (n) { ) ret = ret * base % mod; base = base*base%mod; n >>=…
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id. +----+------------------+ | Id | Email | +----+------------------+ | 1 | john@example.com | | 2 | bob@example.com |…
题目描述: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the ith round, you toggle e…
题目大意 有一个序列 \(a_1,a_2,\ldots,a_n\),有 \(q\) 次操作,每次操作给你两个数 \(x,y\),你可以交换 \(a_x,a_y\),或者什么都不做. 问你所有 \(2^q\) 种情况中逆序对的个数之和. \(n,q\leq 3000\) 题解 考虑对于每一对 \(i,j\),计算 \(q\) 次操作后 \(a_i\) 和 \(a_j\) 的大小关系. 记 \(f_{i,j,k}\) 为操作 \(i\) 次后,\(a_j,a_k\) 这对数中较小的在 \(j\),较…
求所有左节点的和. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int sumOfLeftLeaves(TreeNode* root) { if(ro…
Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 经典的DP题目. 主页君给出3种解…
Reverse Integer 题目要求:给定一个int 类型值,求值的反转,例如以下: Example1: x = 123, return 321      Example2: x = -123, return -321 简单问题一般蕴含细节的处理.思路非常easy,直接贴Java程序: public class Solution { public int reverse(int x) { int head = x/10; int tail = x%10; long re = 0; while…
题目: Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note t…