学习list

1 篇文章 0 订阅

最长回文字符串

给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。

示例 1:

输入: “babad”
输出: “bab”
注意: “aba” 也是一个有效答案。
示例 2:

输入: “cbbd”
输出: “bb”

class Solution {
public:
    string longestPalindrome(string s) {
        if (s.size() < 2) return s;
        int n = s.size(), maxLen = 0, start = 0;
        for (int i = 0; i < n - 1; ++i) {
            searchPalindrome(s, i, i, start, maxLen);
            searchPalindrome(s, i, i + 1, start, maxLen);
        }
        return s.substr(start, maxLen);
    }
    void searchPalindrome(string s, int left, int right, int& start, int& maxLen) {
        while (left >= 0 && right < s.size() && s[left] == s[right]) {
            --left; ++right;
        }
        if (maxLen < right - left - 1) {
            start = left + 1;
            maxLen = right - left - 1;
        }
    }
};

数组最大子串和

public class Solution01 {
    public static void main(String[] args) {
        int[] arr = {6,-3,-2,7,-15,1,2,2};
        System.out.println(FindGreatestSumOfSubArray(arr));
    }
    public static int FindGreatestSumOfSubArray(int[] array) {
        if(array.length==0)
            return 0;
        int sum = array[0];//保存每组的和
        int maxSum = array[0];//连续子数组最大和
        //动态规划
        for(int i = 1;i<array.length;i++){
            sum = Math.max(sum+array[i],array[i]);
            maxSum = Math.max(sum,maxSum);
        }
        return maxSum;
    }

字符串的全排列

 vector<string> Permutation(string str) {
        if (str.length() == 0) {
            return result;
        }
        PermutationHelp(str,0);
        return result;
    }
    
    void PermutationHelp(string str,int start){
        if(start == str.length()){
            result.push_back(str);
            return;
        }
        
        for (int i=start; str[i] != '\0'; i++) {
            if(start != i && str[start] == str[i]){
                continue;
            }
            swap(str[start], str[i]);
            PermutationHelp(str,start+1);
            swap(str[start], str[i]);
        }
    }

2.三数之和最接近target

vector<vector<int>> threeSum(vector<int>& nums) {
    vector<vector<int>> res;
    sort(nums.begin(), nums.end());
    if (nums.empty() || nums.back() < 0 || nums.front() > 0) return {};
    for (int i=0; i<nums.size()-2; ++i) {
        if (nums[i] > 0) break;
        if (i > 0 && nums[i] == nums[i - 1]) continue;
        int target = 0 - nums[i];
        int l = i+1;
        int r = (int)(nums.size() - 1);
        while (l<r) {
            if (nums[l] + nums[r] == target) {
                res.push_back({nums[i],nums[l],nums[r]});
                while (l < r && nums[l] == nums[l + 1]) ++l;
                while (l < r && nums[r] == nums[r - 1]) --r;
                ++l;
                --r;
                
            }else if(nums[l] + nums[r]  > target){
                --r;
            }else{
                ++l;
            }
        }
    }
    return res;
}

  1. 2数按照位相加,大整数运算
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int carry = 0;
        ListNode total = new ListNode(0);
        ListNode root = total;
        while(l1!=null || l2!=null || carry!=0){
            int val1 = l1 != null ? l1.val : 0;
            int val2 = l2 != null ? l2.val : 0;
            int sum = val1+ val2+carry;
            carry = sum/10;
            
            ListNode subNode = new ListNode(sum%10);
            total.next = subNode;
            total = subNode;
            if(l1!=null) l1 = l1.next;
            if(l2!=null) l2 = l2.next;
        }
        return root.next;
    }
}

  1. 重叠字符串问题,ababababab ab
  2. 对称二叉树判断问题
