1、Two Sum
用hashMap保存,用数值做key,用索引做value
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap map = new HashMap();
int[] res = new int[2];
for(int i = 0;i
2. Add Two Numbers
用了两个标志,一个是当前的val 另一个是进位信息more
注意两点,两个表可能不一样长,最后要考虑是不是还有进位
/**
* 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 dummy = new ListNode(-999);
ListNode p = dummy;
ListNode p1 = l1;
ListNode p2 = l2;
int more = 0;
int val = 0;
while(p1 != null && p2 != null){
val = (p1.val + p2.val + more) % 10;
more = (p1.val + p2.val + more) / 10;
ListNode next = new ListNode(val);
p.next = next;
p = next;
p1 = p1.next;
p2 = p2.next;
}
while(p1 != null){
val = (p1.val + more) % 10;
more = (p1.val + more) / 10;
ListNode next = new ListNode(val);
p.next = next;
p = next;
p1 = p1.next;
}
while(p2 != null){
val = (p2.val + more) % 10;
more = (p2.val + more) / 10;
ListNode next = new ListNode(val);
p.next = next;
p = next;
p2 = p2.next;
}
if(more > 0){
ListNode next = new ListNode(more);
p.next = next;
}
return dummy.next;
}
}
3. Longest Substring Without Repeating Characters
用map存储每个字符以及字符所在的位置,同时定义了一个标记位,标记当前的不重复字符串是从哪开始的,标记为同时满足两个条件才会变动,当前的map中包含当前的字符,当前字符在字典中的位置位于标记位后面
class Solution {
public int lengthOfLongestSubstring(String s) {
int maxlength = 0;
HashMap map = new HashMap();
int firstplace = 0;
for(int i = 0; i< s.length();i++){
if((!map.containsKey(s.charAt(i))) || (map.get(s.charAt(i)) < firstplace)){
maxlength = Math.max(maxlength,i-firstplace + 1);
}
else{
firstplace = map.get(s.charAt(i)) + 1;
}
map.put(s.charAt(i),i);
}
return maxlength;
}
}
4. Median of Two Sorted Arrays
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int m = nums1.length;
int n = nums2.length;
if(m > n){
int[] temp = nums1; nums1 = nums2; nums2 = temp;
int tmp = m; m = n; n = tmp;
}
int iMin = 0; int iMax = m; int halfLen = (m + n + 1) / 2;
while(iMin <= iMax){
int i = (iMax + iMin) / 2; int j = halfLen - I;
if(i nums1[I])
iMin = i + 1;
else if(i>iMin && nums1[i-1] > nums2[j])
iMax = i - 1;
else{
int maxLeft = 0;
if (i == 0) { maxLeft = nums2[j-1]; }
else if (j == 0) { maxLeft = nums1[i-1]; }
else { maxLeft = Math.max(nums1[i-1], nums2[j-1]); }
if ( (m + n) % 2 == 1 ) { return maxLeft; }
int minRight = 0;
if (i == m) { minRight = nums2[j]; }
else if (j == n) { minRight = nums1[i]; }
else { minRight = Math.min(nums2[j], nums1[i]); }
return (maxLeft + minRight) / 2.0;
}
}
return 0.0;
}
}
5. Longest Palindromic Substring
循环从每个位置往两边延伸,判断是否是回文串
class Solution {
public String longestPalindrome(String s) {
int start = 0, end = 0;
for (int i = 0; i < s.length(); i++) {
int len1 = expandAroundCenter(s, i, i);
int len2 = expandAroundCenter(s, i, i + 1);
int len = Math.max(len1, len2);
if (len > end - start) {
start = i - (len - 1) / 2;
end = i + len / 2;
}
}
return s.substring(start, end + 1);
}
private int expandAroundCenter(String s, int left, int right) {
int L = left, R = right;
while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) {
L--;
R++;
}
return R - L - 1;
}
}
7. Reverse Integer
rev的类型一定要是long,不能是int
class Solution {
public int reverse(int x) {
long rev= 0;
while( x != 0){
rev= rev*10 + x % 10;
x= x/10;
if( rev > Integer.MAX_VALUE || rev < Integer.MIN_VALUE)
return 0;
}
return (int) rev;
}
}
9. Palindrome Number
负数都返回false,首先判断这个x是几位数,然后每次循环得到最左边的数和最右边的数,然后判断两边是否相等,然后把x截掉两边。
class Solution {
public boolean isPalindrome(int x) {
if(x<0)
return false;
int flag = 1;
while(x / flag >= 10)
flag *= 10;
while(x != 0){
int left = x / flag;
int right = x % 10;
if(left!=right)
return false;
x = x % flag / 10;
flag /= 100;
}
return true;
}
}
10. Regular Expression Matching
采用动态规划的方法,具体的过程看下图以及我在算法中写的注释,可以参考博客:http://blog.csdn.net/hk2291976/article/details/51165010
class Solution {
public boolean isMatch(String s, String p) {
int col = s.length();
int row = p.length();
//建立一个boolean数组,数组长度为 row + 1 * col + 1,多的一列一行代表空串的情况
boolean[][] arr = new boolean[row+1][col+1];
arr[0][0] = true;
//第一行剩余元素全部变为false,因为pattern如果是空串,那么只要s不是空串,都不能匹配
for(int i=1;i<=col;i++)
arr[0][i] = false;
//初始化第0列,此时s是空串,所以只能是x*y*这种形式的
//注意,j-1代表的真实的p的索引
for(int j=1;j<=row;j++)
arr[j][0] = (j>1) && (p.charAt(j-1)=='*') && arr[j-2][0];
//主体循环开始
for(int i=1;i<=col;i++){
for(int j=1;j<=row;j++){
//如果当前的字符是 * 的话,要么当空使用,即arr[j-2][i]为true
//要么不当空使用,此时首先p.charAt(j-2)==s.charAt(i-1) || p.charAt(j-2)=='.',即*前面的字符匹配了当前s的字符
//同时要使arr[j][i-1]为true,如果i-1>0,此时是连续复制的情况,如果i-1=0,那么就是匹配空串的情况,我们知道a*这种形式是可以匹配空串的
if(p.charAt(j-1)=='*')
arr[j][i] = arr[j-2][i] || (p.charAt(j-2)==s.charAt(i-1) || p.charAt(j-2)=='.') && arr[j][i-1];
//如果当前的字符不是*的话,要么相等,要么p是'.',同时还要满足arr[j-1][i-1]为true,前面不匹配,这个匹配了也没用
else
arr[j][i] = (p.charAt(j-1)=='.' || p.charAt(j-1)==s.charAt(i-1)) && arr[j-1][i-1];
}
}
return arr[row][col];
}
}