Chinaunix首页 | 论坛 | 博客
  • 博客访问: 97529
  • 博文数量: 21
  • 博客积分: 145
  • 博客等级: 入伍新兵
  • 技术积分: 250
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-22 17:37
文章分类

全部博文(21)

文章存档

2013年(16)

2012年(5)

我的朋友

分类: C/C++

2013-04-10 21:12:14

链接:
Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. 


Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

思路:不要忘记进位,其它基本上就是链表操作了。

点击(此处)折叠或打开

  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.         // Start typing your C/C++ solution below
  13.         // DO NOT write int main() function
  14.         ListNode *result = NULL;
  15.         int append = 0;
  16.         ListNode *ptr, *pre;
  17.         while (l1 && l2) {
  18.             int sum = l1->val + l2->val + append;
  19.             append = sum / 10;
  20.             ptr = new ListNode(sum % 10);
  21.             if (result == NULL) {
  22.                 result = ptr;
  23.                 pre = ptr;
  24.             } else {
  25.                 pre->next = ptr;
  26.                 pre = ptr;
  27.             }
  28.             l1 = l1->next;
  29.             l2 = l2->next;
  30.         }
  31.         ListNode *restList = l1 ? l1 : l2;
  32.         while (restList) {
  33.             int sum = restList->val + append;
  34.             append = sum / 10;
  35.             ptr = new ListNode(sum % 10);
  36.             if (result == NULL) {
  37.                 result = ptr;
  38.                 pre = ptr;
  39.             } else {
  40.                 pre->next = ptr;
  41.                 pre = ptr;
  42.             }
  43.             restList = restList->next;
  44.         }
  45.         if (append) {
  46.             ptr = new ListNode(append);
  47.             pre->next = ptr;
  48.         }
  49.         
  50.         return result;
  51.     }
  52. };

Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc",
which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.


思路:有点DP的思想,每次计算一s[i]为结束点的长度。

点击(此处)折叠或打开

  1. class Solution {
  2. public:
  3.     int lengthOfLongestSubstring(string s) {
  4.         // Start typing your C/C++ solution below
  5.         // DO NOT write int main() function
  6.         int maxLen = 0;
  7.         int preLen = 0;
  8.         for (int i=0; i<s.size(); i++) {
  9.             int curLen = 1;
  10.             for (int j=i-1; j>=i-preLen; j--)
  11.                 if (s[j] == s[i])
  12.                     break;
  13.                 else
  14.                     curLen++;
  15.             if (curLen > maxLen)
  16.                 maxLen = curLen;
  17.             preLen = curLen;
  18.         }
  19.         
  20.         return maxLen;
  21.     }
  22. };

Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000,
and there exists one unique longest palindromic substring.


思路:求最长回文子串有线性的方法,我给的是O(n^2)的算法,不过也没有超时。
记录以s[i]结尾的所有回文子串的长度,那么在求以s[i+1]结尾的回文子串时候,遍历
以s[i]结尾回文串长度len, 如果s[i-len] == s[i+1],那么形成新的更长回文串。不过求
当前点时候,只要记录前一点状态就可以了,有点DP的思想。

点击(此处)折叠或打开

  1. class Solution {
  2. public:
  3.     string longestPalindrome(string s) {
  4.         // Start typing your C/C++ solution below
  5.         // DO NOT write int main() function
  6.         if (s.size() == 0)
  7.             return "";
  8.             
  9.         int maxLen = 1;
  10.         int pos = 0;
  11.         const int SIZE = 1001;
  12.         int dp[2][SIZE];
  13.         dp[0][0] = dp[1][0] = 0;
  14.         dp[0][1] = dp[1][1] = 1;
  15.         dp[0][2] = -1;
  16.         int pre = 0;
  17.         int cur = 1;
  18.         
  19.         for (int i=1; i<s.size(); i++) {
  20.             pre = (i + 1) % 2;
  21.             cur = i % 2;
  22.             int n = 1;
  23.             for (int j=0; dp[pre][j] != -1; j++) {
  24.                 int lpos = i - 1 - dp[pre][j];
  25.                 if (lpos >= 0 && s[lpos] == s[i]) {
  26.                     dp[cur][++n] = dp[pre][j] + 2;
  27.                 }
  28.             }
  29.             if (dp[cur][n] > maxLen) {
  30.                 maxLen = dp[cur][n];
  31.                 pos = i;
  32.             }
  33.             dp[cur][++n] = -1;
  34.         }
  35.         
  36.         return s.substr(pos-maxLen+1, maxLen);
  37.     }
  38. };

ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P   A   H   N
A P L S I I G
Y   I   R
And then read line by line: "PAHNAPLSIIGYIR"


Write the code that will take a string and make this conversion given a number of rows:

string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".


思路:这道题目没什么难度,就是题意不是很明确。其实就是将原来字符串按照锯齿形排列,
然后按行输出。

点击(此处)折叠或打开

  1. class Solution {
  2. public:
  3.     string convert(string s, int nRows) {
  4.         // Start typing your C/C++ solution below
  5.         // DO NOT write int main() function
  6.         if (nRows <= 1 || s.size() <=1 )
  7.             return s;
  8.         string cvStr = "";
  9.         int base = nRows * 2 - 2;
  10.         for (int n=0; n<nRows; n++) {
  11.             for (int i=0; i<=s.size()/base; i++) {
  12.                 int bias = i*base;
  13.                 if (bias+n < s.size())
  14.                     cvStr += s[bias+n];
  15.                 int complement = (base-n)%base;
  16.                 if (n != complement && bias+complement<s.size())
  17.                     cvStr += s[bias+complement];
  18.             }
  19.         }
  20.         return cvStr;
  21.     }
  22. };

Reverse Integer

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321
思路:水题

点击(此处)折叠或打开

  1. class Solution {
  2. public:
  3.     int reverse(int x) {
  4.         // Start typing your C/C++ solution below
  5.         // DO NOT write int main() function
  6.         
  7.         int result = 0;
  8.         while (x != 0) {
  9.             int tail = x % 10;
  10.             x /= 10;
  11.             result = result*10 + tail;
  12.         }
  13.         
  14.         return result;
  15.     }
  16. };








阅读(4057) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~