bool isSymmetrical(TreeNode* pRoot)
    {
        if(pRoot == NULL) return true;
        TreeNode * left = pRoot->left;
        TreeNode * right = pRoot->right;
        return isSymmetricalHelp(left,right);
    }
    
    bool isSymmetricalHelp(TreeNode* left,TreeNode* right){
        if(left==NULL && right==NULL) return true;
        if(left==NULL || right==NULL || left->val!=right->val) return false;
        return isSymmetricalHelp(left->left,right->right)&&isSymmetricalHelp(left->right,right->left);
    }

  1. 快排
  2. 一个数组先增后减,求峰值

 class Solution {
     public int findPeakElement(int[] nums) {
         return findPeakElement(nums,0,nums.length);
     }
     
     public int findPeakElement(int[] nums,int start,int end){
         int mid = (start+end)/2;
         int leftvalue = nums[mid-1];
         int midValue = nums[mid];
         int rightValue = nums[mid+1];
         //找到数据
         if(leftvalue < midValue && midValue>rightValue){
             return midValue;
         }else if(leftvalue>midValue){//去左半区域查找
             return findPeakElement(nums,start,mid-1);
         }else if(midValue<rightValue){//去右半区域查找
              return findPeakElement(nums,mid+1,end);
         }else{
                return -1;
         }
         
     }
 }

  1. 52张排取出5张,同花顺概率
  2. 字符串 求出最长数字子串,保存最大位置和起始位置,遇到数字就+1

算法题连续子数组的和,有正有负

public:
    int FindGreatestSumOfSubArray(vector<int> array) {
        int sum = array[0];
        int max = array[0];
        for(int i=1;i<array.size();++i){
            sum = sum+array[i];
            if(sum<array[i])
                sum=array[i];
            if(sum>max){
                max = sum;
            }
        }
        return max;
    }
};

leetcode27二叉树的镜像

void Mirror(TreeNode *pRoot) {
        if(pRoot == NULL) return;
        TreeNode * tem;
        tem = pRoot->left;
        pRoot->left= pRoot->right;
        pRoot->right=tem;
        Mirror(pRoot->left);
        Mirror(pRoot->right);
    }

红黑树的实现

牛客网的剑指offer算法题

算法:不含重复字符的最长连续子串的长度

class Solution {
    public int lengthOfLongestSubstring(String s) {
        int[] m = new int[256];
        Arrays.fill(m,-1);
        int res=0,left=-1;
        for(int i=0;i<s.length();++i){
            left = Math.max(left,m[s.charAt(i)]);
            m[s.charAt(i)] = i;
            res = Math.max(res,i-left);
        }
        return res;
    }
}

13、算法:交换树的左右节点

void Mirror(TreeNode *pRoot) {
        if(pRoot == NULL) return;
        TreeNode * tem;
        tem = pRoot->left;
        pRoot->left= pRoot->right;
        pRoot->right=tem;
        Mirror(pRoot->left);
        Mirror(pRoot->right);
    }

/*
class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int res = 0;
        for (int i = 0; i < nums.size(); ++i) {
            if (nums[i] != val) nums[res++] = nums[i];
        }

        for (int i = 0; i < nums.size(); ++i) {
            cout<<"aa:"<<nums[i] <<endl;
        }
        return res;
    }
};*/

一亿个数据,找出前10000个

已知中序排列 先序 求树?

TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
       return buildTree(pre,0,pre.size()-1,vin,0,vin.size()-1);
    }
    
    TreeNode* buildTree(vector<int> pre,int pLeft,int pRight,vector<int> mid,int mLeft,int mRight){
        if(pLeft>pRight || mLeft>mRight) return NULL;
        int i=0;
        for(i=mLeft;i<mRight;i++){
            if(pre[pLeft] == mid[i]) break;
        }
        
        TreeNode * newNode = new TreeNode(pre[pLeft]);
        newNode->left = buildTree(pre,pLeft+1,pLeft+i-mLeft,mid,mLeft,i-1);
        newNode->right = buildTree(pre,pLeft+i-mLeft+1,pRight,mid,i+1,mRight);
        return newNode;
    }

一个求数组中唯一出现的元素

 void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) {
        //map value count
        sort(data.begin(),data.end());
        vector<int> res;
        if(data[0] != data[1]){
            res.push_back(data[0]);
        }
        //5  i< 4 i=3
        for(int i=1;i<data.size()-1;i++){
            if(data[i] != data[i-1] && data[i] != data[i+1]){
                res.push_back(data[i]);
            }
        }
        if(data[data.size()-1] != data[data.size()-2]){
            res.push_back(data[data.size()-1]);
        }
        * num1 = res[0];
        * num2 = res[1];
    }

一个二叉树的前序遍历(递归和非递归)

