class Solution(object):
def strStr(self, haystack, needle):
if needle in haystack:
return haystack.index(needle)
return 1
"""
:type haystack: str
:type needle: str
:rtype: int
"""
James
@JamesG3
Posts made by JamesG3

Simple Python solution using index, best run 38ms

RE: Question: Why there is a inconsistent result with a same testcase when i submit solution ?
@dreamer92 said in Question: Why there is a inconsistent result with a same testcase when i submit solution ?:
interval
For example, [1,2],[2,5],[3,4],[4,6], the solution is removing [2,5], because it takes so much space, and it's overlapping with both [3,4] and [4,6]. After removing [2,5], we need to update temend with the smaller end, which is 4. If we don't update, the temend(5) will still affect the rest of comparison. In other word, update temend is a representation of removing an interval.

Question: Why there is a inconsistent result with a same testcase when i submit solution ?
When i test [[1,2] , [2,3]] using Run Code, it pass with a result 0. However, when i click Submit Solution, the result is Wrong Answer, and it shows my result is 1, not 0. Is there someone knows whats wrong with the code or test case?
# Definition for an interval. # class Interval(object): # def __init__(self, s=0, e=0): # self.start = s # self.end = e class Solution(object): def eraseOverlapIntervals(self, intervals): if len(intervals)==0: return 0 count=0 intervals.sort() temend=intervals[0].end for n in intervals[1:]: if n.start>=temend: temend=n.end else: count+=1 temend=min(temend,n.end) return count """ :type intervals: List[Interval] :rtype: int """

Why sometimes the result is Wrong when i submit a right answer?
Why sometimes the result is Wrong when i submit a right answer?
And what are the test cases like? 
Python math solution with explanation
def numberOfArithmeticSlices(self, A): if len(A)<3: return 0 num=0 count=2 #counter. used for calculations difference=A[1]A[0] #initialize the difference for n in range(2,len(A)): if A[n]A[n1]==difference: count+=1 else: if count>=3: num += (count2)*(count1)/2 #calculate the nth of 1,3,6,10... use formula nth=n(n1)/2 count=2 #reset counter difference=A[n]A[n1] #reset difference if count>=3: num += (count2)*(count1)/2 return num """ :type A: List[int] :rtype: int """

O(n) python solution, beats 90%
def removeDuplicates(self, nums): if len(nums)==0: #check boundary value return 0 if len(nums)==1: return 1 count=1 #initialize number counter and index index=1 while index != len(nums): #read from the second number to the last number if nums[index]==nums[index1]: #if the former number is same, counter add 1 count+=1 else: #if different, reset counter to 1 count=1 if count>2: #if beyond 2 same numbers, delete from the third number del nums[index] count=1 continue index+=1 return len(nums) """ :type nums: List[int] :rtype: int """ ```

1line Python Solution
class Solution(object): def hammingWeight(self, n): return bin(n).count('1') """ :type n: int :rtype: int """

Python solution sharing 56ms
class Solution(object): def majorityElement(self, nums): res=[] for i in set(nums): if(nums.count(i)>len(nums)/3): res.append(i) return res """ :type nums: List[int] :rtype: List[int] """