Guest User

Untitled

a guest
Jul 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.21 KB | None | 0 0
  1. # **LeetCode**
  2.  
  3. #### Solve progres:
  4. - 1.two_numAdd:
  5. ```java
  6. Given nums = [2, 7, 11, 15], target = 9,
  7. Because nums[0]+ nums[1] = 2 + 7 = 9,
  8. return [0, 1].
  9. ```
  10. ## answer:
  11. - 1. Brute:
  12. ```java
  13. for (int i = 0; i < nums.length; i++) {
  14. for (int j = i + 1; j < nums.length; j++) {
  15. if (nums[j] == target - nums[i]) {
  16. return new int[] { i, j };
  17. }
  18. }
  19. }
  20. ```
  21. #### <span style="color:red">☆★Time complexity : O(n^2)、Space complexity : O(1)。☆★</span>
  22.  
  23. - 2. Advanced:
  24. ```java
  25. Map<Integer, Integer> map = new HashMap<>();
  26. for (int i = 0; i < nums.length; i++) {
  27. map.put(nums[i], i);
  28. }
  29. for (int i = 0; i < nums.length; i++) {
  30. int complement = target - nums[i];
  31. if (map.containsKey(complement) && map.get(complement) != i) {
  32. return new int[] { i, map.get(complement)};
  33. }
  34. }
  35. ```
  36. #### <span style="color:red">☆★Time complexity : O(n)、Space complexity : O(n) because the map。☆★</span>
  37.  
  38. - 2.addTwoNumbers:
  39. ## answer:
  40. ```java
  41. ListNode h = new ListNode(0);
  42. ListNode p3=h;
  43. int r=0;
  44. while(l1 !=null || l2!=null){
  45. if(l1 !=null){
  46. r+=l1.val;
  47. l1=l1.next;
  48. }
  49. if(l2 !=null){
  50. r+=l2.val;
  51. l2=l2.next;
  52. }
  53. p3.next= new ListNode(r%10);
  54. r/=10;
  55. p3=p3.next;
  56. }
  57. if(r==1)
  58. p3.next=new ListNode(1);
  59.  
  60. return h.next;
  61. ```
  62. declarate two ListNode(p3,h),p3 use to set value ,and h is final result. The Last r==1 is used to deal with carry.
  63. #### <span style="color:red">☆★Time complexity : O(max(m,n))、Space complexity : O(max(m,n))。☆★</span>
  64.  
  65. - 3. Longest Substring Without Repeating Characters:
  66. ```java
  67. Given "abcabcbb", the answer is "abc", which the length is 3.
  68. Given "pwwkew", the answer is "wke", with the length of 3.
  69. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
  70. ```
  71. ## answer:
  72. - 1:
  73. ```java
  74. String temp="";
  75. String result="";
  76. for(int i=0;i<s.length();i++) {
  77. String curr = String.valueOf(s.charAt(i));
  78. if(!temp.contains(curr)) {
  79. temp+=curr;
  80. }else {
  81. temp = temp.substring(temp.indexOf(curr)+1, temp.length())+curr;
  82. }
  83. if(temp.length() > result.length()) {
  84. result = temp;
  85. }
  86. }
  87. return result.length();
  88. ```
  89. Hint: `temp.substring(temp.indexOf(curr)+1, temp.length())+curr` . the substring method splits after first duplicated character to end of the temp,length()-1 and add new character. EX: ori: abca -> a|bca.
  90. #### <span style="color:red">☆★Time complexity : O(n)、Space complexity : O(min(m,n))。☆★</span>
  91.  
  92. - 2:
  93. ```java
  94. int maxLenth=0;
  95. HashSet<Character> set = new HashSet<>();
  96. int i=0,j=0;
  97.  
  98. while(j<s.length()) {
  99. if(!set.contains(s.charAt(j))) {
  100. set.add(s.charAt(j));
  101. j++;
  102. maxLenth=Math.max(maxLenth, j-i);
  103. }else {
  104. set.remove(s.charAt(i));
  105. i++;
  106. }
  107. }
  108. return maxLenth;
  109. ```
  110. Hint: `set.remove(s.charAt(i))` . It remove duplicated character in the set until not duplicated character. Thus add new character
  111. #### <span style="color:red">☆★Time complexity:O(n)、Space complexity:O(min(m,n))。☆★</span>
  112.  
  113.  
  114. - 5. Longest Palindromic Substring:
  115. ###### Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
  116. ```java
  117. Input: "BANANA"
  118. Output: "ANANA"
  119. ```
  120. ## answer:
  121. - 1. Dynamic Programming:
  122. ```java
  123. int len = s.length();
  124. boolean[][] isP = new boolean[len][len];
  125. int left=0,right=0;
  126. for(int j=1;j<len;j++) {
  127. for(int i=0;i<j;i++) {
  128. boolean innerP = isP[i+1][j-1]||j-i<=2;
  129. if(s.charAt(i)==s.charAt(j)&&innerP) {
  130. isP[i][j]=true;
  131. if(j-i > right-left) {
  132. left=i;
  133. right=j;
  134. }
  135. }
  136. }
  137. }
  138. return s.substring(left, right+1);
  139. ```
  140. Hint: `boolean innerP = isP[i+1][j-1]||j-i<=2;` =>First: `j-i<=2;` is true,it means compare element and next element, EX:AN,NA,AN,NA,ANA,NAN. Second:`isP[i+1][j-1]`, it will saved the palindromic substring between [i][j]. For instance: [0][5] it need to find the result of String [1]~[4] is palindromic substring or not.
  141. #### <span style="color:red">☆★Time complexity : O(n^2)、Space complexity : O(n^2)。☆★</span>
  142. - 7. Reverse Integer:
  143. ###### Given a 32-bit signed integer, reverse digits of an integer. EX:
  144. | Input | Output |
  145. | ------| ------ |
  146. | 123 | 321 |
  147. | -123 | -321 |
  148. | 120 | 21 |
  149.  
  150.  
  151. ## answer:
  152. ```java
  153. public static int reverse(int x) {
  154. boolean sign=false;
  155. if(x<0) {
  156. sign=true;
  157. x=Math.abs(x);
  158. }
  159. int result=0;
  160. char[] ori=String.valueOf(x).toCharArray();
  161. String str="";
  162. if(x>0 && !sign ) {
  163. str+=common(ori.length,ori);
  164. }else {
  165. str+="-";
  166. str+=common(ori.length,ori);
  167. }
  168. try{
  169. result=Integer.valueOf(str);
  170. return result;
  171. }catch(NumberFormatException e){
  172. return 0;
  173. }
  174. }
  175. public static String common(int s_len,char[] ori) {
  176. String str="";
  177. for(int i=s_len-1;i>=0;i--) {
  178. str+=ori[i];
  179. }
  180. return str;
  181. }
  182. ```
  183. #### <span style="color:red">☆★Time complexity : O(1)、Space complexity : O(n)。☆★</span>
  184.  
  185. - 11. Container With Most Water:
  186. ###### Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). EX: [9,8,6,5,3]
  187. | Input | Output |
  188. | ------| ------ |
  189. |9 | (9,0),(9,9)|
  190. |8 | (8,0),(8,8)|
  191. |6 | (6,0),(6,6)|
  192. |5 | (5,0),(5,5)|
  193. |3 | (3,0),(3,3)|
  194.  
  195.  
  196. ## answer:
  197. ```java
  198. public static int maxArea(int[] height) {
  199. int maximum=0,tmp=0;
  200. int i=0,j=height.length-1;
  201. while(i<j) {
  202. tmp = Math.min(height[i], height[j])*Math.abs(i-j);
  203. if(tmp > maximum) {
  204. maximum = tmp;
  205. }
  206. if(height[i] < height[j]) {
  207. i++;
  208. }else {
  209. j--;
  210. }
  211. }
  212. return maximum;
  213. }
  214. ```
  215. #### <span style="color:red">☆★Time complexity : O(n)、Space complexity : O(1) only Constant space。☆★</span>
  216.  
  217. - 11. Roman to Integer:
  218. ###### Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
  219. | Symbol| Value |
  220. | ------| ------ |
  221. |I | 1 |
  222. |V | 5 |
  223. |X | 10 |
  224. |L | 50 |
  225. |C | 100 |
  226. |D | 500 |
  227. |M | 1000 |
  228.  
  229. | Input | Output |
  230. | ------| ------ |
  231. |III | 3 |
  232. |IV | 4 |
  233. |IX | 9 |
  234. |MCMXCIV|1994 |
  235.  
  236.  
  237.  
  238. ## answer:
  239. - 1.method one:
  240. ```java
  241. public static int romanToInt(String s) {
  242. int result=0;
  243. List<Integer> result_list = new ArrayList<Integer>();
  244. for(int i=0;i<s.length();i++) {
  245. if(i==0) {
  246. result_list.add(rToi(s.charAt(i)));
  247. }else {
  248. if(rToi(s.charAt(i-1)) >= rToi(s.charAt(i))){
  249. result_list.add(rToi(s.charAt(i)));
  250. }
  251. else {
  252. result_list.set(i-1, result_list.get(i-1)*-1);
  253. result_list.add(rToi(s.charAt(i)));
  254. }
  255. }
  256. }
  257. for(int i=0;i<result_list.size();i++) {
  258. result+=result_list.get(i);
  259. }
  260. return result;
  261. }
  262. ```
  263. - 2.method two:
  264. ```java
  265. public static int romanToInt(String s) {
  266. int result=0;
  267. int temp=0;
  268. for(int i=0;i<s.length();i++) {
  269. int data = rToi(s.charAt(i));
  270. if(temp >= data)
  271. result+=data;
  272. else
  273. result=data+(result+temp*-2);
  274. temp=data;
  275. }
  276. return result;
  277. }
  278. ```
  279.  
  280. #### <span style="color:red">☆★Time complexity : O(n)、Space complexity : O(1) only Constant space。☆★</span>
Add Comment
Please, Sign In to add comment