[leetcode][Algorithm] -13. Roman to Integer with [Java]

problem class Solution { public int romanToInt(String s) { int N = s.length(); int sum = 0; char a = 0; int b = 0; int c = 0; for(int i=0;i<N;i++){ c = b; a = s.charAt(i); if(a == 'I'){ b = 1; }else if(a == 'V'){ b = 5; }else if(a == 'X'){ b = 10; }else if(a == 'L'){ b = 50; }else if(a == 'C'){ b = 100; }else if(a == 'D'){ b = 500; }else if(a == 'M'){ b = 1000; } // System....

SuCicada

[leetcode][DataBase] - 175. Combine Two Tables with MySQL

题目 select FirstName, LastName, City, State from Person left join Address using (PersonId); or select FirstName, LastName, City, State from Person left join Address on Person.PersonId=Address.PersonId;

SuCicada

[leetcode][DataBase] - 176. Second Highest Salary with [MySQL]

problem select ifnull((select distinct Salary from Employee order by Salary desc limit 1,1), null) as SecondHighestSalary; or other select max(Salary) as SecondHighestSalary from employee where Salary < (select max(Salary) from employee);

SuCicada

[leetcode][DataBase] - 177. Nth Highest Salary with [MySQL]

problem # 184ms select min(aa.Salary) from (select distinct Salary from employee order by Salary desc limit N) as aa where (select count(distinct Salary) from employee) >= N or # 188ms select aa.Salary as ass from (select distinct Salary from employee order by Salary desc limit N) as aa where (select count(distinct Salary) from employee) >= N order by ass limit 1

SuCicada

[leetcode][DataBase] - 178. Rank Scores with [MySQL]

problem select inn.Score,inn.Rank from ( select ss.Score,@row:=@row+1 as Rank from (select @row:=0) a, (select Score from Scores group by Score) as ss order by ss.Score desc ) as inn right join Scores as sss on inn.Score = sss.Score order by inn.Score desc ;

SuCicada

leetcode - 12. Integer to Roman with Java

problem 方法一,一位一位判断。 class Solution { public String intToRoman1(int num) { StringBuilder sb = new StringBuilder(); char ooo[] = new char[]{'I','V','X','L','C','D','M'}; int wei = 0; while(num>0){ int n = num %10; if(n<4){ for(int i=0;i<n;i++){ sb.append(ooo[wei*2]); } }else if(n==4){ sb.append(ooo[wei*2+1]); sb.append(ooo[wei*2]); }else if(n==5){ sb.append(ooo[wei*2+1]); }else if(n>5 && n<9){ for(int i=0;i<n-5;i++){ sb.append(ooo[wei*2]); } sb.append(ooo[wei*2+1]); }else if(n==9){ sb.append(ooo[wei*2+1+1]); sb.append(ooo[wei*2]); } wei++; num = num/10; } return sb.reverse().toString(); } 方法二,因为知道最多4位数,所以打表暴力。 public String intToRoman(int num) { String s1[] = new String[]{"","I","II","III","IV","V","VI","VII","VIII","IX"}; String s2[] = new String[]{"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"}; String s3[] = new String[]{"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}; String s4[] = new String[]{"","M","MM","MMM"}; // return s4[(num/1000)%10]+s3[(num/100)%10]+s2[(num/10)%10]+s1[num%10]; return new StringBuilder()....

SuCicada

leetcode - 1. Two Sum with Java

https://leetcode.com/problems/two-sum/submissions/ import java.util.*; class Main { public int[] fun(int []nums,int target) { Map<Integer,Integer> map = new HashMap<>(); for(int i=0;i<nums.length;i++){ int f = target - nums[i]; if(map.get(f)!=null){ return new int[]{map.get(f),i}; }else{ map.put(nums[i],i); } } return null; } public static void main(String[] args) { int []nums = {12,3,3,53,45,7,7,3}; int []res = new Main().fun(nums,10); System.out.println(Arrays.toString(nums)); System.out.println(Arrays.toString(res)); } }

SuCicada

leetcode - 11. Container With Most Water with Java

problem import java.lang.reflect.Method; import java.util.*; class Solution { public int maxArea(int[] height) { int max = 0; int left = 0; int right = height.length-1; int a,b; while(left<right){ a = height[left]; b = height[right]; max = Math.max(max, Math.min(a,b)*(right-left)); if(a > b){ right -- ; }else{ left ++; } System.out.println(left+" "+right+" "); } return max; } } class Main { public static void main(String[] args) throws Exception{ Solution solution = new Solution(); int i = solution....

SuCicada

leetcode - 2. Add Two Numbers with Java

https://leetcode.com/problems/add-two-numbers/submissions/ /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode res = new ListNode(0); ListNode head = res; int flag = 0; while(l1!=null || l2!=null || flag!=0){ int num = (l1==null?0:l1.val) + (l2==null? 0: l2.val) + flag; res.next = new ListNode(num%10); flag = num/10; res = res....

SuCicada

leetcode - 3. Longest Substring Without Repeating Characters with Java

题目链接 import java.lang.reflect.Method; class Solution { /*两个方法都是:end无所谓的在一个一个移动,begin则是追寻之前的循环体留下的信标 关键在于信标是怎么设置下的,第一种方法是每一次都从序列中寻找信标。 第二种直接记录了下来。 一个用时间换空间,一个用空间换时间。 */ public int lengthOfLongestSubstring1(String s) { int sum = 0; int begin = 0; int end = 0; for(end = begin;end<s.length();end++){ /*每次都移动尾指针*/ for(int i = begin;i<end;i++){ /*验证重复,和end处比较,如果没有重复,说明这个子串可以加入end处的字符 而无论是否重复,都不影响移动尾指针end,影响的只是begin的位置。 */ if(s.charAt(i) == s.charAt(end)){ sum = Math.max(sum,end-begin); begin = i+1; break; } } } /*最后的end会指向最后一个的下一个,所以需要再判断一下夹住的是不是大的, eg: ab or aab */ return Math.max(sum,end-begin); } public int lengthOfLongestSubstring2(String s) { int ascii[] = new int[128]; int sum = 0; int begin = 0; for(int i=0;i<s....

SuCicada

leetcode - 4. Median of Two Sorted Arrays with Java

简单做法, 左边哪一些, 右边拿一下, 看谁小拿谁. 但是时间复杂度达不到要求, 虽然在leetcode上也是很快 public double findMedianSortedArrays1(int[] A,int[] B){ /* time complexity: O((m+n)/2+1) */ int m = A.length; int n = B.length; int index1 = 0; int index2 = 0; int num1=0; int num2=0; for(int i=0;i<(m+n)/2+1;i++){ num2 = num1; if(index1 == m){ num1 = B[index2]; index2 ++; }else if(index2 == n){ num1 = A[index1]; index1++; }else if(A[index1] < B[index2]){ num1 = A[index1]; index1++; }else{ num1 = B[index2]; index2++; } } System....

SuCicada

leetcode - 5. Longest Palindromic Substring with Java

题目在我这里 方法一,大暴力 public String longestPalindrome1(String s) { /* very force: O(s.length()^2) 空间复杂度 O(1) */ int i=0; int j=0; boolean flag = false; int maxi = i; int maxj = j; for(i=0;i<s.length();i++){ for(j=s.length()-1;j>i;j--){ if(s.charAt(i)==s.charAt(j)){ int ii = i; int jj = j; for(jj=j;jj>ii;jj--,ii++){ if(s.charAt(ii)!=s.charAt(jj)){ // flag = true; break; } } System.out.println(ii+" - "+jj); if(ii>=jj){ flag = true; break; } } } if(flag){ if(j-i > maxj-maxi){ maxj = j; maxi = i; } } } if(s....

SuCicada

leetcode - 6. ZigZag Conversion with Java

题目地址 这道题涉及到String, StringBuffer, StringBuilder的知识点 String在拼接时需要new新的String对象, 而它除了hash之外都是final属性, 分配空间会造成时间损耗. 所以String不适合大量拼接. 使用String可是耗时18ms呢 StringBuffer解决了这个问题, 它提升了速度, 是线程安全. 而StringBuilder 和StringBuffer类似, 但它是线程不安全, 这也使得它比StringBuffer拼接更快. 本题只需3ms 有没有更快的呢, 当然有, 还就是它了 char[], 只需2ms(笑) import java.lang.reflect.Method; import java.util.*; class Solution { public String convert(String s, int numRows) { int N = s.length(); // StringBuilder res = new StringBuilder(); char res[] = new char[N]; int cycle = (numRows-1)*2; int index = 0; if(numRows==1){ return s; } for(int row=0;row<numRows;row++){ /* row 0 6 1 4 2 2 2 4 3 0 */ for(int i=row;i<N;i+=cycle){ /* 一个锯齿*/ // res....

SuCicada

leetcode - 7. Reverse Integer with Java

where is problem 搞笑的空间复杂度 使用long,最后进行比较会更小,不知道为什么 class Solution { public int reverse(int x) { boolean minus = x<0; x *= minus?-1:1; int res = 0; while(x>0){ if(res*10/10 != res){ return 0; } res = res*10 + x%10; x = x/10; } return res * (minus?-1:1); } }

SuCicada

leetcode - 8. String to Integer (atoi) with Java

题目 class Solution { public int myAtoi(String str) { int N = str.length(); int minus = 0; long res = 0; for(int i=0;i<N;i++){ char ch = str.charAt(i); System.out.println(ch+" "+minus); if((ch == '-' || ch == '+') && minus == 0){ minus = ch=='+'?1:-1; }else if(ch ==' ' && minus==0){ }else if(ch <='9' && ch>='0'){ if(minus == 0){ minus = 1; } res = res*10 + ch-'0'; if(res != (int)res){ return minus==1?...

SuCicada

leetcode - 9. Palindrome Number with Java

题目 class Solution { public boolean isPalindrome(int x) { if(x<0){ return false; } int a = 0; int b = x; while(x>0){ a = a * 10 + x%10; x = x/10; } return a==b; } }

SuCicada