28. Implement strStr()【easy】】的更多相关文章

28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 解法一: class Solution { public: int strStr(string haystack, string needle) { if (haystack.empty()…
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack public class Solution { public int strStr(String haystack, String needle) { if(haystack==null||needle==null) return -1; int l…
Implement a stack. You can use any data structure inside a stack except stack itself to implement it. Have you met this question in a real interview?     Example push(1) pop() push(2) top() // return 2 pop() isEmpty() // return true push(3) isEmpty()…
实现在一个母字符串中找到第一个子字符串的位置. #include <stdio.h> #include <string.h> #define _IRON_TRUE 1 #define _IRON_FALSE 0 typedef int BOOL; int strStr(char* s1, char* s2) { ; int l1 = strlen(s1); int l2 = strlen(s2); ) ; ; , j = ; ; i <= (l1-l2); i++) { BO…
28. Search a 2D Matrix [easy] Write an efficient algorithm that searches for a value in an mx n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the la…
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should support the following operations:add and find. add - Add the number to an internal data structure.find - Find if there exists any pair of numbers which sum…
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? 解法一: class Solution { public: ListNode* reverseList(ListNode* head) { ListNode * pre = NULL;…
83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 解法一: /** * Defin…
141. Sqrt(x) [easy] Implement int sqrt(int x). Compute and return the square root of x. Example sqrt(3) = 1 sqrt(4) = 2 sqrt(5) = 2 sqrt(10) = 3 Challenge  O(log(x)) 解法一: class Solution { public: /* * @param x: An integer * @return: The sqrt of x */…
1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack = "hello", needle = "ll" Output: 2 Example 2: Inp…
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetcode.com/problems/implement-strstr/ Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of h…
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 思路:子串匹配,朴素匹配.…
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 begin to intersect at n…
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 Credits:Special than…
21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 解法一: /** * Definition for singly-linked list. * struct ListNode { * int val…
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. Follow up:Can you solve it without using extra space? 解法一: /** * Definition for singl…
141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 解法一: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x…
237. Delete Node in a Linked List[easy] Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the l…
234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time and O(1) space? 解法一: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * Lis…
121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an…
661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the average gray scale (rounding down) of all the 8 surrounding cells and itself.…
27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be ch…
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with consta…
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.…
605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die. Given a flowerbed (represe…
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number…
448. Find All Numbers Disappeared in an Array[easy] Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array. Coul…
1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums…
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 differen…
167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such…