华为面试题整理

华为面试题整理

只出现一次的数字
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。

说明:

你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?

解法:要求时间复杂度O(N),空间复杂度O(1)
满足上述要求的解法,目前只有位运算法:
根据位运算异或的性质,可以得到以下的规律:

a ^ 0 = a;
a ^ a = 0;
a ^ b ^ c = a ^ c ^ b;

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int res = 0;
        for(auto x:nums)
            res ^= x;
        return res;
    }
};

还有一个方法,哈希表法,但是至少需要额外的空间,及空间复杂度为O(n),还有就是时间复杂度需不需要考虑将数组中的元素整理到哈希表中的过程中的消耗(O(n))

a_list = list(map(int, input().split()))
a = {}
for i in a_list:
    a.setdefault(i,0)
    a[i] += 1
for k in a.keys():
    if a[k] == 1:
        print(k)

链表相交:
给定两个(单向)链表,判定它们是否相交并返回交点。请注意相交的定义基于节点的引用,而不是基于节点的值。换句话说,如果一个链表的第k个节点与另一个链表的第j个节点是同一节点(引用完全相同),则这两个链表相交。

来源:力扣(LeetCode)

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        if(!headA||!headB) return nullptr;
        
        ListNode* curr_a = headA;
        ListNode* curr_b = headB;

        while(curr_a!=curr_b){
            curr_a = (!curr_a) ? headB : curr_a->next;
            curr_b = (!curr_b) ? headA : curr_b->next;
        }
        return curr_a;
    }
};

旋转数组
给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。

示例 1:

输入:
[1,2,3,4,5,6,7] 和 k = 3
输出:
[5,6,7,1,2,3,4]
解释:

向右旋转 1 步: [7,1,2,3,4,5,6]

向右旋转 2 步: [6,7,1,2,3,4,5]

向右旋转 3 步: [5,6,7,1,2,3,4]

说明:
尽可能想出更多的解决方案,至少有三种不同的方法可以解决这个问题。
要求使用空间复杂度为O(1)的原地算法。

class Solution {
public:
    void rotate(vector<int>& nums, int k) {
        if (nums.empty())
            return;
        for(int i = 0; i < k;++i)
        {
            int tem = nums.back();
            nums.pop_back();
            nums.insert(nums.begin(),tem);
        }
    }
};

环形链表
给定一个链表,判断链表中是否有环。

为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。

示例 1:

输入:
head = [3,2,0,-4]

输出:
true

解释:链表中有一个环,其尾部连接到第二个节点。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        if(!head || !head->next)
            return false;
        ListNode* slow = head;
        ListNode* fast = head->next;
        while(fast != slow)
        {
            if(!fast || !fast->next)
                return false;
            slow = slow->next;
            fast = fast->next->next;
        }
        return true;
        
    }
};

回文链表
请判断一个链表是否为回文链表。

示例 1:

输入:
1->2
输出:
 false
进阶:

你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    bool isPalindrome(ListNode* head) {
        if(head == nullptr || head->next == nullptr)
            return true;
        ListNode* slow = head;
        ListNode* fast = head;
        ListNode* pre = head;
        ListNode* preReverse = nullptr;
        while (fast && fast->next)
        {
            pre = slow;
            slow = slow->next;
            fast = fast->next->next;
            pre->next = preReverse;
            preReverse = pre;
        }
        if (fast)
            slow = slow->next;
        while(pre && slow)
        {
            if(pre->val != slow->val)
                return false;
            pre = pre->next;
            slow = slow->next;
        }
        return true;
    }
};


字符串中最长不重复的子串

#-*- coding = utf-8 -*-
#@Time: 
#@Author : Wang
#@File : LongestSubstr.py
#@Software : PyCharm

s = input()
maxLen,maxStrs,curLen,curStr = 0,"", 0,""
for i in s:
    if i not in curStr:
        curStr += i
        curLen += 1
        if curLen > maxLen:
            maxLen = curLen
        if len(curStr) > len(maxStrs):
            maxStrs = curStr
    else:
        index = s.index(i)
        curStr = curStr[index+1:]
        curStr += i
        curLen = len(curStr)

print(maxLen)
print(maxStrs)
posted @   笑着的程序员  阅读(216)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示