62. Unique Paths

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

How many possible unique paths are there?

Note: m and n will be at most 100.

Example 1:

Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Right -> Down
2. Right -> Down -> Right
3. Down -> Right -> Right

Example 2:

Input: m = 7, n = 3
Output: 28


class Solution {
public:
    int uniquePaths(int m, int n) {
        vector<vector<int> > path(m, vector<int> (n, 1));
        for (int i = 1; i < m; i++)
            for (int j = 1; j < n; j++)
                path[i][j] = path[i - 1][j] + path[i][j - 1];
        return path[m - 1][n - 1];
    }
};

求助:unique constraint 错误

11-30

“/zongtibu”应用程序中的服务器错误。rn--------------------------------------------------------------------------------rnrnORA-00001: unique constraint (CASIC.CASIC_PK) violated rn说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 rnrn异常详细信息: System.Data.OleDb.OleDbException: ORA-00001: unique constraint (CASIC.CASIC_PK) violatedrnrn代码如下:rnrnprivate void Button_add_Click(object sender, System.EventArgs e)rn rn cnn = new OleDbConnection("Provider=OraOLEDB.Oracle.1;Password=casic;Persist Security Info=True;User ID=casic;Data Source=ora8");rn string cn1="Select DISTINCT ID,name,xinghao,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday from schedule";rn rn da1=new OleDbDataAdapter();rn rn da1.SelectCommand=new OleDbCommand(cn1,cnn);rn string cn2 = "Insert Into schedule (ID,name,xinghao,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday) Values('"rn +TextBox_ID.Text+"','"+ TextBox_name.Text+"','"+TextBox_xinghao.Text+"','"rn + TextBox_Mon.Text+"','"+ TextBox_Tues.Text+"','" rn + TextBox_Wed.Text+"','"+ TextBox_Thurs.Text+"','" rn + TextBox_Fri.Text+"','"+ TextBox_Sat.Text+"','"rn + TextBox_Sun.Text+"')";rn rn OleDbCommand cnn1 = new OleDbCommand();rn cnn1.Connection = cnn;rn cnn1.CommandText = cn2;rn da1.InsertCommand = cnn1;rn da1.UpdateCommand = cnn1;rn rn ds1 = new DataSet(); rn da1.Fill(ds1,"work");rn DataTable table1 = ds1.Tables["work"];rn DataRow newRow = table1.NewRow();rn newRow["ID"] = TextBox_ID.Text.ToString();rn newRow["name"] = TextBox_name.Text.ToString();rn newRow["xinghao"] = TextBox_xinghao.Text.ToString();rn newRow["Monday"] = TextBox_Mon.Text.ToString();rn newRow["Tuesday"] = TextBox_Tues.Text.ToString();rn newRow["Wednesday"] = TextBox_Wed.Text.ToString();rn newRow["Thursday"] = TextBox_Thurs.Text.ToString();rn newRow["Friday"] = TextBox_Fri.Text.ToString();rn newRow["Saturday"] = TextBox_Sat.Text.ToString();rn newRow["Sunday"] = TextBox_Sun.Text.ToString();rnrn table1.Rows.Add(newRow);rn da1.Update(ds1,"work");rn Response.Write(" ");rn rn rn奇怪的是,如果添加的ID是以前没用过的,就能添加成功,如果用已存在的的ID,再运行这个函数,就会出现这个错误,大家帮忙看看是什么原因啊?rnrnrn行 379:rn行 380: table1.Rows.Add(newRow);rn行 381: da1.Update(ds1,"work");rn行 382: Response.Write(" ");rn行 383: rn rnrn源文件: c:\inetpub\wwwroot\zongtibu\webform1.aspx.cs 行: 381

提问:关于 unique() 的问题

11-17

问题:输入一段字符串,在程序里先后使用sort()函数和 unique()函数,怎么结果不对?rn比如:我输入 wo ai ni wo ai nirn那么输出为:rnwo ai ni wo ai nirnAfter sort() : ai ai ni ni wo wornAfter unique() : ai ni wo ni wo wo (怎么多一个wo,少一个ai了???)rnAfter stable_sort() : ai ni wo ni wo worn[code=C/C++]rn#include rn#include rn#include rn#include rnusing namespace std;rnrn// comparison function to be used to sort by word lengthrnbool isShorter(const string &s1, const string &s2)rnrn return s1.size() < s2.size();rnrnrn// determine whether a length of a given word is 6 or morernbool GT6(const string &s)rnrn return s.size() >= 6;rnrnrn// return plural version of word if ctr isn't 1rnstring make_plural(size_t ctr, const string &word, const string &ending)rnrn return (ctr == 1) ? word : word + ending;rnrnrnint main()rnrn vector words;rn string s;rn cout << "Please input the story:" << endl;rn while (cin >> s)rn rn words.push_back(s);rn rn vector::iterator ster = words.begin();rn for (ster = words.begin(); ster != words.end(); ++ster)rn rn cout << *ster << " ";rn rn cout << endl;rn // sort words alphabetically so we can find the duplicatesrn rn sort(words.begin(), words.end()); // 按首字母 字典排序rn cout << "After sort() : ";rn for (ster = words.begin(); ster != words.end(); ++ster)rn rn cout << *ster << " ";rn rn cout << endl;rn /* eliminate duplicate words:rn * unique reorders words so that each word appears once in thern * front portion of words and returns an iterator one past the unique range;rn * erase uses a vector operation to remove the nonunique elementsrn */rn vector::iterator end_unique = unique(words.begin(), words.end()); rn cout << "After unique() : ";rn for (ster = words.begin(); ster != words.end(); ++ster)rn rn cout << *ster << " "; // 这里结果不对rn rn cout << endl;rnrn // sort words by size, but maintain alphabetic order for words of the same sizern stable_sort(words.begin(), words.end(), isShorter);rn cout << "After stable_sort() : ";rn for (ster = words.begin(); ster != words.end(); ++ster)rn rn cout << *ster << " ";rn rn cout << endl;rnrnrn return 0;rnrn[/code]

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试