s.trim();
StringBuilder builder=new StringBuilder();
for(int i=0;i<s.length();i++){
if(s.substring(i,i+1).equals(" ")){
builder.append("%20");
}else{
builder.append(s.substring(i,i+1));
}
}
return builder.toString();
public void appendTail(int value) {
stack1.push(value);
}
public int deleteHead() {
//如果栈2是空
if(stack2.empty()){
//如果栈1也是空
if(stack1.empty()){
return -1;
}else{
//栈1一直弹出,直到栈1为空
while (!stack1.empty()) {
stack2.push(stack1.pop());
}
}
}
return stack2.pop();
}
if(right-left<=1){
return Math.min(nums[left],nums[right]);
}
if(nums[left]<nums[right]){
return nums[left];
}
int mid=(left+right)/2;
return Math.min(findMin(nums, left, mid-1),findMin(nums,mid,right));
public class Solution2 {
double res=1;
public double myPow(double x,int n){
while (n>0){
if(n%2==0) {
x *= x;
n/=2;
}
res*=x;
n--;
}
while (n<0){
if(n%2==0){
x*=x;
n/=2;
}
res*=1/x;
n++;
}
return res;
}
}
public boolean isSubStructure(TreeNode A, TreeNode B) {
if(A==null||B==null){
return false;
}
return Compare(A,B)||isSubStructure(A.left,B)||isSubStructure(A.right,B);
}
private boolean Compare(TreeNode A,TreeNode B){
if(B==null){
return true;
}else if(A==null||A.val!=B.val){
return false;
}
return Compare(A.left,B.left)&&Compare(A.right,B.right);
}
Stack<Integer> stack = new Stack<>();
int index=0;
for(int i=0;i<pushed.length;i++){
stack.push(pushed[i]);
while (!stack.isEmpty()&&stack.peek()==popped[index]){
stack.pop();
index++;
}
}
return stack.isEmpty();
if(root==null){
return res;
}
Deque<TreeNode>queue=new LinkedList<>();
queue.addLast(root);
int index=0;
while (!queue.isEmpty()){
List<Integer>singleRes=new ArrayList<>();
int size=queue.size();
for(int i=0;i<size;i++) {
TreeNode node = queue.pollFirst();
singleRes.add(node.val);
if(node.left!=null){
queue.addLast(node.left);
}
if(node.right!=null){
queue.addLast(node.right);
}
}
if(index%2==1){
Collections.reverse(singleRes);
}
res.add(singleRes);
index++;
}
return res;
if(postorder.length==0||postorder.length==1){
return true;
}
return check(postorder,0,postorder.length-1);
}
private boolean check(int[]postorder,int left,int right){
if(left>=right){
return true;
}
int leftEnd=left;
while (leftEnd<right&&postorder[leftEnd]<postorder[right]){
leftEnd++;
}
int temp=leftEnd;
while (temp<right){
if(postorder[temp]<postorder[right]){
return false;
}else{
temp++;
}
}
return check(postorder, left, leftEnd-1)&&check(postorder, leftEnd, right-1);
Map<Node,Node> map=new HashMap<>();
Node temp=head;
while (temp!=null){
map.put(temp,new Node(temp.val));
temp=temp.next;
}
temp=head;
while (temp!=null){
map.get(temp).next=map.get(temp.next);
map.get(temp).random=map.get(temp.random);
temp=temp.next;
}
return map.get(head);
Node pre;
Node head;
public Node treeToDoublyList(Node root) {
if(root==null){
return root;
}
build(root);
pre.right=head;
head.left=pre;
return head;
}
private void build(Node root){
if(root==null){
return;
}
build(root.left);
if(pre==null){
head=root;
}else{
root.left=pre;
pre.right=root;
}
pre=root;
build(root.right);
}
}
class MedianFinder {
private PriorityQueue<Integer>queue1=new PriorityQueue<>();
private PriorityQueue<Integer>queue2=new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
/** initialize your data structure here. */
public MedianFinder() {
}
public void addNum(int num) {
//如果size相等,先加入小顶堆,再poll到大顶堆;
//如果size不相等,先加入到大顶堆,再poll到小顶堆;
if(queue1.size()==queue2.size()){
queue1.add(num);
queue2.add(queue1.poll());
}else{
queue2.add(num);
queue1.add(queue2.poll());
}
}
public double findMedian() {
if(queue1.size()==queue2.size()){
int num1=queue1.peek();
int num2=queue2.peek();
double res=(num1+num2)/2.0;
return res;
}else{
return queue2.peek()/1.0;
}
}
}
//数字范围
long start=1;
//位数
int base=1;
//数字数量
long weight=9;
while (n>base*weight){
n-=base*weight;
base+=1;
weight*=10;
start*=10;
}
long res=start+(n-1)/base;
int index=(n-1)%base;
return Long.toString(res).charAt(index)-'0';
String[]dp=new String[nums.length];
for(int i=0;i<nums.length;i++){
dp[i]=String.valueOf(nums[i]);
}
Arrays.sort(dp, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
Long long1=Long.valueOf(o1+o2);
Long long2=Long.valueOf(o2+o1);
if(long1<long2){
return -1;
}else{
return 1;
}
}
});
StringBuilder res = new StringBuilder();
for(int i=0;i<dp.length;i++){
res.append(dp[i]);
}
return res.toString();
char[] chars = String.valueOf(num).toCharArray();
int[]dp=new int[chars.length+1];
dp[0]=1;
dp[1]=1;
for(int i=2;i<dp.length;i++){
if(chars[i-2]=='1'||(chars[i-2]<='2'&&chars[i-1]<='5')){
dp[i]=dp[i-2]+dp[i-1];
}else{
dp[i]=dp[i-1];
}
}
return dp[dp.length-1];
if(n==1){
return 1;
}
int[]dp=new int[n];
dp[0]=1;
int index1=0;
int index2=0;
int index3=0;
int value1=0;
int value2=0;
int value3=0;
for(int i=1;i<n;i++){
value1=dp[index1]*2;
value2=dp[index2]*3;
value3=dp[index3]*5;
dp[i]=Math.min(value1,Math.min(value2,value3));
if(dp[i]==value1){
index1++;
}
if(dp[i]==value2){
index2++;
}
if(dp[i]==value3){
index3++;
}
}
return dp[dp.length-1];
int len=nums.length;
int left=0;
int right=len-1;
while (left<=right){
int mid=(left+right)/2;
if(nums[mid]==mid){
left=mid+1;
}else{
right=mid-1;
}
}
return left;
int base=0;
for(int i=0;i<nums.length;i++){
base^=nums[i];
}
int s=1;
while ((base&s)==0){
s<<=1;
}
int[]res=new int[2];
for(int i=0;i<nums.length;i++){
if((s&nums[i])==0){
res[0]^=nums[i];
}else{
res[1]^=nums[i];
}
}
return res;
int[]res=new int[2];
int left=0;
int right=nums.length-1;
while (left<right){
if(nums[left]+nums[right]<target){
left++;
}else if(nums[left]+nums[right]>target){
right--;
}else{
res[0]=nums[left];
res[1]=nums[right];
break;
}
}
return res;
int left=1;
int right=1;
int limit=target/2+1;
while (right<=limit&&left<=limit){
sum+=right;
while (sum>=target){
if(sum==target){
int[]singleRes=new int[right-left+1];
int index=0;
for(int i=left;i<=right;i++){
singleRes[index++]=i;
}
res.add(singleRes);
}
sum-=left;
left++;
}
right++;
}
return res.toArray(new int[res.size()][]);
class MaxQueue {
private Deque<Integer> queue1=new LinkedList<>();
private Deque<Integer> queue2=new LinkedList<>();
public MaxQueue() {
}
public int max_value() {
if(queue2.isEmpty()){
return -1;
}
return queue2.peekFirst();
}
public void push_back(int value) {
queue1.addLast(value);
while (!queue2.isEmpty()&&value>queue2.peekLast()){
queue2.pollLast();
}
queue2.offerLast(value);
}
public int pop_front() {
if(queue1.isEmpty()){
return -1;
}
int value = queue1.pollFirst();
if(queue2.peekFirst().equals(value)){
queue2.pollFirst();
}
return value;
}
}
double[]dp=new double[]{1/6d,1/6d,1/6d,1/6d,1/6d,1/6d};
for(int i=2;i<=n;i++){
double[] temp=new double[5*i+1];
for(int j=0;j<dp.length;j++){
for(int k=0;k<6;k++){
temp[j+k]+=dp[j]/6.0;
}
}
dp=temp;
}
return dp;
int max=0;
int min=14;
Set<Integer>set=new HashSet<>();
for(int i=0;i<nums.length;i++){
if(nums[i]==0){
continue;
}
if(set.contains(nums[i])){
return false;
}
max=Math.max(max,nums[i]);
min=Math.min(min,nums[i]);
set.add(nums[i]);
}
return max-min<5;
int index=0;
//总的思路就是,每杀死一个人后,都把他后面的元素往左边移,然后溯源,找到存活的人最初的那个位置。
//如果是n个数,就需要n-1次循环才能溯源到最先存活的位置
int size=1;
for(int i=0;i<n-1;i++){
size++;
index=(index+m)%size;
}
return index;
boolean x=n>1&&(sumNums(n-1))>0;
res+=n;
return res;
while (b!=0){
int c=a^b;
b=(a&b)<<1;
c=a;
}
return a;