原创 leetcode(10) - Regular Expression Matching 正則表達式匹配

. 表示匹配任意字符 * 表示匹配0個或多個字符 bool isMatch(char* s, char* p) { if (*p == '\0') return *s == '\0'; // next char is

原创 leetcode (21) - Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes o

原创 leetcode (22) - Generate Parentheses

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, g

原创 leetcode(35) - Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it wou

原创 深度優先搜索(DFS)

1.前言 深度優先搜索(縮寫DFS)有點類似廣度優先搜索,也是對一個連通圖進行遍歷的算法。它的思想是從一個頂點V0開始,沿着一條路一直走到底,如果發現不能到達目標解,那就返回到上一個節點,然後從另一條路開始走到底,這種儘量往深處走的概念

原创 leetcode(31) - Next Permutation

【題目】 Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of num

原创 leetcode(44) - Wildcard Matching

思路 假設我們用兩個指針分別指向s和p字符串中要匹配的位置,首先分析下通配符匹配過程中會有哪些情況是成功: s的字符和p的字符相等 p中的字符是?,這時無論s的字符是什麼都可以匹配一個 p中遇到了一個*,這時無論s的字符是什麼都

原创 leetcode (28) - Implement strStr()

strstr(str1,str2) 函數用於判斷字符串str2是否是str1的子串。如果是,則該函數返回str2在str1中首次出現的地址;否則,返回NULL。 int strStr(char* haystack, char* nee

原创 leetcode(42) - Trapping Rain Water

給定n個非負整數,代表一個柱狀圖,每一個柱子的寬度爲1,計算下雨之後柱狀圖能裝多少水? 例如: [0,1,0,2,1,0,1,3,2,1,2,1]  返回 6      上述柱狀圖是由數組表示[0,1,0,2,1,0,1,3

原创 leetcode(34) - Search for a Range

Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runt

原创 leetcode(38) - Count and Say

這是一道根據規則推導題目,要求給定序列數n,求出該序列對應的字符串。 char* countAndSay(int n) { if(n == 1) return "1"; char * cur = malloc(

原创 leetcode(43) - Multiply Strings

題目 Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be

原创 leetcode (23) - Merge k Sorted Lists

  算法1: 最傻的做法就是先1、2合併,12結果和3合併,123結果和4合併,…,123..k-1結果和k合併,我們計算一下複雜度。 1、2合併,遍歷2n個節點 12結果和3合併,遍歷3n個節點 123結果和4合併,遍歷4n個節點 …

原创 leetCode(30) - Substring with Concatenation of All Words

Problem: You are given a string, s, and a list of words, words, that are all of the same length. Find all starting in

原创 leetcode(39 40) - Combination Sum I/I I

       深度優先搜索(DFS)是搜索算法的一種。最早接觸DFS應該是在二叉樹的遍歷裏面,二叉樹的先序、中序和後序遍歷實際上都屬於深度優先遍歷,實質就是深度優先搜索,後來在圖的深度優先遍歷中則看到了更純正的深度優先搜索算法。