<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

统计

  • 随笔 - 57
  • 文章 - 7
  • 评论 - 0
  • 引用 - 0

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

Blog

Coder 必备技巧

Compiler for Wurq

搜索

  •  

最新评论

阅读排行榜

评论排行榜

【LeeCode 2017/06/24】6. ZigZag Conversion

 1 class Solution {
 2 public:
 3     string convert(string s, int numRows) {
 4         vector<vector<char>> map;
 5         map.resize(numRows);
 6 
 7         for (int i = 0; i < numRows; i++)
 8             map[i].resize(s.length());
 9 
10         int count = 0;
11         int j = 0;
12         while (count < s.length())
13         {
14             int i = 0;
15             while (count<s.length() && i<numRows)
16                 map[i++][j]= s[count++];
17 
18             j++; i--;
19             while (count<s.length() && i>1)
20                 map[--i][j++= s[count++];
21         }
22         string ans = "";
23         for (size_t i = 0; i < numRows;i++){
24             for (size_t j = 0; j < s.length(); j++){
25                 if (map[i][j] != 0)
26                     ans += map[i][j];
27             }
28         }
29         return ans;
30     }
31 };
32 

posted @ 2017-06-24 10:50 Wurq 阅读(102) | 评论 (0)编辑 收藏
【LeeCode 2017/06/23】13. Roman to Integer

 1 class Solution {
 2 public:
 3     int romanToInt(string s) {
 4         map<charint>num;
 5         num['I'= 1;
 6         num['V'= 5;
 7         num['X'= 10;
 8         num['L'= 50;
 9         num['C'= 100;
10         num['D'= 500;
11         num['M'= 1000;
12         int sum = num[s[0]];
13         for (int i = 1; i < s.length(); i++)
14         {
15             if (num[s[i - 1]] >= num[s[i]])
16                 sum += num[s[i]];
17             else
18                 sum = sum  + num[s[i]] -2* num[s[i - 1]];
19         }
20         return sum;
21     }
22 };
23 

posted @ 2017-06-23 09:34 Wurq 阅读(89) | 评论 (0)编辑 收藏
【LeeCode 2017/06/22】9. Palindrome Number

 1 class Solution {
 2 public:
 3     bool isPalindrome(int x) {
 4         
 5         if (x < 0)return false;
 6 
 7         list<int> l;
 8         while (x){
 9             l.push_back(x % 10);
10             x /= 10;
11         }
12 
13         while (!l.empty()) {
14             if (l.front() == l.back()) {
15                 l.pop_front();
16                 if(!l.empty())
17                     l.pop_back();
18             }
19             else
20                 return false;
21         }
22         return true;
23     }
24 };

posted @ 2017-06-22 10:29 Wurq 阅读(66) | 评论 (0)编辑 收藏
【LeeCode 2017/06/21】7. Reverse Integer

 1 class Solution {
 2 public:
 3     int reverse(int x) {
 4         list<long long>num;
 5         bool mark = false;
 6         if (x < 0){
 7             mark = true;
 8             x = -x;
 9         }
10 
11         while (x){
12             num.push_back(x % 10);
13             x = x / 10;
14         }
15 
16         long long sum = 0;
17         while (!num.empty()) {
18             sum = sum*10+num.front();
19             num.pop_front();
20         }
21 
22         if (mark)
23             sum = -sum;
24 
25         int ans = sum;
26 
27         if (sum > 0X7fffffff  || sum < -0X7fffffff-1)
28             ans = 0;
29 
30         return ans;
31     }
32 };

posted @ 2017-06-21 09:51 Wurq 阅读(65) | 评论 (0)编辑 收藏
【LeeCode 2017/06/18】 3. Longest Substring Without Repeating Characters

 1 class Solution {
 2 public:
 3     int lengthOfLongestSubstring(string s) {
 4         int mark[256= { 0 };
 5         int begin = 0;
 6         int end = 0;
 7         int ret_max = 0;
 8         while (begin<=end &&begin <s.length() && end<s.length())
 9         {
10             char ch = s[end];
11             if (mark[ch] == 0){
12                 mark[ch]++;
13                 end++;
14             }else{
15                 ch = s[begin];
16                 mark[ch]--;
17                 begin++;
18             }
19             ret_max = end - begin > ret_max ? end - begin : ret_max;
20         }
21         return ret_max;
22     }
23 };
24 

posted @ 2017-06-16 09:34 Wurq 阅读(74) | 评论 (0)编辑 收藏
【LeeCode 2017/06/16】 1. Two Sum

 1 class Solution {
 2 public:
 3     vector<int> twoSum(vector<int>& nums, int target)
 4     {
 5         bool mark = true;
 6         vector<int>ret;
 7         for (int i = 0; mark && i < nums.size(); i++)
 8         {
 9             for (int j = 0; mark && j < nums.size(); j++)
10             {
11                 if (i == j)continue;
12                 if (nums[j] == target - nums[i])
13                 {
14                     ret.push_back(i);
15                     ret.push_back(j);
16                     mark = false;
17                 }
18             }
19         }
20         return ret;
21     }
22 };

posted @ 2017-06-15 10:17 Wurq 阅读(91) | 评论 (0)编辑 收藏
【LeeCode 2017/06/17】 2. Add Two Numbers

 1 class Solution {
 2 public:
 3     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) 
 4     {
 5         int sign = 0;
 6         int sum;
 7         ListNode* root = nullptr;
 8         ListNode* p_root = nullptr;
 9         while (l1!=NULL || l2 != NULL || sign)
10         {
11             int val1 = 0;
12             int val2 = 0;
13             if (l1 != NULL){
14                 val1 = l1->val;
15                 l1 = l1->next;
16             }
17             if (l2 != NULL) {
18                 val2 = l2->val; 
19                 l2 = l2->next;
20             }
21 
22             sum = ((val1 + val2)%10 + sign)%10;
23             sign = (val1 + val2 + sign) / 10;
24             if (root == nullptr)
25             {
26                 root = new ListNode(sum);
27                 p_root = root;
28             }
29             else
30             {
31                 p_root->next = new ListNode(sum);
32                 p_root = p_root->next;
33             }
34         }
35         return root;
36     }
37 };

posted @ 2017-06-15 10:15 Wurq 阅读(95) | 评论 (0)编辑 收藏
【LeeCode 2017/06/20】 5. Longest Palindromic Substring

 1 class Solution {
 2 public:
 3     string longestPalindrome(string s) {
 4         int max_len = 0;
 5         string ans;
 6         for (int i = 0; i < s.length(); i++)
 7         {
 8             for (int j = s.length() -1; j >= 0 ; j--)
 9             {
10                 if (s[j] != s[i])continue;
11                 if (j - i + 1 <= max_len)break;
12 
13                 int ii = i;
14                 int jj = j;
15                 while (ii< s.length() && jj >=0 &&
16                     s[ii]==s[jj]&& ii<=jj){
17                     ii++;
18                     jj--;
19                 }
20                 if (ii == jj + 1 || ii == jj + 2)
21                 {
22                     max_len = j - i + 1;
23                     ans = s.substr(i, max_len);
24                 }
25             }
26         }
27         return ans;
28     }
29 };

posted @ 2017-06-14 10:39 Wurq 阅读(58) | 评论 (0)编辑 收藏
【LeeCode 2017/06/15】 476. Number Complement

 1 class Solution {
 2 public:
 3     int findComplement(int num) {
 4         int sum = 0;
 5         int n = 1;
 6         while (num){
 7             sum += ((num & 1^ 1)*n;
 8             num >>= 1;
 9             n *= 2;
10         }
11         return sum;
12     }
13 };

posted @ 2017-06-13 10:58 Wurq 阅读(89) | 评论 (0)编辑 收藏
【LeeCode 2017/06/14】 561. Array Partition I

 1 class Solution {
 2 public:
 3     int arrayPairSum(vector<int>& nums) {
 4         sort(nums.begin(),nums.end());
 5         int sum = 0;
 6         for (int pos = 0; pos < nums.size(); pos+= 2)
 7             sum += nums[pos];
 8         return sum;
 9     }
10 };

posted @ 2017-06-13 10:29 Wurq 阅读(84) | 评论 (0)编辑 收藏
仅列出标题
共6页: 1 2 3 4 5 6