随笔分类 - LeetCode OJ
摘要:```python
class Solution(object): def findComplement(self, num): """ :type num: int :rtype: int """ res=0 i=0 while num>0: if nu...
阅读全文
摘要:"Missing Number" Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example, Gi
阅读全文
摘要:######数组$s_1,s_2$,的中位数分别为$m_1,m_2$,数组长度为$n,m$($nm_2$,则中位数存在于:$ s_1[0...|n/2|]$或 $s_2[|m/2|...m-1]$令 $s_{1new}=s_1[0: |n/2| ],s_{2new}= s_2[ |n/2| :m-1...
阅读全文
摘要:问题链接[https://leetcode.com/problems/word break/]
给定: 1. 字符串 s = "leetcode", 2. 字典 dict = ["leet", "code"]. 判断字符串是否能分解成一个或多个字典里的单词。 Return true bec...
阅读全文
摘要:反转链表:比较简单的问题,可以遍历也可以递归。```python# Definition for singly-linked list.class ListNode: def __init__(self, x): self.val = x self.next = N...
阅读全文
摘要:```python# Definition for singly-linked list.class ListNode: def __init__(self, x): self.val = x self.next = Noneclass Solution: # @param {ListNo...
阅读全文
摘要:**DP**对于第$i$个状态(房子),有两种选择:偷(rob)、不偷(not rob)递推公式为:$f(i)=max\\begin{cases}\\begin{cases}{f(i-1)+val_i,}&{rob_{i-1}==0} \\\{f(i-2)+val_i,}&{rob_{i-1}==1...
阅读全文
摘要:**经典题目**```pyhton# Number of 1 Bits# 00000000000000000000000000001011,return 3.# 2(00000000000000000000000000000010) return 1.class Solution: # @pa...
阅读全文
摘要:```python# Definition for a binary tree nodeclass TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None def disp(self):...
阅读全文
摘要:为了实现sqrt(x),可以将问题看成是求解$x^2-y=0$ ,即sqrt(y)=x;牛顿法是求解方程的近似方法,给定初始点$(x0,f(x0))$,迭代公式为: + (5 -> 6 -> 4)>Output: 7 -> 0 -> 8*以下是c++代码*```cpp/** * D...
阅读全文
摘要:**Given an array of integers, find two numbers such that they add up to a specific target number.**Input: numbers={2, 7, 11, 15}, target=9Output: ...
阅读全文
摘要:Given a digit string, return all possible letter combinations that the number could represent.这里用递归,递归式举例:$f(2)=[a,b,c]$, $f(23)=[a+f(3),b+f(3),c+f(3)...
阅读全文
摘要:>anagram,bing词典上的解释是**颠倒字母而成的词句**,例如*"dog"*,*"god"*就是一对anagram;题目的大致意思是:给出一个由string组成的list,返回其中所有的是anagram的string*以下是python代码*```pythonclass Solution:...
阅读全文
摘要:**Pow(x, n)**,计算$x^n$*以下是C++代码*```cppclass Solution {public: double pow(double x, int n) { // 四种不需要计算的情况 if (n == 0||x==1){ return 1; } if (x == ...
阅读全文
摘要:**题目:** 给定两个string $S$、$T$,计算序列为$T$的$S$的子序列的数量。>例如S = "rabbbit", T = "rabbit",返回结果为3>子序列:例如,"ACE"是"ABCDE"的子序列。**这里我用动态规划的来求解*** 构建DP表,大小为 $T.size ...
阅读全文
摘要:example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 "26进制 -> 10进制" 的转化class Solution: # @param s, a string # @retur...
阅读全文
摘要:比较简单的问题:#include#includeusing namespace std;int main(){ int lengthOfLastWord(const char *s); char a[20]; cin.getline(a,20); cout << length...
阅读全文
摘要:一个简单的问题:c++#includeusing namespace std;int searchInsert(int a[],int n,int target){ int i ,count; if (targeta[n-1]){ count= n; retu...
阅读全文
摘要:使用栈结构解决。#include #include using namespace std;class Solution {public: int trap(int A[], int n) { if (n water_stack; int begin=0; ...
阅读全文

浙公网安备 33010602011771号