代码随想录day11|京东笔试+ ● 20. 有效的括号● 1047. 删除字符串中的所有相邻重复项● 150. 逆波兰表达式求值

 状态:被虐菜了ww,笔试准备的不太好,总结一下,下次努力!

京东笔试:

计算机基础:

1. 排序稳定性

算法平均时间复杂度最坏时间复杂度稳定性
冒泡O(n^2)O(n^2)稳定
插入O(n^2)O(n^2)稳定
快排O(nlogn)O(nlogn)不稳定
选择O(nlogn)O(n^2)不稳定
堆排O(nlogn)O(nlogn)不稳定

 

2. 关系性数据库--完整性

3. 完全图

4.文件目录权限

5. 动态分区存储 最佳匹配 找最小空闲分区

6. Mysql dump table

7. 二叉树链表存储结构 从左到右打印

8. 3比特块密码映射

9. 数据库ER模型--连接陷阱

10. 最小公共子串--算法复杂度分析

11. TCP 链接--SYN报文段

        - 洪泛攻击

        - 初始序号

        - SYN报文段是否包含应用层数据

        - 三次握手第二步, 允许链接,SYN比特设置为0?

C++基础:

1. 使用命名空间成员不包含命名空间

2. std::iota()函数

3. 判断输出结果

const int x;
x = 10;
cout<<x;

4. 判断输出结果

array arr={10,20,30}
-2[array-1]

5. 判断输出结果

class A{
}

cout<<sizeof(A);

4. 判断输出结果

void func(){
    static int s=1;
    s++;
    printf("%d", s);

}

int main(){

func();
func();
func();

return 0;
}

5. C和C++的区别

char c ='a';
printf("%d \n", (int) sizeof('a'));

代码题:

科学输入法

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

void scientificNotation(int num) {
    // 计算指数
    int exponent = 0;
    while (num >= 10) {
        num /= 10;
        exponent++;
    }

    // 输出科学计数法
     cout << num << ".*10^" << exponent << endl;
}

int main() {
    int input;
    
    // 获取用户输入,确保输入大于 100 的正整数
    do {
        cout << "请输入大于100的正整数: ";
        cin >> input;
    } while (input <= 100);

    // 输出科学计数法
    cout << "科学计数法: ";
    scientificNotation(input);

    return 0;
}

1/N重复小数

#include <iostream>
#include <map>

using namespace std;

// 函数来计算1/N的循环小数位数
int cycleLength(int N) {
    map<int, int> remainderPositions;
    int position = 0;
    int remainder = 1;

    while (true) {
        // 计算余数
        remainder = remainder % N;
        // 如果余数为0,则没有循环
        if (remainder == 0)
            return 0;
        // 如果这个余数之前出现过,循环的长度就是当前位置减去它首次出现的位置
        if (remainderPositions.find(remainder) != remainderPositions.end())
            return position - remainderPositions[remainder];
        // 记录这个余数的位置
        remainderPositions[remainder] = position;
        // 余数乘以10进入下一次循环
        remainder *= 10;
        position++;
    }
}

int main() {
    int N;
    cout << "请输入N的值: ";
    cin >> N;

    int length = cycleLength(N);
    if (length > 0)
        cout << "1/" << N << "的循环小数位数为: " << length << endl;
    else
        cout << "1/" << N << "不是循环小数" << endl;

    return 0;
}

素数三元组(忘记题目了)

理论基础 

了解一下 栈与队列的内部实现机智,文中是以C++为例讲解的。 

文章讲解:代码随想录

stack 常用方法:

stack<int> myStack;

- push

- top

- pop

- top

- empty

 232.用栈实现队列 

大家可以先看视频,了解一下模拟的过程,然后写代码会轻松很多。

题目链接/文章讲解/视频讲解:代码随想录

算是熟悉stack的方法

class MyQueue {
public:
    stack<int> stIn;
    stack<int> stOut;
    MyQueue() {
        
    }
    
    void push(int x) {
        stIn.push(x);
    }
    
    int pop() {
        if(stOut.empty()){
            while(!stIn.empty()){
                stOut.push(stIn.top());
                stIn.pop();
            }
        }
        int result = stOut.top();
        stOut.pop();
        return result;
    }
    