Z型打印二叉树,

 vector<vector<int> > Print(TreeNode* pRoot) {
        vector<vector<int>> res;
        vector<vector<int>> tem;
        helpTree(pRoot,0,tem);

        for (int i=0; i<tem.size(); i++) {
            vector<int> arrayRever = tem[i];
            if(i % 2 == 1){
                reverse(arrayRever.begin(),arrayRever.end());
                res.push_back(arrayRever);
            }else{
                res.push_back(arrayRever);
            }
        }
        return res;
    }
    
    void helpTree(TreeNode* pRoot,int level,vector<vector<int>> &res){
        if(pRoot == NULL) return;
        if(level == res.size()) res.push_back({});
        res[level].push_back(pRoot->val);
        if(pRoot->left != NULL) helpTree(pRoot->left,level+1,res);
        if(pRoot->right != NULL) helpTree(pRoot->right,level+1,res);
    }

打印螺旋矩阵,

百万数据找出前k个等。

vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {
    int len=input.size();
    vector<int> vec;
    if(len<k)
        return vec;
    sort(input.begin(),input.end());
    for(int i=0;i<k;i++)
        vec.push_back(input[i]);
    return vec;
    
}

反转链表

ListNode* ReverseList(ListNode* pHead) {
        if (pHead == NULL || pHead->next == NULL)     // 空链或只有一个结点,直接返回头指针
        {
            return pHead;            
        }
        ListNode * p1,*p2,*p3;
        p1=pHead;
        p2=p1->next;
        p3 = p2->next;
        while(p2 != NULL){
            p3=p2->next;
            p2->next = p1;
            p1=p2;
            p2=p3;
        }
        pHead->next = NULL;
        pHead=p1;
        return pHead;
    }

平衡二叉树判断

 bool IsBalanced_Solution(TreeNode* pRoot) {
        if(pRoot == NULL) return true;
        int leftDeep = getTreeDeep(pRoot->left);
        int rightDeep = getTreeDeep(pRoot->right);
        if(abs(leftDeep - rightDeep) > 1) return false;
        
        return IsBalanced_Solution(pRoot->left)&&IsBalanced_Solution(pRoot->right);
    }
    
    int getTreeDeep(TreeNode* pRoot){
        if(pRoot == NULL) return 0;
        int leftDeep = getTreeDeep(pRoot->left);
        int rightDeep = getTreeDeep(pRoot->right);
        return (leftDeep > rightDeep) ? leftDeep+1 : rightDeep+1;
    }

子树判断


 bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
    {
        if(pRoot2 == NULL || pRoot1==NULL) return false;
        if(pRoot1->val == pRoot2->val){
            if(isSubTree(pRoot1,pRoot2)) return true;
        }
        return HasSubtree(pRoot1->left,pRoot2)||HasSubtree(pRoot1->right,pRoot2);
    }

    bool isSubTree(TreeNode* pRoot1, TreeNode* pRoot2){
        if(pRoot2 == NULL) return true;
        if(pRoot1 == NULL) return false;
        if(pRoot1->val != pRoot2->val){
            return false;
        }
        return isSubTree(pRoot1->left,pRoot2->left)&&isSubTree(pRoot1->right,pRoot2->right);
    }


正则表达式


bool match(char* str, char* pattern)
 {
     if(pattern[0] == 0 && str[0] == 0) return true;
     if(pattern[0] !=0 && pattern[1] == '*'){
         if (match(str,pattern+2)) {
             return true;
         }
     }
     
     if(pattern[0] == str[0] || (pattern[0] == '.'&& str[0])){
         if(match(str+1,pattern+1)) return true;
         if(pattern[1] == '*' && match(str+1, pattern)) return true;
     }

     return false;
 }

判断有效括号[]{}

bool isValid(string s) {
    stack<char> parentheses;
    for(int i=0; i< s.size();++i){
        if(s[i] == '(' || s[i] == '[' || s[i] == '{'){
            parentheses.push(s[i]);
        }else{
            if(parentheses.empty()) return false;
            if(s[i] == ')' && parentheses.top() != '(') return false;
            if(s[i] == ']' && parentheses.top() != '[') return false;
            if(s[i] == '}' && parentheses.top() != '{') return false;
            parentheses.pop();
        }
    }
    return parentheses.empty();
}

  1. leetcode上刷了大概100左右, 自己写出来并看最优解
    LeetCode 76. Minimum Window Substring. Hard
    精选 TOP 面试题、 热题TOP100

牛客,剑值藕粉 https://www.nowcoder.com/ta/coding-interviews

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值