对于ai==0连接(i,t,1,0),对于ai>1(s,i,ai-1,0),然后对以相邻的两个点(i,j)连接(i,j,inf,1),注意这里是一个环的形式,所以1和n+1相连 #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; const int N=1005,inf=1e9; int T,n,h[N],cnt,ans,d
344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { return s.split("").reverse().join(""); }; 292. Nim Game 尼姆游戏还是很有意思的,这题有很多地方可以深入理解 /** * @param {number} n * @return {boolean} */ var ca
昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example: Given a = 1 and b = 2, return 3. 因为之前完全没有在实际练习中使用过位运算,所以刚看到这道题目的时候我的第一反应是 1.用乘除代替加减,但是一想,
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-integers/ 题目: 写一个函数,求两个整数之和,要求在函数体内不得使用加减乘除这四个符号. 分析: 对于不能使用正常的四则运算符,一般就是使用位运算了.而本题要想实现加法,只能使用异或了. 需要注意的是,加法的时候涉及进位,而进位的实现利用与运算. 此外,进位之后还有可能产生进位,所以要在循环里
---------------------------------- 使用位运算实现加法: a^b 加不同部分(a&b)<<1 加相同部分递归相加 AC代码: public class Solution { public int getSum(int a, int b) { if(b==0) return a; int t1=a^b; int t2=(a&b)<<1; return getSum(t1,t2); } } 题目来源: https://leetcode.
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example:Given a = 1 and b = 2, return 3. 题目要求:计算两个整型的和,但是不能用+和- 我们知道a+b为((a&b)<<1)+(a^b),因此可以用递归的方法 class Solution { public: int getSum(int a, int b)
E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output Sonya was unable to think of a story for this problem, so here comes the formal description. You are g
C. Sonya and Queries time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multi
B. Filya and Homework time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to com