-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c4ece06
Showing
61 changed files
with
2,210 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package leetcode; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class AddTwoNumbers2 { | ||
public class ListNode { | ||
int val; | ||
ListNode next; | ||
|
||
ListNode(int x) { | ||
val = x; | ||
} | ||
} | ||
|
||
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { | ||
Boolean plusFlag = false; | ||
// ArrayList<Integer> outputList = new ArrayList<Integer>(); | ||
int sum = l1.val + l2.val; | ||
if (sum > 9) { | ||
plusFlag = true; | ||
} | ||
sum = sum % 10; | ||
ListNode head = new ListNode(sum); | ||
ListNode currentNode = head; | ||
while (l1.next != null && l2.next != null) { | ||
// int plus = 0; | ||
ListNode preNode = currentNode; | ||
sum = l1.next.val + l2.next.val; | ||
if (plusFlag) { | ||
sum += 1; | ||
plusFlag = false; | ||
} | ||
if (sum > 9) { | ||
plusFlag = true; | ||
} | ||
|
||
sum = sum % 10; | ||
currentNode = new ListNode(sum); | ||
preNode.next = currentNode; | ||
l1 = l1.next; | ||
l2 = l2.next; | ||
// outputList.add(sum); | ||
} | ||
|
||
while (plusFlag) { | ||
ListNode preNode = currentNode; | ||
if (l1.next != null) { | ||
sum = l1.next.val + 1; | ||
l1 = l1.next; | ||
} else if (l2.next != null) { | ||
sum = l2.next.val + 1; | ||
l2 = l2.next; | ||
} else { | ||
sum = 1; | ||
} | ||
if (sum > 9) { | ||
plusFlag = true; | ||
}else { | ||
plusFlag = false; | ||
} | ||
sum = sum % 10; | ||
currentNode = new ListNode(sum); | ||
preNode.next = currentNode; | ||
} | ||
while (l1.next != null) { | ||
ListNode preNode = currentNode; | ||
currentNode = new ListNode(l1.next.val); | ||
preNode.next = currentNode; | ||
l1 = l1.next; | ||
|
||
} | ||
while (l2.next != null) { | ||
ListNode preNode = currentNode; | ||
currentNode = new ListNode(l2.next.val); | ||
preNode.next = currentNode; | ||
l2 = l2.next; | ||
} | ||
return head; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package leetcode; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Cards914 { | ||
|
||
public static void main(String[] args) { | ||
// TODO Auto-generated method stub | ||
System.out.println(hasGroupsSizeX(new int[] {1,1})); | ||
} | ||
|
||
public static boolean hasGroupsSizeX(int[] deck) { | ||
Map<Integer, Integer> map = new HashMap<Integer, Integer>(); | ||
int minLength = Integer.MAX_VALUE; | ||
for (int i = 0; i < deck.length; i++) { | ||
int v = 1; | ||
if (map.containsKey(deck[i])) { | ||
v = map.get(deck[i]) + 1; | ||
} | ||
map.put(deck[i], v); | ||
} | ||
for (Integer key : map.keySet()) { | ||
if (minLength > map.get(key)) { | ||
minLength = map.get(key); | ||
} | ||
} | ||
for (int i = minLength; i > 1; i--) { | ||
boolean flag = true; | ||
for (Integer key : map.keySet()) { | ||
if (map.get(key) % i != 0) { | ||
flag = false; | ||
break; | ||
} | ||
} | ||
if (flag) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package leetcode; | ||
|
||
public class ContainerWithMostWater11 { | ||
|
||
public static void main(String[] args) { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
public int maxArea(int[] height) { | ||
int max = 0; | ||
int l = 0, r = height.length - 1; | ||
while (l < r) { | ||
int min; | ||
if (height[l] < height[r]) { | ||
min = height[l]; | ||
} else { | ||
min = height[r]; | ||
} | ||
int area = min * (r - l); | ||
if (area > max) { | ||
max = area; | ||
} | ||
if(height[l]<height[r]) { | ||
l++; | ||
}else { | ||
r--; | ||
} | ||
} | ||
return max; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Solution: | ||
def divide(self, dividend: int, divisor: int) -> int: | ||
negativeFlag = False | ||
if dividend>0 and divisor<0: | ||
divisor = 0 - divisor | ||
negativeFlag = True | ||
elif dividend<0 and divisor>0: | ||
dividend = 0 - dividend | ||
negativeFlag=True | ||
elif dividend<0 and divisor<0: | ||
divisor = 0 - divisor | ||
dividend = 0 - dividend | ||
|
||
result = 0 | ||
while dividend>=divisor: | ||
dividend = dividend-divisor | ||
result+=1 | ||
if negativeFlag: | ||
result = 0 - result | ||
return result | ||
|
||
solution = Solution() | ||
print(solution.divide(-2147483648,-1)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Solution: | ||
def divide(self, dividend: int, divisor: int) -> int: | ||
negativeFlag = False | ||
if dividend>0 and divisor<0: | ||
divisor = 0 - divisor | ||
negativeFlag = True | ||
elif dividend<0 and divisor>0: | ||
dividend = 0 - dividend | ||
negativeFlag=True | ||
elif dividend<0 and divisor<0: | ||
divisor = 0 - divisor | ||
dividend = 0 - dividend | ||
|
||
result = 0 | ||
while dividend>=divisor: | ||
tempDivisor = divisor | ||
divisorCount = 1 | ||
while dividend>= tempDivisor: | ||
dividend = dividend-tempDivisor | ||
result+=divisorCount | ||
divisorCount +=divisorCount | ||
tempDivisor += tempDivisor | ||
if negativeFlag: | ||
result = 0 - result | ||
return min(max((-2)**31,result),2**31-1) | ||
|
||
solution = Solution() | ||
print(solution.divide(-2147483648,-1)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Solution: | ||
def __init__(self): | ||
self.results = [] | ||
def divide2ways(self,n,tempArray:list,result:str): | ||
if n==0: | ||
while len(tempArray)>0: | ||
tempArray.pop(-1) | ||
result+=")" | ||
self.results.append(result) | ||
else: | ||
if len(tempArray)!=0: | ||
tempResult = result | ||
tempResult+=")" | ||
newTempArray = list(tempArray) | ||
newTempArray.pop(-1) | ||
self.divide2ways(n,newTempArray,tempResult) | ||
tempArray.append("(") | ||
result+="(" | ||
self.divide2ways(n-1, tempArray, result) | ||
|
||
def generateParenthesis(self, n: int): | ||
# tempArray = [] | ||
self.divide2ways(n,[],"") | ||
return self.results | ||
|
||
if __name__ == "__main__": | ||
solution = Solution() | ||
print(solution.generateParenthesis(1)) |
39 changes: 39 additions & 0 deletions
39
leetcode/HackerRank/SubstringWithConcatenationOfAllWords30.py
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from __future__ import print_function | ||
|
||
if __name__ == '__main__': | ||
n = int(input()) | ||
for i in range(1,n+1): | ||
print(i,end="") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class Solution: | ||
def strStr(self, haystack: str, needle: str) -> int: | ||
i = 0 | ||
if len(haystack)==0 and len(needle)==0: | ||
return 0 | ||
if len(haystack)<len(needle): | ||
return -1 | ||
while i < len(haystack): | ||
flag= True | ||
if len(haystack)-i<len(needle): | ||
return -1 | ||
for j in range(len(needle)): | ||
if haystack[i+j] != needle[j]: | ||
flag=False | ||
break | ||
if flag: | ||
break | ||
i+=1 | ||
if i== len(haystack): | ||
return -1 | ||
return i | ||
|
||
|
||
solution = Solution() | ||
print(solution.strStr("mississippia","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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package leetcode; | ||
|
||
public class IntegertoRoman12 { | ||
|
||
public static void main(String[] args) { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
public String intToRoman(int num) { | ||
while(num-1000>0) { | ||
|
||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
digits = "23" | ||
digitMap = {} | ||
# print(ord("A")) | ||
index = ord("a") | ||
end = ord("z") | ||
for i in range(2,10): | ||
letters = [] | ||
end = 3 | ||
if i==7 or i== 9: | ||
end=4 | ||
for j in range(0,end): | ||
letters.append(chr(index+j)) | ||
index+=end | ||
digitMap[i]=letters | ||
print(digitMap) | ||
result = [] | ||
preResult = [] | ||
if len(digits)<1: | ||
# return [] | ||
print([]) | ||
firstDigit = digits[0] | ||
firstStrs = digitMap[int(firstDigit)] | ||
# BFS!!!!! | ||
for digitChar in firstStrs: | ||
result.append(digitChar) | ||
for i in range(1,len(digits)): | ||
chars = digitMap[int(digits[i])] | ||
preResult = list(result) | ||
result=[] | ||
while len(preResult)>0: | ||
topResult = preResult.pop(0) | ||
for digitChar in chars: | ||
copiedTopResult = topResult | ||
copiedTopResult+=digitChar | ||
result.append(copiedTopResult) | ||
print(result) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package leetcode; | ||
|
||
|
||
public class ListNode { | ||
int val; | ||
ListNode next; | ||
|
||
ListNode(int x) { | ||
val = x; | ||
} | ||
ListNode(int [] a) { | ||
val = a[0]; | ||
ListNode c =new ListNode(val); | ||
for(int i=1;i<a.length;i++) { | ||
|
||
ListNode n =new ListNode(a[1]); | ||
c.next = n; | ||
c=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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package leetcode; | ||
|
||
public class LongestCommonPrefix14 { | ||
|
||
public static void main(String[] args) { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
public String longestCommonPrefix(String[] strs) { | ||
String result = ""; | ||
if(strs.length==0) { | ||
return ""; | ||
} | ||
String first = strs[0]; | ||
int index = 0; | ||
while (index < first.length()) { | ||
boolean flag = true; | ||
for (int i = 1; i < strs.length; i++) { | ||
if (index>=strs[i].length()||strs[i].charAt(index) != first.charAt(index)) { | ||
flag = false; | ||
break; | ||
} | ||
} | ||
if(flag) { | ||
result=result+first.charAt(index); | ||
index++; | ||
}else { | ||
break; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} |
Oops, something went wrong.