0
点赞
收藏
分享

微信扫一扫

Shiro框架认证机制详解

水墨_青花 2024-11-03 阅读 18

文章目录

😎题目

在这里插入图片描述

😉解法

😊1.位运算

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
 int getDecimalValue(struct ListNode* head) {
	struct ListNode *cur = head;
	int  sum = 0;
	while(cur)
	{
		sum = sum << 1 | cur->val;
		cur = cur->next;
	}
	return sum;
}

😁2.栈

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
 #define MAX 100
typedef struct stack{
    int data[MAX];
    int top;
}Stack,*PtrStack;
PtrStack InitStack()
{
    PtrStack S = (PtrStack)malloc(sizeof(Stack));
    S->top = -1;
    return S;
}
void push(PtrStack S,int data)
{
    if(S->top == MAX-1)
        return ;
    S->data[++S->top] = data;
}
int pop(PtrStack S)
{
    if(S->top == -1)
        return -1;
    return S->data[S->top--];
}

int getDecimalValue(struct ListNode* head) {
    int sum = 0;
    PtrStack S = InitStack();
    struct ListNode *cur = head;
    while(cur)
    {
        push(S,cur->val);
        cur = cur->next;
    }
    int i=0;
    while(S->top!=-1)
    {
        sum +=  pow(2,i)*pop(S);
        i++;
    }
    return sum;
}
举报

相关推荐

0 条评论