随笔分类 - leetcode-Integer
摘要:public class Solution { public int singleNumber(int[] A) { int left = A[0]; for(int i=1;i<A.length;i++){ left ^=A[i]; } return left; }}
阅读全文
posted @ 2014-02-22 13:25
krunning
摘要:Given an array of integers, every element appearsthreetimes except for one. Find that single one. 1 public class Solution { 2 public int singleNumber(int[] A) { 3 int bit [] = new int[32]; 4 for(int i=0;i>j; 7 if(rotate==0) break; 8 else bit[j] += ...
阅读全文
posted @ 2014-02-22 13:06
krunning
摘要:Determine whether an integer is a palindrome. Do this without extra space. 1 public class Solution { 2 public boolean isPalindrome(int x) { 3 if(x=10){ 6 div *=10; 7 } 8 while(x>0){ 9 if(x/div!=x%10) return false;10 x = x%div/10;11 ...
阅读全文
posted @ 2014-02-07 03:49
krunning
摘要:Given an arraySofnintegers, are there elementsa,b,cinSsuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero. 1 public class Solution { 2 public ArrayList> threeSum(int[] num) { 3 ArrayList> res = new ArrayList>(); 4 if(num.lengthsum){16 ...
阅读全文
posted @ 2014-02-06 05:28
krunning
摘要:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999. 1 public class Solution { 2 public int romanToInt(String s) { 3 if(s.length()0 && get(s.charAt(i-1))<cur) 8 res += cur-2*get(s.charAt(i-1)); 9 else{10 ...
阅读全文
posted @ 2014-02-06 05:03
krunning
摘要:Given an integer, convert it to a roman numeral. 1 public class Solution { 2 public String intToRoman(int num) { 3 char[] sym = {'I','V','X','L','C','D','M'}; 4 int scale = 1000; 5 StringBuilder sb = new StringBuilder(); 6 for(int i=6;i>=0;i
阅读全文
posted @ 2014-02-06 05:01
krunning
摘要:Implementatoito convert a string to an integer. 1 public class Solution { 2 public int atoi(String str) { 3 char[] s = str.trim().toCharArray(); 4 if(s.length==0){ 5 return 0; 6 } 7 long num = 0; 8 int sign =1; 9 int len = s.length;10 ...
阅读全文
posted @ 2014-02-06 04:58
krunning
摘要:Reverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321 1 public class Solution { 2 public int reverse(int x) { 3 int sign = 1; 4 if(x0){ 9 res = 10*res+x%10;10 x /=10;11 }12 if(res<0) return -1;13 re...
阅读全文
posted @ 2014-02-06 04:57
krunning
浙公网安备 33010602011771号