    int peek() {
        int res = this->pop();
        stOut.push(res);
        return res;
    }
    
    bool empty() {
        return stIn.empty() && stOut.empty();
    }
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue* obj = new MyQueue();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->peek();
 * bool param_4 = obj->empty();
 */

  225. 用队列实现栈 

queue 常见方法

push

size

front

back

pop

可以大家惯性思维,以为还要两个队列来模拟栈,其实只用一个队列就可以模拟栈了。 

建议大家掌握一个队列的方法,更简单一些,可以先看视频讲解

题目链接/文章讲解/视频讲解:代码随想录

class MyStack {
public:
    queue<int> que;

    MyStack() {
        
    }
    
    void push(int x) {
        que.push(x);
    }
    
    int pop() {
        int size = que.size()-1;
        while(size--){
            que.push(que.front());
            que.pop();
        }
        int result = que.front();
        que.pop();
        return result;
        
    }
    
    int top() {
        return que.back();
    }
    
    bool empty() {
        return que.empty();
    }
};

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack* obj = new MyStack();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->top();
 * bool param_4 = obj->empty();
 */

 20. 有效的括号 

讲完了栈实现队列,队列实现栈,接下来就是栈的经典应用了。 

不匹配场景:

全是左括号或是全是右括号

考虑最后return前检查stack是否为0

过程中stack是否为0

题目链接/文章讲解/视频讲解:代码随想录

class Solution {
public:
    bool isValid(string s) {
        if(s.size()%2!=0) return false;
        
        stack<char> right;
        int idx = s.size();
        idx--;
        while(idx>=0){
            if(s[idx]==']'||s[idx]==')'||s[idx]=='}'){
                right.push(s[idx]);
            }else{
                if(s[idx]=='['){
                    if(right.size()==0||right.top()!=']') return false;
                   
                }
                else if(s[idx]=='{'){
                    if(right.size()==0||right.top()!='}'){
                        return false;
                    }
                }
                else{
                        if(right.size()==0||right.top()!=')') return false;
                    }
                    right.pop();
                    
                }
                idx--;
            }
            if(right.size()!=0) return false;
            return true;
        }
    
};

 1047. 删除字符串中的所有相邻重复项 

栈的经典应用。 

栈帮助我们记录了 遍历数组当前元素时候,前一个元素是什么。

class Solution {
public:
    string removeDuplicates(string s) {
        stack<char> right;
        int m = s.size()-1;
        while(m>=0){
            if(!right.empty()){
                if(s[m]==right.top()){
                    right.pop();
                    m--;
                    continue;
                }
            }
            right.push(s[m]);
            m--;

        }
        string result = "";
        while(!right.empty()){
            result+=right.top();
            right.pop();
        }
        return result;
    }
};

题目链接/文章讲解/视频讲解:代码随想录

 150. 逆波兰表达式求值 

熟悉c++函数

  1. std::stol:将字符串转换为long类型的整数。
  2. std::stoll:将字符串转换为long long类型的整数。
  3. std::stoul:将字符串转换为unsigned long类型的整数。
  4. std::stoull:将字符串转换为unsigned long long类型的整数。
  5. std::stof:将字符串转换为float类型的浮点数。
  6. std::stod:将字符串转换为double类型的浮点数。
  7. std::stold:将字符串转换为long double类型的浮点数。

题目链接/文章讲解/视频讲解:代码随想录

要考虑num2, num1在减法和除法时谁先谁后。

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        //int n =tokens.size();
        stack<long long> st;
        for(int i =0;i<tokens.size();i++){
            if(tokens[i]=="+"||tokens[i]=="-"||tokens[i]=="*"||tokens[i]=="/"){
                long long num1 =st.top();
                st.pop();
                long long num2 = st.top();
                st.pop();
                if(tokens[i]=="+") st.push(num1+num2);
                if(tokens[i]=="-") st.push(num2-num1);
                if(tokens[i]=="*") st.push(num1*num2);
                if(tokens[i]=="/") st.push(num2/num1);
            }else{
                st.push(stoll(tokens[i]));
            }
        }
        int result = st.top();
        st.pop();
        return result;
    }
};

  • 21
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值