This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
# @return a string | |
def longestCommonPrefix(self, strs): | |
# computes the LCP of the stringlist strs | |
if strs is None or len(strs) == 0: | |
return "" | |
return reduce(self.lcp, strs) | |
def lcp(self, str1, str2): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
# @param x, a float | |
# @param n, a integer | |
# @return a float | |
def pow(self, x, n): | |
if x == 0.0: | |
return 0.0 | |
if n < 0: | |
# Avoids arithmetic underflow | |
return 1.0/self.pow(x, -n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode(int x) : val(x), next(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
# @param A, a list of integers | |
# @param target, an integer to be inserted | |
# @return integer | |
def searchInsert1(self, A, target): | |
for i,val in enumerate(A): | |
if target <= val: | |
return i | |
return len(A) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Element *findMToLastElement( Element *head, int m ) { | |
if (!head || n < 0) { | |
return NULL; | |
} | |
Element *first, *second = head, head; | |
for(int i = 0; i < m; i++) { | |
first = first->next; | |
if (!first) { | |
return NULL; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
# @param digits, a list of integer digits | |
# @return a list of integer digits | |
def plusOne(self, digits): | |
for i in reversed(range(len(digits))): | |
digits[i] = (digits[i] + 1) % 10 | |
if digits[i] == 0: | |
continue | |
return digits | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
# @return an integer | |
def lengthOfLongestSubstring(self, s): | |
if s is None or len(s) == 0: | |
return 0 | |
i, maxLen = 0, 0 | |
seenVals = set() # seen characters | |
for j in range(len(s)): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
void merge(int A[], int m, int B[], int n) { | |
int i = m - 1; | |
int j = n - 1; | |
int k = m + n - 1; | |
while(i >= 0 && j >= 0) { | |
if (A[i] > B[j]) { | |
A[k--] = A[i--]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cmath> | |
#include <vector> | |
class TurretDefense { | |
public: | |
int firstMiss(std::vector <int>xs, std::vector <int>ys, std::vector <int> times) { | |
int curx = 0; | |
int cury = 0; | |
int curtime = 0; | |
for(int i = 0; i < xs.size(); i++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution(object): | |
def findPeakElement(self, nums): | |
""" | |
:type nums: List[int] | |
:rtype: int | |
""" | |
return self.findPeakRecur(nums, 0, len(nums)-1) | |
def findPeakRecur(self, nums, lo, hi): | |
mid = (lo + hi) // 2 |
OlderNewer