LeetCode Weekly Contest 8

415. Add Strings

  • User Accepted: 765
  • User Tried: 822
  • Total Accepted: 789
  • Total Submissions: 1844
  • Difficulty: Easy

Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.

Note:

  1. The length of both num1 and num2 is < 5100.
  2. Both num1 and num2 contains only digits 0-9.
  3. Both num1 and num2 does not contain any leading zero.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

简单的模拟题。

class Solution {
public:
string addStrings(string num1, string num2) {
int len1 = num1.length(), len2 = num2.length();
int t = 0, c = 0, i = len1-1, j = len2-1;
string ret = "";
while(i>=0 && j>=0){
c = (t + (num1[i]-'0') + (num2[j]-'0'))%10;
t = (t + (num1[i]-'0') + (num2[j]-'0'))/10;
ret.insert(ret.begin(), char('0' + c));
--i; --j;
}
while(i>=0){
c = (t + (num1[i]-'0'))%10;
t = (t + (num1[i]-'0'))/10;
ret.insert(ret.begin(), char('0' + c));
--i;
}
while(j>=0){
c = (t + (num2[j]-'0'))%10;
t = (t + (num2[j]-'0'))/10;
ret.insert(ret.begin(), char('0' + c));
--j;
}
if(t != 0){
ret.insert(ret.begin(), char('0' + t));
}
return ret;
}
};

  

416. Partition Equal Subset Sum

  • User Accepted: 488
  • User Tried: 670
  • Total Accepted: 506
  • Total Submissions: 1689
  • Difficulty: Medium

Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.

Note:
Both the array size and each of the array element will not exceed 100.

Example 1:

Input: [1, 5, 11, 5]

Output: true

Explanation: The array can be partitioned as [1, 5, 5] and [11].

Example 2:

Input: [1, 2, 3, 5]

Output: false

Explanation: The array cannot be partitioned into equal sum subsets.

简单的单重背包问题

class Solution {
public:
bool canPartition(vector<int>& nums) {
int sum = 0, len = nums.size();
for(int i=0; i<len; ++i){
sum += nums[i];
}
if(sum%2 != 0){
return false;
}
int* dp = new int[sum/2 + 1];
for(int i=sum/2; i>=1; --i){
dp[i] = 0;
}
dp[0] = 1;
for(int i=0; i<len; ++i){
for(int j=sum/2; j>=nums[i]; --j){
if(dp[j-nums[i]]){
dp[j] = 1;
}
}
}
bool flag = (dp[sum/2]==1);
delete[] dp;
return flag;
}
};

  

417. Pacific Atlantic Water Flow

  • User
    Accepted: 259
  • User
    Tried: 403
  • Total
    Accepted: 265
  • Total
    Submissions: 1217
  • Difficulty: Medium

Given an m x n matrix of non-negative integers representing the height of each unit cell
in a continent, the "Pacific ocean" touches the left and top edges of
the matrix and the "Atlantic ocean" touches the right and bottom
edges.

Water can only flow in four directions (up,
down, left, or right) from a cell to another one with height equal or lower.

Find the list of grid coordinates where
water can flow to both the Pacific and Atlantic ocean.

Note:

  1. The order of returned grid coordinates does
    not matter.
  2. Both m and n are less than 150.

Example:

Given the following 5x5 matrix:

Pacific ~   ~  
~   ~   ~

~  1  
2   2   3  (5)
*

~  3  
2   3  (4) (4) *

~  2  
4  (5)  3   1  *

~ (6) (7)  1  
4   5  *

~ (5)  1  
1   2   4  *

*   *  
*   *   * Atlantic

Return:

[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with
parentheses in above matrix).

双重bfs,
要注意不能重复采集到顶点。

