第二周ARTS

1.Algorithm
Question:

给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

示例 1:

输入: "abcabcbb"
输出: 3 
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
示例 2:

输入: "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
示例 3:

输入: "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
     请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解决方案如下:

  public static int LengthOfLongestSubstring(string s)
        {
            List ls = new List();
            int n = s.Length;
            int intMaxLength = 0;
            for (int i = 0; i < n; i++)
            {
                if (ls.Contains(s[i]))
                {
                    ls.RemoveRange(0, ls.IndexOf(s[i]) + 1);
                }
                ls.Add(s[i]);
                intMaxLength = ls.Count > intMaxLength ? ls.Count : intMaxLength;
            }
            return intMaxLength;
        }

//leet code结果显示下面这种方式更快一些,但内存消耗更大

        public static int LengthOfLongestSubstring2(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return 0;
            }
            List ls = new List();
            int n = s.Length;          
            int intMaxLength = 1;
            for (int i = 0; i < n; i++)
            {
                if (ls.Find( c => c==s[i])>0)
                {
                    ls.RemoveRange(0, ls.IndexOf(s[i]) + 1);
                }
                ls.Add(s[i]);
                intMaxLength = ls.Count > intMaxLength ? ls.Count : intMaxLength;
            }
            return intMaxLength;
        }

第二周ARTS_第1张图片


2.Review

You Still Don’t Know How to Do Unit Testing :https://stackify.com/unit-testing-basics-best-practices/

Unit Test是程序开发中非常重要的一步,程序员只有吃自己的狗粮,自己写的代码自己测试才能对代码稳定性有一个更直观的了解。本文介绍了作者总结的6个单元测试最佳实践:

  • Arrange, Act, Assert

  • One Assert Per Test Method

  • Avoid Test Interdependence

  • Keep It Short, Sweet, and Visible

  • Recognize Test Setup Pain as a Smell

  • Add Them to the Build

在使用单元测试时通常可以用一些特定的框架,一般如果是开发C#的话那就用NUnit框架,在Java中可以使用JUnit框架,可同步阅读:Unit Testing Best Practices: JUnit Reference Guide:https://dzone.com/articles/unit-testing-best-practices

JUnit 5 User Guide:https://junit.org/junit5/docs/current/user-guide/

3.Tip


本次分享一下如何在winfrom中使用cef内核浏览器,即在winfrom中调用谷歌浏览器内核来渲染网页:

winfrom中现有的webBrowser控件可以干这个是,但webBrowser使用的是IE浏览器渲染,渲染速度也比较慢,兼容性不是很好。目前主流的浏览器还是基于chrome内核的居多。比如360浏览器,搜狗浏览器等。

那怎么来使用呢?结合gitHub:https://github.com/cefsharp/CefSharp提供包可以做一个demo:

新建一个winfrom项目RadForm1,通过nuget引入相关的包:nuget地址:https://www.nuget.org/packages/CefSharp.WinForms/

Install-Package CefSharp.WinForms -Version 79.1.360

引入之后添加如下代码:

RadForm1 .cs代码如下

using CefSharp;
using CefSharp.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WinFormsChromium
{
    public partial class RadForm1 : Form
    {
        public ChromiumWebBrowser browser;

        public void InitBrowser(string url)
        {
            browser = new ChromiumWebBrowser(url);
            panel2.Controls.Add(browser);
            browser.Dock = DockStyle.Fill;
        }

        private void browser_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
        {
            if (e.IsLoading == false)
            {
                browser.ExecuteScriptAsync("alert('All Resources Have Loaded');");
            }
        }

        public RadForm1()
        {
            InitializeComponent();
            Cef.Initialize(new CefSettings());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (browser == null)
            {
                InitBrowser(textBox1.Text);
            }
            else
            {
                browser.Load(textBox1.Text);
            }
        }
    }
}

RadForm1.Designer.cs代码如下:

namespace WinFormsChromium
{
    partial class RadForm1
    {
        /// 
        /// Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        /// Clean up any resources being used.
        /// 
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// 
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 
        private void InitializeComponent()
        {      
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.panel1 = new System.Windows.Forms.Panel();
            this.panel2 = new System.Windows.Forms.Panel();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(658, 38);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(3, 38);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(582, 21);
            this.textBox1.TabIndex = 1;
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.textBox1);
            this.panel1.Controls.Add(this.button1);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(1014, 100);
            this.panel1.TabIndex = 2;
            // 
            // panel2
            // 
            this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel2.Location = new System.Drawing.Point(0, 100);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(1014, 466);
            this.panel2.TabIndex = 3;
            // 
            // RadForm1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1014, 566);
            this.Controls.Add(this.panel2);
            this.Controls.Add(this.panel1);
            this.Name = "RadForm1";
            this.Text = "RadForm1";
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Panel panel2;
    }
}


 运行效果:

第二周ARTS_第2张图片

请注意:cefSharp要求的.Net Freamwork版本在4.5.2之上(包含4.5.2),对于一些老的项目可能就无缘使用了。


4.Share

其实在写代码时,多参考一些行业里的最佳实践,对个人技术视野,技术能力的提升是很有帮助的。通过参考一些前辈们的经验和规范,可以使自我的代码更规整,少出BUG,代码更易读。从而可以很容易的维护,同时也能提升开发效率。当我们这样做了,并坚持践行了,那就很容易在团队中脱颖而出,团队其他人员也会更加信任你,这样才会有源源不断的正反馈。

你可能感兴趣的:(ARTS)