# leetcode周期小结1

1.两数之和

给定 nums = [2, 7, 11, 15], target = 9

class Solution {
public int[] twoSum(int[] nums, int target) {

for(int i=0;i<nums.length;i++)
for(int j=i+1;j<nums.length;j++)
{
if(nums[i]+nums[j]==target)
{
int [] a={i,j};
return a;
}

}
return null;
}

}

public class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();
int[] res = new int[2];
for (int i = 0; i < nums.length; ++i) {
if (m.containsKey(target - nums[i])) {
res[0] = i;
res[1] = m.get(target - nums[i]);
break;
}
m.put(nums[i], i);
}
return res;
}
}

2.两数相加

输入：(2 -> 4 -> 3) + (5 -> 6 -> 4)

/**

* Definition for singly-linked list.
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
class Solution {
int push=0;
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1==null && l2==null && push!=0) return new ListNode(push);
else if(l1==null &&l2==null) return null;

if(l1==null)
if(l2==null)
int sum=l1.val+l2.val+push;
if(sum>=10)
push=1;
else
push=0;
ListNode a=new ListNode(sum%10);
return a;
}

}

1. /**
2.  * Definition for singly-linked list.
3.  * struct ListNode {
4.  *     int val;
5.  *     ListNode *next;
6.  *     ListNode(int x) : val(x), next(NULL) {}
7.  * };
8.  */
9. class Solution {
10. public:
11.     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
12.         int flag = 0;
13.         int val = 0;
14.         ListNode* head1 = l1;
15.         ListNode* head2 = l2;
16.         ListNode* p1;
17.         ListNode* p2;
18.         ListNode* temp;
19.
20.         while(l1!=NULL&&l2!=NULL)
21.         {
22.             val = l1->val+l2->val;
23.
24.             l1->val = val;
25.             l2->val = val;
26.             p1 = l1;
27.             p2 = l2;
28.             l1 = l1->next;
29.             l2 = l2->next;
30.         }
31.     /**
32.     *  默认最长链表为l1，若l2较长则将它们交换
33.     */
34.         if(l2!=NULL&&l1==NULL)
35.         {
36.             temp = l1;
37.             l1 = l2;
38.             l2 = temp;
39.             temp = head1;
42.             temp = p1;
43.             p1 = p2;
44.             p2 = temp;
45.         }
46.     /**
47.     *  做进位判断
48.     */
49.        l1 = head1;
50.        while(l1!=NULL)
51.        {
52.            val = l1->val + flag;
53.            flag = 0;
54.            if(val>9)
55.            {
56.                flag = 1;
57.                val = val - 10;
58.            }
59.            l1->val = val;
60.            p1 = l1;
61.            l1 = l1->next;
62.        }
63.        if (flag==1)
64.        {
65.            p2->val = flag;
66.            p1->next = p2;
67.        }
69.
70.     }
71. };

3.无重复字符的最长子串

class Solution {
public static int lengthOfLongestSubstring(String s) {

int[] ASCII=new int[127];
for(int i=0;i<127;i++) ASCII[i]=-1;
int Flag=0;
int length=0;

for(int i=0;i<s.length();i++)
{
if(ASCII[s.charAt(i)]>=Flag)          //check repeat
{
length=max(length,i-Flag);
Flag=ASCII[s.charAt(i)]+1;
}
ASCII[s.charAt(i)]=i;
}
length=max(length,s.length()-Flag);
return length;
}
public static int max(int a,int b)
{
if(a>=b)
return a;
else
return b;
}

}

1. int LengthOfLongestSubstring(char *str)
2. {
3.     int i,j;
4.     int maxlen = 0; //要初始化，不然产生意外结果！
5.     int n = strlen(str);
6.     int arr[256] = {0};     //设置hash表，索引为字符的值，值为是否存在该字符！
7.
8.     for(i = 0,j = 0;j < n;j++){
9.         while(arr[str[j]] == 1){
10.             arr[str[i]] = 0;
11.             ++i;
12.         }
13.         arr[str[j]] = 1;
14.         maxlen = maxlen > (j-i) ? maxlen : j-i+1;
15.     }
16.
17.     return maxlen;

5.最长回文子串

输入: "babad"



输入: "cbbd"

class Solution {
public static String longestPalindrome(String s) {
if(s.length()==1) return s;
int [] midsize=new int[s.length()];
int [] sidesize=new int[s.length()];
for (int i = 0; i < s.length(); i++) {
runmid(s,i,midsize);
runsize(s,i,sidesize);
}
int maxmid=0;int midpoint=0;
int maxside=0;int sidepoint=0;
for(int i=0;i<s.length();i++)
{
if(midsize[i]>maxmid) {midpoint=i;maxmid=midsize[i];}
if(sidesize[i]>maxside) {sidepoint=i;maxside=sidesize[i];}
}
if(midpoint==0&&sidepoint==0&&s.charAt(0)!=s.charAt(1)) return s.charAt(0)+"";
if(maxmid*2+1>maxside*2&&maxmid!=0) return midtostring(midpoint,s,midsize[midpoint]);
else return sidetostring(sidepoint,s,sidesize[sidepoint]);
}
public static void  runmid(String s,int mid,int  [] size) {
int i=mid+1;
for(;i<s.length()&&2*mid-i>=0;i++)
{
if(s.charAt(i)==s.charAt(2*mid-i)) ;
else break;
}
size[mid]=i-mid-1;

}
public static void runsize(String s,int left,int[] size)
{   int i=left;
String temp="";
for(;i>=0&&left+left+1-i<s.length();i--)
{

if(s.charAt(i)==s.charAt(2*left+1-i)) ;
else break;
}
size[left]=left-i;
}
public static String midtostring(int mid,String s,int size)
{
String a=""+s.charAt(mid);
for(int i=mid+1;i<=mid+size;i++) a=s.charAt(i)+a+s.charAt(i);
return a;
}
public  static String sidetostring(int side,String s,int size)
{   String a="";
for(int i=side;i>side-size;i--) a=s.charAt(i)+a+s.charAt(i);
return a;
}

}

dp[i, j] = 1                                               if i == j

= s[i] == s[j]                                if j = i + 1

= s[i] == s[j] && dp[i + 1][j - 1]    if j > i + 1

class Solution {
public:
string longestPalindrome(string s) {
int dp[s.size()][s.size()] = {0}, left = 0, right = 0, len = 0;
for (int i = 0; i < s.size(); ++i) {
for (int j = 0; j < i; ++j) {
dp[j][i] = (s[i] == s[j] && (i - j < 2 || dp[j + 1][i - 1]));
if (dp[j][i] && len < i - j + 1) {
len = i - j + 1;
left = j;
right = i;
}
}
dp[i][i] = 1;
}
return s.substr(left, right - left + 1);
}
};

class Solution {
public:
string longestPalindrome(string s) {
string t ="\$#";
for (int i = 0; i < s.size(); ++i) {
t += s[i];
t += '#';
}
int p[t.size()] = {0}, id = 0, mx = 0, resId = 0, resMx = 0;
for (int i = 0; i < t.size(); ++i) {
p[i] = mx > i ? min(p[2 * id - i], mx - i) : 1;
while (t[i + p[i]] == t[i - p[i]]) ++p[i];
if (mx < i + p[i]) {
mx = i + p[i];
id = i;
}
if (resMx < p[i]) {
resMx = p[i];
resId = i;
}
}
return s.substr((resId - resMx) / 2, resMx - 1);
}
};

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120