class Solution {
public:
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
vector<pair<int, int>> pacificAtlantic(vector<vector<int>>& matrix) {
vector<pair<int,int>> ret;
int m = matrix.size();
if(m == 0){ return ret; }
int n = matrix[0].size();
if(n == 0){ return ret; } vector<vector<int>> cnt(m, vector<int>(n, 0));
vector<vector<int>> vis(m, vector<int>(n, 0));
int tmp, cur_x, cur_y, tmp_x, tmp_y, i = 0, j = 0;
queue<pair<int, int>> qt;
for(int i=0; i<m; ++i){
qt.push(make_pair(i, 0));
cnt[i][0] += 1;
vis[i][0] = 1;
}
for(int i=1; i<n; ++i){
qt.push(make_pair(0, i));
cnt[0][i] += 1;
vis[0][i] = 1;
}
while(!qt.empty()){
cur_x = qt.front().first; cur_y = qt.front().second;
tmp = matrix[cur_x][cur_y];
qt.pop();
for(int i=0; i<4; ++i){
tmp_x = cur_x + dx[i]; tmp_y = cur_y + dy[i];
if(tmp_x>=0 && tmp_x<m && tmp_y>=0 && tmp_y<n && !vis[tmp_x][tmp_y] && matrix[tmp_x][tmp_y]>=tmp){
qt.push(make_pair(tmp_x, tmp_y));
cnt[tmp_x][tmp_y] += 1;
vis[tmp_x][tmp_y] = 1;
}
}
}
for(int i=0; i<m; ++i){
for(int j=0; j<n; ++j){
vis[i][j] = 0;
}
}
for(int i=0; i<m; ++i){
qt.push(make_pair(i, n-1));
cnt[i][n-1] += 1;
vis[i][n-1] = 1;
}
for(int i=0; i<n-1; ++i){
qt.push(make_pair(m-1, i));
cnt[m-1][i] += 1;
vis[m-1][i] = 1;
}
while(!qt.empty()){
cur_x = qt.front().first; cur_y = qt.front().second;
tmp = matrix[cur_x][cur_y];
qt.pop();
for(int i=0; i<4; ++i){
tmp_x = cur_x + dx[i]; tmp_y = cur_y + dy[i];
if(tmp_x>=0 && tmp_x<m && tmp_y>=0 && tmp_y<n && !vis[tmp_x][tmp_y] && matrix[tmp_x][tmp_y]>=tmp){
qt.push(make_pair(tmp_x, tmp_y));
cnt[tmp_x][tmp_y] += 1;
vis[tmp_x][tmp_y] = 1;
}
}
}
for(int i=0; i<m; ++i){
for(int j=0; j<n; ++j){
if(cnt[i][j] == 2){
ret.push_back(make_pair(i, j));
}
}
}
return ret;
}
};

  

LeetCode Weekly Contest 8的更多相关文章

  1. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  2. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  3. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  4. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

  5. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  6. 【LeetCode Weekly Contest 26 Q3】Friend Circles

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...

  7. 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

  8. 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

  9. LeetCode Weekly Contest 47

    闲着无聊参加了这个比赛,我刚加入战场的时候时间已经过了三分多钟,这个时候已经有20多个大佬做出了4分题,我一脸懵逼地打开第一道题 665. Non-decreasing Array My Submis ...

随机推荐

  1. 十、Android学习第九天——小结(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 十.Android学习第九天——小结 通过这段时间的学习,今晚上来做个小小 ...

  2. java怎么建立JAVA工程项目?

    File->New->Java Project;src->New->Class; 出现packet,运行出错的问题 然后如果不要包packet 的话,不要在此处填写包的名称就行 ...

  3. AngularJS几个基础概念

    作用域 应用的作用域是和应用的数据模型相关联的,同时作用域也是表达式执行的上下文.$scope对象是定义应用业务逻辑.控制器方法和视图属性的地方.作用域是视图和作用域之间的胶水.在应用将视图渲染并呈现 ...

  4. ARM学习篇一 点亮LED

    要点亮LED,先决条件是什么,当然得有相应的硬件设施.板子的整个电路图比较大,我就直接取相关部分. 给发光二级管加上3.3v电压后,通过1k电阻,直接与S3C2440连接.至于为什么要加电阻,大家应该 ...

  5. 我的STL学习之路

    说起STL(标准模板库),相信了解C++的都不会陌生吧^_^.LZ是从大三开始学习C++(ps:不是科班出身),并慢慢接触使用STL的,在学校中使用STL比较多的情况是写数据结构代码,使用STL实现数 ...

  6. CodeForces 466E Information Graph --树形转线性+并查集

    题意:有三种操作: 1.新增一条边从y连向x,此前x没有父节点 2.x接到一份文件,(文件标号逐次递增),然后将这份文件一路上溯,让所有上溯的节点都接到这份文件 3.查询某个节点x是否接到过文件F 解 ...

  7. 洛谷P1126机器人搬重物[BFS]

    题目描述 机器人移动学会(RMI)现在正尝试用机器人搬运物品.机器人的形状是一个直径1.6米的球.在试验阶段,机器人被用于在一个储藏室中搬运货物.储藏室是一个N*M的网格,有些格子为不可移动的障碍.机 ...

  8. ural Cipher Message

    Cipher Message Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Desc ...

  9. nginx作为负载均衡服务器——测试

    i. 需求 nginx作为负载均衡服务器,用户请求先到达nginx,再由nginx根据负载配置将请求转发至 tomcat服务器. nginx负载均衡服务器:192.168.101.3 tomcat1服 ...

  10. [No000060]冷读热读:读书九问

    兵无常势,水无常形,读书亦无法.彼之砒霜,我之佳肴.然读书无法却有道.你我都是使用同一颗大脑在读书.这颗大脑受制于那千千万万年以来,星辰起落,狩猎采集,演化大道. Q1:读物如何分级? 坏书.可用的书 ...