May 10th
来源:互联网 发布:添加不了网络客户端 编辑:程序博客网 时间:2024/05/29 14:29
#include <string>#include <iostream>using namespace std;/* Write a function that takes in a string and returns the length of the longest prefix in which all characters are arranged in alphabetical order. Examples: alphaprefix("ransom") = 1 alphaprefix("google") = 3 alphaprefix("knotty") = 6*/int alphaprefix(string input) { if(input.size() <= 1) return input.size(); int maxLen = 1; int len = 1; for(int i = 1; i < input.size(); ++i) { if(input[i] >= input[i-1]) len++; else break; } return len;}int main(void) { cout << alphaprefix("ransom") << endl; cout << alphaprefix("google") << endl; cout << alphaprefix("knotty") << endl;}
This one is very similar to LeetCode's recover IP Address question, but much simpler!
#include <iostream>#include <vector>using namespace std;/* You are given a int[N] array num, count how many triplets(x, y, z) that num[x] + num[y] + num[z] < K Assuming x < y < z For example: num[] = {5, 3, 6, 1, 8, 10}, K = 13 5 + 3 + 1 = 9 < 13 5 + 6 + 1 = 12 < 13 3 + 6 + 1 = 10 < 13 3 + 1 + 8 = 12 < 13 ----> return 4.*/int countTriplets(vector<int>& nums, int K) { if(nums.size() < 3) return 0; int count = 0; for(int i = 0; i < nums.size() - 2; ++i) { int target = nums[i]; for(int j = i + 1; j < nums.size() - 1; ++j) { target += nums[j]; for(int k = j + 1; k < nums.size(); ++k) { target += nums[k]; if(target < K) { count++; } target -= nums[k]; } target -= nums[j]; } } return count;}// Thought about it a bit. It seems that sort the nums doesn't actually affect the result.// The interviewer only asked for the final number. when we chose 3 numbers, they will always// have x < y < zint sortCountTriplets(vector<int>& nums, int K) { if(nums.size() < 3) return 0; sort(nums.begin(), nums.end()); int count = 0; for(int i = 0; i < nums.size() - 2; ++i) { int start = i + 1; int end = nums.size() - 1; while(start < end) { int sum = nums[i] + nums[start] + nums[end]; if(sum < K) { count += end - start; start++; } else end--; } } return count;}int main(void) { vector<int> nums{5, 3, 6, 1, 8, 10}; cout << countTriplets(nums, 13) << endl;}
0 0
- May 10th
- 1st-5th May Summary && 6th-10th May Plan
- May 10th Thursday (五月 十日 木曜日)
- Top coder competition May 26th,2007
- May 7th Monday (五月 七日 月曜日)
- May 8th Tuesday (五月 八日 火曜日)
- May 9th Wednesday (五月 九日 水曜日)
- May 16th Wednesday (五月 十六日 水曜日)
- May 17th Thursday (五月 十七日 木曜日)
- May 6th Wednesday (五月 六日 水曜日)
- May 7th Thursday (五月 七日 木曜日)
- May 8th Friday (五月 八日 金曜日)
- May 12th Tuesday (五月 十二日 火曜日)
- May 13th Wednesday (五月 十三日 水曜日)
- May 19th Tuesday (五月 十九日 火曜日)
- May 20th Wednesday (五月 二十日 水曜日)
- vimrc on May 6th,2010
- 26th-30th April Summary && 1st-5th May Plan
- matlab debug(二)
- 到底什么是Unikernel?
- L3-1. 凑零钱 PAT团体设计天梯赛
- Google APAC Test 2016 Not So Random 矩阵快递幂(logN复杂度)
- javascript第一天数据类型及声明方式
- May 10th
- 返回指向局部变量的指针或引用的带来的问题讨论
- SQLite的C++类,同时支持ANSI 和UNICODE编码
- 第十一周阅读程序(1)
- L1-6. 连续因子 PAT团体程序设计天梯赛
- java性能优化之一 VO的使用
- Timus 1837 Isenbaev's Number
- 圆柱类的设计
- 第十一周阅读程序(2)